author | Claus Gittinger <cg@exept.de> |
Sat, 10 Nov 2018 00:40:17 +0100 | |
changeset 6506 | 0f7404be1444 |
parent 6224 | 051efa88ece4 |
child 6631 | 476a7030cebb |
permissions | -rw-r--r-- |
6048 | 1 |
" |
2 |
COPYRIGHT (c) 2013 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
4789 | 12 |
"{ Package: 'stx:libwidg' }" |
13 |
||
5694 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
4789 | 16 |
EditTextViewCompletionSupport subclass:#WorkspaceCompletionSupport |
17 |
instanceVariableNames:'' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Interface-Smalltalk' |
|
21 |
! |
|
22 |
||
23 |
!WorkspaceCompletionSupport class methodsFor:'documentation'! |
|
24 |
||
6048 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 2013 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
4789 | 39 |
documentation |
40 |
" |
|
41 |
A completion support using DWIM to complete code for Smalltalk (and JavaScript) |
|
42 |
||
43 |
[author:] |
|
44 |
Claus Gittinger |
|
45 |
||
46 |
[instance variables:] |
|
47 |
||
48 |
[class variables:] |
|
49 |
||
50 |
[see also:] |
|
51 |
DoWhatIMeanSupport |
|
52 |
||
53 |
" |
|
54 |
! ! |
|
55 |
||
56 |
!WorkspaceCompletionSupport methodsFor:'private'! |
|
57 |
||
58 |
computeCompletions |
|
59 |
||
5996 | 60 |
|topView suggestions implementations actions contextOrNil| |
4789 | 61 |
|
5996 | 62 |
"/ a hack - we get better completions, if we know the current context |
63 |
topView := editView topView. |
|
64 |
(topView notNil and:[topView isDebugView]) ifTrue:[ |
|
65 |
contextOrNil := topView selectedContext. |
|
4789 | 66 |
]. |
67 |
||
68 |
UserInformation ignoreIn:[ |
|
6130 | 69 |
DoWhatIMeanSupport new |
70 |
setSelf: (editView simulatedSelf); |
|
4802
a25143f6e512
Added codeAspect to EditTextView to signify what's being edited.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4789
diff
changeset
|
71 |
codeCompletionFor: editView codeAspect |
a25143f6e512
Added codeAspect to EditTextView to signify what's being edited.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4789
diff
changeset
|
72 |
language: editView editedLanguage |
4789 | 73 |
method:editView editedMethod |
74 |
orClass:editView editedClass |
|
75 |
context:contextOrNil |
|
76 |
codeView:editView |
|
77 |
into:[:listOfSuggestions :listOfActions :titleWhenAsking | |
|
6130 | 78 |
"/ (listOfSuggestions contains:[:l | l isEmptyOrNil]) ifTrue:[self halt]. |
4789 | 79 |
suggestions := listOfSuggestions collect:[:entry | entry isArray ifTrue:[entry first] ifFalse:[entry]]. |
80 |
implementations := listOfSuggestions collect:[:entry | entry isArray ifTrue:[entry second] ifFalse:[nil]]. |
|
81 |
actions := listOfActions. |
|
82 |
nil "/ must return nil to avoid DWIM to do it itself (for now) |
|
83 |
] |
|
84 |
]. |
|
85 |
"/ Transcript show:'suggestions: '; showCR:suggestions. |
|
5047
091548040432
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4870
diff
changeset
|
86 |
"/ Transcript show:'actions: '; showCR:actions. |
4789 | 87 |
editView sensor |
88 |
pushUserEvent:#'suggestionsArrived:implementations:actions:autoSelect:' |
|
89 |
for:self |
|
90 |
withArguments:{suggestions . implementations . actions . autoSelect } |
|
91 |
||
92 |
"Created: / 26-09-2013 / 17:44:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
6130 | 93 |
"Modified: / 09-03-2017 / 10:48:44 / cg" |
4789 | 94 |
! |
95 |
||
96 |
suggestionsArrived:suggestionsArg implementations:implementationsArg actions:actionsArg autoSelect:autoSelectArg |
|
97 |
"the background process has generated some suggestions" |
|
98 |
||
5694 | 99 |
|v numShown numFirst numLast numSkipped |
100 |
suggestions implementations actions suggestionOffset keyAndSnippet indexOfSnippet| |
|
4789 | 101 |
|
102 |
(editView sensor hasKeyPressEventFor:nil) ifTrue:[ |
|
103 |
self closeCompletionView. |
|
104 |
^ self |
|
105 |
]. |
|
106 |
||
107 |
implementations := implementationsArg. |
|
108 |
actions := actionsArg. |
|
109 |
||
110 |
suggestions := suggestionsArg ? #(). |
|
6224 | 111 |
suggestions := suggestions reject:[:el | el isNil]. |
112 |
||
5695 | 113 |
numShown := 25. |
5694 | 114 |
suggestions size > numShown ifTrue:[ |
115 |
numFirst := numShown-5. |
|
116 |
numLast := 5. |
|
117 |
numSkipped := suggestions size-numShown. |
|
118 |
suggestions := (suggestions copyTo:numShown-5) |
|
5723 | 119 |
, { ('<< %1 more skipped >>' bindWith:numSkipped) withColor:Color grey } |
5694 | 120 |
, (suggestions copyLast:5). |
121 |
implementations isArray ifTrue:[ |
|
122 |
implementations := (implementations copyTo:numShown-5),#(nil),(implementations copyLast:5). |
|
123 |
]. |
|
124 |
actions isArray ifTrue:[ |
|
125 |
actions := (actions copyTo:numShown-5),#(nil),(actions copyLast:5). |
|
126 |
]. |
|
4789 | 127 |
]. |
128 |
||
129 |
"/ append snipplet, if any (can be easily reached via CRSR-up) |
|
130 |
suggestionOffset := 0. |
|
131 |
indexOfSnippet := nil. |
|
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
132 |
UserPreferences current appendAbbreviationsToCompletionSuggestions ifTrue:[ |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
133 |
(keyAndSnippet := editView findAbbreviationKeyBeforeCursor) notNil ifTrue:[ |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
134 |
|abbrev sniplet i line| |
4789 | 135 |
|
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
136 |
abbrev := keyAndSnippet first. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
137 |
sniplet := keyAndSnippet second. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
138 |
|
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
139 |
"/ if the abbreviation is simply at the end of a longer word, ignore the abbrev. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
140 |
line := editView lineStringBeforeCursor. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
141 |
i := line findLast:[:ch | ch isLetterOrDigit not]. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
142 |
(i < (line size - abbrev size - 1)) ifFalse:[ |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
143 |
sniplet := sniplet copyWithout:$!!. |
4789 | 144 |
|
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
145 |
"/ true, false and self are often found in both lists |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
146 |
(suggestions includes:sniplet) ifFalse:[ |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
147 |
suggestions isEmpty ifFalse:[ suggestions := suggestions copyWith: '-' ]. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
148 |
suggestions := suggestions copyWith: ( '%1 %2' |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
149 |
bindWith:(sniplet asStringCollection first "contractTo:25") |
5723 | 150 |
with: ( ('("',abbrev,'" snippet)') withColor:Color gray)). |
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
151 |
indexOfSnippet := suggestions size. |
4789 | 152 |
|
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
153 |
"/ change below, when reversing the order in above code |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
154 |
"/ suggestionOffset := 2. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
155 |
] |
4789 | 156 |
] |
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
157 |
]. |
4789 | 158 |
]. |
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
159 |
|
4789 | 160 |
suggestions isEmptyOrNil ifTrue:[ |
161 |
self closeCompletionView. |
|
162 |
^ self |
|
163 |
]. |
|
164 |
(v := completionView) isNil ifTrue: [ |
|
165 |
^ self |
|
166 |
]. |
|
167 |
||
168 |
v sensor |
|
169 |
pushUserEvent:#value |
|
170 |
for:[ |
|
171 |
|top idx preselectIdx performCompletion| |
|
172 |
||
173 |
(v == completionView) ifTrue: [ |
|
174 |
top := v topView. |
|
4810
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
175 |
autoSelectArg ifTrue:[ |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
176 |
LastCompletions notNil ifTrue:[ |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
177 |
"/ one of the last completions in list? |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
178 |
idx := LastCompletions findFirst:[:compl | suggestions includes:compl]. |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
179 |
idx ~~ 0 ifTrue:[ |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
180 |
preselectIdx := suggestions indexOf:(LastCompletions at:idx). |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
181 |
]. |
4789 | 182 |
]. |
4810
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
183 |
(preselectIdx isNil and:[suggestions size == 1]) ifTrue:[ |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
184 |
preselectIdx := 1. |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
185 |
]. |
4789 | 186 |
]. |
187 |
preselectIdx notNil ifTrue:[ |
|
188 |
|pref| |
|
189 |
||
190 |
pref := suggestions at:preselectIdx. |
|
4807
8f6cd2048807
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4806
diff
changeset
|
191 |
pref notNil ifTrue:[ |
8f6cd2048807
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4806
diff
changeset
|
192 |
"/ for now, do not move to front (action needs the index) |
8f6cd2048807
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4806
diff
changeset
|
193 |
suggestions at:preselectIdx put:(pref allBold). |
4789 | 194 |
"/ suggestions removeAtIndex:preselectIdx. |
195 |
"/ suggestions addFirst:(pref allBold). |
|
196 |
"/ implementations notNil ifTrue:[ |
|
197 |
"/ implementations removeAtIndex:preselectIdx. |
|
198 |
"/ implementations addFirst:implementations. |
|
199 |
"/ ] |
|
4807
8f6cd2048807
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4806
diff
changeset
|
200 |
]. |
4789 | 201 |
]. |
202 |
||
203 |
performCompletion := |
|
204 |
[:selectedListIndex | |
|
205 |
|indexInSuggestions| |
|
206 |
||
207 |
self closeCompletionView. |
|
208 |
indexInSuggestions := selectedListIndex - suggestionOffset. |
|
209 |
(selectedListIndex == indexOfSnippet) ifTrue:[ |
|
210 |
"/ replace the sniplet |
|
211 |
editView sensor pushUserEvent:#expandAbbreviation for:editView |
|
212 |
] ifFalse:[ |
|
213 |
LastCompletions isNil ifTrue:[ |
|
214 |
LastCompletions := OrderedCollection new. |
|
215 |
]. |
|
216 |
LastCompletions add:(suggestions at:indexInSuggestions). |
|
217 |
LastCompletions size > 200 ifTrue:[ |
|
218 |
LastCompletions removeLast |
|
219 |
]. |
|
5694 | 220 |
|
4789 | 221 |
actions notNil ifTrue:[ |
222 |
actions isBlock ifTrue:[ |
|
5694 | 223 |
(numFirst notNil and:[indexInSuggestions > numFirst]) ifTrue:[ |
224 |
indexInSuggestions := indexInSuggestions + numSkipped - 1. |
|
225 |
]. |
|
4789 | 226 |
actions value:indexInSuggestions |
227 |
] ifFalse:[ |
|
5711 | 228 |
(actions at:indexInSuggestions) valueWithOptionalArgument:indexInSuggestions |
4789 | 229 |
]. |
230 |
]. |
|
231 |
]. |
|
232 |
"/ disabled - user has made his choice; so don't show more suggestions |
|
233 |
"/ editView sensor pushUserEvent:#updateCompletionList for:self |
|
234 |
]. |
|
235 |
||
4809
d4cc6e25b3d3
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
236 |
(autoSelectArg |
d4cc6e25b3d3
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
237 |
and:[ (suggestions size == 1) |
d4cc6e25b3d3
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
238 |
and:[ preselectIdx == 1 |
d4cc6e25b3d3
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
239 |
and:[ preselectIdx ~~ indexOfSnippet ]]]) ifTrue:[ |
4789 | 240 |
"/ do it, right here and now |
241 |
performCompletion value:preselectIdx. |
|
242 |
] ifFalse:[ |
|
243 |
top open. |
|
244 |
v list:suggestions |
|
245 |
expandTabs:false scanForNonStrings:false |
|
246 |
includesNonStrings:false redraw:true. |
|
247 |
||
248 |
implementations notNil ifTrue:[ |
|
249 |
implementations keysAndValuesDo:[:idx :impls | |
|
250 |
|implsMenu| |
|
251 |
||
252 |
impls notEmptyOrNil ifTrue:[ |
|
253 |
implsMenu := Menu new. |
|
254 |
impls do:[:each | |
|
255 |
implsMenu addItem:(MenuItem new label:each name). |
|
256 |
]. |
|
257 |
v subMenuAt:idx put:implsMenu |
|
258 |
]. |
|
259 |
]. |
|
260 |
]. |
|
261 |
||
262 |
v enable:true. |
|
263 |
preselectIdx notNil ifTrue:[ |
|
264 |
"/ very disturbing!! |
|
265 |
v selection:preselectIdx. |
|
266 |
]. |
|
267 |
v extent:completionView preferredExtentForContents. |
|
268 |
v action:performCompletion. |
|
269 |
||
270 |
(top ~~ v) ifTrue:[ |
|
271 |
top resizeToFit. |
|
272 |
top bottom > v device usableHeight ifTrue:[ |
|
273 |
top origin:((top origin x) @ (v device usableHeight - v height)). |
|
274 |
]. |
|
275 |
top raise. |
|
276 |
] |
|
277 |
] |
|
278 |
] |
|
279 |
] |
|
6224 | 280 |
|
281 |
"Modified: / 05-11-2017 / 11:10:47 / cg" |
|
4789 | 282 |
! ! |
283 |
||
284 |
!WorkspaceCompletionSupport class methodsFor:'documentation'! |
|
285 |
||
286 |
version |
|
5694 | 287 |
^ '$Header$' |
4789 | 288 |
! |
289 |
||
290 |
version_CVS |
|
5694 | 291 |
^ '$Header$' |
4789 | 292 |
! ! |
293 |