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