author | Jan Vrany <jan.vrany@labware.com> |
Mon, 15 Nov 2021 20:13:51 +0000 | |
branch | jv |
changeset 6854 | 1f5ae7573af6 |
parent 6085 | 4ecbe3a93aec |
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:[ |
|
69 |
DoWhatIMeanSupport |
|
4802
a25143f6e512
Added codeAspect to EditTextView to signify what's being edited.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4789
diff
changeset
|
70 |
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
|
71 |
language: editView editedLanguage |
4789 | 72 |
method:editView editedMethod |
73 |
orClass:editView editedClass |
|
74 |
context:contextOrNil |
|
75 |
codeView:editView |
|
76 |
into:[:listOfSuggestions :listOfActions :titleWhenAsking | |
|
77 |
"/ (listOfSuggestions contains:[:l | l isEmptyOrNil]) ifTrue:[self halt]. |
|
78 |
suggestions := listOfSuggestions collect:[:entry | entry isArray ifTrue:[entry first] ifFalse:[entry]]. |
|
79 |
implementations := listOfSuggestions collect:[:entry | entry isArray ifTrue:[entry second] ifFalse:[nil]]. |
|
80 |
actions := listOfActions. |
|
81 |
nil "/ must return nil to avoid DWIM to do it itself (for now) |
|
82 |
] |
|
83 |
]. |
|
84 |
"/ Transcript show:'suggestions: '; showCR:suggestions. |
|
5047
091548040432
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4870
diff
changeset
|
85 |
"/ Transcript show:'actions: '; showCR:actions. |
4789 | 86 |
editView sensor |
87 |
pushUserEvent:#'suggestionsArrived:implementations:actions:autoSelect:' |
|
88 |
for:self |
|
89 |
withArguments:{suggestions . implementations . actions . autoSelect } |
|
90 |
||
91 |
"Created: / 26-09-2013 / 17:44:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
92 |
! |
|
93 |
||
94 |
suggestionsArrived:suggestionsArg implementations:implementationsArg actions:actionsArg autoSelect:autoSelectArg |
|
95 |
"the background process has generated some suggestions" |
|
96 |
||
5694 | 97 |
|v numShown numFirst numLast numSkipped |
98 |
suggestions implementations actions suggestionOffset keyAndSnippet indexOfSnippet| |
|
4789 | 99 |
|
100 |
(editView sensor hasKeyPressEventFor:nil) ifTrue:[ |
|
101 |
self closeCompletionView. |
|
102 |
^ self |
|
103 |
]. |
|
104 |
||
105 |
implementations := implementationsArg. |
|
106 |
actions := actionsArg. |
|
107 |
||
108 |
suggestions := suggestionsArg ? #(). |
|
5695 | 109 |
numShown := 25. |
5694 | 110 |
suggestions size > numShown ifTrue:[ |
111 |
numFirst := numShown-5. |
|
112 |
numLast := 5. |
|
113 |
numSkipped := suggestions size-numShown. |
|
114 |
suggestions := (suggestions copyTo:numShown-5) |
|
5723 | 115 |
, { ('<< %1 more skipped >>' bindWith:numSkipped) withColor:Color grey } |
5694 | 116 |
, (suggestions copyLast:5). |
117 |
implementations isArray ifTrue:[ |
|
118 |
implementations := (implementations copyTo:numShown-5),#(nil),(implementations copyLast:5). |
|
119 |
]. |
|
120 |
actions isArray ifTrue:[ |
|
121 |
actions := (actions copyTo:numShown-5),#(nil),(actions copyLast:5). |
|
122 |
]. |
|
4789 | 123 |
]. |
124 |
||
125 |
"/ append snipplet, if any (can be easily reached via CRSR-up) |
|
126 |
suggestionOffset := 0. |
|
127 |
indexOfSnippet := nil. |
|
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
128 |
UserPreferences current appendAbbreviationsToCompletionSuggestions ifTrue:[ |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
129 |
(keyAndSnippet := editView findAbbreviationKeyBeforeCursor) notNil ifTrue:[ |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
130 |
|abbrev sniplet i line| |
4789 | 131 |
|
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
132 |
abbrev := keyAndSnippet first. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
133 |
sniplet := keyAndSnippet second. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
134 |
|
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
135 |
"/ 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
|
136 |
line := editView lineStringBeforeCursor. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
137 |
i := line findLast:[:ch | ch isLetterOrDigit not]. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
138 |
(i < (line size - abbrev size - 1)) ifFalse:[ |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
139 |
sniplet := sniplet copyWithout:$!!. |
4789 | 140 |
|
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
141 |
"/ true, false and self are often found in both lists |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
142 |
(suggestions includes:sniplet) ifFalse:[ |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
143 |
suggestions isEmpty ifFalse:[ suggestions := suggestions copyWith: '-' ]. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
144 |
suggestions := suggestions copyWith: ( '%1 %2' |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
145 |
bindWith:(sniplet asStringCollection first "contractTo:25") |
5723 | 146 |
with: ( ('("',abbrev,'" snippet)') withColor:Color gray)). |
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
147 |
indexOfSnippet := suggestions size. |
4789 | 148 |
|
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
149 |
"/ change below, when reversing the order in above code |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
150 |
"/ suggestionOffset := 2. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
151 |
] |
4789 | 152 |
] |
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
153 |
]. |
4789 | 154 |
]. |
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
155 |
|
4789 | 156 |
suggestions isEmptyOrNil ifTrue:[ |
157 |
self closeCompletionView. |
|
158 |
^ self |
|
159 |
]. |
|
160 |
(v := completionView) isNil ifTrue: [ |
|
161 |
^ self |
|
162 |
]. |
|
163 |
||
164 |
v sensor |
|
165 |
pushUserEvent:#value |
|
166 |
for:[ |
|
167 |
|top idx preselectIdx performCompletion| |
|
168 |
||
169 |
(v == completionView) ifTrue: [ |
|
170 |
top := v topView. |
|
4810
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
171 |
autoSelectArg ifTrue:[ |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
172 |
LastCompletions notNil ifTrue:[ |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
173 |
"/ one of the last completions in list? |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
174 |
idx := LastCompletions findFirst:[:compl | suggestions includes:compl]. |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
175 |
idx ~~ 0 ifTrue:[ |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
176 |
preselectIdx := suggestions indexOf:(LastCompletions at:idx). |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
177 |
]. |
4789 | 178 |
]. |
4810
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
179 |
(preselectIdx isNil and:[suggestions size == 1]) ifTrue:[ |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
180 |
preselectIdx := 1. |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
181 |
]. |
4789 | 182 |
]. |
183 |
preselectIdx notNil ifTrue:[ |
|
184 |
|pref| |
|
185 |
||
186 |
pref := suggestions at:preselectIdx. |
|
4807
8f6cd2048807
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4806
diff
changeset
|
187 |
pref notNil ifTrue:[ |
8f6cd2048807
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4806
diff
changeset
|
188 |
"/ for now, do not move to front (action needs the index) |
8f6cd2048807
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4806
diff
changeset
|
189 |
suggestions at:preselectIdx put:(pref allBold). |
4789 | 190 |
"/ suggestions removeAtIndex:preselectIdx. |
191 |
"/ suggestions addFirst:(pref allBold). |
|
192 |
"/ implementations notNil ifTrue:[ |
|
193 |
"/ implementations removeAtIndex:preselectIdx. |
|
194 |
"/ implementations addFirst:implementations. |
|
195 |
"/ ] |
|
4807
8f6cd2048807
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4806
diff
changeset
|
196 |
]. |
4789 | 197 |
]. |
198 |
||
199 |
performCompletion := |
|
200 |
[:selectedListIndex | |
|
201 |
|indexInSuggestions| |
|
202 |
||
203 |
self closeCompletionView. |
|
204 |
indexInSuggestions := selectedListIndex - suggestionOffset. |
|
205 |
(selectedListIndex == indexOfSnippet) ifTrue:[ |
|
206 |
"/ replace the sniplet |
|
207 |
editView sensor pushUserEvent:#expandAbbreviation for:editView |
|
208 |
] ifFalse:[ |
|
209 |
LastCompletions isNil ifTrue:[ |
|
210 |
LastCompletions := OrderedCollection new. |
|
211 |
]. |
|
212 |
LastCompletions add:(suggestions at:indexInSuggestions). |
|
213 |
LastCompletions size > 200 ifTrue:[ |
|
214 |
LastCompletions removeLast |
|
215 |
]. |
|
5694 | 216 |
|
4789 | 217 |
actions notNil ifTrue:[ |
218 |
actions isBlock ifTrue:[ |
|
5694 | 219 |
(numFirst notNil and:[indexInSuggestions > numFirst]) ifTrue:[ |
220 |
indexInSuggestions := indexInSuggestions + numSkipped - 1. |
|
221 |
]. |
|
4789 | 222 |
actions value:indexInSuggestions |
223 |
] ifFalse:[ |
|
5711 | 224 |
(actions at:indexInSuggestions) valueWithOptionalArgument:indexInSuggestions |
4789 | 225 |
]. |
226 |
]. |
|
227 |
]. |
|
228 |
"/ disabled - user has made his choice; so don't show more suggestions |
|
229 |
"/ editView sensor pushUserEvent:#updateCompletionList for:self |
|
230 |
]. |
|
231 |
||
4809
d4cc6e25b3d3
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
232 |
(autoSelectArg |
d4cc6e25b3d3
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
233 |
and:[ (suggestions size == 1) |
d4cc6e25b3d3
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
234 |
and:[ preselectIdx == 1 |
d4cc6e25b3d3
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
235 |
and:[ preselectIdx ~~ indexOfSnippet ]]]) ifTrue:[ |
4789 | 236 |
"/ do it, right here and now |
237 |
performCompletion value:preselectIdx. |
|
238 |
] ifFalse:[ |
|
239 |
top open. |
|
240 |
v list:suggestions |
|
241 |
expandTabs:false scanForNonStrings:false |
|
242 |
includesNonStrings:false redraw:true. |
|
243 |
||
244 |
implementations notNil ifTrue:[ |
|
245 |
implementations keysAndValuesDo:[:idx :impls | |
|
246 |
|implsMenu| |
|
247 |
||
248 |
impls notEmptyOrNil ifTrue:[ |
|
249 |
implsMenu := Menu new. |
|
250 |
impls do:[:each | |
|
251 |
implsMenu addItem:(MenuItem new label:each name). |
|
252 |
]. |
|
253 |
v subMenuAt:idx put:implsMenu |
|
254 |
]. |
|
255 |
]. |
|
256 |
]. |
|
257 |
||
258 |
v enable:true. |
|
259 |
preselectIdx notNil ifTrue:[ |
|
260 |
"/ very disturbing!! |
|
261 |
v selection:preselectIdx. |
|
262 |
]. |
|
263 |
v extent:completionView preferredExtentForContents. |
|
264 |
v action:performCompletion. |
|
265 |
||
266 |
(top ~~ v) ifTrue:[ |
|
267 |
top resizeToFit. |
|
268 |
top bottom > v device usableHeight ifTrue:[ |
|
269 |
top origin:((top origin x) @ (v device usableHeight - v height)). |
|
270 |
]. |
|
271 |
top raise. |
|
272 |
] |
|
273 |
] |
|
274 |
] |
|
275 |
] |
|
276 |
! ! |
|
277 |
||
278 |
!WorkspaceCompletionSupport class methodsFor:'documentation'! |
|
279 |
||
280 |
version |
|
5694 | 281 |
^ '$Header$' |
4789 | 282 |
! |
283 |
||
284 |
version_CVS |
|
5694 | 285 |
^ '$Header$' |
4789 | 286 |
! ! |
287 |