author | Claus Gittinger <cg@exept.de> |
Fri, 01 Sep 2017 14:30:38 +0200 | |
changeset 6202 | 3b9b166e5fc1 |
parent 6130 | c040bfbfde85 |
child 6224 | 051efa88ece4 |
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 ? #(). |
|
5695 | 111 |
numShown := 25. |
5694 | 112 |
suggestions size > numShown ifTrue:[ |
113 |
numFirst := numShown-5. |
|
114 |
numLast := 5. |
|
115 |
numSkipped := suggestions size-numShown. |
|
116 |
suggestions := (suggestions copyTo:numShown-5) |
|
5723 | 117 |
, { ('<< %1 more skipped >>' bindWith:numSkipped) withColor:Color grey } |
5694 | 118 |
, (suggestions copyLast:5). |
119 |
implementations isArray ifTrue:[ |
|
120 |
implementations := (implementations copyTo:numShown-5),#(nil),(implementations copyLast:5). |
|
121 |
]. |
|
122 |
actions isArray ifTrue:[ |
|
123 |
actions := (actions copyTo:numShown-5),#(nil),(actions copyLast:5). |
|
124 |
]. |
|
4789 | 125 |
]. |
126 |
||
127 |
"/ append snipplet, if any (can be easily reached via CRSR-up) |
|
128 |
suggestionOffset := 0. |
|
129 |
indexOfSnippet := nil. |
|
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
130 |
UserPreferences current appendAbbreviationsToCompletionSuggestions ifTrue:[ |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
131 |
(keyAndSnippet := editView findAbbreviationKeyBeforeCursor) notNil ifTrue:[ |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
132 |
|abbrev sniplet i line| |
4789 | 133 |
|
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
134 |
abbrev := keyAndSnippet first. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
135 |
sniplet := keyAndSnippet second. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
136 |
|
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
137 |
"/ 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
|
138 |
line := editView lineStringBeforeCursor. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
139 |
i := line findLast:[:ch | ch isLetterOrDigit not]. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
140 |
(i < (line size - abbrev size - 1)) ifFalse:[ |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
141 |
sniplet := sniplet copyWithout:$!!. |
4789 | 142 |
|
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
143 |
"/ true, false and self are often found in both lists |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
144 |
(suggestions includes:sniplet) ifFalse:[ |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
145 |
suggestions isEmpty ifFalse:[ suggestions := suggestions copyWith: '-' ]. |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
146 |
suggestions := suggestions copyWith: ( '%1 %2' |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
147 |
bindWith:(sniplet asStringCollection first "contractTo:25") |
5723 | 148 |
with: ( ('("',abbrev,'" snippet)') withColor:Color gray)). |
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
149 |
indexOfSnippet := suggestions size. |
4789 | 150 |
|
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
151 |
"/ change below, when reversing the order in above code |
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
152 |
"/ suggestionOffset := 2. |
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 |
]. |
4870
2714e7b0261e
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4810
diff
changeset
|
157 |
|
4789 | 158 |
suggestions isEmptyOrNil ifTrue:[ |
159 |
self closeCompletionView. |
|
160 |
^ self |
|
161 |
]. |
|
162 |
(v := completionView) isNil ifTrue: [ |
|
163 |
^ self |
|
164 |
]. |
|
165 |
||
166 |
v sensor |
|
167 |
pushUserEvent:#value |
|
168 |
for:[ |
|
169 |
|top idx preselectIdx performCompletion| |
|
170 |
||
171 |
(v == completionView) ifTrue: [ |
|
172 |
top := v topView. |
|
4810
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
173 |
autoSelectArg ifTrue:[ |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
174 |
LastCompletions notNil ifTrue:[ |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
175 |
"/ one of the last completions in list? |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
176 |
idx := LastCompletions findFirst:[:compl | suggestions includes:compl]. |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
177 |
idx ~~ 0 ifTrue:[ |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
178 |
preselectIdx := suggestions indexOf:(LastCompletions at:idx). |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
179 |
]. |
4789 | 180 |
]. |
4810
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
181 |
(preselectIdx isNil and:[suggestions size == 1]) ifTrue:[ |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
182 |
preselectIdx := 1. |
c273ec93c680
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4809
diff
changeset
|
183 |
]. |
4789 | 184 |
]. |
185 |
preselectIdx notNil ifTrue:[ |
|
186 |
|pref| |
|
187 |
||
188 |
pref := suggestions at:preselectIdx. |
|
4807
8f6cd2048807
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4806
diff
changeset
|
189 |
pref notNil ifTrue:[ |
8f6cd2048807
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4806
diff
changeset
|
190 |
"/ for now, do not move to front (action needs the index) |
8f6cd2048807
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4806
diff
changeset
|
191 |
suggestions at:preselectIdx put:(pref allBold). |
4789 | 192 |
"/ suggestions removeAtIndex:preselectIdx. |
193 |
"/ suggestions addFirst:(pref allBold). |
|
194 |
"/ implementations notNil ifTrue:[ |
|
195 |
"/ implementations removeAtIndex:preselectIdx. |
|
196 |
"/ implementations addFirst:implementations. |
|
197 |
"/ ] |
|
4807
8f6cd2048807
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4806
diff
changeset
|
198 |
]. |
4789 | 199 |
]. |
200 |
||
201 |
performCompletion := |
|
202 |
[:selectedListIndex | |
|
203 |
|indexInSuggestions| |
|
204 |
||
205 |
self closeCompletionView. |
|
206 |
indexInSuggestions := selectedListIndex - suggestionOffset. |
|
207 |
(selectedListIndex == indexOfSnippet) ifTrue:[ |
|
208 |
"/ replace the sniplet |
|
209 |
editView sensor pushUserEvent:#expandAbbreviation for:editView |
|
210 |
] ifFalse:[ |
|
211 |
LastCompletions isNil ifTrue:[ |
|
212 |
LastCompletions := OrderedCollection new. |
|
213 |
]. |
|
214 |
LastCompletions add:(suggestions at:indexInSuggestions). |
|
215 |
LastCompletions size > 200 ifTrue:[ |
|
216 |
LastCompletions removeLast |
|
217 |
]. |
|
5694 | 218 |
|
4789 | 219 |
actions notNil ifTrue:[ |
220 |
actions isBlock ifTrue:[ |
|
5694 | 221 |
(numFirst notNil and:[indexInSuggestions > numFirst]) ifTrue:[ |
222 |
indexInSuggestions := indexInSuggestions + numSkipped - 1. |
|
223 |
]. |
|
4789 | 224 |
actions value:indexInSuggestions |
225 |
] ifFalse:[ |
|
5711 | 226 |
(actions at:indexInSuggestions) valueWithOptionalArgument:indexInSuggestions |
4789 | 227 |
]. |
228 |
]. |
|
229 |
]. |
|
230 |
"/ disabled - user has made his choice; so don't show more suggestions |
|
231 |
"/ editView sensor pushUserEvent:#updateCompletionList for:self |
|
232 |
]. |
|
233 |
||
4809
d4cc6e25b3d3
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
234 |
(autoSelectArg |
d4cc6e25b3d3
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
235 |
and:[ (suggestions size == 1) |
d4cc6e25b3d3
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
236 |
and:[ preselectIdx == 1 |
d4cc6e25b3d3
class: WorkspaceCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
237 |
and:[ preselectIdx ~~ indexOfSnippet ]]]) ifTrue:[ |
4789 | 238 |
"/ do it, right here and now |
239 |
performCompletion value:preselectIdx. |
|
240 |
] ifFalse:[ |
|
241 |
top open. |
|
242 |
v list:suggestions |
|
243 |
expandTabs:false scanForNonStrings:false |
|
244 |
includesNonStrings:false redraw:true. |
|
245 |
||
246 |
implementations notNil ifTrue:[ |
|
247 |
implementations keysAndValuesDo:[:idx :impls | |
|
248 |
|implsMenu| |
|
249 |
||
250 |
impls notEmptyOrNil ifTrue:[ |
|
251 |
implsMenu := Menu new. |
|
252 |
impls do:[:each | |
|
253 |
implsMenu addItem:(MenuItem new label:each name). |
|
254 |
]. |
|
255 |
v subMenuAt:idx put:implsMenu |
|
256 |
]. |
|
257 |
]. |
|
258 |
]. |
|
259 |
||
260 |
v enable:true. |
|
261 |
preselectIdx notNil ifTrue:[ |
|
262 |
"/ very disturbing!! |
|
263 |
v selection:preselectIdx. |
|
264 |
]. |
|
265 |
v extent:completionView preferredExtentForContents. |
|
266 |
v action:performCompletion. |
|
267 |
||
268 |
(top ~~ v) ifTrue:[ |
|
269 |
top resizeToFit. |
|
270 |
top bottom > v device usableHeight ifTrue:[ |
|
271 |
top origin:((top origin x) @ (v device usableHeight - v height)). |
|
272 |
]. |
|
273 |
top raise. |
|
274 |
] |
|
275 |
] |
|
276 |
] |
|
277 |
] |
|
278 |
! ! |
|
279 |
||
280 |
!WorkspaceCompletionSupport class methodsFor:'documentation'! |
|
281 |
||
282 |
version |
|
5694 | 283 |
^ '$Header$' |
4789 | 284 |
! |
285 |
||
286 |
version_CVS |
|
5694 | 287 |
^ '$Header$' |
4789 | 288 |
! ! |
289 |