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