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