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