author | Claus Gittinger <cg@exept.de> |
Wed, 09 Oct 2013 15:06:18 +0200 | |
changeset 4808 | b37597d2df93 |
parent 4805 | dd3fd2a4f3e9 |
child 4817 | 597e3af71218 |
permissions | -rw-r--r-- |
4788 | 1 |
"{ Package: 'stx:libwidg' }" |
2 |
||
3 |
Object subclass:#EditTextViewCompletionSupport |
|
4 |
instanceVariableNames:'completionView completionProcess editView autoSelect' |
|
5 |
classVariableNames:'LastCompletions' |
|
6 |
poolDictionaries:'' |
|
7 |
category:'Views-Text' |
|
8 |
! |
|
9 |
||
10 |
!EditTextViewCompletionSupport class methodsFor:'documentation'! |
|
11 |
||
12 |
documentation |
|
13 |
" |
|
14 |
An abstract supperclass to support completion in text views. |
|
15 |
Individual completion engines may create a subclass of |
|
16 |
EditTextCompletionSupport and customize it. |
|
17 |
||
18 |
Basucally, they have to implement #computeCompletions |
|
19 |
||
20 |
[author:] |
|
21 |
Claus Gittinger |
|
22 |
||
23 |
[instance variables:] |
|
24 |
||
25 |
[class variables:] |
|
26 |
||
27 |
[see also:] |
|
28 |
||
29 |
" |
|
30 |
! ! |
|
31 |
||
32 |
!EditTextViewCompletionSupport class methodsFor:'instance creation'! |
|
33 |
||
34 |
for:anEditView |
|
35 |
^ self new editView:anEditView |
|
36 |
! ! |
|
37 |
||
38 |
!EditTextViewCompletionSupport class methodsFor:'queries'! |
|
39 |
||
40 |
isAbstract |
|
41 |
^true |
|
42 |
||
43 |
"Created: / 26-09-2013 / 16:22:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
44 |
! ! |
|
45 |
||
46 |
!EditTextViewCompletionSupport methodsFor:'accessing'! |
|
47 |
||
48 |
editView:anEditTextView |
|
49 |
editView := anEditTextView. |
|
50 |
! ! |
|
51 |
||
52 |
!EditTextViewCompletionSupport methodsFor:'events'! |
|
53 |
||
54 |
buttonPress:button x:x y:y |
|
55 |
self closeCompletionView. |
|
56 |
! |
|
57 |
||
58 |
editViewLostFocus |
|
59 |
completionView notNil ifTrue:[ |
|
4808
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
60 |
"/ this is a hack for Windows: |
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
61 |
"/ on windows, an activate:false event is first sent to my textView, |
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
62 |
"/ then an activate is sent to the completion popup. |
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
63 |
"/ this is done BEFORE the buttonPress event is delivered. |
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
64 |
"/ therefore, allow for the activate of the completionMenu and it's button event to be processed. |
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
65 |
"/ before forcing it to be closed... |
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
66 |
"/ self closeCompletionView |
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
67 |
editView graphicsDevice anyButtonPressed ifTrue:[ |
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
68 |
editView sensor pushUserEvent:#editViewLostFocus for:self. |
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
69 |
]. |
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
70 |
]. |
4788 | 71 |
! |
72 |
||
73 |
handleKeyPress:key x:x y:y |
|
74 |
"return true, if I have eaten this keypress" |
|
75 |
||
76 |
|ch completeImmediate eatCursorLeftOrRight| |
|
77 |
||
78 |
completeImmediate := UserPreferences current immediateCodeCompletion. |
|
79 |
||
80 |
"/ open on CTRL- or TAB-key? |
|
81 |
(completionView isNil or:[completionView realized not]) ifTrue:[ |
|
82 |
editView hasSelection ifFalse:[ |
|
83 |
((ch := editView characterBeforeCursor) notNil "/ i.e. not at begin of line |
|
84 |
and:[ ch isSeparator not ]) ifTrue:[ |
|
85 |
(key == #Control_L or:[ key == #Ctrl or:[ key == #Control_R or:[ key == #Control]]]) ifTrue:[ |
|
86 |
UserPreferences current codeCompletionOnControlKey ifTrue:[ |
|
87 |
autoSelect := true. |
|
88 |
self updateCompletionList |
|
89 |
] |
|
90 |
]. |
|
4805
dd3fd2a4f3e9
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4788
diff
changeset
|
91 |
((key == #Tab) and:[editView sensor shiftDown not]) ifTrue:[ |
4788 | 92 |
UserPreferences current codeCompletionOnTabKey ifTrue:[ |
93 |
autoSelect := true. |
|
94 |
self updateCompletionList. |
|
95 |
^ true |
|
96 |
]. |
|
97 |
] |
|
98 |
]. |
|
99 |
]. |
|
100 |
^ false. |
|
101 |
]. |
|
102 |
||
103 |
"/ key for completion view ? (careful: do not forward too many, it would disturb user's typing) |
|
104 |
key isCharacter ifFalse:[ |
|
105 |
"/ forward to menu |
|
106 |
(completionView notNil) ifTrue:[ |
|
107 |
eatCursorLeftOrRight := false. |
|
108 |
"/ completeImmediate not |
|
109 |
"/ or:[ editView sensor shiftDown |
|
110 |
"/ or:[ editView sensor ctrlDown ]]. |
|
111 |
(key == #CursorDown |
|
112 |
or:[ (key == #CursorUp) |
|
113 |
or:[ ((key == #CursorLeft) and:[eatCursorLeftOrRight]) |
|
114 |
or:[ ((key == #CursorRight) and:[eatCursorLeftOrRight]) |
|
115 |
or:[ ((key == #Return) and:[ completionView hasSelection ]) |
|
116 |
]]]]) ifTrue:[ |
|
117 |
"/ only with shift - normal user typing should not interfere with completion |
|
118 |
true "editView sensor shiftDown" ifTrue:[ |
|
119 |
"/ forward to completion view |
|
120 |
completionView sensor pushUserEvent:#value for:[ completionView keyPress:key x:0 y:0 ]. |
|
121 |
^ true. |
|
122 |
]. |
|
123 |
]. |
|
124 |
||
125 |
(key == #Control_L or:[ key == #Control_R or:[ key == #Control or:[ key == #Ctrl ]]]) ifTrue:[ |
|
126 |
"/ CTRL is a toggle |
|
127 |
self closeCompletionView. |
|
128 |
^ true. |
|
129 |
"/ ^ false |
|
130 |
]. |
|
131 |
(key == #Escape) ifTrue:[ |
|
132 |
self closeCompletionView. |
|
133 |
^ true "/ EAT |
|
134 |
]. |
|
135 |
"/ shift does not close |
|
136 |
(key == #Shift_L or:[ key == #Shift_R or:[ key == #Shift]]) ifTrue:[ |
|
137 |
^ false "/ don' eat |
|
138 |
]. |
|
139 |
||
140 |
self closeCompletionView. |
|
141 |
^ false "/ don' eat |
|
142 |
]. |
|
143 |
]. |
|
144 |
^ false. |
|
145 |
! |
|
146 |
||
147 |
postKeyPress:key |
|
148 |
UserPreferences current immediateCodeCompletion ifFalse:[ |
|
149 |
"/ only update, if already open |
|
150 |
completionView isNil ifTrue:[^ self]. |
|
151 |
]. |
|
152 |
||
153 |
(key == #BackSpace or:[key == #BasicBackspace]) ifTrue:[ |
|
154 |
autoSelect := false. |
|
155 |
self updateCompletionList. |
|
156 |
^ self |
|
157 |
]. |
|
158 |
||
159 |
key isCharacter ifTrue:[ |
|
160 |
key isSeparator ifTrue:[ |
|
161 |
self closeCompletionView |
|
162 |
] ifFalse:[ |
|
163 |
autoSelect := false. |
|
164 |
self updateCompletionList. |
|
165 |
]. |
|
166 |
^ self |
|
167 |
]. |
|
168 |
! ! |
|
169 |
||
170 |
!EditTextViewCompletionSupport methodsFor:'private'! |
|
171 |
||
172 |
release |
|
173 |
self stopCompletionProcess. |
|
174 |
self closeCompletionView. |
|
175 |
super release |
|
176 |
! |
|
177 |
||
178 |
startCompletionProcess |
|
179 |
"start the code completion process in the background" |
|
180 |
||
181 |
|initialList cursorX cursorY| |
|
182 |
||
183 |
"/ terminate any previous process |
|
184 |
self stopCompletionProcess. |
|
185 |
||
186 |
(editView sensor hasKeyPressEventFor:nil) ifTrue:[ |
|
187 |
self closeCompletionView. |
|
188 |
^ self |
|
189 |
]. |
|
190 |
((cursorX := editView xOfCursor) isNil |
|
191 |
or:[ (cursorY := editView yOfCursor) isNil ]) ifTrue:[ |
|
192 |
"/ no cursor - user is selecting, or cursor has been scrolled out of sight. |
|
193 |
self closeCompletionView. |
|
194 |
^ self |
|
195 |
]. |
|
196 |
||
197 |
initialList := #( 'busy...' ). |
|
198 |
||
199 |
self openCompletionView. |
|
200 |
||
201 |
completionProcess := |
|
202 |
[ |
|
203 |
self computeCompletions. |
|
204 |
] forkAt:(Processor activePriority - 1). |
|
205 |
||
206 |
"Modified: / 26-09-2013 / 17:36:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
207 |
! |
|
208 |
||
209 |
stopCompletionProcess |
|
210 |
"kill any background completion process" |
|
211 |
||
212 |
|p| |
|
213 |
||
214 |
(p := completionProcess) notNil ifTrue:[ |
|
215 |
completionProcess := nil. |
|
216 |
p terminate. |
|
217 |
]. |
|
218 |
! |
|
219 |
||
220 |
updateCompletionList |
|
221 |
"called for keypress events" |
|
222 |
||
223 |
self startCompletionProcess. |
|
224 |
! ! |
|
225 |
||
226 |
!EditTextViewCompletionSupport methodsFor:'private-API'! |
|
227 |
||
228 |
closeCompletionView |
|
229 |
|v| |
|
230 |
||
231 |
(v := completionView) notNil ifTrue:[ |
|
232 |
completionView := nil. |
|
233 |
"/ let it close itself - avoids synchronization problems |
|
234 |
v sensor |
|
235 |
pushUserEvent:#value |
|
236 |
for:[ v topView destroy ] |
|
237 |
]. |
|
238 |
! |
|
239 |
||
240 |
computeCompletions |
|
241 |
"Actually compute the completions and update the completion view." |
|
242 |
||
243 |
self subclassResponsibility |
|
244 |
||
245 |
"Created: / 26-09-2013 / 17:35:17 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
246 |
! |
|
247 |
||
248 |
openCompletionView |
|
249 |
"Opens the completion view with an initial list. Called as soon as |
|
250 |
completion is initiated but completion options are not yet computed." |
|
251 |
||
252 |
self openCompletionView: (Array with: 'Busy...') |
|
253 |
||
254 |
"Created: / 26-09-2013 / 17:06:12 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
255 |
! |
|
256 |
||
257 |
openCompletionView: list |
|
258 |
"Makes sure the completion view is opened and with given `list`." |
|
259 |
||
260 |
| movePos topView | |
|
261 |
"/ move the window |
|
262 |
movePos := editView device translatePoint:(editView xOfCursor @ editView yOfCursor) fromView:editView toView:nil. |
|
263 |
movePos := movePos - (editView viewOrigin x @ 0). |
|
264 |
"/ currently, we have to stay away a bit, to avoid getting the focus |
|
265 |
movePos := movePos + (60 @ (editView font height)). |
|
266 |
||
267 |
completionView isNil ifTrue:[ |
|
268 |
completionView := CodeCompletionHelpMenuView new. |
|
269 |
completionView name:'completion'. |
|
270 |
completionView level:0. |
|
271 |
completionView list:list. |
|
272 |
completionView enable:false. |
|
273 |
completionView extent:completionView preferredExtentForContents. |
|
274 |
completionView font: editView font. |
|
275 |
topView := CodeCompletionHelpView with:completionView. |
|
276 |
topView origin:movePos. |
|
277 |
topView resizeToFit. |
|
278 |
"/ topView open. |
|
279 |
] ifFalse:[ |
|
280 |
completionView list:list. |
|
281 |
topView := completionView topView. |
|
282 |
topView ~~ completionView ifTrue:[ |
|
283 |
topView origin:movePos. |
|
284 |
topView resizeToFit. |
|
285 |
] |
|
286 |
]. |
|
287 |
||
288 |
"Created: / 26-09-2013 / 17:07:34 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
289 |
! ! |
|
290 |
||
291 |
!EditTextViewCompletionSupport class methodsFor:'documentation'! |
|
292 |
||
293 |
version |
|
4808
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
294 |
^ '$Header: /cvs/stx/stx/libwidg/EditTextViewCompletionSupport.st,v 1.3 2013-10-09 13:06:18 cg Exp $' |
4788 | 295 |
! |
296 |
||
297 |
version_CVS |
|
4808
b37597d2df93
class: EditTextViewCompletionSupport
Claus Gittinger <cg@exept.de>
parents:
4805
diff
changeset
|
298 |
^ '$Header: /cvs/stx/stx/libwidg/EditTextViewCompletionSupport.st,v 1.3 2013-10-09 13:06:18 cg Exp $' |
4788 | 299 |
! ! |
300 |