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:[
|
|
60 |
self closeCompletionView
|
|
61 |
]
|
|
62 |
!
|
|
63 |
|
|
64 |
handleKeyPress:key x:x y:y
|
|
65 |
"return true, if I have eaten this keypress"
|
|
66 |
|
|
67 |
|ch completeImmediate eatCursorLeftOrRight|
|
|
68 |
|
|
69 |
completeImmediate := UserPreferences current immediateCodeCompletion.
|
|
70 |
|
|
71 |
"/ open on CTRL- or TAB-key?
|
|
72 |
(completionView isNil or:[completionView realized not]) ifTrue:[
|
|
73 |
editView hasSelection ifFalse:[
|
|
74 |
((ch := editView characterBeforeCursor) notNil "/ i.e. not at begin of line
|
|
75 |
and:[ ch isSeparator not ]) ifTrue:[
|
|
76 |
(key == #Control_L or:[ key == #Ctrl or:[ key == #Control_R or:[ key == #Control]]]) ifTrue:[
|
|
77 |
UserPreferences current codeCompletionOnControlKey ifTrue:[
|
|
78 |
autoSelect := true.
|
|
79 |
self updateCompletionList
|
|
80 |
]
|
|
81 |
].
|
|
82 |
(key == #Tab) ifTrue:[
|
|
83 |
UserPreferences current codeCompletionOnTabKey ifTrue:[
|
|
84 |
autoSelect := true.
|
|
85 |
self updateCompletionList.
|
|
86 |
^ true
|
|
87 |
].
|
|
88 |
]
|
|
89 |
].
|
|
90 |
].
|
|
91 |
^ false.
|
|
92 |
].
|
|
93 |
|
|
94 |
"/ key for completion view ? (careful: do not forward too many, it would disturb user's typing)
|
|
95 |
key isCharacter ifFalse:[
|
|
96 |
"/ forward to menu
|
|
97 |
(completionView notNil) ifTrue:[
|
|
98 |
eatCursorLeftOrRight := false.
|
|
99 |
"/ completeImmediate not
|
|
100 |
"/ or:[ editView sensor shiftDown
|
|
101 |
"/ or:[ editView sensor ctrlDown ]].
|
|
102 |
(key == #CursorDown
|
|
103 |
or:[ (key == #CursorUp)
|
|
104 |
or:[ ((key == #CursorLeft) and:[eatCursorLeftOrRight])
|
|
105 |
or:[ ((key == #CursorRight) and:[eatCursorLeftOrRight])
|
|
106 |
or:[ ((key == #Return) and:[ completionView hasSelection ])
|
|
107 |
]]]]) ifTrue:[
|
|
108 |
"/ only with shift - normal user typing should not interfere with completion
|
|
109 |
true "editView sensor shiftDown" ifTrue:[
|
|
110 |
"/ forward to completion view
|
|
111 |
completionView sensor pushUserEvent:#value for:[ completionView keyPress:key x:0 y:0 ].
|
|
112 |
^ true.
|
|
113 |
].
|
|
114 |
].
|
|
115 |
|
|
116 |
(key == #Control_L or:[ key == #Control_R or:[ key == #Control or:[ key == #Ctrl ]]]) ifTrue:[
|
|
117 |
"/ CTRL is a toggle
|
|
118 |
self closeCompletionView.
|
|
119 |
^ true.
|
|
120 |
"/ ^ false
|
|
121 |
].
|
|
122 |
(key == #Escape) ifTrue:[
|
|
123 |
self closeCompletionView.
|
|
124 |
^ true "/ EAT
|
|
125 |
].
|
|
126 |
"/ shift does not close
|
|
127 |
(key == #Shift_L or:[ key == #Shift_R or:[ key == #Shift]]) ifTrue:[
|
|
128 |
^ false "/ don' eat
|
|
129 |
].
|
|
130 |
|
|
131 |
self closeCompletionView.
|
|
132 |
^ false "/ don' eat
|
|
133 |
].
|
|
134 |
].
|
|
135 |
^ false.
|
|
136 |
!
|
|
137 |
|
|
138 |
postKeyPress:key
|
|
139 |
UserPreferences current immediateCodeCompletion ifFalse:[
|
|
140 |
"/ only update, if already open
|
|
141 |
completionView isNil ifTrue:[^ self].
|
|
142 |
].
|
|
143 |
|
|
144 |
(key == #BackSpace or:[key == #BasicBackspace]) ifTrue:[
|
|
145 |
autoSelect := false.
|
|
146 |
self updateCompletionList.
|
|
147 |
^ self
|
|
148 |
].
|
|
149 |
|
|
150 |
key isCharacter ifTrue:[
|
|
151 |
key isSeparator ifTrue:[
|
|
152 |
self closeCompletionView
|
|
153 |
] ifFalse:[
|
|
154 |
autoSelect := false.
|
|
155 |
self updateCompletionList.
|
|
156 |
].
|
|
157 |
^ self
|
|
158 |
].
|
|
159 |
! !
|
|
160 |
|
|
161 |
!EditTextViewCompletionSupport methodsFor:'private'!
|
|
162 |
|
|
163 |
release
|
|
164 |
self stopCompletionProcess.
|
|
165 |
self closeCompletionView.
|
|
166 |
super release
|
|
167 |
!
|
|
168 |
|
|
169 |
startCompletionProcess
|
|
170 |
"start the code completion process in the background"
|
|
171 |
|
|
172 |
|initialList cursorX cursorY|
|
|
173 |
|
|
174 |
"/ terminate any previous process
|
|
175 |
self stopCompletionProcess.
|
|
176 |
|
|
177 |
(editView sensor hasKeyPressEventFor:nil) ifTrue:[
|
|
178 |
self closeCompletionView.
|
|
179 |
^ self
|
|
180 |
].
|
|
181 |
((cursorX := editView xOfCursor) isNil
|
|
182 |
or:[ (cursorY := editView yOfCursor) isNil ]) ifTrue:[
|
|
183 |
"/ no cursor - user is selecting, or cursor has been scrolled out of sight.
|
|
184 |
self closeCompletionView.
|
|
185 |
^ self
|
|
186 |
].
|
|
187 |
|
|
188 |
initialList := #( 'busy...' ).
|
|
189 |
|
|
190 |
self openCompletionView.
|
|
191 |
|
|
192 |
completionProcess :=
|
|
193 |
[
|
|
194 |
self computeCompletions.
|
|
195 |
] forkAt:(Processor activePriority - 1).
|
|
196 |
|
|
197 |
"Modified: / 26-09-2013 / 17:36:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
198 |
!
|
|
199 |
|
|
200 |
stopCompletionProcess
|
|
201 |
"kill any background completion process"
|
|
202 |
|
|
203 |
|p|
|
|
204 |
|
|
205 |
(p := completionProcess) notNil ifTrue:[
|
|
206 |
completionProcess := nil.
|
|
207 |
p terminate.
|
|
208 |
].
|
|
209 |
!
|
|
210 |
|
|
211 |
updateCompletionList
|
|
212 |
"called for keypress events"
|
|
213 |
|
|
214 |
self startCompletionProcess.
|
|
215 |
! !
|
|
216 |
|
|
217 |
!EditTextViewCompletionSupport methodsFor:'private-API'!
|
|
218 |
|
|
219 |
closeCompletionView
|
|
220 |
|v|
|
|
221 |
|
|
222 |
(v := completionView) notNil ifTrue:[
|
|
223 |
completionView := nil.
|
|
224 |
"/ let it close itself - avoids synchronization problems
|
|
225 |
v sensor
|
|
226 |
pushUserEvent:#value
|
|
227 |
for:[ v topView destroy ]
|
|
228 |
].
|
|
229 |
!
|
|
230 |
|
|
231 |
computeCompletions
|
|
232 |
"Actually compute the completions and update the completion view."
|
|
233 |
|
|
234 |
self subclassResponsibility
|
|
235 |
|
|
236 |
"Created: / 26-09-2013 / 17:35:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
237 |
!
|
|
238 |
|
|
239 |
openCompletionView
|
|
240 |
"Opens the completion view with an initial list. Called as soon as
|
|
241 |
completion is initiated but completion options are not yet computed."
|
|
242 |
|
|
243 |
self openCompletionView: (Array with: 'Busy...')
|
|
244 |
|
|
245 |
"Created: / 26-09-2013 / 17:06:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
246 |
!
|
|
247 |
|
|
248 |
openCompletionView: list
|
|
249 |
"Makes sure the completion view is opened and with given `list`."
|
|
250 |
|
|
251 |
| movePos topView |
|
|
252 |
"/ move the window
|
|
253 |
movePos := editView device translatePoint:(editView xOfCursor @ editView yOfCursor) fromView:editView toView:nil.
|
|
254 |
movePos := movePos - (editView viewOrigin x @ 0).
|
|
255 |
"/ currently, we have to stay away a bit, to avoid getting the focus
|
|
256 |
movePos := movePos + (60 @ (editView font height)).
|
|
257 |
|
|
258 |
completionView isNil ifTrue:[
|
|
259 |
completionView := CodeCompletionHelpMenuView new.
|
|
260 |
completionView name:'completion'.
|
|
261 |
completionView level:0.
|
|
262 |
completionView list:list.
|
|
263 |
completionView enable:false.
|
|
264 |
completionView extent:completionView preferredExtentForContents.
|
|
265 |
completionView font: editView font.
|
|
266 |
topView := CodeCompletionHelpView with:completionView.
|
|
267 |
topView origin:movePos.
|
|
268 |
topView resizeToFit.
|
|
269 |
"/ topView open.
|
|
270 |
] ifFalse:[
|
|
271 |
completionView list:list.
|
|
272 |
topView := completionView topView.
|
|
273 |
topView ~~ completionView ifTrue:[
|
|
274 |
topView origin:movePos.
|
|
275 |
topView resizeToFit.
|
|
276 |
]
|
|
277 |
].
|
|
278 |
|
|
279 |
"Created: / 26-09-2013 / 17:07:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
280 |
! !
|
|
281 |
|
|
282 |
!EditTextViewCompletionSupport class methodsFor:'documentation'!
|
|
283 |
|
|
284 |
version
|
|
285 |
^ '$Header: /cvs/stx/stx/libwidg/EditTextViewCompletionSupport.st,v 1.1 2013-09-26 17:07:20 vrany Exp $'
|
|
286 |
!
|
|
287 |
|
|
288 |
version_CVS
|
|
289 |
^ '$Header: /cvs/stx/stx/libwidg/EditTextViewCompletionSupport.st,v 1.1 2013-09-26 17:07:20 vrany Exp $'
|
|
290 |
! !
|
|
291 |
|