author | Claus Gittinger <cg@exept.de> |
Tue, 09 Jul 2019 20:53:39 +0200 | |
changeset 6083 | 7a2c0a30e75c |
parent 6082 | 96c1001f95ef |
child 6105 | 82efed08b020 |
permissions | -rw-r--r-- |
2376 | 1 |
" |
2 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
5542 | 3 |
All Rights Reserved |
2376 | 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 |
" |
|
12 |
"{ Package: 'stx:libwidg2' }" |
|
13 |
||
4710 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
2376 | 16 |
Object subclass:#DoWhatIMeanSupport |
5512 | 17 |
instanceVariableNames:'tree tokens languageOrNil classOrNil methodOrNil contextOrNil |
18 |
instanceOrNil codeView rememberedScopeNodes rememberedNodes |
|
5980 | 19 |
codeAspect partialString partialStringInterval classResolverHook' |
5512 | 20 |
classVariableNames:'LastSource LastParseTree LastScanTokens LastChoices |
21 |
LastCompletedSelectors Verbose' |
|
22 |
poolDictionaries:'' |
|
23 |
category:'System-Support' |
|
2376 | 24 |
! |
25 |
||
3854
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
26 |
Array variableSubclass:#InputCompletionResult |
5512 | 27 |
instanceVariableNames:'' |
28 |
classVariableNames:'' |
|
29 |
poolDictionaries:'' |
|
30 |
privateIn:DoWhatIMeanSupport |
|
3854
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
31 |
! |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
32 |
|
2376 | 33 |
!DoWhatIMeanSupport class methodsFor:'documentation'! |
34 |
||
35 |
copyright |
|
36 |
" |
|
37 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
5542 | 38 |
All Rights Reserved |
2376 | 39 |
|
40 |
This software is furnished under a license and may be used |
|
41 |
only in accordance with the terms of that license and with the |
|
42 |
inclusion of the above copyright notice. This software may not |
|
43 |
be provided or otherwise made available to, or used by, any |
|
44 |
other person. No title to or ownership of the software is |
|
45 |
hereby transferred. |
|
46 |
" |
|
47 |
! |
|
48 |
||
49 |
documentation |
|
50 |
" |
|
4352 | 51 |
Attention: this is currently being rewritten and refactored. |
52 |
Don't get mad at the ugly (and duplicate) code. |
|
53 |
Will cleanup when finished. |
|
54 |
||
2376 | 55 |
misc collected UI support (functional) |
4548 | 56 |
These used to be in the Smalltalk and SystemBrowser class; |
3299
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
57 |
however, they are only needed for programmers, and some of the stuff is useful in multiple |
4548 | 58 |
places. |
3667 | 59 |
Therefore it is: |
5542 | 60 |
1) not needed for standalone executables |
61 |
2) published here to avoid multiple implementations |
|
2376 | 62 |
|
63 |
[author:] |
|
5542 | 64 |
Claus Gittinger (cg@exept.de) |
2376 | 65 |
|
3299
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
66 |
" |
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
67 |
! ! |
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
68 |
|
5869 | 69 |
!DoWhatIMeanSupport class methodsFor:'cleanup'! |
70 |
||
71 |
lowSpaceCleanup |
|
72 |
LastSource := LastParseTree := LastScanTokens := nil. |
|
73 |
||
74 |
"Created: / 01-08-2018 / 22:31:39 / Claus Gittinger" |
|
75 |
! ! |
|
76 |
||
3299
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
77 |
!DoWhatIMeanSupport class methodsFor:'code completion'! |
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
78 |
|
4426
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
79 |
codeCompletionFor: aspect language: languageOrNil method:methodOrNil orClass:classOrNil context:contextOrNil codeView:codeView into:actionBlock |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
80 |
"aspect is so-called code-aspect symbol saying what's edited - #method, #expression, #classDefinition... |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
81 |
contextOrNil is the current context, if this is called from the debugger; |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
82 |
nil, if called from the browser. |
4591 | 83 |
If nonNil, we can make better guesses, |
84 |
because we actually know what a variable's type is." |
|
4426
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
85 |
|
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
86 |
^ self new |
4591 | 87 |
codeCompletionFor: aspect |
88 |
language: languageOrNil |
|
89 |
method:methodOrNil orClass:classOrNil |
|
90 |
context:contextOrNil |
|
91 |
codeView:codeView into:actionBlock |
|
4426
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
92 |
|
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
93 |
"Created: / 27-09-2013 / 10:19:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
94 |
! |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
95 |
|
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
96 |
codeCompletionForLanguage: languageOrNil class: classOrNil context:contextOrNil codeView:codeView |
3900
82fa031be815
comment/format in: #codeCompletionForClass:context:codeView:
Claus Gittinger <cg@exept.de>
parents:
3896
diff
changeset
|
97 |
"contextOrNil is the current context, if this is called from the debugger; |
3557 | 98 |
nil, if called from the browser. |
4591 | 99 |
If nonNil, we can make better guesses, |
100 |
because we actually know what a variable's type is." |
|
4238 | 101 |
|
4548 | 102 |
^ self new |
4591 | 103 |
codeCompletionForLanguage: languageOrNil |
104 |
class:classOrNil |
|
105 |
context:contextOrNil |
|
106 |
codeView:codeView |
|
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
107 |
|
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
108 |
"Created: / 18-09-2013 / 13:34:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
109 |
! ! |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
110 |
|
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
111 |
!DoWhatIMeanSupport class methodsFor:'code completion - obsolete'! |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
112 |
|
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
113 |
codeCompletionForClass:classOrNil context:contextOrNil codeView:codeView |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
114 |
<resource: #obsolete> |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
115 |
"contextOrNil is the current context, if this is called from the debugger; |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
116 |
nil, if called from the browser. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
117 |
If nonNil, we can make better guesses, because we actually know what a variable's type is. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
118 |
This is not yet done, sigh" |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
119 |
|
4548 | 120 |
^ self |
5542 | 121 |
codeCompletionForLanguage: nil class:classOrNil context:contextOrNil codeView:codeView |
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
122 |
|
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
123 |
"Modified: / 18-09-2013 / 13:34:57 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
4238 | 124 |
! |
125 |
||
126 |
codeCompletionForMethod:methodOrNil orClass:classOrNil context:contextOrNil codeView:codeView into:actionBlock |
|
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
127 |
<resource: #obsolete> |
4238 | 128 |
"contextOrNil is the current context, if this is called from the debugger; |
129 |
nil, if called from the browser. |
|
130 |
If nonNil, we can make better guesses, because we actually know what a variable's type is. |
|
131 |
This is not yet done, sigh" |
|
132 |
||
133 |
^ self new |
|
5542 | 134 |
codeCompletionForMethod:methodOrNil orClass:classOrNil |
135 |
context:contextOrNil |
|
136 |
codeView:codeView into:actionBlock |
|
3299
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
137 |
! ! |
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
138 |
|
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
139 |
!DoWhatIMeanSupport class methodsFor:'code completion-helpers'! |
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
140 |
|
3300 | 141 |
findNodeForInterval:interval in:source |
5272 | 142 |
"utility" |
143 |
||
144 |
|tree node remember| |
|
145 |
||
146 |
interval isEmpty ifTrue: [^ nil]. |
|
147 |
RBParser isNil ifTrue: [^ nil]. |
|
148 |
||
149 |
source = LastSource ifTrue:[ |
|
150 |
tree := LastParseTree. |
|
151 |
] ifFalse:[ |
|
152 |
remember := true. |
|
153 |
LastSource := LastParseTree := nil. |
|
154 |
tree := RBParser |
|
155 |
parseMethod:source |
|
156 |
onError: |
|
157 |
[:str :err ":nodesSoFar" | |
|
158 |
"/ Transcript showCR:'Parse-Error: ',str. |
|
159 |
nil |
|
160 |
]. |
|
161 |
||
162 |
tree isNil ifTrue:[ |
|
163 |
"/ try to parse as an expression |
|
164 |
tree := RBParser |
|
165 |
parseExpression:source |
|
166 |
onError: |
|
167 |
[:str :err ":nodesSoFar" | |
|
168 |
"Transcript showCR:'Parse-Error: ',str." |
|
169 |
nil |
|
170 |
]. |
|
171 |
||
172 |
tree isNil ifTrue:[ |
|
173 |
"/ try to parse the selected text alone as expression |
|
174 |
remember := false. |
|
175 |
tree := RBParser |
|
5973 | 176 |
parseExpression:(source copyFrom:(interval start max:1) to:((interval stop max:1) min:source size)) |
5272 | 177 |
onError: |
178 |
[:str :err ":nodesSoFar" | |
|
179 |
"Transcript showCR:'Parse-Error: ',str." |
|
180 |
nil |
|
181 |
]. |
|
182 |
^ tree |
|
183 |
]. |
|
184 |
]. |
|
185 |
remember ifTrue:[ |
|
186 |
LastSource := source. |
|
187 |
LastParseTree := tree. |
|
188 |
]. |
|
189 |
]. |
|
190 |
||
191 |
Error handle:[:ex | |
|
192 |
] do:[ |
|
193 |
node := tree whichNodeIsContainedBy:interval. |
|
194 |
]. |
|
195 |
node isNil ifTrue: [ |
|
196 |
node := tree bestNodeFor: interval. |
|
197 |
node isNil ifTrue: [ |
|
198 |
node := self findNodeIn:tree forInterval:interval |
|
199 |
]. |
|
200 |
]. |
|
201 |
^ node |
|
202 |
||
203 |
"Modified: / 06-07-2011 / 12:42:53 / cg" |
|
5973 | 204 |
"Modified: / 17-12-2018 / 17:32:30 / Claus Gittinger" |
3300 | 205 |
! |
206 |
||
3299
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
207 |
findNodeForInterval:interval in:source allowErrors:allowErrors |
4238 | 208 |
^ self new findNodeForInterval:interval in:source allowErrors:allowErrors |
4080
413b8c8a0161
added: #findNodeForInterval:in:allowErrors:mustBeMethod:
Claus Gittinger <cg@exept.de>
parents:
4070
diff
changeset
|
209 |
! |
413b8c8a0161
added: #findNodeForInterval:in:allowErrors:mustBeMethod:
Claus Gittinger <cg@exept.de>
parents:
4070
diff
changeset
|
210 |
|
413b8c8a0161
added: #findNodeForInterval:in:allowErrors:mustBeMethod:
Claus Gittinger <cg@exept.de>
parents:
4070
diff
changeset
|
211 |
findNodeForInterval:interval in:source allowErrors:allowErrors mustBeMethod:mustBeMethod |
413b8c8a0161
added: #findNodeForInterval:in:allowErrors:mustBeMethod:
Claus Gittinger <cg@exept.de>
parents:
4070
diff
changeset
|
212 |
"if mustBeMethod is true, do not try a regular expression (as in a workspace)." |
413b8c8a0161
added: #findNodeForInterval:in:allowErrors:mustBeMethod:
Claus Gittinger <cg@exept.de>
parents:
4070
diff
changeset
|
213 |
|
4238 | 214 |
^ self new |
5542 | 215 |
findNodeForInterval:interval in:source allowErrors:allowErrors mustBeMethod:mustBeMethod |
3306
40e860fd2f02
argument-name completion in a methods selector pattern
Claus Gittinger <cg@exept.de>
parents:
3303
diff
changeset
|
216 |
! |
40e860fd2f02
argument-name completion in a methods selector pattern
Claus Gittinger <cg@exept.de>
parents:
3303
diff
changeset
|
217 |
|
40e860fd2f02
argument-name completion in a methods selector pattern
Claus Gittinger <cg@exept.de>
parents:
3303
diff
changeset
|
218 |
findNodeForInterval:interval inParseTree:parseTree |
4238 | 219 |
^ self new findNodeForInterval:interval inParseTree:parseTree |
3299
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
220 |
! |
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
221 |
|
5272 | 222 |
findNodeIn:aTree forInterval:anInterval |
223 |
"utility" |
|
224 |
||
225 |
|nodeFound wouldReturn| |
|
226 |
||
227 |
anInterval isEmpty ifTrue:[^nil]. |
|
228 |
||
229 |
nodeFound := nil. |
|
230 |
aTree nodesDo:[:eachNode | |
|
231 |
(eachNode intersectsInterval:anInterval) ifTrue:[ |
|
232 |
(nodeFound isNil or:[nodeFound == eachNode parent]) ifTrue:[ |
|
233 |
nodeFound := eachNode |
|
234 |
] ifFalse:[ |
|
235 |
(nodeFound parent == eachNode parent |
|
236 |
and:[ eachNode start >= nodeFound start |
|
237 |
and:[ eachNode stop <= nodeFound stop ] ]) ifTrue:[ |
|
238 |
] ifFalse:[ |
|
239 |
(nodeFound parent notNil |
|
240 |
and:[nodeFound parent isCascade and:[eachNode parent isCascade]]) ifFalse:[^ nil] |
|
241 |
] |
|
242 |
] |
|
243 |
] ifFalse:[ |
|
244 |
nodeFound notNil ifTrue:[ |
|
245 |
"/ already found one - beyond that one; leave |
|
246 |
wouldReturn notNil ifTrue:[wouldReturn := nodeFound]. |
|
247 |
] |
|
248 |
]. |
|
249 |
]. |
|
250 |
"/ (wouldReturn notNil and:[wouldReturn ~~ node]) ifTrue:[self halt]. |
|
251 |
^ nodeFound |
|
252 |
||
253 |
"Modified: / 20-11-2006 / 12:31:12 / cg" |
|
2376 | 254 |
! ! |
255 |
||
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
256 |
!DoWhatIMeanSupport class methodsFor:'input completion support'! |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
257 |
|
2975 | 258 |
classCategoryCompletion:aPartialCategory inEnvironment:anEnvironment |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
259 |
"given a partial class category name, return an array consisting of |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
260 |
2 entries: 1st: the best (longest) match |
6022 | 261 |
2nd: collection consisting of matching categories" |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
262 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
263 |
|matches best lcName| |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
264 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
265 |
matches := IdentitySet new. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
266 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
267 |
"/ search for exact match |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
268 |
anEnvironment allClassesDo:[:aClass | |
6022 | 269 |
|category| |
270 |
||
271 |
category := aClass category. |
|
272 |
(category notNil and:[category startsWith:aPartialCategory]) ifTrue:[ |
|
273 |
matches add:category |
|
274 |
] |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
275 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
276 |
matches isEmpty ifTrue:[ |
6022 | 277 |
"/ search for case-ignoring match |
278 |
lcName := aPartialCategory asLowercase. |
|
279 |
anEnvironment allClassesDo:[:aClass | |
|
280 |
|category| |
|
281 |
||
282 |
category := aClass category. |
|
283 |
(category notNil and:[category asLowercase startsWith:lcName]) ifTrue:[ |
|
284 |
matches add:category |
|
285 |
]. |
|
286 |
]. |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
287 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
288 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
289 |
matches isEmpty ifTrue:[ |
6022 | 290 |
^ Array with:aPartialCategory with:(Array with:aPartialCategory) |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
291 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
292 |
matches size == 1 ifTrue:[ |
6022 | 293 |
^ Array with:matches first with:(matches asArray) |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
294 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
295 |
matches := matches asSortedCollection. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
296 |
best := matches longestCommonPrefix. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
297 |
^ Array with:best with:matches asArray |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
298 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
299 |
" |
4548 | 300 |
Smalltalk classCategoryCompletion:'Sys' |
301 |
Smalltalk classCategoryCompletion:'System' |
|
302 |
Smalltalk classCategoryCompletion:'System-BinaryStorage' |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
303 |
" |
2975 | 304 |
|
305 |
"Created: / 10-08-2006 / 13:06:45 / cg" |
|
6022 | 306 |
"Modified: / 03-03-2019 / 22:42:47 / Claus Gittinger" |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
307 |
! |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
308 |
|
2830 | 309 |
classNameEntryCompletionBlock |
310 |
"this block can be used in a dialog to perform className completion" |
|
311 |
||
2977 | 312 |
^ self entryCompletionBlockFor:#'classnameCompletion:inEnvironment:' |
2830 | 313 |
|
2977 | 314 |
"Modified: / 10-08-2006 / 13:22:02 / cg" |
2830 | 315 |
! |
316 |
||
2975 | 317 |
classnameCompletion:aPartialClassName filter:filterBlock inEnvironment:anEnvironment |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
318 |
"given a partial classname, return an array consisting of |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
319 |
2 entries: 1st: the best (longest) match |
5052 | 320 |
2nd: collection consisting of matching names" |
4548 | 321 |
|
322 |
|searchName matches matchedNamesWithoutPrefix ignCaseMatches best isMatchString cls nsPrefix |
|
3876
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
323 |
others lcSearchName tryToMatch idx words w1 w2 rslt bestMatch matchesForLongestPrefix| |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
324 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
325 |
(words := aPartialClassName asCollectionOfWords) size > 1 ifTrue:[ |
5052 | 326 |
w1 := words first. |
327 |
w2 := words second. |
|
328 |
rslt := self classnameCompletion:w1 filter:filterBlock inEnvironment:anEnvironment. |
|
329 |
bestMatch := rslt first. |
|
330 |
matches := rslt second. |
|
331 |
('class' copyTo:(w2 size min:5)) = w2 ifTrue:[ |
|
332 |
matches := matches collect:[:m | m , ' class']. |
|
333 |
bestMatch := bestMatch , ' class'. |
|
334 |
]. |
|
335 |
^ InputCompletionResult bestName:bestMatch matchingNames:matches |
|
336 |
]. |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
337 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
338 |
(aPartialClassName startsWith:'Smalltalk::') ifTrue:[ |
5052 | 339 |
nsPrefix := 'Smalltalk::'. |
340 |
searchName := aPartialClassName withoutPrefix:'Smalltalk::' |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
341 |
] ifFalse:[ |
5052 | 342 |
nsPrefix := ''. |
343 |
searchName := aPartialClassName. |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
344 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
345 |
|
3876
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
346 |
searchName := searchName asUppercaseFirst. |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
347 |
lcSearchName := searchName asLowercase. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
348 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
349 |
isMatchString := searchName includesMatchCharacters. |
5052 | 350 |
(searchName size > 2 |
351 |
and:[ searchName first = $* |
|
352 |
and:[ searchName last = $* ]] |
|
353 |
) ifTrue:[ |
|
354 |
isMatchString := false. |
|
355 |
searchName := searchName unquote:$*. |
|
356 |
]. |
|
357 |
||
358 |
searchName isEmpty ifTrue:[ |
|
359 |
matches := Smalltalk allClassesForWhich:filterBlock. |
|
360 |
^ InputCompletionResult bestName:searchName matchingNames:#() |
|
361 |
]. |
|
362 |
||
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
363 |
matches := OrderedCollection new. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
364 |
matchedNamesWithoutPrefix := Set new. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
365 |
ignCaseMatches := OrderedCollection new. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
366 |
others := OrderedCollection new. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
367 |
|
4548 | 368 |
tryToMatch := |
5052 | 369 |
[:className :fullClassName| |
370 |
|addIt| |
|
371 |
||
372 |
isMatchString ifTrue:[ |
|
373 |
addIt := searchName match:className |
|
374 |
] ifFalse:[ |
|
375 |
addIt := className includesString:searchName. |
|
376 |
]. |
|
377 |
addIt ifTrue:[ |
|
378 |
matches add:(nsPrefix , fullClassName). |
|
379 |
matchedNamesWithoutPrefix add:className. |
|
380 |
] ifFalse:[ |
|
381 |
"/ try ignoring case |
|
382 |
||
383 |
isMatchString ifTrue:[ |
|
384 |
addIt := searchName match:className caseSensitive:false |
|
385 |
] ifFalse:[ |
|
386 |
addIt := className includesString:lcSearchName caseSensitive:false. |
|
387 |
addIt ifFalse:[ |
|
388 |
others add:className |
|
389 |
] |
|
390 |
]. |
|
391 |
addIt ifTrue:[ |
|
392 |
ignCaseMatches add:(nsPrefix , fullClassName). |
|
393 |
matchedNamesWithoutPrefix add:className. |
|
394 |
]. |
|
395 |
]. |
|
396 |
addIt |
|
397 |
]. |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
398 |
|
2975 | 399 |
anEnvironment allClassesForWhich:filterBlock do:[:aClass | |
5052 | 400 |
|addIt fullClassName classNameWithoutPrefix| |
401 |
||
402 |
aClass isMeta ifFalse:[ |
|
403 |
fullClassName := aClass name. |
|
404 |
classNameWithoutPrefix := aClass nameWithoutPrefix. |
|
405 |
||
406 |
addIt := tryToMatch value:fullClassName value:fullClassName. |
|
407 |
addIt ifFalse:[ |
|
408 |
classNameWithoutPrefix ~~ fullClassName ifTrue:[ |
|
409 |
tryToMatch value:classNameWithoutPrefix value:fullClassName. |
|
410 |
]. |
|
411 |
]. |
|
412 |
] |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
413 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
414 |
|
3876
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
415 |
"/ matches isEmpty ifTrue:[ |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
416 |
"/ matches := ignCaseMatches. |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
417 |
"/ ]. |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
418 |
matches := matches , ignCaseMatches. |
3854
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
419 |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
420 |
"/ matches isEmpty ifTrue:[ |
3876
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
421 |
"/ matches := ignCaseMatches. |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
422 |
"/ |
3876
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
423 |
"/"/ matches isEmpty ifTrue:[ |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
424 |
"/"/ | nearBy | |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
425 |
"/"/ nearBy := SortedCollection new sortBlock:[:a :b | a key < b key]. |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
426 |
"/"/ others do:[:className | |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
427 |
"/"/ |lcClassName dist cmpName| |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
428 |
"/"/ |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
429 |
"/"/ lcClassName := className asLowercase. |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
430 |
"/"/ dist := lcClassName levenshteinTo:lcSearchName s:9 k:1 c:0 i:9 d:2. |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
431 |
"/"/ |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
432 |
"/"/ cmpName := lcClassName copyTo:(lcSearchName size min:lcClassName size). |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
433 |
"/"/ dist := dist min:(cmpName levenshteinTo:lcSearchName s:9 k:1 c:0 i:9 d:2). |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
434 |
"/"/ cmpName := lcClassName copyTo:(lcSearchName size + 1 min:lcClassName size). |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
435 |
"/"/ dist := dist min:(cmpName levenshteinTo:lcSearchName s:9 k:1 c:0 i:9 d:2). |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
436 |
"/"/ dist < 4 ifTrue:[ |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
437 |
"/"/ nearBy add:( dist -> (nsPrefix , className) ). |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
438 |
"/"/ ] |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
439 |
"/"/ ]. |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
440 |
"/"/ matches := nearBy collect:[:eachPair | eachPair value]. |
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
441 |
"/"/ ]. |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
442 |
"/ ]. |
3876
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
443 |
|
6e63fce83590
fixed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3867
diff
changeset
|
444 |
matches isEmpty ifTrue:[ |
5052 | 445 |
^ InputCompletionResult bestName:searchName matchingNames:(Array with:searchName) |
4548 | 446 |
]. |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
447 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
448 |
matches size == 1 ifTrue:[ |
5052 | 449 |
best := matches first. |
450 |
^ InputCompletionResult bestName:best matchingNames:(matches asArray) |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
451 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
452 |
|
4548 | 453 |
matches |
5052 | 454 |
sort:[:name1 :name2 | |
455 |
"name1 comes before:name2 iff" |
|
456 |
((name2 includes:$:) and:[(name1 includes:$:) not]) |
|
457 |
or:[ ((name1 includes:$:) == (name2 includes:$:)) |
|
458 |
and:[ (name1 size < name2 size) |
|
459 |
or: [ name1 < name2 ]] |
|
460 |
] |
|
461 |
]. |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
462 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
463 |
isMatchString ifTrue:[ |
5052 | 464 |
best := searchName. |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
465 |
] ifFalse:[ |
5052 | 466 |
matchesForLongestPrefix := matches select:[:m | m asLowercase startsWith:lcSearchName]. |
467 |
best := ignCaseMatches isEmpty |
|
468 |
ifTrue:[ matchesForLongestPrefix longestCommonPrefix ] |
|
469 |
ifFalse:[ matchesForLongestPrefix longestCommonPrefixCaseSensitive:false ]. |
|
470 |
||
471 |
best size < aPartialClassName size "best size == 0" ifTrue:[ |
|
472 |
best := matchedNamesWithoutPrefix longestCommonPrefix. |
|
473 |
]. |
|
474 |
best size == 0 ifTrue:[ |
|
475 |
"if tried again, return next match" |
|
476 |
idx := ((matches indexOf:aPartialClassName) + 1) \\ matches size. |
|
477 |
idx ~~ 1 ifTrue:[ |
|
478 |
^ InputCompletionResult bestName:(matches at:idx) matchingNames:(matches asArray) |
|
479 |
]. |
|
480 |
]. |
|
481 |
best size < aPartialClassName size ifTrue:[ |
|
482 |
best := aPartialClassName. |
|
483 |
]. |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
484 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
485 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
486 |
cls := anEnvironment classNamed:best. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
487 |
(cls isBehavior and:[cls isNameSpace]) ifTrue:[ |
5052 | 488 |
(matches conform:[:each | each = best |
489 |
or:[each startsWith:(best , '::')]]) |
|
490 |
ifTrue:[ |
|
491 |
best := best , '::' |
|
492 |
]. |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
493 |
]. |
3854
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
494 |
^ InputCompletionResult bestName:best matchingNames:matches asArray |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
495 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
496 |
" |
4548 | 497 |
Smalltalk classnameCompletion:'Arr' |
498 |
Smalltalk classnameCompletion:'Arra' |
|
499 |
Smalltalk classnameCompletion:'arra' |
|
500 |
Smalltalk classnameCompletion:'*rray' |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
501 |
" |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
502 |
|
2975 | 503 |
"Created: / 10-08-2006 / 13:01:08 / cg" |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
504 |
! |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
505 |
|
2975 | 506 |
classnameCompletion:aPartialClassName inEnvironment:anEnvironment |
507 |
"given a partial classname, return an array consisting of |
|
508 |
2 entries: 1st: the best (longest) match |
|
5542 | 509 |
2nd: collection consisting of matching names" |
2975 | 510 |
|
511 |
^ self |
|
5542 | 512 |
classnameCompletion:aPartialClassName |
513 |
filter:[:cls | true] |
|
514 |
inEnvironment:anEnvironment |
|
2975 | 515 |
|
516 |
" |
|
4148
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
517 |
self classnameCompletion:'Arr' inEnvironment:Smalltalk |
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
518 |
self classnameCompletion:'Arra' inEnvironment:Smalltalk |
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
519 |
self classnameCompletion:'arra' inEnvironment:Smalltalk |
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
520 |
self classnameCompletion:'*rray' inEnvironment:Smalltalk |
2975 | 521 |
" |
522 |
||
523 |
"Created: / 24-11-1995 / 17:24:45 / cg" |
|
524 |
"Modified: / 10-08-2006 / 13:01:30 / cg" |
|
525 |
! |
|
526 |
||
2977 | 527 |
entryCompletionBlockFor:completionSelector |
528 |
"this block can be used in a dialog to perform className completion" |
|
529 |
||
530 |
^ [:contents :field | |
|
5687 | 531 |
|s what m| |
532 |
||
533 |
s := contents withoutSpaces. |
|
534 |
field topView withCursor:(Cursor questionMark) do:[ |
|
535 |
what := self perform:completionSelector with:s with:Smalltalk. |
|
536 |
]. |
|
537 |
||
538 |
field contents:(what first). |
|
539 |
(what at:2) size ~~ 1 ifTrue:[ |
|
540 |
field device beepInEditor |
|
541 |
] |
|
2977 | 542 |
]. |
543 |
||
544 |
"Created: / 10-08-2006 / 13:21:37 / cg" |
|
545 |
! |
|
546 |
||
2975 | 547 |
globalNameCompletion:aPartialGlobalName inEnvironment:anEnvironment |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
548 |
"given a partial globalName, return an array consisting of |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
549 |
2 entries: 1st: the best (longest) match |
5542 | 550 |
2nd: collection consisting of matching names" |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
551 |
|
3863 | 552 |
^ self globalNameCompletion:aPartialGlobalName inEnvironment:anEnvironment match:true |
553 |
||
554 |
" |
|
4548 | 555 |
Smalltalk globalnameCompletion:'Arr' |
556 |
Smalltalk globalnameCompletion:'Arra' |
|
557 |
Smalltalk globalnameCompletion:'arra' |
|
558 |
Smalltalk globalnameCompletion:'*rray' |
|
3863 | 559 |
" |
560 |
||
561 |
"Created: / 10-08-2006 / 13:06:23 / cg" |
|
562 |
! |
|
563 |
||
564 |
globalNameCompletion:aPartialGlobalName inEnvironment:anEnvironment match:doMatch |
|
565 |
"given a partial globalName, return an array consisting of |
|
566 |
2 entries: 1st: the best (longest) match |
|
5542 | 567 |
2nd: collection consisting of matching names" |
3863 | 568 |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
569 |
|searchName matches ignCaseMatches best isMatchString| |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
570 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
571 |
searchName := aPartialGlobalName. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
572 |
searchName isEmpty ifTrue:[ |
5542 | 573 |
^ Array with:searchName with:#() |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
574 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
575 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
576 |
(searchName at:1) isLowercase ifTrue:[ |
5542 | 577 |
searchName := searchName copy asUppercaseFirst |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
578 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
579 |
|
3863 | 580 |
isMatchString := doMatch and:[ searchName includesMatchCharacters ]. |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
581 |
matches := OrderedCollection new. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
582 |
ignCaseMatches := OrderedCollection new. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
583 |
anEnvironment keysDo:[:aGlobalName | |
5542 | 584 |
| addIt| |
585 |
||
586 |
isMatchString ifTrue:[ |
|
587 |
addIt := searchName match:aGlobalName |
|
588 |
] ifFalse:[ |
|
589 |
addIt := aGlobalName startsWith:searchName |
|
590 |
]. |
|
591 |
addIt ifTrue:[ |
|
592 |
matches add:aGlobalName |
|
593 |
] ifFalse:[ |
|
594 |
"/ try ignoring case |
|
595 |
isMatchString ifTrue:[ |
|
596 |
addIt := searchName match:aGlobalName caseSensitive:false |
|
597 |
] ifFalse:[ |
|
598 |
addIt := aGlobalName asLowercase startsWith:searchName asLowercase |
|
599 |
]. |
|
600 |
addIt ifTrue:[ |
|
601 |
ignCaseMatches add:aGlobalName |
|
602 |
] |
|
603 |
] |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
604 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
605 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
606 |
matches isEmpty ifTrue:[ |
5542 | 607 |
matches := ignCaseMatches |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
608 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
609 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
610 |
matches isEmpty ifTrue:[ |
5542 | 611 |
^ Array with:searchName with:(Array with:searchName) |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
612 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
613 |
matches size == 1 ifTrue:[ |
5542 | 614 |
^ Array with:matches first with:(matches asArray) |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
615 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
616 |
matches := matches asSortedCollection. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
617 |
isMatchString ifTrue:[ |
5542 | 618 |
best := searchName. |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
619 |
] ifFalse:[ |
5542 | 620 |
best := matches longestCommonPrefix. |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
621 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
622 |
^ Array with:best with:matches asArray |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
623 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
624 |
" |
4548 | 625 |
Smalltalk globalnameCompletion:'Arr' |
626 |
Smalltalk globalnameCompletion:'Arra' |
|
627 |
Smalltalk globalnameCompletion:'arra' |
|
628 |
Smalltalk globalnameCompletion:'*rray' |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
629 |
" |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
630 |
|
2975 | 631 |
"Created: / 10-08-2006 / 13:06:23 / cg" |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
632 |
! |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
633 |
|
4051 | 634 |
methodProtocolCompletion:aPartialProtocolName inEnvironment:anEnvironment |
635 |
"given a partial method protocol name, return an array consisting of |
|
4548 | 636 |
2 entries: 1st: the best (longest) match |
6022 | 637 |
2nd: collection consisting of matching protocols" |
4051 | 638 |
|
639 |
|matches best lcName| |
|
640 |
||
641 |
matches := IdentitySet new. |
|
642 |
||
643 |
"/ search for exact match |
|
644 |
anEnvironment allMethodsWithSelectorDo:[:eachMethod :eachSelector | |
|
6022 | 645 |
|protocol| |
646 |
||
647 |
protocol := eachMethod category. |
|
648 |
(protocol notNil and:[protocol startsWith:aPartialProtocolName]) ifTrue:[ |
|
649 |
matches add:protocol |
|
650 |
]. |
|
4051 | 651 |
]. |
652 |
matches isEmpty ifTrue:[ |
|
6022 | 653 |
"/ search for case-ignoring match |
654 |
lcName := aPartialProtocolName asLowercase. |
|
655 |
anEnvironment allMethodsWithSelectorDo:[:eachMethod :eachSelector | |
|
656 |
|protocol| |
|
657 |
||
658 |
protocol := eachMethod category. |
|
659 |
(protocol notNil and:[protocol asLowercase startsWith:lcName]) ifTrue:[ |
|
660 |
matches add:protocol |
|
661 |
]. |
|
662 |
]. |
|
4051 | 663 |
]. |
664 |
||
665 |
matches isEmpty ifTrue:[ |
|
6022 | 666 |
^ Array with:aPartialProtocolName with:(Array with:aPartialProtocolName) |
4051 | 667 |
]. |
668 |
matches size == 1 ifTrue:[ |
|
6022 | 669 |
^ Array with:matches first with:(matches asArray) |
4051 | 670 |
]. |
671 |
matches := matches asSortedCollection. |
|
672 |
best := matches longestCommonPrefix. |
|
673 |
^ Array with:best with:matches asArray |
|
674 |
||
675 |
" |
|
676 |
Smalltalk methodProtocolCompletion:'doc' |
|
4548 | 677 |
Smalltalk methodProtocolCompletion:'docu' |
678 |
Smalltalk methodProtocolCompletion:'documenta' |
|
4051 | 679 |
" |
680 |
||
681 |
"Created: / 10-08-2006 / 13:05:27 / cg" |
|
682 |
"Modified: / 16-03-2011 / 12:30:45 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
6022 | 683 |
"Modified: / 03-03-2019 / 22:43:07 / Claus Gittinger" |
4051 | 684 |
! |
685 |
||
2975 | 686 |
nameSpaceCompletion:aPartialClassName inEnvironment:anEnvironment |
687 |
"given a partial name, return an array consisting of |
|
688 |
2 entries: 1st: the best (longest) match |
|
5542 | 689 |
2nd: collection consisting of matching names" |
2975 | 690 |
|
691 |
^ self |
|
5542 | 692 |
classnameCompletion:aPartialClassName |
693 |
filter:[:cls | cls isNameSpace] |
|
694 |
inEnvironment:anEnvironment |
|
2975 | 695 |
|
696 |
" |
|
4548 | 697 |
DoWhatIMeanSupport nameSpaceCompletion:'To' inEnvironment:Smalltalk |
2975 | 698 |
" |
699 |
||
700 |
"Created: / 10-08-2006 / 13:02:16 / cg" |
|
701 |
! |
|
702 |
||
703 |
packageCompletion:aPartialPackage inEnvironment:anEnvironment |
|
704 |
"given a partial package name, return an array consisting of |
|
705 |
2 entries: 1st: the best (longest) match |
|
4976 | 706 |
2nd: collection consisting of matching packages" |
2975 | 707 |
|
708 |
|matches best lcName| |
|
709 |
||
4976 | 710 |
matches := Smalltalk allPackageIDs |
711 |
select:[:package | package startsWith:aPartialPackage]. |
|
2975 | 712 |
|
713 |
matches isEmpty ifTrue:[ |
|
4976 | 714 |
"/ search for case-ignoring match |
715 |
lcName := aPartialPackage asLowercase. |
|
716 |
anEnvironment allClassesDo:[:aClass | |
|
717 |
|package| |
|
718 |
||
719 |
package := aClass package. |
|
720 |
(package notNil and:[package asLowercase startsWith:lcName]) ifTrue:[ |
|
721 |
matches add:package |
|
722 |
]. |
|
723 |
]. |
|
2975 | 724 |
]. |
725 |
||
726 |
matches isEmpty ifTrue:[ |
|
4976 | 727 |
^ Array with:aPartialPackage with:(Array with:aPartialPackage) |
2975 | 728 |
]. |
729 |
matches size == 1 ifTrue:[ |
|
4976 | 730 |
^ Array with:matches first with:(matches asArray) |
2975 | 731 |
]. |
732 |
matches := matches asSortedCollection. |
|
733 |
best := matches longestCommonPrefix. |
|
734 |
^ Array with:best with:matches asArray |
|
735 |
||
736 |
" |
|
4548 | 737 |
DoWhatIMeanSupport packageCompletion:'stx:' inEnvironment:Smalltalk |
738 |
DoWhatIMeanSupport packageCompletion:'stx:libw' inEnvironment:Smalltalk |
|
2975 | 739 |
" |
740 |
||
741 |
"Created: / 10-08-2006 / 13:05:07 / cg" |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
742 |
! |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
743 |
|
2977 | 744 |
packageNameEntryCompletionBlock |
745 |
"this block can be used in a dialog to perform className completion" |
|
746 |
||
747 |
^ self entryCompletionBlockFor:#'packageCompletion:inEnvironment:' |
|
748 |
||
749 |
"Created: / 10-08-2006 / 13:22:31 / cg" |
|
750 |
! |
|
751 |
||
4148
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
752 |
poolnameCompletion:aPartialClassName inEnvironment:anEnvironment |
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
753 |
"given a partial poolname, return an array consisting of |
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
754 |
2 entries: 1st: the best (longest) match |
5542 | 755 |
2nd: collection consisting of matching names" |
4148
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
756 |
|
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
757 |
^ self |
5542 | 758 |
classnameCompletion:aPartialClassName |
759 |
filter:[:cls | cls isSharedPool] |
|
760 |
inEnvironment:anEnvironment |
|
4148
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
761 |
|
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
762 |
" |
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
763 |
self poolnameCompletion:'Win' inEnvironment:Smalltalk |
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
764 |
self poolnameCompletion:'Z' inEnvironment:Smalltalk |
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
765 |
self poolnameCompletion:'a' inEnvironment:Smalltalk |
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
766 |
" |
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
767 |
! |
b314d59c1654
added: #poolnameCompletion:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
4146
diff
changeset
|
768 |
|
4049
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
769 |
resourceCompletion:aPartialResourceName inEnvironment:anEnvironment match:doMatch ignoreCase:ignoreCase |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
770 |
"given a partial resource name, return an array consisting of |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
771 |
2 entries: 1st: the longest match |
5713 | 772 |
2nd: collection consisting of matching defined resources" |
4049
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
773 |
|
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
774 |
|matches best lcSym isMatch| |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
775 |
|
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
776 |
matches := IdentitySet new. |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
777 |
|
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
778 |
isMatch := doMatch and:[aPartialResourceName includesMatchCharacters]. |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
779 |
|
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
780 |
anEnvironment allMethodsWithSelectorDo:[:eachMethod :eachSelector | |
5713 | 781 |
eachMethod hasResource ifTrue:[ |
782 |
eachMethod resources keysDo:[:eachResourceName | |
|
783 |
|addToMatches| |
|
784 |
||
785 |
isMatch ifTrue:[ |
|
786 |
addToMatches := aPartialResourceName match:eachResourceName caseSensitive:ignoreCase not |
|
787 |
] ifFalse:[ |
|
788 |
ignoreCase ifTrue:[ |
|
789 |
addToMatches := eachResourceName asLowercase startsWith:aPartialResourceName asLowercase |
|
790 |
] ifFalse:[ |
|
791 |
addToMatches := eachResourceName startsWith:aPartialResourceName |
|
792 |
] |
|
793 |
]. |
|
794 |
addToMatches ifTrue:[ |
|
795 |
matches add:eachResourceName |
|
796 |
]. |
|
797 |
]. |
|
798 |
]. |
|
4049
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
799 |
]. |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
800 |
(matches isEmpty and:[ignoreCase not]) ifTrue:[ |
5713 | 801 |
"/ search for case-ignoring match |
802 |
lcSym := aPartialResourceName asLowercase. |
|
803 |
anEnvironment allMethodsWithSelectorDo:[:eachMethod :eachSelector | |
|
804 |
eachMethod hasResource ifTrue:[ |
|
805 |
eachMethod resources keysDo:[:eachResourceName | |
|
806 |
|addToMatches| |
|
807 |
||
808 |
isMatch ifTrue:[ |
|
809 |
addToMatches := aPartialResourceName match:eachResourceName caseSensitive:false |
|
810 |
] ifFalse:[ |
|
811 |
addToMatches := eachResourceName asLowercase startsWith:lcSym |
|
812 |
]. |
|
813 |
addToMatches ifTrue:[ |
|
814 |
matches add:eachResourceName |
|
815 |
]. |
|
816 |
]. |
|
817 |
]. |
|
818 |
]. |
|
4049
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
819 |
]. |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
820 |
|
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
821 |
matches isEmpty ifTrue:[ |
5713 | 822 |
^ Array with:aPartialResourceName with:#() |
4049
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
823 |
]. |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
824 |
matches size == 1 ifTrue:[ |
5713 | 825 |
^ Array with:matches first with:(matches asArray) |
4049
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
826 |
]. |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
827 |
matches := matches asSortedCollection. |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
828 |
best := matches longestCommonPrefix. |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
829 |
^ Array with:best with:matches asArray |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
830 |
|
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
831 |
" |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
832 |
DoWhatIMeanSupport resourceCompletion:'*debug*' inEnvironment:Smalltalk match:true ignoreCase:false |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
833 |
DoWhatIMeanSupport resourceCompletion:'context' inEnvironment:Smalltalk match:true ignoreCase:false |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
834 |
DoWhatIMeanSupport resourceCompletion:'key' inEnvironment:Smalltalk match:true ignoreCase:false |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
835 |
DoWhatIMeanSupport resourceCompletion:'cont' inEnvironment:Smalltalk match:true ignoreCase:false |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
836 |
" |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
837 |
|
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
838 |
"Created: / 06-07-2011 / 12:04:41 / cg" |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
839 |
! |
b5540a857436
added: #resourceCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
4047
diff
changeset
|
840 |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
841 |
selectorCompletion:aPartialSymbolName inEnvironment:anEnvironment |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
842 |
"given a partial selector, return an array consisting of |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
843 |
2 entries: 1st: the longest match |
5542 | 844 |
2nd: collection consisting of matching implemented selectors" |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
845 |
|
3863 | 846 |
^ self selectorCompletion:aPartialSymbolName inEnvironment:anEnvironment match:false |
847 |
! |
|
848 |
||
849 |
selectorCompletion:aPartialSymbolName inEnvironment:anEnvironment match:doMatch |
|
850 |
"given a partial selector, return an array consisting of |
|
851 |
2 entries: 1st: the longest match |
|
5542 | 852 |
2nd: collection consisting of matching implemented selectors" |
4548 | 853 |
|
854 |
^ self |
|
5542 | 855 |
selectorCompletion:aPartialSymbolName |
856 |
inEnvironment:anEnvironment |
|
857 |
match:doMatch |
|
858 |
ignoreCase:false |
|
3955
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
859 |
|
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
860 |
" |
4548 | 861 |
DoWhatIMeanSupport selectorCompletion:'inst*p' inEnvironment:Smalltalk match:true |
862 |
DoWhatIMeanSupport selectorCompletion:'inst*pl' inEnvironment:Smalltalk match:true |
|
863 |
DoWhatIMeanSupport selectorCompletion:'at:p' inEnvironment:Smalltalk match:true |
|
3955
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
864 |
DoWhatIMeanSupport selectorCompletion:'nextP' inEnvironment:Smalltalk match:true |
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
865 |
DoWhatIMeanSupport selectorCompletion:'nextp' inEnvironment:Smalltalk match:true |
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
866 |
" |
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
867 |
|
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
868 |
"Modified: / 07-06-1996 / 08:44:33 / stefan" |
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
869 |
"Modified: / 26-10-2010 / 20:30:27 / cg" |
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
870 |
! |
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
871 |
|
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
872 |
selectorCompletion:aPartialSymbolName inEnvironment:anEnvironment match:doMatch ignoreCase:ignoreCase |
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
873 |
"given a partial selector, return an array consisting of |
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
874 |
2 entries: 1st: the longest match |
5714 | 875 |
2nd: collection consisting of matching implemented selectors" |
3955
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
876 |
|
3863 | 877 |
|matches best lcSym isMatch| |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
878 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
879 |
matches := IdentitySet new. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
880 |
|
3863 | 881 |
isMatch := doMatch and:[aPartialSymbolName includesMatchCharacters]. |
882 |
||
3751 | 883 |
anEnvironment allMethodsWithSelectorDo:[:eachMethod :eachSelector | |
5714 | 884 |
|addToMatches| |
885 |
||
886 |
isMatch ifTrue:[ |
|
887 |
addToMatches := (aPartialSymbolName match:eachSelector caseSensitive:ignoreCase not) |
|
888 |
] ifFalse:[ |
|
889 |
ignoreCase ifTrue:[ |
|
890 |
addToMatches := (eachSelector asLowercase startsWith:aPartialSymbolName asLowercase) |
|
891 |
] ifFalse:[ |
|
892 |
addToMatches := (eachSelector startsWith:aPartialSymbolName) |
|
893 |
] |
|
894 |
]. |
|
895 |
addToMatches ifTrue:[ |
|
896 |
matches add:eachSelector |
|
897 |
]. |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
898 |
]. |
3955
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
899 |
(matches isEmpty and:[ignoreCase not]) ifTrue:[ |
5714 | 900 |
"/ search for case-ignoring match |
901 |
lcSym := aPartialSymbolName asLowercase. |
|
902 |
anEnvironment allMethodsWithSelectorDo:[:eachMethod :eachSelector | |
|
903 |
|addToMatches| |
|
904 |
||
905 |
isMatch ifTrue:[ |
|
906 |
addToMatches := (aPartialSymbolName match:eachSelector caseSensitive:false) |
|
907 |
] ifFalse:[ |
|
908 |
addToMatches := (eachSelector asLowercase startsWith:lcSym) |
|
909 |
]. |
|
910 |
addToMatches ifTrue:[ |
|
911 |
matches add:eachSelector |
|
912 |
]. |
|
913 |
]. |
|
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
914 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
915 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
916 |
matches isEmpty ifTrue:[ |
5714 | 917 |
^ Array with:aPartialSymbolName with:#() "/ (Array with:aPartialSymbolName) |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
918 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
919 |
matches size == 1 ifTrue:[ |
5714 | 920 |
^ Array with:matches first with:(matches asArray) |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
921 |
]. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
922 |
matches := matches asSortedCollection. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
923 |
best := matches longestCommonPrefix. |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
924 |
^ Array with:best with:matches asArray |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
925 |
|
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
926 |
" |
4548 | 927 |
DoWhatIMeanSupport selectorCompletion:'inst*p' inEnvironment:Smalltalk match:true |
928 |
DoWhatIMeanSupport selectorCompletion:'inst*pl' inEnvironment:Smalltalk match:true |
|
929 |
DoWhatIMeanSupport selectorCompletion:'at:p' inEnvironment:Smalltalk match:true |
|
3867
03f7ad841731
changed: #selectorCompletion:inEnvironment:match:
Claus Gittinger <cg@exept.de>
parents:
3864
diff
changeset
|
930 |
DoWhatIMeanSupport selectorCompletion:'nextP' inEnvironment:Smalltalk match:true |
03f7ad841731
changed: #selectorCompletion:inEnvironment:match:
Claus Gittinger <cg@exept.de>
parents:
3864
diff
changeset
|
931 |
DoWhatIMeanSupport selectorCompletion:'nextp' inEnvironment:Smalltalk match:true |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
932 |
" |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
933 |
|
3955
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
934 |
"Modified: / 07-06-1996 / 08:44:33 / stefan" |
94fb4a47d108
added: #selectorCompletion:inEnvironment:match:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
3929
diff
changeset
|
935 |
"Created: / 26-10-2010 / 20:30:06 / cg" |
2661
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
936 |
! ! |
f68913f2facf
moved all input completion methods from Smalltalk to here.
Claus Gittinger <cg@exept.de>
parents:
2560
diff
changeset
|
937 |
|
2376 | 938 |
!DoWhatIMeanSupport class methodsFor:'rename support'! |
939 |
||
940 |
goodRenameDefaultFor:oldName lastOld:lastOldName lastNew:lastNewName |
|
2513 | 941 |
"generate a reasonable default for a rename operation. |
942 |
(used for rename category etc.)" |
|
2468 | 943 |
|
4065
ebea28fa8bab
changed: #goodRenameDefaultFor:lastOld:lastNew:
Claus Gittinger <cg@exept.de>
parents:
4058
diff
changeset
|
944 |
|prefix suffix lastNewSize lastOldSize left right inserted deleted tryAgain| |
2376 | 945 |
|
946 |
lastNewName isNil ifTrue:[ ^ nil]. |
|
947 |
||
948 |
lastNewSize := lastNewName size. |
|
949 |
lastOldSize := lastOldName size. |
|
950 |
||
951 |
(lastNewName endsWith:lastOldName) ifTrue:[ |
|
5143 | 952 |
"last rename was |
953 |
'foo' -> 'Xfoo' |
|
954 |
then, a good default for |
|
955 |
'bar' would be 'Xbar' |
|
956 |
" |
|
957 |
prefix := lastNewName copyTo:(lastNewSize - lastOldSize). |
|
958 |
^ (prefix , oldName). |
|
2376 | 959 |
]. |
960 |
(lastOldName endsWith:lastNewName) ifTrue:[ |
|
5143 | 961 |
"last rename was |
962 |
'Xfoo' -> 'foo' |
|
963 |
then, a good default for |
|
964 |
'Xbar' would be 'bar' |
|
965 |
" |
|
966 |
prefix := lastOldName copyTo:(lastOldSize - lastNewSize). |
|
967 |
(oldName startsWith:prefix) ifTrue:[ |
|
968 |
^ (oldName copyFrom:prefix size+1). |
|
969 |
] |
|
2514 | 970 |
]. |
971 |
(lastOldName asLowercase = lastNewName asLowercase) ifTrue:[ |
|
5143 | 972 |
(lastOldName first ~= lastNewName first) ifTrue:[ |
5552 | 973 |
(lastOldName isLowercaseFirst = oldName isLowercaseFirst) ifTrue:[ |
5143 | 974 |
"last rename was |
975 |
'xfoo' -> 'Xfoo' |
|
976 |
then, a good default for |
|
977 |
'xbar' would be 'Xbar' |
|
978 |
" |
|
979 |
lastOldName first isLowercase ifTrue:[ |
|
980 |
^ oldName asUppercaseFirst "oldName first asUppercase asString , (oldName copyFrom:2)". |
|
981 |
] ifFalse:[ |
|
982 |
^ oldName asLowercaseFirst "oldName first asLowercase asString , (oldName copyFrom:2)". |
|
983 |
] |
|
984 |
] |
|
985 |
]. |
|
2376 | 986 |
]. |
987 |
(lastOldName withoutSeparators = lastNewName) ifTrue:[ |
|
5143 | 988 |
"last rename was |
989 |
' foo ' -> 'foo' |
|
990 |
then, a good default for |
|
991 |
' bar ' would be 'bar' |
|
992 |
" |
|
993 |
^ oldName withoutSeparators. |
|
2376 | 994 |
]. |
995 |
(lastNewName startsWith:lastOldName) ifTrue:[ |
|
5143 | 996 |
"last rename was |
997 |
'foo' -> 'fooX' |
|
998 |
then, a good default for |
|
999 |
'bar' would be 'barX' |
|
1000 |
" |
|
1001 |
suffix := lastNewName copyLast:(lastNewSize - lastOldSize). |
|
1002 |
^ (oldName , suffix). |
|
2376 | 1003 |
]. |
1004 |
(lastOldName startsWith:lastNewName) ifTrue:[ |
|
5143 | 1005 |
"last rename was |
1006 |
'fooX' -> 'foo' |
|
1007 |
then, a good default for |
|
1008 |
'barX' would be 'bar' |
|
1009 |
" |
|
1010 |
suffix := lastOldName copyLast:(lastOldSize - lastNewSize). |
|
1011 |
(oldName endsWith:suffix) ifTrue:[ |
|
1012 |
^ (oldName copyButLast:suffix size). |
|
1013 |
] |
|
2376 | 1014 |
]. |
2558 | 1015 |
|
2557 | 1016 |
prefix := lastOldName commonPrefixWith:lastNewName. |
1017 |
suffix := lastOldName commonSuffixWith:lastNewName. |
|
2559 | 1018 |
|
6013 | 1019 |
(prefix size ~~ 0) ifTrue:[ |
1020 |
(suffix size ~~ 0) ifTrue:[ |
|
5143 | 1021 |
|
1022 |
prefix := prefix copyTo:(((lastNewName size - suffix size) min:(lastOldName size - suffix size)) min:prefix size). |
|
1023 |
||
1024 |
"last rename was |
|
1025 |
'fooR' -> 'fooXR' |
|
1026 |
then, a good default for |
|
1027 |
'barR' would be 'barXR' |
|
1028 |
" |
|
1029 |
left := lastOldName copyTo:prefix size. |
|
1030 |
right := lastOldName copyLast:suffix size. |
|
1031 |
lastNewSize > lastOldSize ifTrue:[ |
|
1032 |
inserted := (lastNewName copyFrom:(left size + 1)) copyButLast:(right size). |
|
6013 | 1033 |
inserted size ~~ 0 ifTrue:[ |
5143 | 1034 |
(oldName startsWith:prefix) ifTrue:[ |
1035 |
^ oldName copyWithAll:inserted insertedAfterIndex:prefix size |
|
1036 |
]. |
|
1037 |
]. |
|
1038 |
]. |
|
1039 |
(oldName string endsWith:suffix string) ifTrue:[ |
|
1040 |
deleted := (lastOldName string copyFrom:(prefix size + 1)) copyButLast:(suffix size). |
|
1041 |
(oldName size-suffix size-deleted size + 1) >= 1 ifTrue:[ |
|
1042 |
((oldName copyFrom:oldName size-suffix size-deleted size + 1) copyTo:deleted size) = deleted ifTrue:[ |
|
1043 |
"last rename was |
|
1044 |
'fooXR' -> 'fooR' |
|
1045 |
then, a good default for |
|
1046 |
'barXS' would be 'barS' |
|
1047 |
" |
|
1048 |
^ (oldName copyTo:oldName size-suffix size-deleted size) , suffix |
|
1049 |
] |
|
1050 |
] |
|
1051 |
] |
|
1052 |
]. |
|
1053 |
||
1054 |
(oldName endsWith:(lastOldName copyFrom:prefix size+1)) ifTrue:[ |
|
1055 |
"last rename was |
|
1056 |
'fooX' -> 'fooY' |
|
1057 |
then, a good default for |
|
1058 |
'barX' would be 'barY' |
|
1059 |
" |
|
1060 |
left := oldName copyButLast:(lastOldName copyFrom:prefix size+1) size. |
|
1061 |
right := lastNewName copyFrom:prefix size+1. |
|
1062 |
^ left , right |
|
1063 |
] |
|
2557 | 1064 |
]. |
2376 | 1065 |
|
6013 | 1066 |
suffix size ~~ 0 ifTrue:[ |
5143 | 1067 |
|prefix2| |
1068 |
||
1069 |
"last rename was: |
|
1070 |
'fooSUFF1' -> 'barSUFF1' |
|
1071 |
then, a good default for |
|
1072 |
'fooSUFF2' -> 'barSUFF2' |
|
1073 |
" |
|
1074 |
prefix := lastOldName copyTo:(lastOldName size - suffix size). "/ the foo |
|
1075 |
(oldName startsWith:prefix) ifTrue:[ |
|
1076 |
prefix2 := lastNewName copyTo:(lastNewName size - suffix size). "/ the bar |
|
1077 |
^ prefix2,(oldName copyFrom:(prefix size+1)). |
|
1078 |
]. |
|
4065
ebea28fa8bab
changed: #goodRenameDefaultFor:lastOld:lastNew:
Claus Gittinger <cg@exept.de>
parents:
4058
diff
changeset
|
1079 |
]. |
ebea28fa8bab
changed: #goodRenameDefaultFor:lastOld:lastNew:
Claus Gittinger <cg@exept.de>
parents:
4058
diff
changeset
|
1080 |
|
ebea28fa8bab
changed: #goodRenameDefaultFor:lastOld:lastNew:
Claus Gittinger <cg@exept.de>
parents:
4058
diff
changeset
|
1081 |
"/ was there something stripped at the end? |
4548 | 1082 |
suffix := oldName commonSuffixWith:lastOldName. |
6013 | 1083 |
(suffix size ~~ 0) ifTrue:[ |
5143 | 1084 |
^ self |
1085 |
goodRenameDefaultFor:(oldName copyButLast:suffix size) |
|
1086 |
lastOld:(lastOldName copyButLast:suffix size) |
|
1087 |
lastNew:lastNewName. |
|
4065
ebea28fa8bab
changed: #goodRenameDefaultFor:lastOld:lastNew:
Claus Gittinger <cg@exept.de>
parents:
4058
diff
changeset
|
1088 |
]. |
2382
d89d86edaae4
separated default for arbitrary renames and deault for files.
Claus Gittinger <cg@exept.de>
parents:
2376
diff
changeset
|
1089 |
^ nil |
2557 | 1090 |
|
1091 |
" |
|
2558 | 1092 |
self goodRenameDefaultFor:'bar' lastOld:'foo' lastNew:'fooXX' |
1093 |
self goodRenameDefaultFor:'bar' lastOld:'foo' lastNew:'XXfoo' |
|
1094 |
||
4548 | 1095 |
self goodRenameDefaultFor:'barXX' lastOld:'fooXX' lastNew:'foo' |
2558 | 1096 |
self goodRenameDefaultFor:'XXbar' lastOld:'XXfoo' lastNew:'foo' |
1097 |
||
4548 | 1098 |
self goodRenameDefaultFor:'barXX' lastOld:'fooXX' lastNew:'fooYY' |
1099 |
self goodRenameDefaultFor:'XXbar' lastOld:'XXfoo' lastNew:'foo' |
|
1100 |
||
1101 |
self goodRenameDefaultFor:'bar2' lastOld:'foo1' lastNew:'foo01' |
|
2559 | 1102 |
self goodRenameDefaultFor:'barXY' lastOld:'fooXY' lastNew:'fooY' |
4548 | 1103 |
self goodRenameDefaultFor:'bar' lastOld:'foo' lastNew:'fXoo' |
1104 |
self goodRenameDefaultFor:'bar' lastOld:'foo' lastNew:'fXXXoo' |
|
1105 |
self goodRenameDefaultFor:'bar' lastOld:'foo' lastNew:'foXXXo' |
|
1106 |
||
1107 |
self goodRenameDefaultFor:'bar001' lastOld:'foo001' lastNew:'foo002_001' |
|
1108 |
self goodRenameDefaultFor:'CoastCore-CSFoo' lastOld:'CoastCore-CSBar' lastNew:'Coast-Core-CSBar' |
|
1109 |
||
1110 |
self goodRenameDefaultFor:'mti.odt2.level1HeadlineStyle' |
|
5143 | 1111 |
lastOld:'mti.odt2.level1HeadlineMatchPattern' |
1112 |
lastNew:'Key_odt2_level1HeadlineMatchPattern' |
|
2557 | 1113 |
" |
4065
ebea28fa8bab
changed: #goodRenameDefaultFor:lastOld:lastNew:
Claus Gittinger <cg@exept.de>
parents:
4058
diff
changeset
|
1114 |
|
5552 | 1115 |
"Modified: / 22-06-2017 / 06:56:55 / cg" |
6013 | 1116 |
"Modified: / 01-03-2019 / 14:53:49 / Claus Gittinger" |
2382
d89d86edaae4
separated default for arbitrary renames and deault for files.
Claus Gittinger <cg@exept.de>
parents:
2376
diff
changeset
|
1117 |
! |
d89d86edaae4
separated default for arbitrary renames and deault for files.
Claus Gittinger <cg@exept.de>
parents:
2376
diff
changeset
|
1118 |
|
d89d86edaae4
separated default for arbitrary renames and deault for files.
Claus Gittinger <cg@exept.de>
parents:
2376
diff
changeset
|
1119 |
goodRenameDefaultForFile:oldName lastOld:lastOldName lastNew:lastNewName |
2513 | 1120 |
"generate a reasonable default for a file rename operation. |
4548 | 1121 |
(Try to rename multiple files in the new fileBrowser, |
2513 | 1122 |
to see what this is doing)" |
2468 | 1123 |
|
2382
d89d86edaae4
separated default for arbitrary renames and deault for files.
Claus Gittinger <cg@exept.de>
parents:
2376
diff
changeset
|
1124 |
|prefix suffix t |
d89d86edaae4
separated default for arbitrary renames and deault for files.
Claus Gittinger <cg@exept.de>
parents:
2376
diff
changeset
|
1125 |
lastOldWOSuffix lastNewWOSuffix oldWOSuffix lastOldRest oldRest lastNewRest |
d89d86edaae4
separated default for arbitrary renames and deault for files.
Claus Gittinger <cg@exept.de>
parents:
2376
diff
changeset
|
1126 |
lastRemoved lastInserted default| |
d89d86edaae4
separated default for arbitrary renames and deault for files.
Claus Gittinger <cg@exept.de>
parents:
2376
diff
changeset
|
1127 |
|
d89d86edaae4
separated default for arbitrary renames and deault for files.
Claus Gittinger <cg@exept.de>
parents:
2376
diff
changeset
|
1128 |
default := self goodRenameDefaultFor:oldName lastOld:lastOldName lastNew:lastNewName. |
d89d86edaae4
separated default for arbitrary renames and deault for files.
Claus Gittinger <cg@exept.de>
parents:
2376
diff
changeset
|
1129 |
default notNil ifTrue:[ ^ default]. |
d89d86edaae4
separated default for arbitrary renames and deault for files.
Claus Gittinger <cg@exept.de>
parents:
2376
diff
changeset
|
1130 |
|
3135
7ad9ff29225e
changed #classCategoryLoadFromRepository
Claus Gittinger <cg@exept.de>
parents:
2977
diff
changeset
|
1131 |
lastOldWOSuffix := lastOldName asFilename nameWithoutSuffix. |
7ad9ff29225e
changed #classCategoryLoadFromRepository
Claus Gittinger <cg@exept.de>
parents:
2977
diff
changeset
|
1132 |
lastNewWOSuffix := lastNewName asFilename nameWithoutSuffix. |
7ad9ff29225e
changed #classCategoryLoadFromRepository
Claus Gittinger <cg@exept.de>
parents:
2977
diff
changeset
|
1133 |
oldWOSuffix := oldName asFilename nameWithoutSuffix. |
2376 | 1134 |
|
2434 | 1135 |
"/ suffix change ? |
1136 |
lastOldWOSuffix = lastNewWOSuffix ifTrue:[ |
|
6013 | 1137 |
lastOldName asFilename suffix ~= lastNewName asFilename suffix ifTrue:[ |
1138 |
^ (oldName asFilename withSuffix:(lastNewName asFilename suffix)) pathName |
|
1139 |
]. |
|
2434 | 1140 |
]. |
1141 |
||
2553 | 1142 |
default := self goodRenameDefaultFor:oldWOSuffix lastOld:lastOldWOSuffix lastNew:lastNewWOSuffix. |
4548 | 1143 |
default notNil ifTrue:[ |
6013 | 1144 |
lastOldRest := lastOldName copyFrom:lastOldWOSuffix size + 1. |
1145 |
lastNewRest := lastNewName copyFrom:lastNewWOSuffix size + 1. |
|
1146 |
oldRest := oldName copyFrom:oldWOSuffix size + 1. |
|
1147 |
||
1148 |
^ default , lastNewRest |
|
2553 | 1149 |
]. |
1150 |
||
2376 | 1151 |
prefix := lastOldWOSuffix commonPrefixWith:oldWOSuffix. |
1152 |
(lastNewWOSuffix startsWith:prefix) ifTrue:[ |
|
6013 | 1153 |
lastOldRest := lastOldWOSuffix copyFrom:prefix size + 1. |
1154 |
lastNewRest := lastNewWOSuffix copyFrom:prefix size + 1. |
|
1155 |
oldRest := oldWOSuffix copyFrom:prefix size + 1. |
|
1156 |
||
1157 |
(lastNewRest endsWith:lastOldRest) ifTrue:[ |
|
1158 |
t := lastNewRest copyButLast:lastOldRest size. |
|
1159 |
^ ((prefix , t , oldRest) asFilename withSuffix:oldName asFilename suffix) name |
|
1160 |
]. |
|
2376 | 1161 |
]. |
1162 |
||
1163 |
suffix := lastOldWOSuffix commonSuffixWith:lastNewWOSuffix. |
|
6013 | 1164 |
suffix size ~~ 0 ifTrue:[ |
1165 |
"/ last change changed something at the beginning |
|
1166 |
prefix := oldWOSuffix commonPrefixWith:lastOldWOSuffix. |
|
1167 |
prefix size ~~ 0 ifTrue:[ |
|
1168 |
"/ this name starts with the same characters |
|
1169 |
lastRemoved := lastOldWOSuffix copyButLast:suffix size. |
|
1170 |
lastInserted := lastNewWOSuffix copyButLast:suffix size. |
|
1171 |
(lastRemoved startsWith:lastInserted) ifTrue:[ |
|
1172 |
oldWOSuffix size >= lastInserted size ifTrue:[ |
|
1173 |
^ (oldWOSuffix copyTo:lastInserted size) , (oldName copyFrom:lastRemoved size + 1) |
|
1174 |
] |
|
1175 |
]. |
|
1176 |
^ lastInserted , (oldName copyFrom:lastRemoved size + 1) |
|
1177 |
]. |
|
2376 | 1178 |
]. |
1179 |
||
1180 |
^ nil |
|
3135
7ad9ff29225e
changed #classCategoryLoadFromRepository
Claus Gittinger <cg@exept.de>
parents:
2977
diff
changeset
|
1181 |
|
7ad9ff29225e
changed #classCategoryLoadFromRepository
Claus Gittinger <cg@exept.de>
parents:
2977
diff
changeset
|
1182 |
"Modified: / 07-11-2006 / 13:58:39 / cg" |
6013 | 1183 |
"Modified: / 01-03-2019 / 14:53:36 / Claus Gittinger" |
2376 | 1184 |
! ! |
1185 |
||
2515
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1186 |
!DoWhatIMeanSupport class methodsFor:'typing distance'! |
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1187 |
|
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1188 |
isKey:k1 nextTo:k2 |
2774 | 1189 |
"return true, if k1 and k2 are adjacent keys on the keyboard. |
3299
1cd4b22434a6
code completion stuff moved to here (for reuse)
Claus Gittinger <cg@exept.de>
parents:
3198
diff
changeset
|
1190 |
This is used to specially priorize plausible typing errors of adjacent keys. |
4386 | 1191 |
CAVEAT: hard coded us-, german and french keyboards here; data should come from somewhere else." |
2515
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1192 |
|
3316 | 1193 |
^ self isKey:k1 nextTo:k2 onKeyboard:(self keyboard) |
2515
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1194 |
|
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1195 |
" |
4548 | 1196 |
self isKey:$a nextTo:$a |
1197 |
self isKey:$a nextTo:$s |
|
1198 |
self isKey:$a nextTo:$q |
|
1199 |
self isKey:$a nextTo:$w |
|
1200 |
self isKey:$a nextTo:$z |
|
1201 |
self isKey:$a nextTo:$x |
|
2515
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1202 |
" |
3316 | 1203 |
|
1204 |
"Modified: / 16-01-2008 / 17:17:31 / cg" |
|
2515
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1205 |
! |
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1206 |
|
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1207 |
isKey:k1 nextTo:k2 onKeyboard:keys |
5040 | 1208 |
"return true, if k1 and k2 are adjacent keys on the keyboard defined by keys. |
1209 |
This is used to specially priorize plausible typing errors of adjacent keys. |
|
1210 |
(typo checker uses a modified levenshtein, |
|
1211 |
in which keys next to each other are valued differently)" |
|
2515
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1212 |
|
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1213 |
|row1 row2 col1 col2| |
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1214 |
|
2774 | 1215 |
row1 := keys findFirst:[:eachRow | col1 := eachRow indexOf:k1. col1 ~~ 0]. |
2517 | 1216 |
row1 == 0 ifTrue:[^ false]. |
2774 | 1217 |
row2 := keys findFirst:[:eachRow | col2 := eachRow indexOf:k2. col2 ~~ 0]. |
2517 | 1218 |
row2 == 0 ifTrue:[^ false]. |
2515
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1219 |
|
2774 | 1220 |
^ (row1-row2) abs <= 1 and:[(col1-col2) abs <= 1] |
2515
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1221 |
|
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1222 |
" |
2774 | 1223 |
self isKey:$a nextTo:$q |
2517 | 1224 |
self isKey:$a nextTo:$x |
2515
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1225 |
" |
5040 | 1226 |
|
1227 |
"Modified (comment): / 01-05-2016 / 12:19:24 / cg" |
|
3316 | 1228 |
! |
1229 |
||
1230 |
keyboard |
|
4548 | 1231 |
"the keyboard layout |
4386 | 1232 |
(useful to figure out which keys are nearby a key, to find possible typing errors) |
1233 |
CAVEAT: hard coded us-, german and french keyboards here; data should come from somewhere else." |
|
3316 | 1234 |
|
1235 |
|lang| |
|
1236 |
||
1237 |
lang := UserPreferences current language. |
|
1238 |
lang == #de ifTrue:[ |
|
5542 | 1239 |
^ #( |
1240 |
'1234567890-' |
|
1241 |
'*qwertzuiop' |
|
1242 |
'**asdfghjkl:' |
|
1243 |
'***yxcvbnm' |
|
1244 |
). |
|
3316 | 1245 |
]. |
1246 |
||
1247 |
lang == #fr ifTrue:[ |
|
5542 | 1248 |
^ #( |
1249 |
'1234567890' |
|
1250 |
'*azertyuiop' |
|
1251 |
'**qsdfghjklm' |
|
1252 |
'***wxcvbn,' |
|
1253 |
). |
|
3316 | 1254 |
]. |
1255 |
||
4548 | 1256 |
^ #( |
5542 | 1257 |
'1234567890-' |
1258 |
'*qwertyuiop' |
|
1259 |
'**asdfghjkl:' |
|
1260 |
'***zxcvbnm' |
|
4386 | 1261 |
). |
3316 | 1262 |
|
1263 |
" |
|
4548 | 1264 |
self keyboard |
3316 | 1265 |
" |
1266 |
||
1267 |
"Created: / 16-01-2008 / 17:17:13 / cg" |
|
2515
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1268 |
! ! |
69cbb89c127e
+isKey:nextTo: for simple typo detection
Claus Gittinger <cg@exept.de>
parents:
2514
diff
changeset
|
1269 |
|
4238 | 1270 |
!DoWhatIMeanSupport methodsFor:'code completion'! |
1271 |
||
4426
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1272 |
codeCompletionFor: codeAspectArg language: languageOrNilArg method:methodOrNilArg orClass:classOrNilArg context:contextOrNilArg codeView:codeViewArg into:actionBlock |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1273 |
"provide code completion information by analyzing what the editing state is in codeViewArg |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1274 |
(cursor position, characters around cursor etc.) and calling back into actionBlock, passing |
4591 | 1275 |
the info as argument. |
1276 |
The interface has been defined in that way |
|
1277 |
(and tight coupling with internals of the editor) because |
|
1278 |
1) the completer needs to know about the text around the cursor position |
|
1279 |
2) the edit operation for completion may be non-trivial |
|
1280 |
(although not yet fully implemented, non-local rewrite procedures may and will be added in the future |
|
4426
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1281 |
For example, in many situations, both a completion of a unary selector before the cursor, |
4591 | 1282 |
or adding another keyword part after the cursor is possible. |
4426
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1283 |
Thus, this provides a list of completions PLUS a list of edit operations (as per completion), to |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1284 |
perform the completion. |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1285 |
The caller has to open a dialog, providing the suggestions, and perform the corresponding edit operation. |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1286 |
An additional array containing a textual description for each suggestion is also provided, which could |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1287 |
be shown as info or appended to the suggestions (such as 'complete variable', 'complete keyword', etc. |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1288 |
|
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1289 |
ContextOrNil is the current context, if this is called from the debugger; |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1290 |
or nil, if called from the browser. |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1291 |
If nonNil, we can make better guesses, because we actually know what a variable's type is" |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1292 |
|
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1293 |
| language | |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1294 |
|
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1295 |
codeAspect := codeAspectArg. |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1296 |
|
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1297 |
languageOrNilArg notNil ifTrue:[ |
4591 | 1298 |
language := languageOrNilArg |
4426
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1299 |
] ifFalse:[ |
4591 | 1300 |
contextOrNilArg notNil ifTrue:[ |
1301 |
| method | |
|
1302 |
||
1303 |
method := contextOrNilArg method. |
|
1304 |
method notNil ifTrue:[ |
|
1305 |
language := method programmingLanguage |
|
1306 |
] ifFalse:[ |
|
1307 |
contextOrNilArg isJavaContext ifTrue:[ |
|
1308 |
language := JavaLanguage instance |
|
1309 |
] ifFalse:[ |
|
1310 |
language := SmalltalkLanguage instance. |
|
1311 |
]. |
|
1312 |
]. |
|
1313 |
] ifFalse:[ |
|
1314 |
methodOrNilArg notNil ifTrue:[ |
|
1315 |
language := methodOrNilArg programmingLanguage |
|
1316 |
] ifFalse:[ |
|
1317 |
classOrNilArg notNil ifTrue:[ |
|
1318 |
language := classOrNilArg programmingLanguage |
|
1319 |
] |
|
1320 |
] |
|
1321 |
]. |
|
4426
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1322 |
]. |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1323 |
|
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1324 |
language notNil ifTrue:[ |
4591 | 1325 |
language isSmalltalk ifTrue:[ |
1326 |
^self codeCompletionForSmalltalkMethod: methodOrNilArg orClass: classOrNilArg context: contextOrNilArg codeView: codeViewArg into: actionBlock |
|
1327 |
]. |
|
1328 |
language isSTXJavaScript ifTrue:[ |
|
1329 |
^self codeCompletionForJavascriptMethod: methodOrNilArg orClass: classOrNilArg context: contextOrNilArg codeView: codeViewArg into: actionBlock |
|
1330 |
]. |
|
4426
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1331 |
]. |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1332 |
|
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1333 |
"/ No completion support for given language |
5051 | 1334 |
"/ self breakPoint: #cg. |
4426
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1335 |
self breakPoint: #jv. |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1336 |
|
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1337 |
"Created: / 27-09-2013 / 10:21:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
4451
8db9cfa578c0
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4444
diff
changeset
|
1338 |
"Modified: / 26-11-2013 / 23:43:16 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
4426
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1339 |
! |
76a0c105e8f4
Updated to take codeAspect as an argument for completion.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4423
diff
changeset
|
1340 |
|
4392
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1341 |
codeCompletionForLanguage: languageOrNil class: classOrNilArg context:contextOrNilArg codeView:codeViewArg |
5043 | 1342 |
"going to become OBSOLETE; migrating to use the the new 'xxx: into:' protocol. |
4392
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1343 |
contextOrNil is the current context, if this is called from the debugger; |
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1344 |
nil, if called from the browser. |
4591 | 1345 |
If nonNil, we can make better guesses, |
1346 |
because we actually know what a variable's type is." |
|
4392
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1347 |
|
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1348 |
| language | |
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1349 |
|
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1350 |
languageOrNil notNil ifTrue:[ |
4591 | 1351 |
language := languageOrNil |
4392
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1352 |
] ifFalse:[ |
4591 | 1353 |
contextOrNilArg notNil ifTrue:[ |
1354 |
language := contextOrNilArg method programmingLanguage. |
|
1355 |
] ifFalse:[ |
|
1356 |
classOrNilArg notNil ifTrue:[ |
|
1357 |
language := classOrNilArg programmingLanguage. |
|
1358 |
] |
|
1359 |
]. |
|
4392
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1360 |
]. |
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1361 |
|
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1362 |
language notNil ifTrue:[ |
4591 | 1363 |
language isSmalltalk ifTrue:[ |
4731 | 1364 |
classOrNil := classOrNilArg. |
1365 |
contextOrNil := contextOrNilArg. |
|
4591 | 1366 |
^self codeCompletionForSmalltalkClass: classOrNil context: contextOrNil codeView: codeViewArg |
1367 |
]. |
|
4392
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1368 |
]. |
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1369 |
|
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1370 |
"/ No completion support for given language |
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1371 |
self breakPoint: #cg. |
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1372 |
self breakPoint: #jv. |
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1373 |
|
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1374 |
"Created: / 18-09-2013 / 13:49:27 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
5007 | 1375 |
! |
1376 |
||
1377 |
setClass: classOrNilArg andContext:contextOrNilArg |
|
1378 |
classOrNil := classOrNilArg. |
|
1379 |
contextOrNil := contextOrNilArg. |
|
5272 | 1380 |
! |
1381 |
||
1382 |
setSelf: instanceOrNilArg |
|
1383 |
instanceOrNil := instanceOrNilArg. |
|
4392
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1384 |
! ! |
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1385 |
|
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1386 |
!DoWhatIMeanSupport methodsFor:'code completion - JavaScript'! |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1387 |
|
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1388 |
codeCompletionForJavascriptMethod:methodOrNilArg orClass:classOrNilArg context:contextOrNilArg codeView:codeViewArg into:actionBlock |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1389 |
"provide code completion information by analyzing what the editing state is in codeViewArg |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1390 |
(cursor position, characters around cursor etc.) and calling back into actionBlock, passing |
4591 | 1391 |
the info as argument. |
1392 |
The interface has been defined in that way |
|
1393 |
(and tight coupling with internals of the editor) because |
|
1394 |
1) the completer needs to know about the text around the cursor position |
|
1395 |
2) the edit operation for completion may be non-trivial |
|
1396 |
(although not yet fully implemented, non-local rewrite procedures may and will be added in the future |
|
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1397 |
For example, in many situations, both a completion of a unary selector before the cursor, |
4591 | 1398 |
or adding another keyword part after the cursor is possible. |
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1399 |
Thus, this provides a list of completions PLUS a list of edit operations (as per completion), to |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1400 |
perform the completion. |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1401 |
The caller has to open a dialog, providing the suggestions, and perform the corresponding edit operation. |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1402 |
An additional array containing a textual description for each suggestion is also provided, which could |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1403 |
be shown as info or appended to the suggestions (such as 'complete variable', 'complete keyword', etc. |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1404 |
|
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1405 |
ContextOrNil is the current context, if this is called from the debugger; |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1406 |
or nil, if called from the browser. |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1407 |
If nonNil, we can make better guesses, because we actually know what a variable's type is" |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1408 |
|
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1409 |
languageOrNil := STXJavaScriptLanguage instance. |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1410 |
methodOrNil := methodOrNilArg. |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1411 |
classOrNil := classOrNilArg. |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1412 |
codeView := codeViewArg. |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1413 |
contextOrNil := contextOrNilArg. |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1414 |
|
4423
c21baefab5d0
Call to JavaScriptCompletionEngine when completing for JS
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4422
diff
changeset
|
1415 |
JavaScriptCompletionEngine notNil ifTrue:[ |
4591 | 1416 |
JavaScriptCompletionEngine new |
1417 |
completeForMethod: methodOrNil class: classOrNil context: contextOrNil codeView: codeView into: actionBlock. |
|
4548 | 1418 |
]. |
4423
c21baefab5d0
Call to JavaScriptCompletionEngine when completing for JS
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4422
diff
changeset
|
1419 |
|
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1420 |
self information:'Not yet supported'. |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1421 |
|
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1422 |
"Created: / 18-09-2013 / 16:24:36 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
4423
c21baefab5d0
Call to JavaScriptCompletionEngine when completing for JS
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4422
diff
changeset
|
1423 |
"Modified: / 25-09-2013 / 15:13:34 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1424 |
! ! |
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1425 |
|
4392
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1426 |
!DoWhatIMeanSupport methodsFor:'code completion - Smalltalk'! |
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1427 |
|
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1428 |
codeCompletionForSmalltalkClass: classOrNilArg context:contextOrNilArg codeView:codeViewArg |
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1429 |
|
4379 | 1430 |
"OBSOLETE; migrating to use the the new 'xxx: into:' protocol. |
4352 | 1431 |
contextOrNil is the current context, if this is called from the debugger; |
4238 | 1432 |
nil, if called from the browser. |
1433 |
If nonNil, we can make better guesses, because we actually know what a variable's type is. |
|
1434 |
This is not yet done, sigh" |
|
4548 | 1435 |
|
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1436 |
|crsrPos char interval source node parent checkedNode instanceOrNilArg |
4710 | 1437 |
forceNewMessageSend classOfReceiver prevChar| |
4238 | 1438 |
|
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1439 |
languageOrNil := SmalltalkLanguage instance. |
4238 | 1440 |
classOrNil := classOrNilArg. |
1441 |
codeView := codeViewArg. |
|
1442 |
||
1443 |
crsrPos := codeView characterPositionOfCursor"-1". |
|
1444 |
char := codeView characterAtCharacterPosition:crsrPos. |
|
4301 | 1445 |
"/ Transcript show:crsrPos; show:' '; showCR:char. |
4238 | 1446 |
[crsrPos > 1 and:[char isSeparator or:['.' includes:char]]] whileTrue:[ |
4710 | 1447 |
crsrPos := crsrPos - 1. |
1448 |
char := codeView characterAtCharacterPosition:crsrPos. |
|
4238 | 1449 |
]. |
1450 |
||
1451 |
interval := codeView selectedInterval. |
|
4301 | 1452 |
"/ Transcript show:'iv: '; showCR:interval. |
4238 | 1453 |
interval isEmpty ifTrue:[ |
4710 | 1454 |
interval := crsrPos"-1" to:crsrPos. |
1455 |
"/ Transcript show:'iv2: '; showCR:interval. |
|
4238 | 1456 |
]. |
1457 |
||
1458 |
source := codeView contentsAsString string. |
|
1459 |
source := source copyTo:crsrPos. |
|
1460 |
||
1461 |
"/ this is too naive and stupid; if there is a syntactic error, |
|
1462 |
"/ we will not find a node for a long time (stepping back more and more, |
|
1463 |
"/ until reaching the beginning). This leads to a thousand and more times reparsing |
|
1464 |
"/ without any progress. |
|
1465 |
"/ TODO: do it vice-versa, in that the parser does a callOut for every node generated |
|
1466 |
"/ as it parses the code. Stop, when the interval is hit. |
|
1467 |
"/ that will also work for syntactic incorrect source code. |
|
1468 |
classOrNil notNil ifTrue:[ |
|
4710 | 1469 |
node := self findNodeForInterval:interval in:source allowErrors:true mustBeMethod:true. |
1470 |
"/ Transcript show:'nd1: '; showCR:node. |
|
4238 | 1471 |
]. |
1472 |
node isNil ifTrue:[ |
|
4710 | 1473 |
node := self findNodeForInterval:interval in:source allowErrors:true mustBeMethod:false. |
1474 |
"/ Transcript show:'nd2 try: '; showCR:node. |
|
1475 |
node isNil ifTrue:[ |
|
1476 |
"/ Transcript showCR:'No parseNode found (syntax error before or in comment?)'. |
|
1477 |
self breakPoint:#cg. |
|
1478 |
"/ self information:'No parseNode found (syntax error before or in comment?)'. |
|
1479 |
^ self. |
|
1480 |
] |
|
4238 | 1481 |
]. |
1482 |
||
4346 | 1483 |
forceNewMessageSend := false. |
1484 |
||
1485 |
"/ if there a separator between the variable's name and the cursor position... |
|
4710 | 1486 |
prevChar := codeView characterBeforeCursor. |
1487 |
(prevChar isSeparator or:[ ')}]''' includes:prevChar ]) ifTrue:[ |
|
1488 |
(node isVariable |
|
1489 |
and:[ (parent := node parent) notNil |
|
1490 |
and:[ parent isMessage ]] |
|
1491 |
) ifTrue:[ |
|
1492 |
"/ completion after a variable node... |
|
1493 |
parent isKeyword ifTrue:[ |
|
1494 |
"/ and it is a keyword message, we complete the keyword message instead |
|
1495 |
node := parent. |
|
1496 |
] ifFalse:[ |
|
1497 |
"/ otherwise, a unary message is probably intended to be sent to the variable. |
|
1498 |
"/ (however, no character is available to determine what is useful) |
|
1499 |
forceNewMessageSend := true. |
|
1500 |
]. |
|
1501 |
] ifFalse:[ |
|
1502 |
(node isMessage and:[node isUnary]) ifTrue:[ |
|
1503 |
"/ expanding <rcvr> foo |<- cursor here (i.e. a space after foo) |
|
1504 |
"/ |
|
1505 |
forceNewMessageSend := true. |
|
4346 | 1506 |
"/ "/ can we see what we get from foo? |
4548 | 1507 |
"/ classOfReceiver := self |
4346 | 1508 |
"/ classOfReceiver:node receiver |
1509 |
"/ inClass:classOrNil instance:instanceOrNil context:contextOrNil. |
|
1510 |
"/ classOfReceiver notNil ifTrue:[ |
|
1511 |
"/ |mthd| |
|
1512 |
"/ |
|
1513 |
"/ mthd := classOfReceiver lookupMethodFor:node selector. |
|
1514 |
"/ mthd notNil ifTrue:[ |
|
1515 |
"/ self halt. |
|
1516 |
"/ (ParseTreeSearcher isDefinitelyGetterMethod:mthd) ifTrue:[ |
|
1517 |
"/ forceNewMessageSend := true. |
|
1518 |
"/ ] |
|
1519 |
"/ ] |
|
1520 |
"/ ]. |
|
4710 | 1521 |
] |
1522 |
] |
|
4346 | 1523 |
]. |
1524 |
||
1525 |
forceNewMessageSend ifTrue:[ |
|
4710 | 1526 |
"/ completion with nothing to start (right after a variable) |
1527 |
"/ see what the variable can understand and present the most useful stuff (very thin ice here) |
|
1528 |
classOfReceiver := self classOfNode:node. |
|
1529 |
classOfReceiver isNil ifTrue:[ |
|
1530 |
"/ it does not make sense to offer anything, if we don't have any idea of what this |
|
1531 |
"/ will be... |
|
5687 | 1532 |
Screen current beepInEditor. |
4710 | 1533 |
] ifFalse:[ |
1534 |
|superClass possible choice| |
|
1535 |
||
1536 |
possible := classOfReceiver selectors. |
|
1537 |
superClass := classOfReceiver superclass. |
|
4938 | 1538 |
[superClass notNil "and:[(possible size + superClass selectors size) < 500]"] whileTrue:[ |
4710 | 1539 |
possible := possible,superClass selectors. |
1540 |
superClass := superClass superclass. |
|
1541 |
]. |
|
1542 |
possible := possible copy sort. |
|
4938 | 1543 |
choice := self askUserForCompletion:('Message to "%1"' bindWith:node formattedCode) for:codeView from:possible. |
4710 | 1544 |
choice isNil ifTrue:[ |
1545 |
Screen current beep. |
|
1546 |
^ self |
|
1547 |
]. |
|
1548 |
||
1549 |
codeView |
|
1550 |
undoableDo:[ |
|
1551 |
codeView insertStringAtCursor:choice |
|
1552 |
] |
|
1553 |
info:'Completion'. |
|
1554 |
]. |
|
1555 |
^ self |
|
4238 | 1556 |
]. |
1557 |
||
1558 |
node isVariable ifTrue:[ |
|
4710 | 1559 |
self codeCompletionForVariable:node inClass:classOrNil codeView:codeView. |
1560 |
^ self. |
|
4238 | 1561 |
]. |
1562 |
node isLiteral ifTrue:[ |
|
4710 | 1563 |
node value isSymbol ifTrue:[ |
1564 |
self codeCompletionForLiteralSymbol:node inClass:classOrNil codeView:codeView. |
|
1565 |
^ self. |
|
1566 |
]. |
|
1567 |
^ self "/ huh - strings or what? |
|
4238 | 1568 |
]. |
1569 |
||
1570 |
checkedNode := node. |
|
1571 |
[checkedNode notNil] whileTrue:[ |
|
4710 | 1572 |
checkedNode isMessage ifTrue:[ |
1573 |
"/ completion in a message-send |
|
1574 |
contextOrNilArg notNil ifTrue:[ |
|
4329 | 1575 |
"/ |rcvrNode idx rcvr val| |
1576 |
"/ |
|
1577 |
"/ (rcvrNode := checkedNode receiver) isVariable ifTrue:[ |
|
1578 |
"/ rcvrNode isSelf ifTrue:[ |
|
1579 |
"/ classOrNil := contextOrNil receiver class. |
|
1580 |
"/ ] ifFalse:[ |
|
1581 |
"/ (idx := contextOrNil argAndVarNames indexOf:rcvrNode name) ~~ 0 ifTrue:[ |
|
1582 |
"/ val := contextOrNil argsAndVars at:idx. |
|
1583 |
"/ classOrNil := val class. |
|
1584 |
"/ ] ifFalse:[ |
|
1585 |
"/ (idx := contextOrNil receiver class allInstVarNames indexOf:rcvrNode name) ~~ 0 ifTrue:[ |
|
1586 |
"/ val := contextOrNil receiver instVarNamed:rcvrNode name. |
|
1587 |
"/ classOrNil := val class. |
|
1588 |
"/ ] |
|
1589 |
"/ ] |
|
1590 |
"/ ] |
|
1591 |
"/ ]. |
|
4710 | 1592 |
instanceOrNilArg := contextOrNilArg receiver |
1593 |
]. |
|
1594 |
self |
|
1595 |
codeCompletionForMessage:checkedNode |
|
1596 |
inClass:classOrNil instance:instanceOrNilArg |
|
1597 |
context:contextOrNilArg codeView:codeView. |
|
1598 |
^ self |
|
1599 |
]. |
|
1600 |
checkedNode isMethod ifTrue:[ |
|
1601 |
"/ completion in a method's selector pattern |
|
1602 |
self codeCompletionForMethodSpec:checkedNode. |
|
1603 |
^ self. |
|
1604 |
]. |
|
1605 |
checkedNode := checkedNode parent. |
|
4238 | 1606 |
]. |
1607 |
||
1608 |
self information:'Node is neither variable nor message.'. |
|
1609 |
||
4392
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1610 |
"Created: / 18-09-2013 / 15:25:16 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1611 |
! |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1612 |
|
4392
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1613 |
codeCompletionForSmalltalkMethod:methodOrNilArg orClass:classOrNilArg context:contextOrNilArg codeView:codeViewArg into:actionBlock |
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1614 |
"provide code completion information by analyzing what the editing state is in codeViewArg |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1615 |
(cursor position, characters around cursor etc.) and calling back into actionBlock, passing |
4591 | 1616 |
the info as argument. |
1617 |
The interface has been defined in that way |
|
5509 | 1618 |
(and tight coupling with internals of the editor) |
1619 |
because |
|
4591 | 1620 |
1) the completer needs to know about the text around the cursor position |
1621 |
2) the edit operation for completion may be non-trivial |
|
5509 | 1622 |
(although not yet fully implemented, non-local rewrite procedures may and will be added in the future) |
1623 |
||
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1624 |
For example, in many situations, both a completion of a unary selector before the cursor, |
4591 | 1625 |
or adding another keyword part after the cursor is possible. |
4926 | 1626 |
Thus, this provides a list of completions PLUS a list of edit operations (as per completion), |
1627 |
to perform the completion. |
|
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1628 |
The caller has to open a dialog, providing the suggestions, and perform the corresponding edit operation. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1629 |
An additional array containing a textual description for each suggestion is also provided, which could |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1630 |
be shown as info or appended to the suggestions (such as 'complete variable', 'complete keyword', etc. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1631 |
|
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1632 |
ContextOrNil is the current context, if this is called from the debugger; |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1633 |
or nil, if called from the browser. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1634 |
If nonNil, we can make better guesses, because we actually know what a variable's type is" |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1635 |
|
4524 | 1636 |
|crsrPos char interval i source partialSource cursorLineSource |
5080 | 1637 |
suggestions actions title suggestionCollector| |
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1638 |
|
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
1639 |
languageOrNil := SmalltalkLanguage instance. |
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1640 |
methodOrNil := methodOrNilArg. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1641 |
classOrNil := classOrNilArg. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1642 |
codeView := codeViewArg. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1643 |
contextOrNil := contextOrNilArg. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1644 |
|
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1645 |
"/ classOrNil isNil ifTrue:[ |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1646 |
"/ self information:'No class'. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1647 |
"/ ^ self. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1648 |
"/ ]. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1649 |
|
4524 | 1650 |
crsrPos := codeView characterPositionOfCursor. |
4592 | 1651 |
char := codeView characterAtCharacterPosition:(crsrPos-1 max:1). |
4591 | 1652 |
[crsrPos > 1 and:[char isSeparator "or:['.' includes:char]"]] whileTrue:[ |
1653 |
crsrPos := crsrPos - 1. |
|
1654 |
char := codeView characterAtCharacterPosition:crsrPos. |
|
1655 |
]. |
|
4711 | 1656 |
char == $. ifTrue:[ |
4591 | 1657 |
"/ either at end of statement or after a character constant |
1658 |
crsrPos == 1 ifTrue:[^ self]. |
|
1659 |
(codeView characterAtCharacterPosition:crsrPos-1) == $$ ifFalse:[^ self]. |
|
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1660 |
]. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1661 |
|
5080 | 1662 |
suggestionCollector := |
5509 | 1663 |
[:listOfSuggestions :listOfActionsOrBlock :titleWhenAsking| |
1664 |
||
5080 | 1665 |
"/ may be called multiple times!! |
5509 | 1666 |
"/ may also be called for duplicate suggestions!! |
5080 | 1667 |
suggestions isNil ifTrue:[ |
1668 |
suggestions := listOfSuggestions. |
|
1669 |
actions := listOfActionsOrBlock. |
|
1670 |
] ifFalse:[ |
|
1671 |
suggestions := suggestions asOrderedCollection. |
|
1672 |
actions isBlock ifTrue:[ |
|
5523 | 1673 |
actions := Array new:(suggestions size) withAll:actions. |
5080 | 1674 |
]. |
1675 |
actions := actions asOrderedCollection. |
|
5509 | 1676 |
listOfSuggestions doWithIndex:[:suggestion :idx | |
1677 |
(suggestions includes:suggestion) ifFalse:[ |
|
1678 |
suggestions add:suggestion. |
|
1679 |
listOfActionsOrBlock isBlock ifTrue:[ |
|
1680 |
actions add:listOfActionsOrBlock. |
|
1681 |
] ifFalse:[ |
|
1682 |
actions add:(listOfActionsOrBlock at:idx). |
|
1683 |
] |
|
1684 |
] |
|
5080 | 1685 |
]. |
1686 |
]. |
|
5509 | 1687 |
title := titleWhenAsking. |
5080 | 1688 |
]. |
1689 |
||
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1690 |
interval := crsrPos-1 to:crsrPos. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1691 |
|
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1692 |
source := codeView contentsAsString string. |
5931 | 1693 |
partialSource := source copyToMax:crsrPos. |
5509 | 1694 |
partialSource isWhitespace ifTrue:[^ self]. |
1695 |
||
4524 | 1696 |
methodOrNilArg isNil ifTrue:[ |
4926 | 1697 |
(partialSource endsWith:Character cr) ifTrue:[ |
1698 |
partialSource := partialSource copyButLast. |
|
1699 |
]. |
|
5504 | 1700 |
cursorLineSource := partialSource copy. |
1701 |
||
5509 | 1702 |
"/ this cares for doIt expressions in the last cursor line; |
1703 |
"/ however, we skip this, if the source starts with a lowercase letter |
|
1704 |
"/ (then it is likely, that the user wants to define a new method) |
|
5552 | 1705 |
(partialSource isLowercaseFirst) ifFalse:[ |
5509 | 1706 |
|
1707 |
"/ first try parsing the current cursor line. |
|
1708 |
"/ this helps doIts in a workspace, where additional garbage is often before the actual expression to be evaluated |
|
1709 |
(i := partialSource lastIndexOf:Character cr) ~~ 0 ifTrue:[ |
|
1710 |
"/ because cursorPositions and node-positions are required elsewhere to be correct, |
|
1711 |
"/ I cannot just snip off the line and parse that one alone (later corrections will do so at wrong position). |
|
1712 |
"/ Instead, create a copy of the whole source, with the stuff before the cursor line being blanked out. |
|
1713 |
"/ However, because somewhere else, we fetch characters from the codeView using the index, |
|
1714 |
"/ we must preserve the line structure (i.e. keep crs). |
|
1715 |
1 to:i do:[:pos | |
|
1716 |
(cursorLineSource at:pos) ~= Character cr ifTrue:[ |
|
1717 |
cursorLineSource at:pos put:Character space. |
|
1718 |
] |
|
1719 |
]. |
|
1720 |
]. |
|
1721 |
self |
|
1722 |
tryCodeCompletionWithSource:cursorLineSource nodeInterval:interval |
|
1723 |
at:crsrPos mustBeExpression:true |
|
1724 |
into:suggestionCollector. |
|
1725 |
]. |
|
4524 | 1726 |
]. |
4933 | 1727 |
|
5528 | 1728 |
true "suggestions isNil" ifTrue:[ |
1729 |
"/ try parsing the partial source (from beginning up to the cursor) |
|
1730 |
self |
|
1731 |
tryCodeCompletionWithSource:partialSource nodeInterval:interval |
|
1732 |
at:crsrPos mustBeExpression:(classOrNilArg isNil and:[methodOrNilArg isNil]) |
|
1733 |
into:suggestionCollector. |
|
1734 |
]. |
|
1735 |
||
5509 | 1736 |
true "suggestions isEmptyOrNil" ifTrue:[ |
5076 | 1737 |
"/ try parsing the whole source |
1738 |
self |
|
1739 |
tryCodeCompletionWithSource:source nodeInterval:interval |
|
1740 |
at:crsrPos mustBeExpression:false |
|
5080 | 1741 |
into:suggestionCollector |
5076 | 1742 |
]. |
1743 |
||
1744 |
suggestions isEmptyOrNil ifTrue:[ |
|
1745 |
"/ nothing found |
|
1746 |
^ self |
|
4933 | 1747 |
]. |
1748 |
actionBlock value:suggestions value:actions value:title. |
|
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1749 |
|
4392
9570ef5f05c3
Added language discrimination to code completion entry-entry proint method.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4391
diff
changeset
|
1750 |
"Created: / 18-09-2013 / 15:25:36 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
5552 | 1751 |
"Modified: / 22-06-2017 / 06:56:30 / cg" |
5931 | 1752 |
"Modified: / 02-11-2018 / 20:17:48 / Claus Gittinger" |
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1753 |
! ! |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1754 |
|
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1755 |
!DoWhatIMeanSupport methodsFor:'code completion - obsolete'! |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1756 |
|
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1757 |
codeCompletionForClass:classOrNilArg context:contextOrNil codeView:codeViewArg |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1758 |
<resource: #obsolete> |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1759 |
"OBSOLETE; migrating to use the the new 'xxx: into:' protocol. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1760 |
contextOrNil is the current context, if this is called from the debugger; |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1761 |
nil, if called from the browser. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1762 |
If nonNil, we can make better guesses, because we actually know what a variable's type is. |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1763 |
This is not yet done, sigh" |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1764 |
|
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1765 |
^self codeCompletionForLanguage: nil class:classOrNilArg context:contextOrNil codeView:codeViewArg |
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1766 |
|
4238 | 1767 |
"Modified: / 04-07-2006 / 18:48:26 / fm" |
4346 | 1768 |
"Modified: / 28-08-2013 / 17:15:25 / cg" |
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1769 |
"Modified: / 18-09-2013 / 14:15:27 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
4352 | 1770 |
! |
1771 |
||
1772 |
codeCompletionForMethod:methodOrNilArg orClass:classOrNilArg context:contextOrNilArg codeView:codeViewArg into:actionBlock |
|
4391
903ec83fb9cd
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4390
diff
changeset
|
1773 |
<resource: #obsolete> |
4524 | 1774 |
^ self |
5542 | 1775 |
codeCompletionForSmalltalkMethod:methodOrNilArg orClass:classOrNilArg context:contextOrNilArg codeView:codeViewArg into:actionBlock |
4238 | 1776 |
! ! |
1777 |
||
1778 |
!DoWhatIMeanSupport methodsFor:'code completion-helpers'! |
|
1779 |
||
4548 | 1780 |
askUserForCompletion:what for:codeView at:position from:allTheBest |
4238 | 1781 |
|list choice lastChoice| |
1782 |
||
1783 |
"/ cg: until the new stuff works,... |
|
1784 |
^ self old_askUserForCompletion:what for:codeView from:allTheBest. |
|
1785 |
||
4346 | 1786 |
"/ allTheBest isEmpty ifTrue:[ |
1787 |
"/ ^ nil |
|
1788 |
"/ ]. |
|
1789 |
"/ allTheBest size == 1 ifTrue:[ |
|
1790 |
"/ ^ allTheBest first |
|
1791 |
"/ ]. |
|
1792 |
"/ list := allTheBest. |
|
1793 |
"/ LastChoices notNil ifTrue:[ |
|
1794 |
"/ lastChoice := LastChoices at:what ifAbsent:nil. |
|
1795 |
"/ lastChoice notNil ifTrue:[ |
|
1796 |
"/ list := { lastChoice allBold } , (list copyWithout:lastChoice). |
|
1797 |
"/ ]. |
|
1798 |
"/ ]. |
|
4548 | 1799 |
"/ choice := Tools::CodeCompletionMenu |
4346 | 1800 |
"/ openFor:codeView |
1801 |
"/ at:position |
|
1802 |
"/ with:allTheBest. |
|
1803 |
"/ LastChoices isNil ifTrue:[ |
|
1804 |
"/ LastChoices := Dictionary new. |
|
1805 |
"/ ]. |
|
1806 |
"/ LastChoices at:what put:choice. |
|
1807 |
"/ ^ choice string |
|
4238 | 1808 |
|
1809 |
"Created: / 16-02-2010 / 10:09:57 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
4346 | 1810 |
"Modified: / 28-08-2013 / 16:41:35 / cg" |
4238 | 1811 |
! |
1812 |
||
1813 |
askUserForCompletion:what for:codeView from:allTheBest |
|
1814 |
|list resources choice lastChoice| |
|
1815 |
||
1816 |
allTheBest isEmpty ifTrue:[ ^ nil ]. |
|
1817 |
allTheBest size == 1 ifTrue:[ ^ allTheBest first ]. |
|
1818 |
||
1819 |
list := allTheBest. |
|
1820 |
LastChoices notNil ifTrue:[ |
|
5542 | 1821 |
lastChoice := LastChoices at:what ifAbsent:nil. |
1822 |
lastChoice notNil ifTrue:[ |
|
1823 |
list := {lastChoice. nil. } , (list copyWithout:lastChoice). |
|
1824 |
]. |
|
4238 | 1825 |
]. |
1826 |
||
1827 |
list size < 30 ifTrue:[ |
|
5542 | 1828 |
|menu idx exitKey| |
1829 |
||
1830 |
menu := PopUpMenu labels:list. |
|
1831 |
menu hideOnKeyFilter:[:key | |hide| |
|
1832 |
hide := ( #( CursorDown CursorUp Escape Return ) includes: key) not. |
|
1833 |
hide ifTrue:[ |
|
1834 |
exitKey := key. |
|
1835 |
]. |
|
1836 |
hide]. |
|
1837 |
||
1838 |
idx := menu startUp. |
|
1839 |
idx == 0 ifTrue:[ |
|
1840 |
exitKey notNil ifTrue:[ |
|
1841 |
codeView keyPress:exitKey x:0 y:0. |
|
1842 |
]. |
|
1843 |
^ nil |
|
1844 |
]. |
|
1845 |
choice := list at:idx. |
|
4238 | 1846 |
] ifFalse:[ |
5542 | 1847 |
resources := codeView application isNil |
1848 |
ifTrue:[ codeView resources] |
|
1849 |
ifFalse:[ codeView application resources ]. |
|
1850 |
||
1851 |
choice := Dialog |
|
1852 |
choose:(resources string:'Choose ',what) |
|
1853 |
fromList:list |
|
1854 |
lines:20 |
|
1855 |
title:(resources string:'Code completion'). |
|
1856 |
choice isNil ifTrue:[^ nil]. |
|
4238 | 1857 |
]. |
1858 |
||
1859 |
LastChoices isNil ifTrue:[ |
|
5542 | 1860 |
LastChoices := Dictionary new. |
4238 | 1861 |
]. |
1862 |
LastChoices at:what put:choice. |
|
1863 |
^ choice |
|
1864 |
||
1865 |
"Created: / 10-11-2006 / 14:00:53 / cg" |
|
1866 |
! |
|
1867 |
||
5106 | 1868 |
codeCompletionForBlockArgument:node into:actionBlock |
1869 |
! |
|
1870 |
||
5900 | 1871 |
codeCompletionForLiteralString:partialString node:node considerAll:considerAll into:actionBlock |
1872 |
"/ nothing done here, but redefined in subclasses which may (i.e. expecco code completer) |
|
1873 |
||
1874 |
"Created: / 29-08-2018 / 16:31:22 / Claus Gittinger" |
|
1875 |
! |
|
1876 |
||
1877 |
codeCompletionForLiteralString:partialString node:node in:source considerAll:considerAll into:actionBlock |
|
1878 |
"/ nothing done here, but redefined in subclasses which may (i.e. expecco code completer) |
|
1879 |
||
1880 |
"Created: / 29-08-2018 / 16:47:28 / Claus Gittinger" |
|
1881 |
! |
|
1882 |
||
4382 | 1883 |
codeCompletionForLiteralSymbol:nodeOrNil element:tokenOrNil considerAll:considerAll into:actionBlock |
5542 | 1884 |
"looking for all symbols is way too much and imprecise; |
4352 | 1885 |
experiment: only present symbols which are used by the class, |
4544 | 1886 |
and classes in that class category, or at least: implemented as method. |
1887 |
We'll see..." |
|
4352 | 1888 |
|
4382 | 1889 |
|sym possibleCompletions longest editAction start stop addSymbol |
1890 |
parentSelector parent symbolSelectorClass| |
|
4238 | 1891 |
|
4302 | 1892 |
"/ Transcript show:'lit in '; show:methodOrNil; show:' / '; showCR:classOrNil. |
4238 | 1893 |
|
4382 | 1894 |
start := (nodeOrNil ? tokenOrNil) start. |
1895 |
stop := (nodeOrNil ? tokenOrNil) stop. |
|
4352 | 1896 |
(codeView characterAtCharacterPosition:stop) == $' ifTrue:[ |
5031 | 1897 |
^ self. |
4352 | 1898 |
]. |
1899 |
||
4382 | 1900 |
sym := (nodeOrNil ? tokenOrNil) value. |
4544 | 1901 |
possibleCompletions := Set new. |
4238 | 1902 |
|
4352 | 1903 |
addSymbol := |
5031 | 1904 |
[:aSymbol | |
1905 |
(aSymbol startsWith:sym) ifTrue:[ |
|
1906 |
(aSymbol = sym) ifFalse:[ |
|
1907 |
possibleCompletions add:aSymbol |
|
1908 |
]. |
|
1909 |
]. |
|
1910 |
]. |
|
4352 | 1911 |
|
4382 | 1912 |
(nodeOrNil notNil |
1913 |
and:[ (parent := nodeOrNil parent) notNil |
|
1914 |
and:[ parent isMessage ]]) ifTrue:[ |
|
5031 | 1915 |
parentSelector := parent selector. |
1916 |
( #( perform: perform:ifNotUnderstood: ) includes: parentSelector) ifTrue:[ |
|
1917 |
symbolSelectorClass := self classOfNode:parent receiver. |
|
1918 |
]. |
|
1919 |
( #( #'onChangeSend:' ) includes: parentSelector) ifTrue:[ |
|
1920 |
"/ assume that send-target will be self. |
|
1921 |
(methodOrNil notNil and:[ methodOrNil selector notNil and:[ methodOrNil selector isUnarySelector ]]) ifTrue:[ |
|
1922 |
addSymbol value:(methodOrNil selector,'Changed'). |
|
1923 |
]. |
|
1924 |
symbolSelectorClass := classOrNil. |
|
1925 |
]. |
|
1926 |
( #( #'onChangeSend:to:' ) includes: parentSelector) ifTrue:[ |
|
1927 |
symbolSelectorClass := self classOfNode:parent arguments second. |
|
1928 |
]. |
|
5542 | 1929 |
|
5074 | 1930 |
"/ actually, I found that this gives good suggestions. |
1931 |
symbolSelectorClass isNil ifTrue:[ |
|
1932 |
symbolSelectorClass := classOrNil. |
|
1933 |
]. |
|
5542 | 1934 |
|
5031 | 1935 |
symbolSelectorClass notNil ifTrue:[ |
1936 |
symbolSelectorClass withAllSuperclassesDo:[:cls | |
|
1937 |
cls ~~ Object ifTrue:[ |
|
1938 |
cls ~~ Model ifTrue:[ |
|
5462 | 1939 |
cls selectorsDo:addSymbol. |
5031 | 1940 |
] |
1941 |
] |
|
1942 |
] |
|
1943 |
]. |
|
4382 | 1944 |
]. |
1945 |
||
4368 | 1946 |
(considerAll or:[classOrNil isNil]) ifTrue:[ |
5031 | 1947 |
Smalltalk allClassesDo:[:cls | |
1948 |
cls theNonMetaclass methodDictionary keys do:addSymbol. |
|
1949 |
cls theMetaclass methodDictionary keys do:addSymbol. |
|
1950 |
]. |
|
1951 |
||
1952 |
"/ Symbol allInstancesDo:addSymbol. |
|
4368 | 1953 |
] ifFalse:[ |
5031 | 1954 |
Smalltalk allClassesInCategory:classOrNil do:[:cls | |
1955 |
cls theNonMetaclass instAndClassMethodsDo:[:mthd | |
|
1956 |
mthd usedSymbols do:addSymbol |
|
1957 |
] |
|
1958 |
]. |
|
4238 | 1959 |
]. |
4352 | 1960 |
|
5031 | 1961 |
"/ add symbolic literals (especially for spec methods) |
1962 |
classOrNil notNil ifTrue:[ |
|
1963 |
classOrNil theMetaclass instAndClassMethodsDo:[:mthd | |
|
1964 |
mthd literalsDo:[:lit | |
|
1965 |
lit isSymbol ifTrue:[ addSymbol value: lit ] |
|
5542 | 1966 |
]. |
1967 |
]. |
|
1968 |
]. |
|
1969 |
||
4544 | 1970 |
possibleCompletions := possibleCompletions asOrderedCollection sort. |
4352 | 1971 |
|
1972 |
longest := possibleCompletions longestCommonPrefix. |
|
1973 |
possibleCompletions remove:longest ifAbsent:[]. |
|
1974 |
possibleCompletions addFirst: longest. |
|
1975 |
||
1976 |
editAction := |
|
5031 | 1977 |
[:chosenIndex | |
1978 |
|chosen oldSym oldLen newLen| |
|
1979 |
||
1980 |
chosen := possibleCompletions at:chosenIndex. |
|
1981 |
chosen notNil ifTrue:[ |
|
1982 |
(codeView characterAtCharacterPosition:start) == $# ifTrue:[ |
|
1983 |
start := start + 1. |
|
1984 |
]. |
|
1985 |
(codeView characterAtCharacterPosition:start) == $' ifTrue:[ |
|
1986 |
start := start + 1. |
|
1987 |
]. |
|
1988 |
||
1989 |
oldSym := (codeView textFromCharacterPosition:start to:stop) asString string withoutSeparators. |
|
1990 |
||
1991 |
codeView |
|
1992 |
undoableDo:[ |
|
1993 |
codeView replaceFromCharacterPosition:start to:stop with:chosen |
|
1994 |
] |
|
1995 |
info:'Completion'. |
|
1996 |
||
1997 |
(chosen startsWith:oldSym) ifTrue:[ |
|
1998 |
oldLen := stop - start + 1. |
|
1999 |
newLen := chosen size. |
|
2000 |
codeView selectFromCharacterPosition:start+oldLen to:start+newLen-1. |
|
2001 |
codeView dontReplaceSelectionOnInput |
|
2002 |
]. |
|
2003 |
] |
|
2004 |
]. |
|
4352 | 2005 |
|
2006 |
actionBlock value:possibleCompletions value:editAction value:'symbol'. |
|
4238 | 2007 |
|
2008 |
"Modified: / 16-02-2010 / 10:15:05 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
5462 | 2009 |
"Modified: / 10-02-2017 / 10:32:54 / cg" |
5542 | 2010 |
"Modified (comment): / 17-05-2017 / 12:13:33 / mawalch" |
4238 | 2011 |
! |
2012 |
||
2013 |
codeCompletionForMessage:node into:actionBlock |
|
5031 | 2014 |
"find good completions for a message selector in a message-send node" |
2015 |
||
5613 | 2016 |
|selector lcSelector selectorWithoutColon lcSelectorWithoutColon |
2017 |
bestSelectors parentSelector newParentSelector bestSelectors2 bestWithParenthesis allBest numArgs |
|
4548 | 2018 |
newParts nSelParts oldLen newLen selectorParts |
5112 | 2019 |
parentNode nodeReceiver "selectorsSentInCode" selectorsImplementedInClass |
5044 | 2020 |
editAction parentNodeClassIfKnown |
5038 | 2021 |
receiverNodeClassIfKnown |
5071 | 2022 |
offerParenthisationAroundNode parenthesisAroundIndex parentNodeToParenthesize |
2023 |
offerValueInsertion valueToInsert valueToInsertIndex valueInfo |
|
5043 | 2024 |
classesFromAssignmentsToReceiver otherMessagesToReceiver |
5103 | 2025 |
canParenthesize classesOfReceiver| |
5040 | 2026 |
|
5112 | 2027 |
Verbose == true ifTrue:[ |
2028 |
Transcript show:'node '; show:node; show:' ; '. |
|
2029 |
Transcript show:'msg in '; show:methodOrNil; show:' / '; showCR:classOrNil. |
|
2030 |
]. |
|
2031 |
||
5038 | 2032 |
offerParenthisationAroundNode := nil. |
5071 | 2033 |
offerValueInsertion := false. |
5040 | 2034 |
|
4238 | 2035 |
selector := node selector. |
4936 | 2036 |
lcSelector := selector asLowercase. |
5613 | 2037 |
|
2038 |
selectorWithoutColon := selector. |
|
2039 |
lcSelectorWithoutColon := lcSelector. |
|
2040 |
(selectorWithoutColon includes:$:) ifTrue:[ |
|
2041 |
selectorWithoutColon := selector upTo:$:. |
|
2042 |
]. |
|
2043 |
(lcSelectorWithoutColon includes:$:) ifTrue:[ |
|
2044 |
lcSelectorWithoutColon := lcSelector upTo:$:. |
|
2045 |
]. |
|
2046 |
||
4352 | 2047 |
parentNode := node parent. |
4408 | 2048 |
nodeReceiver := node receiver. |
5103 | 2049 |
nodeReceiver notNil ifTrue:[ |
2050 |
classesOfReceiver := self classesOfNode:nodeReceiver. |
|
2051 |
]. |
|
6036 | 2052 |
"/ Verbose := true |
5112 | 2053 |
Verbose == true ifTrue:[ |
2054 |
Transcript show:node; show:' -> '; showCR:classesOfReceiver. |
|
5738 | 2055 |
( node isVariable and:[node name = 'self']) ifTrue:[self breakPoint:#cg]. |
5112 | 2056 |
]. |
2057 |
||
4366 | 2058 |
"/ if there is already space before the cursor, and the parent node is not a message, |
4620 | 2059 |
"/ do not attempt to complete the current message. |
4366 | 2060 |
"/ If it is a message, we will look for parent-message completion also below (best2 stuff) |
5385 | 2061 |
(codeView characterBeforeCursor ? Character space) isSeparator ifTrue:[ |
5038 | 2062 |
selector isKeyword ifFalse:[ |
5041 | 2063 |
self codeCompletionForMessageTo:node into:actionBlock. |
5040 | 2064 |
^ self |
4610 | 2065 |
]. |
4352 | 2066 |
]. |
5040 | 2067 |
|
4620 | 2068 |
"/ only do this if the node-message has no parents around |
2069 |
node parentheses isEmptyOrNil ifTrue:[ |
|
5112 | 2070 |
Verbose == true ifTrue:[ |
2071 |
Transcript show:'try for: '; showCR:nodeReceiver |
|
2072 |
]. |
|
2073 |
bestSelectors := self findBest:nodeReceiver for:selector |
|
2074 |
inClasses:classesOfReceiver |
|
4620 | 2075 |
] ifFalse:[ |
2076 |
bestSelectors := OrderedCollection new. |
|
2077 |
]. |
|
5040 | 2078 |
|
4548 | 2079 |
"/ if the receiver is a real variable, |
4386 | 2080 |
"/ we can look for other messages being sent to that variable in the current method. |
5042 | 2081 |
"/ Also, if there are assignments to it (like constants or '<class> new'), use that as a hint... |
4548 | 2082 |
(tree notNil |
4610 | 2083 |
and:[ nodeReceiver isVariable |
2084 |
and:[ nodeReceiver isSelf not |
|
2085 |
and:[ nodeReceiver isSuper not ]]]) |
|
4366 | 2086 |
ifTrue:[ |
5042 | 2087 |
|receiverName possibleClasses possibleClassesFromOtherSends| |
5040 | 2088 |
|
2089 |
receiverName := nodeReceiver name. |
|
5486 | 2090 |
|
2091 |
classesOfReceiver notEmptyOrNil ifTrue:[ |
|
2092 |
possibleClasses := classesOfReceiver. |
|
2093 |
] ifFalse:[ |
|
2094 |
classesFromAssignmentsToReceiver := self classesFromAssignmentTo:receiverName in:tree. |
|
2095 |
||
2096 |
possibleClasses := classesFromAssignmentsToReceiver. |
|
2097 |
possibleClasses isEmpty ifTrue:[ |
|
2098 |
"/ messages sent |
|
2099 |
otherMessagesToReceiver := self messagesSentTo:receiverName in:tree. |
|
2100 |
otherMessagesToReceiver remove:selector ifAbsent:[]. |
|
2101 |
||
2102 |
otherMessagesToReceiver notEmpty ifTrue:[ |
|
2103 |
"/ classes which respond to all |
|
2104 |
possibleClassesFromOtherSends := |
|
2105 |
Smalltalk |
|
2106 |
allClassesForWhich:[:cls | |
|
2107 |
cls isLoaded |
|
2108 |
and:[ otherMessagesToReceiver |
|
2109 |
conform:[:eachSelectorSent | cls canUnderstand:eachSelectorSent]] |
|
2110 |
]. |
|
2111 |
possibleClasses := possibleClasses , possibleClassesFromOtherSends. |
|
2112 |
]. |
|
2113 |
]. |
|
5040 | 2114 |
|
5486 | 2115 |
"/ if the receiver is a classVar/classInstVar, |
2116 |
"/ include the class of its current value and UndefinedObject. |
|
2117 |
"/ This helps to complete class methods and (lazy) initializer code. |
|
2118 |
(classOrNil notNil) ifTrue:[ |
|
2119 |
|tryValue currentValue| |
|
2120 |
||
2121 |
tryValue := false. |
|
2122 |
(classOrNil theNonMetaclass allClassVarNames includes: receiverName) ifTrue:[ |
|
2123 |
tryValue := true. |
|
2124 |
currentValue := classOrNil theNonMetaclass classVarAt:receiverName. |
|
2125 |
] ifFalse:[ |
|
2126 |
(classOrNil isMeta and:[ classOrNil allInstVarNames includes: receiverName ]) ifTrue:[ |
|
2127 |
tryValue := true. |
|
2128 |
currentValue := classOrNil theNonMetaclass instVarNamed:receiverName. |
|
2129 |
]. |
|
2130 |
]. |
|
2131 |
tryValue ifTrue:[ |
|
2132 |
possibleClasses := { currentValue class } , possibleClasses. |
|
2133 |
]. |
|
5040 | 2134 |
]. |
4610 | 2135 |
]. |
5486 | 2136 |
|
4610 | 2137 |
(possibleClasses notEmpty and:[possibleClasses size < 15]) ifTrue:[ |
4933 | 2138 |
bestSelectors := |
2139 |
(possibleClasses |
|
2140 |
collectAll:[:eachClass | |
|
2141 |
Parser findBest:30 selectorsFor:selector in:eachClass forCompletion:true. |
|
2142 |
] as:Set) asOrderedCollection. |
|
5040 | 2143 |
|
4619 | 2144 |
"/ if any of those is a prefix-keyword of the selector, |
2145 |
"/ do not offer it (i.e. ifTrue:ifFalse: is already present, don't offer ifTrue:ifFalse: again. |
|
5613 | 2146 |
bestSelectors := bestSelectors reject: [:sel | |
2147 |
(selector startsWith: sel) or: [selector endsWith: sel] |
|
2148 |
]. |
|
4610 | 2149 |
]. |
4619 | 2150 |
]. |
5040 | 2151 |
|
4352 | 2152 |
"/ if we are behind a keyword messages colon, |
2153 |
"/ only look for matching prefix selectors; |
|
2154 |
"/ also, a good completion is to insert an argument; |
|
2155 |
"/ the name of the variable from the implementation, as comment, and selected might be a good one!! |
|
5038 | 2156 |
"/ Array new:1 |
4352 | 2157 |
selector isKeyword ifTrue:[ |
5385 | 2158 |
(node arguments size = selector argumentCount) ifTrue:[ |
5040 | 2159 |
offerParenthisationAroundNode := node. |
5042 | 2160 |
"/ Transcript show:'2:'; showCR:node. |
5038 | 2161 |
]. |
5040 | 2162 |
|
4610 | 2163 |
codeView characterBeforeCursor == $: ifTrue:[ |
5613 | 2164 |
(bestSelectors select:[:sel | sel asLowercase startsWith:lcSelectorWithoutColon]) isEmpty ifTrue:[ |
4610 | 2165 |
"/ nothing better around |
5080 | 2166 |
|argIndex argNames argNameStrings impls| |
5040 | 2167 |
|
4610 | 2168 |
argIndex := node selectorParts size. |
2169 |
argNames := Set new. |
|
5080 | 2170 |
argNameStrings := OrderedCollection new. |
4610 | 2171 |
impls := Smalltalk allImplementorsOf:selector. |
2172 |
impls size < 10 ifTrue:[ |
|
2173 |
impls do:[:eachImplClass | |
|
2174 |
|mthd argName| |
|
5040 | 2175 |
|
4610 | 2176 |
mthd := (eachImplClass compiledMethodAt:selector). |
4764 | 2177 |
argName := (mthd methodArgNames ? #()) at:argIndex ifAbsent:nil. |
2178 |
argName notNil ifTrue:[ |
|
5080 | 2179 |
(argNames includes:argName) ifFalse:[ |
2180 |
argNames add:argName. |
|
2181 |
argNameStrings add:(argName allItalic,' hint only: argName in (' ,mthd mclass name allBold,' ',mthd methodDefinitionTemplate). |
|
2182 |
]. |
|
4764 | 2183 |
]. |
4610 | 2184 |
]. |
5080 | 2185 |
argNameStrings notEmptyOrNil ifTrue: [ |
2186 |
argNameStrings := argNameStrings asOrderedCollection sort. |
|
2187 |
actionBlock value:argNameStrings value:[:selIndex | ] value: 'argument name hint'. |
|
4610 | 2188 |
^ self. |
2189 |
] |
|
2190 |
] |
|
5038 | 2191 |
]. |
4610 | 2192 |
]. |
4369 | 2193 |
] ifFalse:[ |
4610 | 2194 |
"/ when completing a non-keyword AND the parent is a keyword message, |
5080 | 2195 |
"/ only consider longer keyword messages or unary messages. |
2196 |
"/ unless the node is parenthesized |
|
2197 |
node hasParentheses ifFalse:[ |
|
2198 |
(parentNode notNil and:[ parentNode isKeywordMessage ]) ifTrue:[ |
|
5613 | 2199 |
bestSelectors := bestSelectors select:[:sel | |
2200 |
sel isUnarySelector |
|
2201 |
or:[ sel startsWith:parentNode selector] |
|
2202 |
] |
|
5080 | 2203 |
] |
4610 | 2204 |
] |
4352 | 2205 |
]. |
5040 | 2206 |
|
2207 |
"/ bestSelectors := bestSelectors asOrderedCollection. |
|
2208 |
"/ bestSelectors sort:[:a :b | a size < b size]. |
|
2209 |
||
5038 | 2210 |
(selector isUnarySelector and:[ parentNode notNil and:[ parentNode isMessage ]]) ifTrue:[ |
5058 | 2211 |
(parentSelector := parentNode selector) isKeyword ifTrue:[ |
5471 | 2212 |
"/ if it's a unary message AND the parent is a keyword node, look for parent completion too. |
4610 | 2213 |
"/ i.e. look if there is a longer keyword possible |
5058 | 2214 |
newParentSelector := parentSelector,selector. |
5112 | 2215 |
bestSelectors2 := self |
2216 |
findBest:(parentNode receiver) for:newParentSelector |
|
2217 |
inClasses:(self classesOfNode:parentNode receiver). |
|
5058 | 2218 |
bestSelectors2 := bestSelectors2 select:[:sel | sel isKeyword and:[ sel startsWith:parentSelector]]. |
4610 | 2219 |
bestSelectors2 := bestSelectors2 asOrderedCollection sort:[:a :b | a size < b size]. |
2220 |
bestSelectors := bestSelectors reject:[:sel | bestSelectors2 includes:sel]. |
|
5040 | 2221 |
|
5038 | 2222 |
"/ if the parent has a valid selector, offer parenthization |
5058 | 2223 |
(Smalltalk someImplementorOf:parentSelector) notNil ifTrue:[ |
5038 | 2224 |
offerParenthisationAroundNode := parentNode. |
5040 | 2225 |
"/ Transcript show:'2:'; showCR:parentNode. |
5038 | 2226 |
]. |
5528 | 2227 |
parentSelector := newParentSelector. |
4610 | 2228 |
] ifFalse:[ |
2229 |
|kwSels| |
|
5040 | 2230 |
|
5103 | 2231 |
"/ if its a unary message AND the parent is a unary or binary node, |
2232 |
"/ try again, sending the partial message |
|
4610 | 2233 |
"/ as a keyword to the parent node. |
5103 | 2234 |
"/ this is the case when after "foo binOp bar if", or "foo unOp bar if" |
2235 |
"/ which should include ifTrue: in the suggestion result. |
|
2236 |
||
2237 |
"/ suggestion will transform from (the incorrectly parsed) |
|
4610 | 2238 |
"/ foo == (shift if) |
5103 | 2239 |
"/ |
4610 | 2240 |
"/ nonKWsel-msg(parent) |
2241 |
"/ / \ |
|
2242 |
"/ / \ |
|
2243 |
"/ rcvr sel-unary(node) |
|
2244 |
"/ / |
|
2245 |
"/ / |
|
2246 |
"/ arg |
|
2247 |
"/ |
|
2248 |
"/ into: |
|
2249 |
"/ (foo == shift) if |
|
2250 |
"/ |
|
2251 |
"/ nonKWsel-msg(parent) |
|
2252 |
"/ / \ |
|
2253 |
"/ / \ |
|
2254 |
"/ rcvr sel-unary(node) |
|
2255 |
"/ / |
|
2256 |
"/ / |
|
2257 |
"/ arg |
|
5040 | 2258 |
|
5103 | 2259 |
"/ but only do this, if typing to the end of the parent message |
2260 |
"/ (i.e. after (foo == shift) <- |
|
2261 |
"/ or after foo bar baz <- |
|
2262 |
"/ not if typing into an existing message |
|
2263 |
"/ (i.e. into foo == shift <- more |
|
2264 |
"/ or into foo bar <- baz |
|
2265 |
codeView characterPositionOfCursor >= parentNode stop ifTrue:[ |
|
5112 | 2266 |
kwSels := self |
2267 |
findBest:parentNode for:selector |
|
2268 |
inClasses:(self classesOfNode:parentNode). |
|
5103 | 2269 |
kwSels := kwSels select:[:sel | sel isKeyword]. |
2270 |
||
2271 |
kwSels := kwSels asOrderedCollection sort:[:a :b | a size < b size]. |
|
5040 | 2272 |
|
5103 | 2273 |
bestSelectors := bestSelectors reject:[:sel | kwSels includes:sel]. |
5040 | 2274 |
|
5103 | 2275 |
"/ these need to go to bestSelectors (see editAction) |
2276 |
parentNodeClassIfKnown := self classOfNode:parentNode. |
|
2277 |
(parentNodeClassIfKnown notNil and:[ parentNodeClassIfKnown includesBehavior: Boolean ]) ifTrue:[ |
|
2278 |
"/ this is so common, that it deserves a special case: |
|
5453 | 2279 |
"/ if we complete an 'if' after some boolean message e.g '(a == b) if' |
5103 | 2280 |
"/ throw out the very unlikely ifNil, ifEmpty etc. messages (which are inherited by Object, but absolutely unrealistic) |
2281 |
bestSelectors := self |
|
2282 |
withoutSelectorsUnlikelyFor:parentNodeClassIfKnown |
|
2283 |
from:bestSelectors |
|
2284 |
forPartial:selector. |
|
2285 |
kwSels := self |
|
2286 |
withoutSelectorsUnlikelyFor:parentNodeClassIfKnown |
|
2287 |
from:kwSels |
|
2288 |
forPartial:selector. |
|
2289 |
||
2290 |
"/ put keyword selectors in front, because they are very likely |
|
2291 |
bestSelectors := kwSels , bestSelectors. |
|
2292 |
] ifFalse:[ |
|
2293 |
"/ put them at the end |
|
2294 |
bestSelectors := bestSelectors , kwSels. |
|
2295 |
]. |
|
2296 |
] |
|
4610 | 2297 |
] |
4358 | 2298 |
]. |
5040 | 2299 |
|
5038 | 2300 |
(selector isUnarySelector and:[ node isMessage ]) ifTrue:[ |
4610 | 2301 |
receiverNodeClassIfKnown := self classOfNode:nodeReceiver. |
5453 | 2302 |
|
2303 |
(receiverNodeClassIfKnown notNil) ifTrue:[ |
|
2304 |
true "(receiverNodeClassIfKnown includesBehavior: Boolean)" ifTrue:[ |
|
2305 |
bestSelectors := bestSelectors select:[:sel | receiverNodeClassIfKnown canUnderstand:sel]. |
|
2306 |
bestSelectors := self |
|
2307 |
withoutSelectorsUnlikelyFor:receiverNodeClassIfKnown |
|
2308 |
from:bestSelectors |
|
2309 |
forPartial:selector. |
|
2310 |
]. |
|
4610 | 2311 |
]. |
4375 | 2312 |
]. |
5043 | 2313 |
|
5044 | 2314 |
Verbose == true ifTrue:[ |
2315 |
Transcript show:'parentNode: '; showCR:parentNode. |
|
2316 |
Transcript show:'parentNode: '; showCR:parentNode class. |
|
2317 |
Transcript show:'sel: '; showCR:selector. |
|
2318 |
]. |
|
5043 | 2319 |
|
2320 |
canParenthesize := false. |
|
2321 |
parentNode notNil ifTrue:[ |
|
2322 |
parentNode isMessage ifTrue:[ |
|
2323 |
(((parentNode selector isUnarySelector not) and:[selector isUnarySelector]) |
|
2324 |
or:[ ((parentNode selector isKeyword) and:[selector isBinarySelector]) ]) ifTrue:[ |
|
2325 |
canParenthesize := true. |
|
2326 |
] |
|
2327 |
] ifFalse:[ |
|
2328 |
offerParenthisationAroundNode isNil ifTrue:[ |
|
2329 |
selector isKeyword ifTrue:[ |
|
2330 |
offerParenthisationAroundNode := node. |
|
2331 |
]. |
|
2332 |
]. |
|
2333 |
]. |
|
2334 |
]. |
|
2335 |
canParenthesize ifTrue:[ |
|
5038 | 2336 |
"/ completing an already existing keyword or binary message with something starting with |
4610 | 2337 |
"/ if, and, or or while. |
5203 | 2338 |
"/ Here, offer a special completion which inserts parentheses / brackets around the already |
4610 | 2339 |
"/ existing message. Do this only, if the existing message makes sense. |
5038 | 2340 |
"/ expr wh |
2341 |
"/ -> |
|
2342 |
"/ [expr] whileXX:[] |
|
5040 | 2343 |
true "(( |
5038 | 2344 |
#( 'ifTrue' 'ifFalse' 'and' 'or' 'do' 'keysAndValuesDo' 'whileTrue' 'whileFalse' 'ensure' 'on') |
5040 | 2345 |
) contains:[:part | part startsWith:selector])" ifTrue:[ |
5038 | 2346 |
(Smalltalk someImplementorOf:parentNode selector) notNil ifTrue:[ |
4610 | 2347 |
|selsP selsB| |
5040 | 2348 |
|
5038 | 2349 |
selsP := #( 'ifTrue:' 'ifFalse:' 'and' 'or' 'do' 'keysAndValuesDo' ) |
4610 | 2350 |
select:[:sel | sel startsWith:selector] |
5038 | 2351 |
thenCollect:[:sel | '(',parentNode selector,') ',sel]. |
5040 | 2352 |
( #( 'whileTrue:' 'whileFalse:' 'ensure:' 'on:do:' ) contains:[:sel | sel startsWith:selector]) |
2353 |
ifFalse:[ |
|
2354 |
selsP := selsP copyWith:'(',parentNode selector,') ',selector |
|
2355 |
]. |
|
5038 | 2356 |
selsB := #( 'whileTrue:' 'whileFalse:' 'ensure:' 'on:do:' ) |
4610 | 2357 |
select:[:sel | sel startsWith:selector] |
5038 | 2358 |
thenCollect:[:sel | '[',parentNode selector,'] ',sel]. |
2359 |
parentNodeToParenthesize := parentNode. |
|
2360 |
bestWithParenthesis := selsP , selsB. |
|
4610 | 2361 |
]. |
2362 |
]. |
|
4421 | 2363 |
] ifFalse:[ |
5038 | 2364 |
"/ also offer adding brackets for a while expression |
2365 |
"/ expr wh |
|
2366 |
"/ -> |
|
2367 |
"/ [expr] whileXX:[] |
|
2368 |
(( |
|
2369 |
#( 'whileTrue' 'whileFalse' 'ensure' 'on') |
|
2370 |
) contains:[:part | part startsWith:selector]) ifTrue:[ |
|
2371 |
(node receiver isBlock) ifFalse:[ |
|
2372 |
|sels| |
|
5040 | 2373 |
|
5038 | 2374 |
(node receiver isMessage not |
2375 |
or:[ (Smalltalk someImplementorOf:node receiver selector) notNil ]) ifTrue:[ |
|
2376 |
sels := #( 'whileTrue:' 'whileFalse:' 'ensure:' 'on:do:' ) |
|
2377 |
select:[:sel | sel startsWith:selector] |
|
2378 |
thenCollect:[:sel | '[...] ',sel]. |
|
2379 |
parentNodeToParenthesize := node receiver. |
|
2380 |
bestWithParenthesis := sels. |
|
2381 |
]. |
|
2382 |
]. |
|
4610 | 2383 |
]. |
4416 | 2384 |
]. |
5040 | 2385 |
|
5419 | 2386 |
allBest := (bestSelectors ? #()) , (bestSelectors2 ? #()) asOrderedCollection. |
5613 | 2387 |
self sortSelectors:allBest forSelector:selectorWithoutColon lcSelector:lcSelectorWithoutColon. |
4936 | 2388 |
|
4388 | 2389 |
"/ sort: prefixes first. |
5058 | 2390 |
parentSelector notNil ifTrue:[ |
5044 | 2391 |
allBest := self |
2392 |
splitSelectorList:allBest |
|
2393 |
by:[:sel | |
|
5613 | 2394 |
(sel asLowercase startsWith:lcSelectorWithoutColon) |
5058 | 2395 |
or:[sel startsWith:parentSelector]]. |
4388 | 2396 |
]. |
5040 | 2397 |
|
4358 | 2398 |
"/ if receiver is super, always include the method's own selector |
4408 | 2399 |
nodeReceiver isSuper ifTrue:[ |
4610 | 2400 |
(tree isMethod) ifTrue:[ |
2401 |
|mSel| |
|
5040 | 2402 |
|
4610 | 2403 |
mSel := tree selector. |
2404 |
mSel notNil ifTrue:[ |
|
5613 | 2405 |
(mSel startsWith:selectorWithoutColon) ifTrue:[ |
4610 | 2406 |
"/ already the word before the cursor? |
2407 |
(mSel ~= selector) ifTrue:[ |
|
5419 | 2408 |
allBest removeAndAddFirst:mSel. |
4610 | 2409 |
] |
2410 |
] |
|
2411 |
] |
|
2412 |
] |
|
4238 | 2413 |
]. |
5040 | 2414 |
|
5071 | 2415 |
"/ another convenient hack; |
2416 |
"/ if we have just typed in foo == |
|
2417 |
"/ and the value of foo is a known literal, |
|
2418 |
"/ offer inserting this literal. This is great in a debugger... |
|
2419 |
( #(#'==' #'=' #'~=' #'~~') includes:selector ) ifTrue:[ |
|
2420 |
|val| |
|
2421 |
||
2422 |
(val := self valueOfNode:nodeReceiver) notNil ifTrue:[ |
|
5285 | 2423 |
"/ this can raise an error, if val does not like to generate a storeString |
2424 |
"/ (it is recursive, or an X11GraphicsContext, for example) |
|
2425 |
Error handle:[:ex | |
|
2426 |
] do:[ |
|
2427 |
valueToInsert := val storeString. |
|
2428 |
offerValueInsertion := true. |
|
2429 |
nodeReceiver isVariable ifTrue:[ |
|
2430 |
valueInfo := ' (current value of %1)' bindWith:nodeReceiver name. |
|
2431 |
] ifFalse:[ |
|
2432 |
valueInfo := ' (current value of expression)'. |
|
2433 |
]. |
|
2434 |
valueInfo := valueInfo withColor:(Color grey). |
|
5071 | 2435 |
]. |
2436 |
]. |
|
2437 |
]. |
|
2438 |
||
2439 |
(allBest isEmptyOrNil |
|
2440 |
and:[bestWithParenthesis isEmptyOrNil |
|
2441 |
and:[offerParenthisationAroundNode isNil |
|
2442 |
and:[offerValueInsertion not]]] |
|
2443 |
) ifTrue:[ |
|
5038 | 2444 |
^ self |
2445 |
]. |
|
5040 | 2446 |
|
5042 | 2447 |
"/ "/ see what is aready sent to this variable inside the code |
2448 |
"/ "/ and also what is assigned to it. |
|
2449 |
"/ nodeReceiver notNil ifTrue:[ |
|
2450 |
"/ nodeReceiver isVariable ifTrue:[ |
|
2451 |
"/ rememberedNodes notNil ifTrue:[ |
|
2452 |
"/ selectorsSentInCode := |
|
2453 |
"/ (rememberedNodes |
|
2454 |
"/ select:[:node | |
|
2455 |
"/ node isMessage |
|
2456 |
"/ and:[node receiver isVariable |
|
2457 |
"/ and:[node receiver name = nodeReceiver name]]] |
|
2458 |
"/ thenCollect:[:node | |
|
2459 |
"/ node selector] |
|
2460 |
"/ ) asSet. |
|
2461 |
"/ ] ifFalse:[ |
|
2462 |
"/ selectorsSentInCode := Set new. |
|
2463 |
"/ tree allMessageNodesDo:[:msg | |
|
2464 |
"/ (msg receiver isVariable |
|
2465 |
"/ and:[msg receiver name = nodeReceiver name] |
|
2466 |
"/ ) ifTrue:[ |
|
2467 |
"/ selectorsSentInCode add:msg selector |
|
2468 |
"/ ]. |
|
2469 |
"/ ]. |
|
2470 |
"/ selectorsSentInCode remove:selector ifAbsent:[]. |
|
2471 |
"/ ]. |
|
2472 |
"/ ]. |
|
2473 |
"/ ]. |
|
2474 |
||
4936 | 2475 |
nodeReceiver notNil ifTrue:[ |
2476 |
|classOrNil| |
|
5042 | 2477 |
|
2478 |
classOrNil := self classOfNode:nodeReceiver. |
|
2479 |
classOrNil isNil ifTrue:[ |
|
2480 |
classesFromAssignmentsToReceiver size == 1 ifTrue:[ |
|
2481 |
classOrNil := classesFromAssignmentsToReceiver anElement |
|
2482 |
]. |
|
2483 |
]. |
|
2484 |
classOrNil notNil ifTrue:[ |
|
2485 |
selectorsImplementedInClass := classOrNil selectors. |
|
2486 |
"/ selectorsImplementedInClass := Set new. |
|
2487 |
"/ classOrNil withAllSuperclassesDo:[:cls | |
|
2488 |
"/ cls theNonMetaclass ~~ Object ifTrue:[ |
|
2489 |
"/ selectorsImplementedInClass addAll:cls selectors. |
|
2490 |
"/ ] |
|
2491 |
"/ ] |
|
4936 | 2492 |
] |
5606 | 2493 |
]. |
2494 |
||
4936 | 2495 |
selectorsImplementedInClass notNil ifTrue:[ |
5528 | 2496 |
(parentSelector notNil and:[parentSelector includes:$:]) ifTrue:[ |
2497 |
selectorsImplementedInClass := selectorsImplementedInClass reject:[:sel | sel isKeywordSelector]. |
|
2498 |
]. |
|
2499 |
||
5040 | 2500 |
"/ the one's implemented in the class itself are moved to the top of the list. |
5044 | 2501 |
allBest := self |
2502 |
splitSelectorList:allBest |
|
2503 |
by:[:sel | selectorsImplementedInClass includes:sel]. |
|
4936 | 2504 |
]. |
5042 | 2505 |
otherMessagesToReceiver "selectorsSentInCode" notNil ifTrue:[ |
4610 | 2506 |
"/ the one's already sent in the code are moved to the top of the list. |
5040 | 2507 |
"/ trouble is: parser bails out on error, so most of the time, we only see |
2508 |
"/ selectors sent previously. sigh. |
|
5044 | 2509 |
allBest := self |
2510 |
splitSelectorList:allBest |
|
2511 |
by:[:sel | otherMessagesToReceiver "selectorsSentInCode" includes:sel]. |
|
4238 | 2512 |
]. |
5040 | 2513 |
|
5528 | 2514 |
(parentSelector notNil and:[parentSelector includes:$:]) ifTrue:[ |
2515 |
allBest := self |
|
2516 |
splitSelectorList:allBest |
|
2517 |
by:[:sel | sel startsWith:parentSelector]. |
|
2518 |
]. |
|
2519 |
||
5040 | 2520 |
"/ this makes it very slow |
4548 | 2521 |
"/false ifTrue:[ |
2522 |
"/ srchClass notNil ifTrue:[ |
|
2523 |
"/ implClass := srchClass whichClassIncludesSelector:best. |
|
2524 |
"/ ] ifFalse:[ |
|
2525 |
"/ implClass := Smalltalk allClasses select:[:cls | (cls includesSelector:best) or:[cls class includesSelector:best]]. |
|
2526 |
"/ implClass size == 1 ifTrue:[ |
|
2527 |
"/ implClass := implClass first. |
|
2528 |
"/ ] ifFalse:[ |
|
2529 |
"/ implClass := nil |
|
2530 |
"/ ] |
|
2531 |
"/ ]. |
|
2532 |
"/ |
|
2533 |
"/ info := best storeString. |
|
2534 |
"/ implClass notNil ifTrue:[ |
|
6082 | 2535 |
"/ info := implClass name , ' » ' , info. |
4548 | 2536 |
"/ ]. |
2537 |
"/ self information:info. |
|
2538 |
"/]. |
|
5040 | 2539 |
|
5528 | 2540 |
(parentSelector notNil and:[parentSelector includes:$:]) ifTrue:[ |
2541 |
"/ the one's which are a prefix are moved towards the top of the list |
|
2542 |
allBest := self splitSelectorList:allBest |
|
5613 | 2543 |
by:[:sel | sel notNil and:[sel asLowercase startsWith:lcSelectorWithoutColon]]. |
5528 | 2544 |
]. |
2545 |
||
5040 | 2546 |
"/ heuristic hack: |
2547 |
"/ 'i' and 'w' generate lists in which ifXXX / whileXXX are not at the top of the list. |
|
2548 |
"/ we know, that those are most often wanted!! |
|
2549 |
selector size <= 2 ifTrue:[ |
|
5044 | 2550 |
allBest := self |
2551 |
splitSelectorList:allBest |
|
2552 |
by:[:sel | |
|
5040 | 2553 |
#(ifTrue: ifFalse: isNil notNil whileTrue whileFalse) includes:sel |
2554 |
]. |
|
2555 |
]. |
|
2556 |
||
5606 | 2557 |
"/ sort again: prefixes must always come before |
5613 | 2558 |
allBest sortBySelector:#size. |
2559 |
self sortSelectors:allBest forSelector:selectorWithoutColon lcSelector:lcSelectorWithoutColon. |
|
2560 |
"/ self sortUsefulSelectorsIn:allBest. "/cosmetics |
|
5040 | 2561 |
|
5528 | 2562 |
(parentSelector notNil and:[parentSelector includes:$:]) ifTrue:[ |
2563 |
allBest := (allBest |
|
2564 |
select:[:sel | sel startsWith:parentSelector] |
|
2565 |
thenCollect:[:sel | sel copyFrom:(parentSelector lastIndexOf:$:)+1]) |
|
2566 |
, |
|
2567 |
(allBest |
|
2568 |
reject:[:sel | sel startsWith:parentSelector]). |
|
2569 |
]. |
|
2570 |
||
5040 | 2571 |
"/ parenthesizers always at the end. |
2572 |
bestWithParenthesis notEmptyOrNil ifTrue:[ |
|
2573 |
allBest := allBest , bestWithParenthesis. |
|
2574 |
]. |
|
2575 |
||
5038 | 2576 |
"/ self at:1 put:#foo |
2577 |
"/ Array new:10 |
|
2578 |
offerParenthisationAroundNode notNil ifTrue:[ |
|
2579 |
allBest := allBest copyWith:( '(',selector,')' ). |
|
2580 |
parenthesisAroundIndex := allBest size. |
|
2581 |
]. |
|
5071 | 2582 |
offerValueInsertion ifTrue:[ |
2583 |
allBest := allBest copyWith:( '... ',(valueToInsert contractTo:30),valueInfo). |
|
2584 |
valueToInsertIndex := allBest size. |
|
2585 |
]. |
|
5040 | 2586 |
|
4548 | 2587 |
editAction := |
4610 | 2588 |
[:index | |
5040 | 2589 |
|crsrPos chosen parenthesisToInsert action| |
2590 |
||
5038 | 2591 |
action := nil. |
4610 | 2592 |
crsrPos := codeView characterPositionOfCursor. |
2593 |
chosen := allBest at:index. |
|
5040 | 2594 |
|
4610 | 2595 |
chosen ~= selector ifTrue:[ |
5038 | 2596 |
(bestWithParenthesis notNil and:[bestWithParenthesis includes:chosen]) ifTrue:[ |
2597 |
"/ for input like: |
|
2598 |
"/ chosen at: 10 if |
|
2599 |
"/ put parenthesis around, and add ifTrue/ifFalse |
|
2600 |
"/ i.e.: (chosen at:10) ifTrue:[] |
|
5040 | 2601 |
|
5038 | 2602 |
"/ for input like: |
2603 |
"/ a > 10 wh |
|
2604 |
"/ put brackets around and add whileTrue/whileFalse |
|
2605 |
"/ i.e.: [a > 10] whileTrue:[] |
|
5040 | 2606 |
parenthesisToInsert := chosen first == $( ifTrue:'()' ifFalse:'[]'. |
2607 |
chosen := (chosen copyFrom:(chosen lastIndexOf:parenthesisToInsert second)+1) withoutSeparators. |
|
5038 | 2608 |
] ifFalse:[ |
2609 |
(offerParenthisationAroundNode notNil and:[index = parenthesisAroundIndex]) ifTrue:[ |
|
2610 |
"/ for input like: |
|
2611 |
"/ Array new:10 |
|
2612 |
"/ put parenthesis around. |
|
2613 |
"/ i.e.: (Array new:10) |
|
2614 |
action := |
|
2615 |
[ |
|
2616 |
codeView insertString:'(' atCharacterPosition:offerParenthisationAroundNode start. |
|
2617 |
codeView insertString:')' atCharacterPosition:offerParenthisationAroundNode stop+2. |
|
2618 |
codeView cursorToCharacterPosition:(offerParenthisationAroundNode stop+2); cursorRight. |
|
2619 |
]. |
|
5071 | 2620 |
] ifFalse:[ |
2621 |
(offerValueInsertion and:[index = valueToInsertIndex]) ifTrue:[ |
|
2622 |
"/ for input like: |
|
2623 |
"/ foo == |
|
2624 |
"/ insert a value |
|
2625 |
"/ i.e.: foo == #someSymbol |
|
2626 |
action := |
|
2627 |
[ |
|
2628 |
codeView characterBeforeCursor isSeparator ifFalse:[ |
|
2629 |
codeView insertStringAtCursor:' '. |
|
2630 |
]. |
|
2631 |
codeView insertStringAtCursor:valueToInsert. |
|
2632 |
"/ codeView cursorRight. |
|
2633 |
]. |
|
2634 |
] |
|
2635 |
]. |
|
4610 | 2636 |
]. |
5040 | 2637 |
|
5038 | 2638 |
action isNil ifTrue:[ |
2639 |
numArgs := chosen numArgs. |
|
2640 |
(bestSelectors2 notEmptyOrNil and:[bestSelectors2 includes:chosen]) ifTrue:[ |
|
2641 |
selectorParts := parentNode selectorParts , node selectorParts. |
|
2642 |
] ifFalse:[ |
|
2643 |
selectorParts := node selectorParts. |
|
2644 |
]. |
|
2645 |
nSelParts := selectorParts size. |
|
5040 | 2646 |
|
5038 | 2647 |
newParts := chosen asCollectionOfSubstringsSeparatedBy:$:. |
6013 | 2648 |
newParts := newParts select:[:part | part size ~~ 0]. |
5040 | 2649 |
|
5038 | 2650 |
action := |
2651 |
[ |
|
2652 |
|positionOfFirstArg newCursorPosition stop checkForArgumentTemplates |
|
2653 |
newPart oldPartialToken start| |
|
5040 | 2654 |
|
5038 | 2655 |
checkForArgumentTemplates := (selector isUnarySelector and:[chosen isKeywordSelector]). |
2656 |
numArgs > nSelParts ifTrue:[ |
|
2657 |
"/ new selector has more arguments; append them |
|
2658 |
stop := selectorParts last stop. |
|
2659 |
codeView deleteFromCharacterPosition:stop+1 to:crsrPos-1. |
|
5040 | 2660 |
|
5038 | 2661 |
"/ append the rest ... |
2662 |
(numArgs min:newParts size) downTo:(nSelParts+1) do:[:idx | |
|
2663 |
|newPart| |
|
5040 | 2664 |
|
5038 | 2665 |
newPart := newParts at:idx. |
2666 |
newPart := newPart , ':'. |
|
5040 | 2667 |
|
5038 | 2668 |
(codeView characterAtCharacterPosition:stop) == $: ifFalse:[ |
2669 |
newPart := ':' , newPart. |
|
2670 |
]. |
|
2671 |
newPart := (codeView characterAtCharacterPosition:stop) asString , newPart. |
|
5040 | 2672 |
|
5038 | 2673 |
codeView replaceFromCharacterPosition:stop to:stop with:newPart. |
2674 |
"/ remember the leftMost replacement's end as new cursor position |
|
2675 |
newCursorPosition := stop + newPart size |
|
2676 |
]. |
|
2677 |
checkForArgumentTemplates := true. |
|
2678 |
]. |
|
5040 | 2679 |
|
5038 | 2680 |
"/ replace existing parts |
2681 |
(nSelParts min:newParts size) downTo:1 do:[:idx | |
|
2682 |
|skipColon| |
|
5040 | 2683 |
|
5038 | 2684 |
skipColon := 0. |
2685 |
newPart := newParts at:idx. |
|
2686 |
oldPartialToken := selectorParts at:idx. |
|
2687 |
start := oldPartialToken start. |
|
2688 |
stop := oldPartialToken stop. |
|
5040 | 2689 |
|
5038 | 2690 |
(chosen endsWith:$:) ifTrue:[ |
2691 |
(codeView characterAtCharacterPosition:stop+1) == $: ifFalse:[ |
|
2692 |
newPart := newPart , ':'. |
|
2693 |
] ifTrue:[ |
|
2694 |
skipColon := 1. |
|
2695 |
] |
|
2696 |
] ifFalse:[ |
|
2697 |
(codeView characterAtCharacterPosition:stop) == $: ifTrue:[ |
|
2698 |
newPart := newPart , ':' |
|
2699 |
] ifFalse:[ |
|
2700 |
|nextChar| |
|
5040 | 2701 |
|
5038 | 2702 |
nextChar := codeView characterAtCharacterPosition:stop+1. |
2703 |
nextChar isSeparator ifFalse:[ |
|
2704 |
nextChar == $. ifFalse:[ |
|
2705 |
newPart := newPart , ' ' |
|
2706 |
]. |
|
2707 |
] |
|
2708 |
] |
|
2709 |
"/ codeView replaceFromCharacterPosition:start to:stop with:(newPart , ':'). |
|
2710 |
"/ ] ifFalse:[ |
|
2711 |
"/ codeView replaceFromCharacterPosition:start to:stop with:newPart. |
|
2712 |
]. |
|
5040 | 2713 |
|
5038 | 2714 |
oldPartialToken value ~= newPart ifTrue:[ |
2715 |
codeView replaceFromCharacterPosition:start to:stop with:newPart. |
|
5040 | 2716 |
|
5038 | 2717 |
oldLen := stop - start + 1. |
2718 |
newLen := newPart size. |
|
5040 | 2719 |
|
5038 | 2720 |
"/ codeView selectFromCharacterPosition:start+oldLen to:start+newLen-1. |
2721 |
"/ remember the leftMost replacement's end as new cursor position |
|
2722 |
newCursorPosition := start + newPart size + skipColon. "/ (newLen-oldLen) + 1. |
|
2723 |
"/ codeView cursorToCharacterPosition:newCursorPosition. |
|
2724 |
]. |
|
2725 |
]. |
|
2726 |
newCursorPosition notNil ifTrue:[ |
|
2727 |
codeView cursorToCharacterPosition:newCursorPosition-1. |
|
2728 |
codeView cursorRight. "/ avoid going to the next line !! |
|
2729 |
]. |
|
2730 |
codeView dontReplaceSelectionOnInput. |
|
5040 | 2731 |
|
5038 | 2732 |
checkForArgumentTemplates ifTrue:[ |
2733 |
"/ add opening brackets, etc. |
|
2734 |
self insertAdditonalStuffAfterSelector:chosen. |
|
2735 |
]. |
|
5040 | 2736 |
parenthesisToInsert notNil ifTrue:[ |
5038 | 2737 |
|sav pos| |
5040 | 2738 |
|
5038 | 2739 |
sav := codeView characterPositionOfCursor-1. |
2740 |
"/ check if already parenthized |
|
5040 | 2741 |
parentNodeToParenthesize hasParentheses ifTrue:[ |
2742 |
pos := parentNodeToParenthesize parentheses first first. |
|
5038 | 2743 |
codeView selectFromCharacterPosition:pos to:pos. |
5040 | 2744 |
codeView replaceSelectionBy:(parenthesisToInsert copyFirst:1) asString. |
2745 |
||
2746 |
pos := parentNodeToParenthesize parentheses first last. |
|
5038 | 2747 |
codeView selectFromCharacterPosition:pos to:pos. |
5040 | 2748 |
codeView replaceSelectionBy:(parenthesisToInsert copyLast:1) asString. |
5038 | 2749 |
codeView cursorToCharacterPosition:sav; cursorRight |
2750 |
] ifFalse:[ |
|
5040 | 2751 |
codeView insertString:(parenthesisToInsert copyLast:1) atCharacterPosition:node receiver stop+1. |
2752 |
codeView insertString:(parenthesisToInsert copyFirst:1) atCharacterPosition:parentNodeToParenthesize start. |
|
5038 | 2753 |
codeView cursorToCharacterPosition:sav+2; cursorRight |
2754 |
]. |
|
2755 |
]. |
|
2756 |
]. |
|
2757 |
]. |
|
5040 | 2758 |
|
4610 | 2759 |
codeView |
5038 | 2760 |
undoableDo:action |
2761 |
info:'Completion'. |
|
4610 | 2762 |
]. |
2763 |
]. |
|
4352 | 2764 |
actionBlock value:allBest value:editAction value:nil. |
4238 | 2765 |
|
2766 |
"Created: / 10-11-2006 / 13:18:27 / cg" |
|
2767 |
"Modified: / 16-02-2010 / 10:33:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
5613 | 2768 |
"Modified: / 30-09-2017 / 14:12:47 / cg" |
6013 | 2769 |
"Modified: / 01-03-2019 / 14:52:35 / Claus Gittinger" |
6036 | 2770 |
"Modified (comment): / 18-03-2019 / 11:22:48 / Claus Gittinger" |
5040 | 2771 |
! |
2772 |
||
2773 |
codeCompletionForMessageTo:node into:actionBlock |
|
5106 | 2774 |
"find good suggestions for a message send to node, with no input yet. |
2775 |
I.e. right after a receiver (w.o. any input yet)" |
|
5040 | 2776 |
|
5080 | 2777 |
|knownClass suggestions selectorsImplementedInClass mostUseful editActions pos| |
5040 | 2778 |
|
5606 | 2779 |
(knownClass := self classOfNode:node) isEmptyOrNil ifTrue:[ |
5621 | 2780 |
"/ self breakPoint:#cg. |
5113 | 2781 |
"/ self classOfNode:node. |
5106 | 2782 |
^ self |
2783 |
]. |
|
2784 |
||
5109 | 2785 |
Verbose == true ifTrue:[ |
2786 |
Transcript show:node; show:' -> '; showCR:knownClass. |
|
2787 |
]. |
|
5040 | 2788 |
selectorsImplementedInClass := Set new. |
2789 |
||
2790 |
knownClass withAllSuperclassesDo:[:cls | |
|
2791 |
cls ~~ Object ifTrue:[ |
|
2792 |
selectorsImplementedInClass addAll:cls selectors. |
|
2793 |
] |
|
2794 |
]. |
|
2795 |
||
2796 |
knownClass isMeta ifTrue:[ |
|
2797 |
selectorsImplementedInClass := |
|
2798 |
selectorsImplementedInClass reject:[:sel | |
|
6022 | 2799 |
|mthd cat| |
5040 | 2800 |
|
2801 |
mthd := knownClass lookupMethodFor:sel. |
|
6022 | 2802 |
mthd notNil |
2803 |
and:[(cat := mthd category) notNil |
|
2804 |
and:[(cat startsWith: 'documentation')]] |
|
5040 | 2805 |
]. |
2806 |
||
2807 |
knownClass theNonMetaclass isAbstract ifTrue:[ |
|
2808 |
mostUseful := selectorsImplementedInClass select:[:sel | |
|
2809 |
knownClass implements:sel |
|
2810 |
] |
|
2811 |
] ifFalse:[ |
|
2812 |
mostUseful := selectorsImplementedInClass select:[:sel | |
|
6022 | 2813 |
|mthd cat| |
5040 | 2814 |
|
2815 |
mthd := knownClass lookupMethodFor:sel. |
|
6022 | 2816 |
mthd notNil |
2817 |
and:[(cat := mthd category) notNil |
|
2818 |
and:[cat startsWith: 'instance']] |
|
5040 | 2819 |
]. |
2820 |
] |
|
2821 |
] ifFalse:[ |
|
2822 |
mostUseful := |
|
2823 |
#( |
|
2824 |
"/ blocks |
|
2825 |
ifTrue: ifFalse: whileTrue: whileFalse: on:do: ensure: |
|
2826 |
whileTrue whileFalse loop |
|
2827 |
"/ any |
|
2828 |
isNil notNil isEmpty notEmpty |
|
2829 |
). |
|
2830 |
]. |
|
2831 |
||
2832 |
mostUseful notNil ifTrue:[ |
|
2833 |
suggestions := |
|
2834 |
(selectorsImplementedInClass select:[:sel | mostUseful includes:sel]) asNewOrderedCollection sort |
|
2835 |
, |
|
2836 |
(selectorsImplementedInClass reject:[:sel | mostUseful includes:sel]) asNewOrderedCollection sort. |
|
2837 |
] ifFalse:[ |
|
2838 |
suggestions := selectorsImplementedInClass asNewOrderedCollection sort. |
|
2839 |
]. |
|
2840 |
||
5043 | 2841 |
suggestions := suggestions reject:[:sel | sel first == $_]. |
5040 | 2842 |
self sortUsefulSelectorsIn:suggestions. "/cosmetics |
5041 | 2843 |
|
2844 |
pos := codeView characterPositionOfCursor. |
|
5080 | 2845 |
editActions := suggestions collect:[:word | |
2846 |
self editActionToReplaceCodeFrom:pos to:pos-1 by:word. |
|
2847 |
]. |
|
2848 |
actionBlock value:suggestions value:editActions value:nil. |
|
5040 | 2849 |
|
2850 |
"Created: / 01-05-2016 / 17:01:21 / cg" |
|
5621 | 2851 |
"Modified: / 10-10-2017 / 16:57:21 / cg" |
6022 | 2852 |
"Modified: / 03-03-2019 / 22:41:42 / Claus Gittinger" |
4238 | 2853 |
! |
2854 |
||
4548 | 2855 |
codeCompletionForMethodSpec:node |
4238 | 2856 |
"completion in a method's selector pattern" |
2857 |
||
4548 | 2858 |
self |
5312 | 2859 |
codeCompletionForMethodSpec:node |
5613 | 2860 |
into:[:suggestions :action :whatIsIt | |
5312 | 2861 |
|
2862 |
|chosen| |
|
2863 |
||
2864 |
chosen := self askUserForCompletion:whatIsIt for:codeView |
|
2865 |
at:node start from:suggestions. |
|
2866 |
chosen notNil ifTrue:[ |
|
2867 |
action value:(suggestions indexOf:chosen) |
|
2868 |
]. |
|
2869 |
]. |
|
4352 | 2870 |
|
2871 |
"/ |crsrPos |
|
2872 |
"/ selectorSoFar matchingSelectors |
|
4548 | 2873 |
"/ selectors distances best rest |
2874 |
"/ allExistingMethods nameBag namesByCount selectors1 selectors2| |
|
4352 | 2875 |
"/ |
2876 |
"/ crsrPos := codeView characterPositionOfCursor - 1. |
|
2877 |
"/ |
|
2878 |
"/ selectorSoFar := ''. |
|
2879 |
"/ node selectorParts doWithIndex:[:partToken :argNr| |
|
2880 |
"/ |part| |
|
2881 |
"/ |
|
2882 |
"/ part := partToken value. |
|
2883 |
"/ selectorSoFar := selectorSoFar , part. |
|
2884 |
"/ |
|
2885 |
"/ (crsrPos >= partToken start |
|
2886 |
"/ and:[crsrPos <= partToken stop]) ifTrue:[ |
|
2887 |
"/ (classOrNil notNil and:[classOrNil isMeta]) ifTrue:[ |
|
2888 |
"/ matchingSelectors := Smalltalk allClasses |
|
2889 |
"/ inject:(Set new) |
|
2890 |
"/ into:[:theSet :eachClass | |
|
2891 |
"/ |md| |
|
2892 |
"/ |
|
2893 |
"/ md := eachClass theMetaclass methodDictionary. |
|
2894 |
"/ theSet addAll:(md keys select:[:sel |sel startsWith:selectorSoFar]). |
|
2895 |
"/ theSet. |
|
2896 |
"/ ]. |
|
5312 | 2897 |
"/ "/ don't forget the stuff in the class-line |
4352 | 2898 |
"/ Metaclass withAllSuperclassesDo:[:cls | |
2899 |
"/ matchingSelectors addAll:(cls methodDictionary keys select:[:sel |sel startsWith:selectorSoFar]). |
|
2900 |
"/ ]. |
|
2901 |
"/ ] ifFalse:[ |
|
2902 |
"/ matchingSelectors := Smalltalk allClasses |
|
2903 |
"/ inject:(Set new) |
|
2904 |
"/ into:[:theSet :eachClass | |
|
2905 |
"/ |md| |
|
2906 |
"/ |
|
2907 |
"/ md := eachClass theNonMetaclass methodDictionary. |
|
2908 |
"/ theSet addAll:(md keys select:[:sel |sel startsWith:selectorSoFar]). |
|
2909 |
"/ theSet. |
|
2910 |
"/ ]. |
|
2911 |
"/ ]. |
|
2912 |
"/ selectors := matchingSelectors asOrderedCollection. |
|
2913 |
"/ |
|
4548 | 2914 |
"/ "/ if there is only one, and user has already entered it, he might want to complete the argument-name |
2915 |
"/ (selectors size == 1 |
|
4352 | 2916 |
"/ and:[selectors first = selectorSoFar]) ifTrue:[ |
2917 |
"/ selectorSoFar numArgs == 0 ifTrue:[ ^ self ]. |
|
2918 |
"/ |
|
2919 |
"/ allExistingMethods := (Smalltalk allImplementorsOf:selectorSoFar asSymbol) |
|
2920 |
"/ collect:[:cls | cls compiledMethodAt:selectorSoFar asSymbol]. |
|
2921 |
"/ nameBag := Bag new. |
|
2922 |
"/ allExistingMethods do:[:eachMethod | nameBag addAll:(eachMethod methodArgNames ? #())]. |
|
4548 | 2923 |
"/ namesByCount := nameBag valuesAndCounts sort:[:a :b | a value < b value]. |
2924 |
"/ "/ take the one which occurs most often |
|
4352 | 2925 |
"/ best := self askUserForCompletion:'argument' for:codeView at: node start from:(namesByCount collect:[:a | a key]). |
2926 |
"/ |
|
2927 |
"/ codeView |
|
2928 |
"/ undoableDo:[ |
|
2929 |
"/ (crsrPos+1) >= codeView contents size ifTrue:[ |
|
2930 |
"/ codeView paste:best. |
|
2931 |
"/ ] ifFalse:[ |
|
2932 |
"/ codeView insertString:best atCharacterPosition:crsrPos+1. |
|
2933 |
"/ ] |
|
2934 |
"/ ] |
|
2935 |
"/ info:'completion'. |
|
4548 | 2936 |
"/ codeView cursorToCharacterPosition:(crsrPos + best size - 1). |
4352 | 2937 |
"/ ] ifFalse:[ |
2938 |
"/ "the ones implemented in superclasses are shown first" |
|
2939 |
"/ classOrNil notNil ifTrue:[ |
|
2940 |
"/ selectors1 := selectors select:[:sel | classOrNil respondsTo:sel]. "/ in super |
|
2941 |
"/ selectors2 := selectors reject:[:sel | selectors1 includes:sel ]. "/ not in super |
|
2942 |
"/ ] ifFalse:[ |
|
2943 |
"/ selectors1 := selectors |
|
2944 |
"/ ]. |
|
2945 |
"/ |
|
2946 |
"/ distances := selectors1 collect:[:each | each spellAgainst:selectorSoFar]. |
|
2947 |
"/ distances sortWith:selectors1. |
|
2948 |
"/ selectors1 reverse. |
|
2949 |
"/ selectors := selectors1. |
|
2950 |
"/ |
|
2951 |
"/ selectors2 notEmptyOrNil ifTrue:[ |
|
2952 |
"/ distances := selectors2 collect:[:each | each spellAgainst:selectorSoFar]. |
|
2953 |
"/ distances sortWith:selectors2. |
|
2954 |
"/ selectors2 reverse. |
|
2955 |
"/ selectors1 := selectors1 collect:[:sel | sel allBold]. |
|
2956 |
"/ selectors := selectors1,selectors2. |
|
2957 |
"/ ]. |
|
4548 | 2958 |
"/ |
4352 | 2959 |
"/ best := self askUserForCompletion:'selector' for:codeView at:(node start) from:selectors. |
2960 |
"/ best isNil ifTrue:[^ self]. |
|
2961 |
"/ |
|
2962 |
"/ rest := best copyFrom:selectorSoFar size. |
|
2963 |
"/ codeView |
|
4548 | 2964 |
"/ undoableDo:[ |
2965 |
"/ codeView |
|
2966 |
"/ replaceFromCharacterPosition:crsrPos+1 |
|
2967 |
"/ to:crsrPos+1 |
|
2968 |
"/ with:rest |
|
4352 | 2969 |
"/ ] |
2970 |
"/ info:'Completion'. |
|
4548 | 2971 |
"/ codeView cursorToCharacterPosition:(crsrPos+1 + rest size - 1). |
4352 | 2972 |
"/ ]. |
4548 | 2973 |
"/ codeView cursorRight. "/ kludge to make it visible |
4352 | 2974 |
"/ ]. |
2975 |
"/ ]. |
|
4238 | 2976 |
|
2977 |
"Modified: / 04-07-2006 / 18:48:26 / fm" |
|
2978 |
"Created: / 10-11-2006 / 13:46:44 / cg" |
|
2979 |
"Modified: / 16-02-2010 / 10:13:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2980 |
"Modified: / 01-06-2012 / 20:31:36 / cg" |
|
5613 | 2981 |
"Modified (format): / 30-09-2017 / 12:58:32 / cg" |
4238 | 2982 |
! |
2983 |
||
4352 | 2984 |
codeCompletionForMethodSpec:node into:actionBlock |
4238 | 2985 |
"completion in a method's selector pattern" |
2986 |
||
4370 | 2987 |
|crsrPos crsrLine crsrCol |
4238 | 2988 |
selectorSoFar matchingSelectors |
5146 | 2989 |
selectors allExistingMethods nameBag namesByCount |
5044 | 2990 |
allSelectors |
2991 |
selectorsForIsXXXTests selectorsSentInClass selectorsImplementedInSuper |
|
2992 |
editAction editActionForArg argNames selectorsForVars |
|
5637 | 2993 |
selectorTypedSoFar selectorTypedSoFarLC addIfNotYetImplemented alreadyOK processMenu| |
5044 | 2994 |
|
2995 |
allSelectors := Set new. |
|
4548 | 2996 |
|
2997 |
"/ Transcript showCR:'m'. |
|
4370 | 2998 |
crsrLine := codeView cursorLine. |
2999 |
crsrCol := codeView cursorCol. |
|
4238 | 3000 |
crsrPos := codeView characterPositionOfCursor - 1. |
3001 |
||
4691 | 3002 |
selectorTypedSoFar := node selector. |
5637 | 3003 |
selectorTypedSoFarLC := selectorTypedSoFar asLowercase. |
3004 |
||
4691 | 3005 |
selectorTypedSoFar isUnarySelector ifTrue:[ |
4618 | 3006 |
"/ user has just begun to edit a selector. |
3007 |
"/ often, a good completion are the names of instVars for which no corresponding getter/setter exists |
|
3008 |
classOrNil notNil ifTrue:[ |
|
5044 | 3009 |
selectorsForVars := Set new. |
3010 |
selectorsImplementedInSuper := Set new. |
|
3011 |
selectorsSentInClass := Set new. |
|
3012 |
selectorsForIsXXXTests := Set new. |
|
3013 |
||
3014 |
addIfNotYetImplemented := |
|
3015 |
[:list :sel | |
|
5738 | 3016 |
"/ sel = 'max:' ifTrue:[self breakPoint:#cg]. |
5637 | 3017 |
(sel sameAs: selectorTypedSoFar) ifFalse:[ |
5044 | 3018 |
(allSelectors includes:sel) ifFalse:[ |
3019 |
(classOrNil implements:sel asSymbol) ifFalse:[ |
|
5598 | 3020 |
list add:sel. |
5044 | 3021 |
allSelectors add:sel. |
3022 |
]. |
|
3023 |
]. |
|
3024 |
]. |
|
3025 |
]. |
|
3026 |
||
4618 | 3027 |
classOrNil instVarNames do:[:nm | |
4691 | 3028 |
(nm startsWith:selectorTypedSoFar) ifTrue:[ |
5044 | 3029 |
addIfNotYetImplemented value:selectorsForVars value:nm. |
3030 |
addIfNotYetImplemented value:selectorsForVars value:(nm,':'). |
|
4618 | 3031 |
] |
3032 |
]. |
|
3033 |
classOrNil isMeta ifTrue:[ |
|
3034 |
classOrNil theNonMetaclass classVarNames do:[:nm | |
|
3035 |
|nmSel| |
|
3036 |
||
3037 |
nmSel := nm asLowercaseFirst. |
|
5637 | 3038 |
(nmSel startsWith:selectorTypedSoFarLC) ifTrue:[ |
5044 | 3039 |
addIfNotYetImplemented value:selectorsForVars value:nmSel. |
3040 |
addIfNotYetImplemented value:selectorsForVars value:(nmSel,':'). |
|
3041 |
] |
|
4618 | 3042 |
]. |
3043 |
] ifFalse:[ |
|
3044 |
"/ isXXX ? |
|
4691 | 3045 |
(('is',classOrNil nameWithoutPrefix) startsWith:selectorTypedSoFar ) ifTrue:[ |
5044 | 3046 |
addIfNotYetImplemented value:selectorsForIsXXXTests value:('is',classOrNil nameWithoutPrefix). |
4618 | 3047 |
]. |
3048 |
]. |
|
4691 | 3049 |
|
3050 |
"/ and also messages sent by the class itself |
|
3051 |
classOrNil methodsDo:[:m | |
|
3052 |
m messagesSentToSelf do:[:sel | |
|
3053 |
(sel startsWith:selectorTypedSoFar) ifTrue:[ |
|
5044 | 3054 |
addIfNotYetImplemented value:selectorsSentInClass value:sel. |
4691 | 3055 |
] |
3056 |
] |
|
3057 |
]. |
|
3058 |
classOrNil isMeta ifFalse:[ |
|
3059 |
classOrNil theMetaclass methodsDo:[:m | |
|
3060 |
m messagesSent do:[:sel | |
|
3061 |
(sel startsWith:selectorTypedSoFar) ifTrue:[ |
|
5044 | 3062 |
addIfNotYetImplemented value:selectorsSentInClass value:sel. |
3063 |
] |
|
3064 |
] |
|
3065 |
] |
|
3066 |
]. |
|
3067 |
"/ and also messages implemented by superclasses (except Object) |
|
3068 |
classOrNil allSuperclassesDo:[:eachSuperclass | |
|
3069 |
eachSuperclass ~~ Object ifTrue:[ |
|
5462 | 3070 |
eachSuperclass selectorsDo:[:sel | |
5044 | 3071 |
(sel startsWith:selectorTypedSoFar) ifTrue:[ |
3072 |
addIfNotYetImplemented value:selectorsImplementedInSuper value:sel. |
|
4691 | 3073 |
] |
3074 |
] |
|
3075 |
] |
|
3076 |
]. |
|
5598 | 3077 |
"/ if on the instance side, and the class side has menu- or windowspecs, |
3078 |
"/ also add messages named as callbacks or aspects |
|
3079 |
(classOrNil theNonMetaclass inheritsFrom:ApplicationModel) ifTrue:[ |
|
3080 |
processMenu := |
|
3081 |
[:menu | |
|
3082 |
menu itemsDo:[:item | |
|
3083 |
|sel subMenu| |
|
3084 |
||
3085 |
(sel := item choice) notNil ifTrue:[ |
|
3086 |
addIfNotYetImplemented value:selectorsSentInClass value:sel |
|
3087 |
]. |
|
3088 |
(sel := item itemValue) notNil ifTrue:[ |
|
3089 |
addIfNotYetImplemented value:selectorsSentInClass value:sel |
|
3090 |
]. |
|
3091 |
(sel := item submenuChannel) notNil ifTrue:[ |
|
3092 |
addIfNotYetImplemented value:selectorsSentInClass value:sel |
|
3093 |
]. |
|
3094 |
(subMenu := item submenu) notNil ifTrue:[ |
|
3095 |
processMenu value:subMenu |
|
3096 |
]. |
|
3097 |
]. |
|
3098 |
]. |
|
3099 |
||
3100 |
classOrNil withAllSuperclassesDo:[:eachSuperclass | |
|
3101 |
eachSuperclass theMetaclass methodDictionary keysAndValuesDo:[:sel :mthd | |
|
3102 |
|spec bodyNode retVal specArray menu| |
|
3103 |
||
3104 |
(mthd hasMenuResource) ifTrue:[ |
|
3105 |
bodyNode := mthd parseTree body. |
|
3106 |
bodyNode lastIsReturn ifTrue:[ |
|
3107 |
(retVal := bodyNode statements last value) isLiteralArray ifTrue:[ |
|
3108 |
specArray := retVal value. |
|
3109 |
menu := specArray decodeAsLiteralArray. |
|
3110 |
processMenu value:menu. |
|
3111 |
]. |
|
3112 |
]. |
|
3113 |
]. |
|
3114 |
(mthd hasCanvasResource) ifTrue:[ |
|
3115 |
bodyNode := mthd parseTree body. |
|
3116 |
bodyNode lastIsReturn ifTrue:[ |
|
3117 |
(retVal := bodyNode statements last value) isLiteralArray ifTrue:[ |
|
3118 |
specArray := retVal value. |
|
3119 |
spec := specArray decodeAsLiteralArray. |
|
3120 |
spec aspectSelectors do:[:sel | addIfNotYetImplemented value:selectorsSentInClass value:sel]. |
|
3121 |
spec valueSelectors do:[:sel | addIfNotYetImplemented value:selectorsSentInClass value:sel]. |
|
3122 |
spec actionSelectors do:[:sel | addIfNotYetImplemented value:selectorsSentInClass value:sel]. |
|
3123 |
]. |
|
3124 |
]. |
|
3125 |
]. |
|
3126 |
]. |
|
3127 |
eachSuperclass selectorsDo:[:sel | |
|
3128 |
(sel startsWith:selectorTypedSoFar) ifTrue:[ |
|
3129 |
addIfNotYetImplemented value:selectorsImplementedInSuper value:sel. |
|
3130 |
] |
|
3131 |
] |
|
3132 |
] |
|
3133 |
]. |
|
4618 | 3134 |
]. |
4371 | 3135 |
]. |
3136 |
||
4238 | 3137 |
selectorSoFar := ''. |
5044 | 3138 |
matchingSelectors := Set new. |
3139 |
||
4238 | 3140 |
node selectorParts doWithIndex:[:partToken :argNr| |
4618 | 3141 |
|part| |
3142 |
||
3143 |
part := partToken value. |
|
3144 |
selectorSoFar := selectorSoFar , part. |
|
3145 |
||
5044 | 3146 |
(crsrPos >= partToken start |
3147 |
and:[crsrPos <= partToken stop |
|
3148 |
or:[ (crsrPos == (partToken stop+1)) |
|
3149 |
and:[codeView characterBeforeCursor == Character space]] ] |
|
3150 |
) ifTrue:[ |
|
4618 | 3151 |
(classOrNil notNil and:[classOrNil isMeta]) ifTrue:[ |
5044 | 3152 |
Smalltalk |
3153 |
allClassesDo:[:eachClass | |
|
3154 |
|md| |
|
3155 |
||
3156 |
md := eachClass theMetaclass methodDictionary. |
|
3157 |
matchingSelectors addAll:(md keys |
|
3158 |
select:[:sel |sel startsWith:selectorSoFar]). |
|
3159 |
]. |
|
3160 |
"/ do not forget the stuff in the class-line |
|
4618 | 3161 |
Metaclass withAllSuperclassesDo:[:cls | |
5044 | 3162 |
matchingSelectors addAll:(cls methodDictionary keys |
3163 |
select:[:sel |sel startsWith:selectorSoFar]). |
|
4618 | 3164 |
]. |
3165 |
] ifFalse:[ |
|
5044 | 3166 |
Smalltalk |
3167 |
allClassesDo:[:eachClass | |
|
3168 |
|md| |
|
3169 |
||
3170 |
md := eachClass theNonMetaclass methodDictionary. |
|
3171 |
matchingSelectors addAll:(md keys select:[:sel |sel startsWith:selectorSoFar]). |
|
3172 |
]. |
|
4618 | 3173 |
]. |
5044 | 3174 |
]. |
3175 |
]. |
|
3176 |
alreadyOK := matchingSelectors includes:selectorTypedSoFar. |
|
3177 |
matchingSelectors remove:selectorTypedSoFar ifAbsent:[]. |
|
3178 |
||
3179 |
selectors := Set new. |
|
3180 |
||
5637 | 3181 |
selectorsForVars notNil ifTrue:[ |
3182 |
selectors addAll:selectorsForVars |
|
3183 |
]. |
|
5044 | 3184 |
selectorsSentInClass notNil ifTrue:[ selectors addAll:selectorsSentInClass ]. |
3185 |
selectorsImplementedInSuper notNil ifTrue:[ selectors addAll:selectorsImplementedInSuper ]. |
|
3186 |
selectorsForIsXXXTests notNil ifTrue:[ selectors addAll:selectorsForIsXXXTests ]. |
|
3187 |
||
5637 | 3188 |
(selectors count:[:sel | sel asLowercase startsWith:selectorTypedSoFarLC]) == 0 ifTrue:[ |
3189 |
selectors addAll:matchingSelectors. |
|
3190 |
]. |
|
3191 |
||
5044 | 3192 |
selectors := selectors asOrderedCollection. |
3193 |
selectors sort:[:a :b | a size < b size]. |
|
5637 | 3194 |
|
5044 | 3195 |
selectorsSentInClass notEmptyOrNil ifTrue:[ |
3196 |
selectors := self |
|
3197 |
splitSelectorList:selectors |
|
3198 |
by:[:sel | selectorsSentInClass includes:sel]. |
|
3199 |
]. |
|
3200 |
selectorsImplementedInSuper notEmptyOrNil ifTrue:[ |
|
3201 |
selectors := self |
|
3202 |
splitSelectorList:selectors |
|
3203 |
by:[:sel | selectorsImplementedInSuper includes:sel]. |
|
3204 |
]. |
|
5637 | 3205 |
|
3206 |
selectors := self |
|
3207 |
splitSelectorList:selectors |
|
3208 |
by:[:sel | sel asLowercase startsWith:selectorTypedSoFarLC]. |
|
3209 |
||
3210 |
selectorsForVars notEmptyOrNil ifTrue:[ |
|
3211 |
selectors := self |
|
3212 |
splitSelectorList:selectors |
|
3213 |
by:[:sel | selectorsForVars includes:sel]. |
|
3214 |
]. |
|
5044 | 3215 |
selectorsForIsXXXTests notEmptyOrNil ifTrue:[ |
3216 |
selectors := self |
|
3217 |
splitSelectorList:selectors |
|
3218 |
by:[:sel | selectorsForIsXXXTests includes:sel]. |
|
3219 |
]. |
|
3220 |
||
3221 |
selectors size > 100 ifTrue:[ |
|
3222 |
selectors := selectors copyTo:100. |
|
3223 |
]. |
|
3224 |
||
3225 |
"/ if there is only one, and user has already entered it, |
|
3226 |
"/ he might want to complete the argument-name |
|
3227 |
(alreadyOK and:[selectorSoFar numArgs > 0]) ifTrue:[ |
|
3228 |
allExistingMethods := (Smalltalk allImplementorsOf:selectorSoFar asSymbol) |
|
3229 |
collect:[:cls | cls compiledMethodAt:selectorSoFar asSymbol]. |
|
3230 |
nameBag := Bag new. |
|
3231 |
allExistingMethods do:[:eachMethod | nameBag addAll:(eachMethod methodArgNames ? #())]. |
|
6082 | 3232 |
namesByCount := nameBag valuesAndCounts copy sort:[:a :b | a value < b value]. |
5044 | 3233 |
namesByCount := namesByCount copyToMax:5. |
3234 |
||
3235 |
"/ take the one which occurs most often |
|
3236 |
argNames := (namesByCount collect:[:a | a key]). |
|
3237 |
argNames do:[:eachArgName | |
|
3238 |
selectors add:('%1 %2 %3' |
|
5089 | 3239 |
bindWith:(selectorSoFar withColor:Color darkGrey) |
5044 | 3240 |
with:eachArgName |
5089 | 3241 |
with:('(argument)' allItalic withColor:Color darkGrey)). |
5044 | 3242 |
]. |
3243 |
||
3244 |
editActionForArg := |
|
3245 |
[:chosenItem | |
|
3246 |
|chosenName| |
|
3247 |
||
3248 |
chosenName := chosenItem |
|
3249 |
copyFrom:(selectorSoFar size+2) |
|
3250 |
to:(chosenItem indexOfSeparatorStartingAt:selectorSoFar size+2)-1. |
|
4618 | 3251 |
codeView |
3252 |
undoableDo:[ |
|
5044 | 3253 |
(crsrPos+1) >= codeView contents size ifTrue:[ |
3254 |
codeView paste:chosenName. |
|
3255 |
codeView cursorToCharacterPosition:(crsrPos + chosenName size - 1). |
|
3256 |
] ifFalse:[ |
|
3257 |
codeView cursorToCharacterPosition:crsrPos. |
|
3258 |
codeView cursorRight. |
|
3259 |
codeView insertStringAtCursor:chosenName. |
|
3260 |
codeView selectFromCharacterPosition:crsrPos+1 to:crsrPos+1+chosenName size-1. |
|
3261 |
codeView dontReplaceSelectionOnInput |
|
3262 |
]. |
|
4618 | 3263 |
] |
5044 | 3264 |
info:'argname completion'. |
4618 | 3265 |
]. |
5044 | 3266 |
]. |
3267 |
||
3268 |
"/ no, still more possibilities for the selector. |
|
3269 |
"/ the ones implemented in superclasses are shown first. |
|
5146 | 3270 |
(classOrNil notNil and:[classOrNil superclass notNil]) ifTrue:[ |
5044 | 3271 |
selectors := self |
3272 |
splitSelectorList:selectors |
|
3273 |
by:[:sel | classOrNil superclass implements:sel]. |
|
3274 |
]. |
|
3275 |
||
3276 |
"/"/ distances := selectors collect:[:each | each spellAgainst:selectorSoFar]. |
|
3277 |
"/"/ distances sortWith:selectors. |
|
3278 |
"/"/ selectors reverse. |
|
3279 |
||
3280 |
editAction := |
|
3281 |
[:selectedCompletionIndex | |
|
3282 |
|chosen rest| |
|
3283 |
||
3284 |
chosen := selectors at:selectedCompletionIndex. |
|
3285 |
(editActionForArg notNil and:[ chosen includesSeparator ]) ifTrue:[ |
|
3286 |
editActionForArg value:chosen. |
|
3287 |
] ifFalse:[ |
|
3288 |
rest := chosen copyFrom:selectorSoFar size + 1. |
|
3289 |
codeView |
|
3290 |
undoableDo:[ |
|
3291 |
codeView insertString:rest atLine:crsrLine col:crsrCol. |
|
3292 |
] |
|
3293 |
info:'Completion'. |
|
3294 |
codeView cursorToCharacterPosition:(crsrPos+1 + rest size - 1). |
|
3295 |
codeView cursorRight. "/ kludge to make it visible |
|
3296 |
]. |
|
4618 | 3297 |
]. |
5044 | 3298 |
|
3299 |
actionBlock |
|
3300 |
value:selectors |
|
3301 |
value:editAction |
|
3302 |
value:'selector'. |
|
4238 | 3303 |
|
3304 |
"Modified: / 04-07-2006 / 18:48:26 / fm" |
|
3305 |
"Created: / 10-11-2006 / 13:46:44 / cg" |
|
3306 |
"Modified: / 16-02-2010 / 10:13:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
5637 | 3307 |
"Modified: / 19-11-2017 / 14:47:34 / cg" |
4238 | 3308 |
! |
3309 |
||
5106 | 3310 |
codeCompletionForTempVariable:node into:actionBlock |
3311 |
! |
|
3312 |
||
4238 | 3313 |
codeCompletionForVariable:node into:actionBlock |
3314 |
|nonMetaClass crsrPos nm parent |
|
3315 |
allVariables allDistances variablesAlreadyAdded nodeVal |
|
5081 | 3316 |
char getDistanceComputeBlockWithWeight addWithFactorBlock allTheBest bestAssoc |
3317 |
globalFactor localFactor selectorOfMessageToNode implementors argIdx namesUsed kwPart editActions suggestions nameIsOK longerNames setOfNames otherArgNames |
|
5076 | 3318 |
suggestionsWithInfo isLeftSideOfAssignment| |
5551 | 3319 |
|
4302 | 3320 |
"/ Transcript show:'var in '; show:methodOrNil; show:' / '; showCR:classOrNil. |
4238 | 3321 |
classOrNil notNil ifTrue:[ |
4559 | 3322 |
nonMetaClass := classOrNil theNonMetaclass. |
4238 | 3323 |
]. |
5551 | 3324 |
|
4238 | 3325 |
nm := node name. |
5076 | 3326 |
parent := node parent. |
3327 |
isLeftSideOfAssignment := false. |
|
5551 | 3328 |
|
5076 | 3329 |
(parent notNil) ifTrue:[ |
3330 |
(parent isMessage) ifTrue:[ |
|
3331 |
node == parent receiver ifTrue:[ |
|
3332 |
selectorOfMessageToNode := parent selector |
|
3333 |
] |
|
3334 |
] ifFalse:[ |
|
3335 |
(parent isAssignment) ifTrue:[ |
|
3336 |
isLeftSideOfAssignment := (node == node parent variable). |
|
5551 | 3337 |
]. |
3338 |
]. |
|
3339 |
]. |
|
3340 |
||
5040 | 3341 |
crsrPos := codeView characterPositionOfCursor. |
5551 | 3342 |
|
4238 | 3343 |
"/ if we are behind the variable and a space has already been entered, |
3344 |
"/ the user is probably looking for a message selector. |
|
3345 |
"/ If the variable represents a global, present its instance creation messages |
|
5551 | 3346 |
char := codeView characterBeforeCursor. |
5076 | 3347 |
|
3348 |
(isLeftSideOfAssignment not and:[char == Character space]) ifTrue:[ |
|
4559 | 3349 |
nm knownAsSymbol ifTrue:[ |
3350 |
classOrNil isNil ifTrue:[ |
|
3351 |
nodeVal := Smalltalk at:nm asSymbol. |
|
3352 |
] ifFalse:[ |
|
3353 |
nodeVal := classOrNil topNameSpace at:nm asSymbol ifAbsent:[Smalltalk at:nm asSymbol]. |
|
3354 |
]. |
|
3355 |
nodeVal isBehavior ifTrue:[ |
|
4954 | 3356 |
|selectors selectors1 selectors2| |
3357 |
||
3358 |
selectors1 := OrderedSet new. |
|
3359 |
selectors2 := OrderedSet new. |
|
5551 | 3360 |
nodeVal class |
4954 | 3361 |
withAllSuperclassesDo:[:cls | |
3362 |
cls methodDictionary keysAndValuesDo:[:sel :mthd | |
|
3363 |
|cat| |
|
6022 | 3364 |
cat := mthd category. |
3365 |
(cat notNil and:[cat sameAs: 'instance creation']) ifTrue:[ |
|
4954 | 3366 |
selectors1 add:sel |
3367 |
] ifFalse:[ |
|
3368 |
"/ other category: look if it sends new, new: etc. |
|
3369 |
(mthd messagesSent includesAny:#(new new: basicNew basicNew:)) ifTrue:[ |
|
3370 |
selectors2 add:sel |
|
5551 | 3371 |
]. |
4954 | 3372 |
] |
3373 |
] |
|
3374 |
]. |
|
5551 | 3375 |
|
4954 | 3376 |
selectors := selectors1 order sort , #('-') , selectors2 order sort. |
5080 | 3377 |
editActions := selectors collect:[:word | self editActionToInsert:word]. |
3378 |
actionBlock value:selectors value:editActions value:nil. |
|
3379 |
"/ editAction := |
|
3380 |
"/ [:answer | |
|
3381 |
"/ |s| |
|
3382 |
"/ s := answer isInteger ifTrue:[selectors at:answer] ifFalse:[answer]. |
|
3383 |
"/ codeView |
|
3384 |
"/ undoableDo:[ |
|
3385 |
"/ codeView insertString:s atCharacterPosition:crsrPos. |
|
3386 |
"/ codeView cursorToCharacterPosition:crsrPos+s size. |
|
3387 |
"/ ] |
|
3388 |
"/ info:'completion'. |
|
3389 |
"/ ]. |
|
3390 |
"/ actionBlock value:selectors value:editAction value:nil. |
|
4559 | 3391 |
^ self. |
3392 |
]. |
|
3393 |
]. |
|
4238 | 3394 |
]. |
5551 | 3395 |
|
4352 | 3396 |
"/ this is pure voodoo magic (tries to make a good spelling weight, |
3397 |
"/ by weighting the number of startsWith characters into the spelling distance...) |
|
4238 | 3398 |
getDistanceComputeBlockWithWeight := |
4559 | 3399 |
[:weight | |
3400 |
[:each | |
|
3401 |
|dist factor| |
|
3402 |
||
3403 |
dist := each spellAgainst:nm. |
|
3404 |
factor := 1. |
|
3405 |
||
5027 | 3406 |
"/ bump the weight-factor if the name string is included |
3407 |
(each includesString:nm) ifTrue:[ |
|
3408 |
(each startsWith:nm) ifTrue:[ |
|
3409 |
factor := 6 * weight * nm size. |
|
5551 | 3410 |
] ifFalse:[ |
5360 | 3411 |
(each endsWith:nm) ifTrue:[ |
3412 |
factor := 5 * weight * nm size. |
|
3413 |
] ifFalse:[ |
|
3414 |
nm size > 1 ifTrue:[ |
|
3415 |
factor := 4 * weight * nm size |
|
5551 | 3416 |
]. |
3417 |
]. |
|
3418 |
]. |
|
4559 | 3419 |
] ifFalse:[ |
5027 | 3420 |
(each includesString:nm caseSensitive:false) ifTrue:[ |
3421 |
(each asLowercase startsWith:nm asLowercase) ifTrue:[ |
|
3422 |
factor := 4 * weight * nm size. |
|
3423 |
] ifFalse:[ |
|
5360 | 3424 |
(each asLowercase endsWith:nm asLowercase) ifTrue:[ |
3425 |
factor := 3 * weight * nm size. |
|
3426 |
] ifFalse:[ |
|
3427 |
nm size > 1 ifTrue:[ |
|
3428 |
factor := 2 * weight * nm size. |
|
3429 |
] |
|
5027 | 3430 |
] |
3431 |
] |
|
3432 |
] |
|
5551 | 3433 |
]. |
4559 | 3434 |
dist := dist + (weight*factor). |
3435 |
||
3436 |
each -> (dist * weight) |
|
3437 |
] |
|
3438 |
]. |
|
4238 | 3439 |
|
3440 |
nameIsOK := false. |
|
3441 |
addWithFactorBlock := |
|
4559 | 3442 |
[:eachNames :factor | |
4689 | 3443 |
|distanceComputeBlock| |
3444 |
||
3445 |
distanceComputeBlock := (getDistanceComputeBlockWithWeight value:factor). |
|
4559 | 3446 |
eachNames do:[:nameToAdd | |
5040 | 3447 |
(nameToAdd = nm) ifTrue:[ |
3448 |
nameIsOK := true |
|
3449 |
] ifFalse:[ "/ not again |
|
4559 | 3450 |
(variablesAlreadyAdded includes:nameToAdd) ifFalse:[ "/ not again |
3451 |
variablesAlreadyAdded add:nameToAdd. |
|
3452 |
allVariables add:nameToAdd. |
|
4689 | 3453 |
allDistances add:(distanceComputeBlock value:nameToAdd). |
4559 | 3454 |
] |
3455 |
] |
|
3456 |
] |
|
3457 |
]. |
|
4238 | 3458 |
|
3459 |
nm isUppercaseFirst ifTrue:[ |
|
4559 | 3460 |
globalFactor := 2. "/ favour globals |
3461 |
localFactor := 1. |
|
4238 | 3462 |
] ifFalse:[ |
4559 | 3463 |
globalFactor := 1. "/ favour locals |
3464 |
localFactor := 2. |
|
4238 | 3465 |
]. |
3466 |
||
3467 |
variablesAlreadyAdded := Set new. |
|
3468 |
allVariables := OrderedCollection new. |
|
3469 |
allDistances := OrderedCollection new. |
|
3470 |
||
3471 |
"/ are we in the method's selector spec ? |
|
4548 | 3472 |
(parent notNil |
4238 | 3473 |
and:[parent isMethod |
3474 |
and:[parent arguments includes:node]]) ifTrue:[ |
|
4559 | 3475 |
"/ yes - |
4784 | 3476 |
"/ now that's cool: look how the name of this argument is in other implementations |
4559 | 3477 |
"/ of this method, and take that as a basis of the selection |
3478 |
||
3479 |
implementors := SystemBrowser |
|
3480 |
findImplementorsOf:(parent selector) |
|
3481 |
in:(Smalltalk allClasses) |
|
3482 |
ignoreCase:false. |
|
3483 |
"/ which argument is it |
|
3484 |
argIdx := parent arguments indexOf:node. |
|
3485 |
implementors size > 50 ifTrue:[ |
|
3486 |
implementors := implementors asOrderedCollection copyTo:50. |
|
3487 |
]. |
|
3488 |
namesUsed := implementors |
|
3489 |
collect:[:eachImplementor | |
|
3490 |
|parseTree| |
|
3491 |
parseTree := eachImplementor parseTree. |
|
5539 | 3492 |
(parseTree notNil and:[parseTree arguments size >= argIdx]) |
4559 | 3493 |
ifFalse:nil |
3494 |
ifTrue:[ (parseTree arguments at:argIdx) name] ] |
|
3495 |
thenSelect:[:a | a notNil]. |
|
3496 |
||
3497 |
addWithFactorBlock value:namesUsed value:(2 * localFactor). |
|
3498 |
||
4784 | 3499 |
"/ try some commonly used arg names, such as aBoolean, anInteger, etc. |
3500 |
nm size > 1 ifTrue:[ |
|
3501 |
|tryClassNamesWith| |
|
3502 |
||
3503 |
((nm startsWith:'a') and:[(nm at:2) isUppercase]) ifTrue:[ |
|
3504 |
tryClassNamesWith := 'a' |
|
3505 |
] ifFalse:[ |
|
3506 |
(nm size > 2 and:[ (nm startsWith:'an') and:[(nm at:3) isUppercase]]) ifTrue:[ |
|
3507 |
tryClassNamesWith := 'an'. |
|
3508 |
]. |
|
3509 |
]. |
|
3510 |
tryClassNamesWith notNil ifTrue:[ |
|
5551 | 3511 |
addWithFactorBlock |
3512 |
value:(Smalltalk keys |
|
4784 | 3513 |
collect:[:className | tryClassNamesWith,className] |
3514 |
thenSelect:[:name | name startsWith:nm]) |
|
3515 |
value:(1.5 * localFactor) |
|
3516 |
]. |
|
3517 |
]. |
|
3518 |
||
4559 | 3519 |
classOrNil notNil ifTrue:[ |
3520 |
"/ also, look for the keyword before the argument, |
|
3521 |
"/ and see if there is such an instVar |
|
3522 |
"/ if so, add it with -Arg |
|
3523 |
parent selector isKeyword ifTrue:[ |
|
3524 |
kwPart := parent selector keywords at:argIdx. |
|
3525 |
(classOrNil allInstVarNames includes:(kwPart copyButLast:1)) ifTrue:[ |
|
3526 |
addWithFactorBlock |
|
3527 |
value:(classOrNil allInstVarNames collect:[:nm| nm,'Arg']) |
|
3528 |
value:(1 * localFactor). |
|
3529 |
]. |
|
3530 |
]. |
|
3531 |
"/ look for the variable names of any other method in that class |
|
3532 |
otherArgNames := Set new. |
|
3533 |
classOrNil methodDictionary keysAndValuesDo:[:sel :mthd | |
|
3534 |
|parseTree| |
|
3535 |
parseTree := mthd parseTree. |
|
6013 | 3536 |
(parseTree notNil and:[parseTree arguments size ~~ 0]) |
4559 | 3537 |
ifFalse:nil |
3538 |
ifTrue:[ otherArgNames addAll:(parseTree arguments collect:[:each | each name])] ]. |
|
3539 |
addWithFactorBlock value:otherArgNames value:(1.5 * localFactor). |
|
3540 |
]. |
|
5551 | 3541 |
addWithFactorBlock |
3542 |
value:(codeView previousReplacements |
|
5043 | 3543 |
collect:[:p | p value asString] |
5551 | 3544 |
thenSelect:[:s | s isValidSmalltalkIdentifier]) |
5043 | 3545 |
value:(1.3 * localFactor). |
4238 | 3546 |
] ifFalse:[ |
4559 | 3547 |
"/ locals in the block/method |
5040 | 3548 |
|names nameSpace| |
4559 | 3549 |
|
3550 |
names := OrderedCollection withAll:node allVariablesOnScope. |
|
3551 |
setOfNames := Set withAll:names. |
|
3552 |
||
3553 |
rememberedScopeNodes notNil ifTrue:[ |
|
3554 |
"/ notNil when a parseError occurred. |
|
3555 |
rememberedScopeNodes do:[:eachScope | |
|
3556 |
(eachScope isMethod or:[eachScope isBlock]) ifTrue:[ |
|
3557 |
eachScope argumentNames do:[:eachName | |
|
3558 |
(setOfNames includes:eachName) ifFalse:[ names add:eachName. setOfNames add:eachName ] |
|
3559 |
] |
|
3560 |
] ifFalse:[ |
|
3561 |
eachScope isSequence ifTrue:[ |
|
3562 |
eachScope temporaryNames do:[:eachName | |
|
3563 |
(setOfNames includes:eachName) ifFalse:[ names add:eachName. setOfNames add:eachName ] |
|
3564 |
] |
|
3565 |
] ifFalse:[ |
|
3566 |
] |
|
3567 |
]. |
|
3568 |
"/ (setOfNames includesAll:(eachScope allDefinedVariables)) ifFalse:[ self halt]. |
|
3569 |
]. |
|
5040 | 3570 |
|
4559 | 3571 |
rememberedScopeNodes do:[:eachScope | |
3572 |
eachScope variableNodesDo:[:var | |
|
3573 |
(setOfNames includes:var name) ifFalse:[ |
|
3574 |
names add:var name. setOfNames add:var name |
|
3575 |
] |
|
3576 |
] |
|
3577 |
] |
|
3578 |
] ifFalse:[ |
|
5076 | 3579 |
"/ tree must be there; in order to get as-yet-undeclared method locals |
3580 |
"/ (further down the code), add them also |
|
4559 | 3581 |
tree variableNodesDo:[:var | |
5076 | 3582 |
|name| |
5551 | 3583 |
|
5076 | 3584 |
name := var name. |
5552 | 3585 |
(name isLowercaseFirst or:[isLeftSideOfAssignment not]) ifTrue:[ |
5076 | 3586 |
(setOfNames includes:name) ifFalse:[ |
5551 | 3587 |
names add:name. |
5076 | 3588 |
setOfNames add:name |
3589 |
] |
|
4559 | 3590 |
] |
3591 |
] |
|
3592 |
]. |
|
3593 |
||
3594 |
addWithFactorBlock value:names value:(4 * localFactor). |
|
3595 |
||
3596 |
classOrNil notNil ifTrue:[ |
|
3597 |
"/ instance variables |
|
3598 |
addWithFactorBlock value:classOrNil instVarNames value:(3 * localFactor). |
|
3599 |
||
3600 |
"/ inherited instance variables |
|
3601 |
classOrNil superclass notNil ifTrue:[ |
|
3602 |
addWithFactorBlock value:classOrNil superclass allInstVarNames value:(2.5 * localFactor). |
|
3603 |
]. |
|
3604 |
]. |
|
3605 |
||
3606 |
"/ magic: |
|
3607 |
"/ if the node to be expanded is the receiver in a message, look for the selector sent to it |
|
5771 | 3608 |
"/ retrieve names which respond to those messages a higher weight |
4559 | 3609 |
selectorOfMessageToNode notNil ifTrue:[ |
3610 |
|responders nonResponders| |
|
3611 |
||
5551 | 3612 |
"/ responding to that message |
4559 | 3613 |
classOrNil notNil ifTrue:[ |
3614 |
"/ private classes |
|
3615 |
addWithFactorBlock value:(nonMetaClass privateClasses collect:[:cls | cls nameWithoutPrefix]) |
|
3616 |
value:(2.75 * globalFactor). |
|
3617 |
||
3618 |
"/ class variables |
|
3619 |
names := nonMetaClass classVarNames. |
|
5771 | 3620 |
responders := OrderedCollection new. |
3621 |
nonResponders := OrderedCollection new. |
|
3622 |
names do:[:classVar | |
|
3623 |
|varValue| |
|
3624 |
||
3625 |
varValue := nonMetaClass classVarAt:classVar. |
|
3626 |
((varValue isBridgeProxy not |
|
3627 |
and:[ varValue respondsTo:selectorOfMessageToNode]) |
|
3628 |
ifTrue:[responders] |
|
3629 |
ifFalse:[nonResponders]) add:classVar |
|
3630 |
]. |
|
4559 | 3631 |
|
3632 |
addWithFactorBlock value:responders value:(3.0 * globalFactor). |
|
3633 |
addWithFactorBlock value:nonResponders value:(0.5 * 1.5 * globalFactor). |
|
3634 |
||
3635 |
"/ superclass var names |
|
3636 |
nonMetaClass allSuperclassesDo:[:superClass | |
|
3637 |
names := superClass classVarNames. |
|
3638 |
responders := names select:[:classVar | (superClass classVarAt:classVar) respondsTo:selectorOfMessageToNode]. |
|
3639 |
nonResponders := names reject:[:classVar | (superClass classVarAt:classVar) respondsTo:selectorOfMessageToNode]. |
|
3640 |
||
3641 |
addWithFactorBlock value:responders value:(2.75 * globalFactor). |
|
3642 |
addWithFactorBlock value:nonResponders value:(0.5 * 1 * globalFactor). |
|
3643 |
]. |
|
3644 |
||
5076 | 3645 |
isLeftSideOfAssignment ifFalse:[ |
3646 |
"/ namespace vars |
|
3647 |
classOrNil topNameSpace ~~ Smalltalk ifTrue:[ |
|
3648 |
names := classOrNil topNameSpace keys. |
|
3649 |
names := names reject:[:nm | nm includes:$:]. |
|
3650 |
names := names select:[:nm | nm isUppercaseFirst ]. |
|
3651 |
responders := names select:[:nsVar | |c| c := classOrNil topNameSpace at:nsVar. c isBehavior not or:[c isLoaded and:[c respondsTo:selectorOfMessageToNode]]]. |
|
3652 |
nonResponders := names reject:[:nsVar | |c| c := classOrNil topNameSpace at:nsVar. c isBehavior not or:[c isLoaded and:[c respondsTo:selectorOfMessageToNode]]]. |
|
3653 |
addWithFactorBlock value:responders value:(2.5 * globalFactor). |
|
3654 |
addWithFactorBlock value:nonResponders value:(0.5 * 1.5 * globalFactor). |
|
3655 |
]. |
|
4559 | 3656 |
]. |
3657 |
]. |
|
3658 |
||
5076 | 3659 |
isLeftSideOfAssignment ifFalse:[ |
3660 |
"/ globals |
|
3661 |
names := Smalltalk keys. |
|
3662 |
names := names reject: |
|
3663 |
[:nm | |
|
3664 |
(nm includes:$:) and:[ (Smalltalk at:nm) isBehavior not] |
|
3665 |
]. |
|
3666 |
names := names reject:[:nm | nm startsWith:'Undeclared:::' ]. |
|
3667 |
||
3668 |
names := names select:[:nm | nm isUppercaseFirst ] as:OrderedCollection. |
|
3669 |
responders := names select:[:glblVar | |c| c := Smalltalk at:glblVar. c isBehavior not or:[c isLoaded and:[c respondsTo:selectorOfMessageToNode]]]. |
|
3670 |
nonResponders := names reject:[:glblVar | |c| c := Smalltalk at:glblVar. c isBehavior not or:[c isLoaded and:[c respondsTo:selectorOfMessageToNode]]]. |
|
3671 |
addWithFactorBlock value:responders value:(1.5 * globalFactor). |
|
3672 |
addWithFactorBlock value:nonResponders value:(0.5 * 1.5 * globalFactor). |
|
3673 |
]. |
|
5551 | 3674 |
|
5076 | 3675 |
isLeftSideOfAssignment ifFalse:[ "/ no, we will not suggest assigning to pool vars |
3676 |
classOrNil notNil ifTrue:[ |
|
3677 |
"/ pool variables |
|
3678 |
classOrNil theNonMetaclass sharedPoolNames do:[:poolName | |
|
3679 |
|pool names| |
|
3680 |
||
3681 |
pool := Smalltalk at:poolName. |
|
5224 | 3682 |
pool notNil ifTrue:[ |
3683 |
names := pool classVarNames. |
|
3684 |
names := names select:[:nm | nm isUppercaseFirst ]. |
|
3685 |
responders := names select:[:glblVar | |c| c := Smalltalk at:glblVar. c isBehavior not or:[c isLoaded and:[c respondsTo:selectorOfMessageToNode]]]. |
|
3686 |
nonResponders := names reject:[:glblVar | |c| c := Smalltalk at:glblVar. c isBehavior not or:[c isLoaded and:[c respondsTo:selectorOfMessageToNode]]]. |
|
3687 |
addWithFactorBlock value:responders value:(2.5 * globalFactor). |
|
3688 |
addWithFactorBlock value:nonResponders value:(0.5 * 2.5 * globalFactor). |
|
3689 |
]. |
|
5076 | 3690 |
]. |
3691 |
] |
|
4559 | 3692 |
] |
3693 |
] ifFalse:[ |
|
3694 |
classOrNil notNil ifTrue:[ |
|
5551 | 3695 |
isLeftSideOfAssignment ifFalse:[ |
5076 | 3696 |
"/ private classes |
3697 |
addWithFactorBlock value:(nonMetaClass privateClasses collect:[:cls | cls nameWithoutPrefix]) |
|
3698 |
value:(2.75 * globalFactor). |
|
3699 |
]. |
|
5551 | 3700 |
|
4559 | 3701 |
"/ class variables |
3702 |
addWithFactorBlock value:nonMetaClass classVarNames value:(3.0 * globalFactor). |
|
3703 |
nonMetaClass superclass notNil ifTrue:[ |
|
3704 |
addWithFactorBlock value:nonMetaClass superclass allClassVarNames value:(2.5 * globalFactor). |
|
3705 |
]. |
|
3706 |
||
5551 | 3707 |
isLeftSideOfAssignment ifFalse:[ |
5076 | 3708 |
"/ namespace vars |
3709 |
classOrNil topNameSpace ~~ Smalltalk ifTrue:[ |
|
3710 |
names := classOrNil topNameSpace keys. |
|
3711 |
names := names reject:[:nm | nm includes:$:]. |
|
3712 |
names := names select:[:nm | nm isUppercaseFirst ]. |
|
3713 |
addWithFactorBlock value:names value:(2.5 * globalFactor). |
|
3714 |
]. |
|
3715 |
"/ namespace vars |
|
3716 |
((nameSpace := classOrNil nameSpace) notNil and:[nameSpace ~~ Smalltalk]) ifTrue:[ |
|
3717 |
names := nameSpace isNameSpace ifTrue:[nameSpace keys] ifFalse:[nameSpace privateClasses collect:[:c | c nameWithoutPrefix]]. |
|
3718 |
names := names select:[:nm | nm isUppercaseFirst ]. |
|
3719 |
names := names reject:[:nm | nm includes:$:]. |
|
3720 |
addWithFactorBlock value:names value:(2.5 * globalFactor). |
|
3721 |
]. |
|
4559 | 3722 |
]. |
5076 | 3723 |
isLeftSideOfAssignment ifFalse:[ "/ no, we will not suggest assigning to pool vars |
3724 |
"/ pool variables |
|
3725 |
classOrNil theNonMetaclass sharedPoolNames do:[:poolName | |
|
3726 |
|pool names| |
|
3727 |
||
3728 |
pool := Smalltalk at:poolName. |
|
3729 |
pool isNil ifTrue:[ |
|
3730 |
Transcript showCR:'non existent pool: ',poolName |
|
3731 |
] ifFalse:[ |
|
3732 |
names := pool classVarNames. |
|
3733 |
addWithFactorBlock value:names value:(2.5 * globalFactor). |
|
3734 |
] |
|
3735 |
]. |
|
4559 | 3736 |
]. |
5076 | 3737 |
]. |
3738 |
||
5551 | 3739 |
isLeftSideOfAssignment ifFalse:[ |
5076 | 3740 |
"/ globals |
3741 |
names := OrderedCollection new. |
|
3742 |
Smalltalk keysDo:[:k | |
|
5784 | 3743 |
|val| |
3744 |
||
5076 | 3745 |
(k isUppercaseFirst |
3746 |
and:[ (k startsWith:'Undeclared:::') not |
|
3747 |
and:[ ((k includes:$:) and:[ (k includesString:'::') not]) not ]]) ifTrue:[ |
|
5784 | 3748 |
"/ ignore obsolete classes |
3749 |
val := Smalltalk classNamed:k. |
|
3750 |
(val isBehavior and:[val isObsolete]) ifFalse:[ |
|
3751 |
names add:k |
|
3752 |
] |
|
4559 | 3753 |
] |
3754 |
]. |
|
5551 | 3755 |
|
5076 | 3756 |
"/ only consider all globals, if the first char of the completed name is uppercase; |
3757 |
"/ otherwise, only consider names with a caseInsensitve prefix match |
|
5552 | 3758 |
nm isUppercaseFirst ifFalse:[ |
5076 | 3759 |
names := names select:[:globalName | globalName asLowercase startsWith: nm]. |
3760 |
]. |
|
3761 |
addWithFactorBlock value:names value:(1.5 * globalFactor). |
|
4665 | 3762 |
]. |
4559 | 3763 |
]. |
3764 |
||
5551 | 3765 |
isLeftSideOfAssignment ifFalse:[ |
5076 | 3766 |
"/ pseudos - assuming that thisContext is seldom used. |
3767 |
"/ also assuming, that nil is short so its usually typed in. |
|
3768 |
addWithFactorBlock value:#('self') value:(2.5 * localFactor). |
|
3769 |
addWithFactorBlock value:#('nil') value:(0.5 * localFactor). |
|
3770 |
addWithFactorBlock value:#('super' 'false' 'true') value:(2 * localFactor). |
|
3771 |
addWithFactorBlock value:#('thisContext') value:(1 * localFactor). |
|
3772 |
]. |
|
4238 | 3773 |
]. |
3774 |
||
3775 |
allDistances isEmpty ifTrue:[^ self]. |
|
3776 |
||
4410 | 3777 |
"/ nope (foo := foo + 1) should be possible!! |
3778 |
"/ (parent notNil and:[parent isAssignment]) ifTrue:[ |
|
3779 |
"/ "/ remove the left side of the assignment (to avoid foo := foo suggestions) |
|
3780 |
"/ |i| |
|
3781 |
"/ |
|
3782 |
"/ i := allDistances findFirst:[:entry | entry key = parent variable name]. |
|
3783 |
"/ i ~~ 0 ifTrue:[ |
|
3784 |
"/ allDistances removeAtIndex:i |
|
3785 |
"/ ]. |
|
3786 |
"/ ]. |
|
4385 | 3787 |
|
4238 | 3788 |
bestAssoc := allDistances at:1. |
3789 |
bestAssoc := allDistances inject:bestAssoc into:[:el :best | el value > best value |
|
4559 | 3790 |
ifTrue:[el] |
3791 |
ifFalse:[best] |
|
3792 |
]. |
|
4548 | 3793 |
|
3794 |
allDistances sort:[:a :b | |
|
4559 | 3795 |
a value > b value ifTrue:[ |
3796 |
true |
|
3797 |
] ifFalse:[ |
|
3798 |
a value = b value ifTrue:[ |
|
3799 |
a key < b key |
|
3800 |
] ifFalse:[ |
|
3801 |
false |
|
3802 |
] |
|
3803 |
] |
|
3804 |
]. |
|
4352 | 3805 |
|
3806 |
allTheBest := allDistances. |
|
3807 |
||
4238 | 3808 |
nameIsOK ifTrue:[ |
4559 | 3809 |
"/ if the name already exists, only allow longer names, if there are |
5360 | 3810 |
longerNames := allTheBest select:[:assoc | (assoc key startsWith:nm) or:[assoc key endsWith:nm]]. |
5033 | 3811 |
longerNames size < 30 ifTrue:[ |
3812 |
longerNames := allTheBest select:[:assoc | assoc key includesString:nm caseSensitive:false]. |
|
3813 |
]. |
|
5040 | 3814 |
longerNames isEmpty ifTrue:[ |
3815 |
"/ no better name |
|
3816 |
^ self |
|
4559 | 3817 |
]. |
5040 | 3818 |
allTheBest := longerNames. |
4238 | 3819 |
]. |
5040 | 3820 |
|
5033 | 3821 |
allTheBest size > 20 ifTrue:[ |
3822 |
allTheBest := allTheBest copyTo:20. |
|
3823 |
"/ "/ remove all those which are below some threshold or are a prefix |
|
3824 |
"/ 0.2 to:0.9 by:0.1 do:[:delta | |
|
3825 |
"/ |bestValue n| |
|
5551 | 3826 |
"/ |
5033 | 3827 |
"/ "/ if still too many, remove more and more |
3828 |
"/ allTheBest size > 50 ifTrue:[ |
|
3829 |
"/ bestValue := bestAssoc value * delta. |
|
3830 |
"/ n := allTheBest select:[:entry | (entry key startsWith:nm) or:[ entry value >= bestValue ]]. |
|
3831 |
"/ n size >= 15 ifTrue:[ allTheBest := n ]. |
|
3832 |
"/ ] |
|
3833 |
"/ ]. |
|
3834 |
"/ allTheBest size > 20 ifTrue:[ |
|
3835 |
"/ "/ remove all those which are below some threshold |
|
3836 |
"/ 0.2 to:0.9 by:0.1 do:[:delta | |
|
3837 |
"/ |bestValue n| |
|
3838 |
"/ "/ if still too many, remove more and more |
|
3839 |
"/ allTheBest size > 20 ifTrue:[ |
|
3840 |
"/ bestValue := bestAssoc value * delta. |
|
3841 |
"/ allTheBest := allTheBest select:[:entry | entry value >= bestValue ]. |
|
3842 |
"/ ] |
|
3843 |
"/ ]. |
|
3844 |
"/ ]. |
|
4238 | 3845 |
]. |
3846 |
suggestions := allTheBest collect:[:assoc | assoc key]. |
|
3847 |
||
4352 | 3848 |
"/ finally, the trick is to bring them into a reasonable order... |
3849 |
"/ sort the prefix matchers by length, the others by spelling distance |
|
3850 |
"/ and bring the prefix-matchers towards the beginning |
|
3851 |
suggestions := ((suggestions select:[:s | s startsWith:nm]) sort:[:a :b | a size < b size ]) |
|
4559 | 3852 |
, |
3853 |
(suggestions reject:[:s | s startsWith:nm]). |
|
4352 | 3854 |
|
4417 | 3855 |
"/ if super is among them, add a full call to the completions |
3856 |
(suggestions includes:'super') ifTrue:[ |
|
4559 | 3857 |
(tree notNil |
3858 |
and:[ tree isMethod ]) ifTrue:[ |
|
3859 |
Error handle:[:ex | |
|
3860 |
Transcript showCR:'parse error in code completion ignored'. |
|
3861 |
] do:[ |
|
3862 |
suggestions addFirst:('super ',(Parser methodSpecificationForSelector:tree selector argNames:(tree argumentNames)),'.'). |
|
3863 |
] |
|
3864 |
]. |
|
4417 | 3865 |
]. |
4238 | 3866 |
|
5551 | 3867 |
suggestionsWithInfo := |
3868 |
suggestions |
|
4620 | 3869 |
collect:[:eachName | |
5040 | 3870 |
|val kind valAndKind printString| |
3871 |
||
3872 |
valAndKind := self valueAndKindOfVariable:eachName. |
|
3873 |
valAndKind isNil ifTrue:[ |
|
4620 | 3874 |
eachName |
3875 |
] ifFalse:[ |
|
5040 | 3876 |
val := valAndKind first. |
3877 |
kind := valAndKind second. |
|
3878 |
||
3879 |
val isBehavior ifTrue:[ |
|
3880 |
val isLoaded ifFalse:[ |
|
5090 | 3881 |
eachName,' (= ', ('autoloaded class in ',(val category ? 'unknown category')) allItalic,' )' |
5040 | 3882 |
] ifTrue:[ |
3883 |
val isNameSpace ifTrue:[ |
|
3884 |
eachName,' ( ', 'namespace' allItalic,' )' |
|
3885 |
] ifFalse:[ |
|
5090 | 3886 |
eachName,' (= ', ('class in ',(val category ? 'unknown category')) allItalic,' )' |
5040 | 3887 |
] |
3888 |
] |
|
3889 |
] ifFalse:[ |
|
3890 |
"/ Parser findBest:30 selectorsFor:'isLite' in:nil forCompletion:true |
|
3891 |
||
3892 |
(val isLiteral and:[ (printString := val printString) size < 15 ]) ifTrue:[ |
|
5090 | 3893 |
eachName,' (= ',printString allItalic,' )' |
5040 | 3894 |
] ifFalse:[ |
5370 | 3895 |
eachName,' (= ',val class nameWithArticle allItalic,' )' |
5040 | 3896 |
]. |
3897 |
]. |
|
4620 | 3898 |
]. |
3899 |
]. |
|
3900 |
||
5081 | 3901 |
editActions := suggestions collect:[:word | |
5080 | 3902 |
self editActionToReplaceNode:node by:word. |
5551 | 3903 |
]. |
5080 | 3904 |
actionBlock value:suggestionsWithInfo value:editActions value:nil. |
4238 | 3905 |
|
3906 |
"Created: / 10-11-2006 / 13:16:33 / cg" |
|
3907 |
"Modified: / 16-02-2010 / 10:13:13 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
5551 | 3908 |
"Modified (comment): / 12-06-2017 / 12:34:46 / mawalch" |
5552 | 3909 |
"Modified: / 22-06-2017 / 06:56:42 / cg" |
6022 | 3910 |
"Modified: / 03-03-2019 / 22:51:33 / Claus Gittinger" |
5041 | 3911 |
! |
3912 |
||
5080 | 3913 |
editActionToInsert:aString |
3914 |
^ [:index | |
|
3915 |
codeView |
|
3916 |
undoableDo:[ |
|
3917 |
codeView insertSelectedStringAtCursor:aString. |
|
3918 |
codeView dontReplaceSelectionOnInput |
|
3919 |
] |
|
3920 |
info:'Completion'. |
|
3921 |
]. |
|
3922 |
! |
|
3923 |
||
3924 |
editActionToInsertFromSuggestions:suggestions |
|
3925 |
^ [:index | |
|
3926 |
|answer| |
|
3927 |
||
3928 |
answer := suggestions at:index. |
|
3929 |
codeView |
|
3930 |
undoableDo:[ |
|
3931 |
codeView insertSelectedStringAtCursor:answer. |
|
3932 |
codeView dontReplaceSelectionOnInput |
|
3933 |
] |
|
3934 |
info:'Completion'. |
|
3935 |
]. |
|
3936 |
! |
|
3937 |
||
3938 |
editActionToReplaceCodeFrom:start to:stop by:aString |
|
3939 |
^ [:index | |
|
3940 |
|oldVar oldLen newLen insertWithSpace| |
|
3941 |
||
3942 |
insertWithSpace := false. |
|
3943 |
||
3944 |
start <= stop ifTrue:[ |
|
3945 |
oldVar := (codeView textFromCharacterPosition:start to:stop) asString string withoutSeparators. |
|
3946 |
] ifFalse:[ |
|
3947 |
codeView characterBeforeCursor == Character space ifTrue:[ |
|
3948 |
insertWithSpace := true. |
|
3949 |
]. |
|
3950 |
]. |
|
3951 |
||
3952 |
oldLen := stop - start + 1. |
|
3953 |
newLen := aString size. |
|
3954 |
||
3955 |
codeView |
|
3956 |
undoableDo:[ |
|
3957 |
insertWithSpace ifTrue:[ |
|
3958 |
codeView insertSelectedStringAtCursor:aString |
|
3959 |
] ifFalse:[ |
|
3960 |
codeView replaceFromCharacterPosition:start to:stop with:aString. |
|
3961 |
||
3962 |
(aString startsWith:oldVar) ifTrue:[ |
|
3963 |
codeView selectFromCharacterPosition:start+oldLen to:start+newLen-1. |
|
3964 |
] ifFalse:[ |
|
3965 |
codeView selectFromCharacterPosition:start to:start+newLen-1. |
|
3966 |
]. |
|
3967 |
]. |
|
3968 |
codeView dontReplaceSelectionOnInput |
|
3969 |
] |
|
3970 |
info:'Completion'. |
|
3971 |
||
3972 |
]. |
|
3973 |
||
3974 |
"Created: / 01-05-2016 / 18:47:40 / cg" |
|
3975 |
! |
|
3976 |
||
5041 | 3977 |
editActionToReplaceCodeFrom:start to:stop byWordIn:suggestions |
3978 |
^ [:index | |
|
3979 |
|answer oldVar oldLen newLen insertWithSpace| |
|
3980 |
||
3981 |
insertWithSpace := false. |
|
3982 |
||
3983 |
answer := suggestions at:index. |
|
3984 |
start <= stop ifTrue:[ |
|
3985 |
oldVar := (codeView textFromCharacterPosition:start to:stop) asString string withoutSeparators. |
|
3986 |
] ifFalse:[ |
|
3987 |
codeView characterBeforeCursor == Character space ifTrue:[ |
|
3988 |
insertWithSpace := true. |
|
3989 |
]. |
|
3990 |
]. |
|
3991 |
||
3992 |
oldLen := stop - start + 1. |
|
3993 |
newLen := answer size. |
|
3994 |
||
3995 |
codeView |
|
3996 |
undoableDo:[ |
|
3997 |
insertWithSpace ifTrue:[ |
|
3998 |
codeView insertSelectedStringAtCursor:answer |
|
3999 |
] ifFalse:[ |
|
4000 |
codeView replaceFromCharacterPosition:start to:stop with:answer. |
|
4001 |
||
4002 |
(answer startsWith:oldVar) ifTrue:[ |
|
4003 |
codeView selectFromCharacterPosition:start+oldLen to:start+newLen-1. |
|
4004 |
] ifFalse:[ |
|
4005 |
codeView selectFromCharacterPosition:start to:start+newLen-1. |
|
4006 |
]. |
|
4007 |
]. |
|
4008 |
codeView dontReplaceSelectionOnInput |
|
4009 |
] |
|
4010 |
info:'Completion'. |
|
4011 |
||
4012 |
]. |
|
4013 |
||
4014 |
"Created: / 01-05-2016 / 18:47:40 / cg" |
|
4015 |
! |
|
4016 |
||
5080 | 4017 |
editActionToReplaceNode:node by:word |
4018 |
^ self editActionToReplaceCodeFrom:node start to:node stop by:word |
|
4019 |
||
4020 |
"Created: / 01-05-2016 / 18:44:09 / cg" |
|
4021 |
! |
|
4022 |
||
5041 | 4023 |
editActionToReplaceNode:node byWordIn:suggestions |
5114 | 4024 |
^ self editActionToReplaceCodeFrom:(node start) to:(node stop) byWordIn:suggestions |
5041 | 4025 |
|
4026 |
"Created: / 01-05-2016 / 18:44:09 / cg" |
|
4238 | 4027 |
! |
4028 |
||
5112 | 4029 |
findBest:node for:selector inClasses:srchClassesArg |
5606 | 4030 |
"find the best suggestions for a partial selector in a given set of classes. |
4031 |
Notice: the returned collection is unsorted; it needs some postprocessing to |
|
4032 |
present the most reasonable items first" |
|
4033 |
||
5112 | 4034 |
|srchClasses bestSelectors |
4035 |
allMessagesSentToVariable classesImplementingAllMessages| |
|
4036 |
||
4037 |
srchClasses := srchClassesArg. |
|
4038 |
Verbose == true ifTrue:[ |
|
4039 |
Transcript show:'node: '; showCR:node. |
|
4040 |
Transcript show:'srchClasses: '; showCR:srchClasses. |
|
4041 |
]. |
|
4042 |
||
4043 |
srchClasses isEmptyOrNil ifTrue:[ |
|
4044 |
node isVariable ifTrue:[ |
|
4045 |
allMessagesSentToVariable := Set new. |
|
4046 |
rememberedNodes do:[:eachNode | |
|
4047 |
eachNode allMessageNodesDo:[:eachMessage | |
|
4048 |
|msgReceiver msgSelector| |
|
4049 |
||
4050 |
(msgReceiver := eachMessage receiver) isVariable ifTrue:[ |
|
4051 |
msgReceiver name = node name ifTrue:[ |
|
4052 |
(msgSelector := eachMessage selector) ~= selector ifTrue:[ |
|
4053 |
allMessagesSentToVariable add:msgSelector |
|
4054 |
] |
|
4055 |
] |
|
4056 |
] |
|
4057 |
] |
|
4058 |
]. |
|
4059 |
allMessagesSentToVariable notEmpty ifTrue:[ |
|
4060 |
"/ consider classes which implement all those messages. |
|
4061 |
classesImplementingAllMessages := Smalltalk allImplementorsOf:(allMessagesSentToVariable first). |
|
4062 |
allMessagesSentToVariable do:[:eachSelector | |
|
4063 |
classesImplementingAllMessages := classesImplementingAllMessages |
|
4064 |
select:[:cls | cls implements:eachSelector]. |
|
4065 |
]. |
|
4066 |
srchClasses := classesImplementingAllMessages. |
|
4067 |
]. |
|
4068 |
]. |
|
4069 |
]. |
|
4070 |
bestSelectors := Set new. |
|
5514 | 4071 |
(srchClasses isEmptyOrNil or:[srchClasses size > 100]) ifTrue:[ |
5112 | 4072 |
bestSelectors addAll:( Parser findBest:50 selectorsFor:selector in:nil forCompletion:true ). |
4073 |
Verbose == true ifTrue:[ |
|
4074 |
Transcript show:'bestSelectors (1): '; showCR:bestSelectors. |
|
4075 |
]. |
|
5514 | 4076 |
] ifFalse:[ |
5112 | 4077 |
srchClasses do:[:srchClass | |
4078 |
|bestForThisClass| |
|
4079 |
||
4080 |
bestForThisClass := Parser findBest:50 selectorsFor:selector in:srchClass forCompletion:true. |
|
4081 |
bestForThisClass := self |
|
4082 |
withoutSelectorsUnlikelyFor:srchClass |
|
4083 |
from:bestForThisClass |
|
4084 |
forPartial:selector. |
|
4085 |
Verbose == true ifTrue:[ |
|
4086 |
Transcript show:'bestSelectors (2): '; showCR:bestForThisClass. |
|
4087 |
]. |
|
4088 |
bestSelectors addAll:bestForThisClass. |
|
4089 |
]. |
|
4090 |
]. |
|
4091 |
"/ remove the already typed-in selector itself, in case. |
|
4092 |
bestSelectors remove:selector ifAbsent:[]. |
|
4093 |
bestSelectors := bestSelectors asOrderedCollection. |
|
4094 |
^ bestSelectors |
|
5514 | 4095 |
|
4096 |
"Modified: / 13-03-2017 / 18:07:28 / cg" |
|
5606 | 4097 |
"Modified (comment): / 15-09-2017 / 11:00:01 / cg" |
5112 | 4098 |
! |
4099 |
||
4238 | 4100 |
findNodeForInterval:interval in:source |
5272 | 4101 |
^ self class findNodeForInterval:interval in:source |
4238 | 4102 |
|
4103 |
"Modified: / 06-07-2011 / 12:42:53 / cg" |
|
4104 |
! |
|
4105 |
||
4106 |
findNodeForInterval:interval in:source allowErrors:allowErrors |
|
4107 |
^ self |
|
5542 | 4108 |
findNodeForInterval:interval in:source allowErrors:allowErrors |
4109 |
mustBeMethod:false |
|
4238 | 4110 |
|
4111 |
"Modified: / 16-09-2011 / 14:52:28 / cg" |
|
4112 |
! |
|
4113 |
||
4114 |
findNodeForInterval:interval in:source allowErrors:allowErrors mustBeMethod:mustBeMethod |
|
4115 |
"if mustBeMethod is true, do not try a regular expression (as in a workspace)." |
|
4116 |
||
4352 | 4117 |
^ self |
5542 | 4118 |
findNodeForInterval:interval in:source allowErrors:allowErrors |
4119 |
mustBeMethod:mustBeMethod mustBeExpression:false |
|
4352 | 4120 |
! |
4121 |
||
4122 |
findNodeForInterval:interval in:source allowErrors:allowErrors mustBeMethod:mustBeMethod mustBeExpression:mustBeExpression |
|
5070 | 4123 |
"parse source, and find the node which is in the given interval |
4124 |
(typically a selection or a word in the source). |
|
5040 | 4125 |
|
4126 |
parse it as expression or method; |
|
4127 |
if mustBeMethod is true, do not try as expression; |
|
4128 |
if mustBeExpression is true, do not try as method |
|
4129 |
expression syntax parsing is done in workspaces (doIt). |
|
4130 |
||
4131 |
Big hack as workaround a limitation of RBParser: |
|
4132 |
in case of an error, the parent chain of a node is usually not yet set. |
|
4133 |
(because the code is written as: |
|
4134 |
parentNode addChild:(self parseChild) |
|
4135 |
and the parent-chain of the parsed child is set in addChild). |
|
5070 | 4136 |
However: |
5040 | 4137 |
when doing code completion, having invalid syntax to parse is the normal case. |
5070 | 4138 |
|
5040 | 4139 |
Workaround: |
5070 | 4140 |
remember created nodes as the parse proceeds. |
4141 |
Thus, I can construct a partial the parent chain. |
|
5040 | 4142 |
" |
4367 | 4143 |
|
4548 | 4144 |
|intersectingNodes smallestIntersectingNode firstIntersectingNode |
4145 |
lastIntersectingNode onErrorBlock |
|
4373 | 4146 |
nodeGenerationHook parserClass parser currentScopeNodes bestNode| |
4238 | 4147 |
|
5900 | 4148 |
partialString := nil. |
4238 | 4149 |
interval isEmpty ifTrue: [^ nil]. |
5040 | 4150 |
|
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
4151 |
languageOrNil notNil ifTrue:[ |
4934 | 4152 |
parserClass := languageOrNil parserClass. |
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
4153 |
] ifFalse:[ |
4934 | 4154 |
classOrNil notNil ifTrue:[ |
4155 |
parserClass := classOrNil programmingLanguage parserClass. |
|
4156 |
] |
|
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
4157 |
]. |
4548 | 4158 |
parserClass notNil ifTrue:[ |
4934 | 4159 |
"/ hack |
4160 |
parserClass == Parser ifTrue: [ |
|
4161 |
parserClass := RBParser. |
|
4162 |
]. |
|
4373 | 4163 |
] ifFalse:[ |
4934 | 4164 |
parserClass := RBParser. |
4373 | 4165 |
]. |
4166 |
parserClass isNil ifTrue: [^ nil]. |
|
4238 | 4167 |
|
4361 | 4168 |
rememberedScopeNodes := nil. |
4434 | 4169 |
rememberedNodes := OrderedCollection new. |
4361 | 4170 |
|
4238 | 4171 |
"/ LastSource := nil. |
4172 |
source = LastSource ifTrue:[ |
|
4934 | 4173 |
tree := LastParseTree. |
4174 |
tokens := LastScanTokens. |
|
4238 | 4175 |
] ifFalse:[ |
4934 | 4176 |
intersectingNodes := OrderedCollection new. |
4177 |
currentScopeNodes := IdentitySet new. |
|
5900 | 4178 |
|
4934 | 4179 |
onErrorBlock := |
5900 | 4180 |
[:str :err :nodesSoFarFromParser | |
4181 |
|nodes nodesSoFar partialStringStart partialStringEnd| |
|
4182 |
||
4183 |
nodesSoFar := nodesSoFarFromParser. |
|
4184 |
((str includesString:'Unmatched') and:[str includesString:' in string literal']) ifTrue:[ |
|
5910 | 4185 |
parser isNil ifTrue:[ |
4186 |
"/ happesn during init of parser, when the first step is done before |
|
4187 |
"/ parser is set (and returned) - sigh. |
|
5911 | 4188 |
^ nil |
5910 | 4189 |
]. |
5900 | 4190 |
partialStringStart := parser scanner tokenStart. |
4191 |
partialStringEnd := parser scanner currentPosition. |
|
4192 |
(source at:partialStringStart) == $' ifTrue:[partialStringStart := partialStringStart + 1]. |
|
4193 |
partialString := source copyFrom:partialStringStart to:partialStringEnd. |
|
4194 |
partialStringInterval := partialStringStart to:partialStringEnd. |
|
4195 |
"/ error is reported by scanner - no nodesSoFar passed in |
|
4196 |
nodesSoFar := parser nodesSoFar. |
|
5901 | 4197 |
^ nodesSoFar detectMax:[:node | node endPosition ? -1] |
5900 | 4198 |
]. |
4934 | 4199 |
allowErrors ifTrue:[ |
4200 |
rememberedScopeNodes := currentScopeNodes. |
|
4201 |
firstIntersectingNode notNil ifTrue:[ |
|
4202 |
^ firstIntersectingNode |
|
4203 |
]. |
|
4204 |
nodesSoFar notNil ifTrue:[ |
|
4205 |
nodes := nodesSoFar asOrderedCollection |
|
4206 |
collect:[:nd | nd whichNodeIntersects:interval] |
|
4207 |
thenSelect:[:nd | nd notNil ]. |
|
4208 |
nodes size == 1 ifTrue:[ |
|
4209 |
^ nodes first |
|
4210 |
]. |
|
4211 |
] |
|
4212 |
]. |
|
4213 |
nil |
|
4214 |
]. |
|
4215 |
||
5658 | 4216 |
self debuggingCodeFor:#DWIM is:[ |
4934 | 4217 |
Transcript show:'looking for: '; showCR:interval. |
4218 |
]. |
|
4219 |
||
4220 |
nodeGenerationHook := |
|
4221 |
[:node | |
|
4222 |
rememberedNodes add:node. |
|
4223 |
||
4224 |
"/ would like to return here as soon as the node has been created by the parser; |
|
4225 |
"/ however, at that time, its parent(chain) is not yet created and so we might not know |
|
4226 |
"/ what the semantic interpretation (especially: scope of variable) will be. |
|
4227 |
"/ therefore, we parse all, and return the found node at the end. |
|
4228 |
(node isMethod or:[node isBlock or:[node isSequence]]) ifTrue:[ |
|
4229 |
currentScopeNodes add:node. |
|
4230 |
] ifFalse:[ |
|
5658 | 4231 |
self debuggingCodeFor:#DWIM is:[ |
4232 |
node isMessage ifTrue:[ |
|
4233 |
Transcript show:node; show:' '; show:node start; show:'->'; showCR:node stop. |
|
4234 |
]. |
|
4235 |
]. |
|
4934 | 4236 |
|
4237 |
(node intersectsInterval:interval) ifTrue:[ |
|
5658 | 4238 |
self debuggingCodeFor:#DWIM is:[ |
4934 | 4239 |
Transcript showCR:'yes'. |
4240 |
]. |
|
4241 |
intersectingNodes add:node. |
|
4242 |
firstIntersectingNode isNil ifTrue:[ |
|
4243 |
firstIntersectingNode := lastIntersectingNode := smallestIntersectingNode := node |
|
4244 |
] ifFalse:[ |
|
4245 |
|lenNode lenSmallest| |
|
4246 |
||
4247 |
lenNode := (node stop - node start). |
|
4248 |
lenSmallest := (smallestIntersectingNode stop - smallestIntersectingNode start). |
|
4249 |
lenNode < lenSmallest ifTrue:[ |
|
4250 |
smallestIntersectingNode := node. |
|
4251 |
]. |
|
4252 |
node start > lastIntersectingNode start ifTrue:[ |
|
4253 |
lastIntersectingNode := node. |
|
4254 |
]. |
|
4255 |
]. |
|
4256 |
]. |
|
4257 |
]. |
|
4258 |
]. |
|
4259 |
||
4260 |
"/ one of the big problems when using the RBParser here is |
|
4261 |
"/ that it behaves badly when a syntax error is encountered; |
|
4262 |
"/ for example, a node's parent is usually set AFTER the children are |
|
4263 |
"/ completely parsed (for example, a blockNode gets the parent-method only |
|
4264 |
"/ after parsing). Thus, when an error is encountered, we cannot walk |
|
4265 |
"/ the parent chain, and therefore will not see the outer locals/args of |
|
4266 |
"/ an inner scope (allVariablesOnScope returns only a partial set). |
|
4267 |
"/ A walkaround is to remember Method/Block nodes as created in the above node generation. |
|
4268 |
"/ The disadvantage of it is that we do not have correct scope information, until the node's |
|
4269 |
"/ parent gets set eventually, thus we might consider locals from sibling blocks. |
|
4270 |
"/ See rememberedScopeNodes handling above. |
|
4271 |
"/ Those other nodes are only remembered for failed parses; |
|
4272 |
"/ if the parse is ok, rememberedScopeNodes will be nil. |
|
4273 |
||
4274 |
mustBeExpression ifFalse:[ |
|
4275 |
tree := parserClass |
|
4276 |
parseMethod: source |
|
4277 |
setup:[:p | |
|
4278 |
parser := p. |
|
4279 |
p rememberNodes:true. |
|
4280 |
p rememberTokens:true. |
|
4281 |
p nodeGenerationCallback:nodeGenerationHook |
|
4282 |
] |
|
4283 |
onError: onErrorBlock. |
|
4284 |
parser notNil ifTrue:[ tokens := parser rememberedTokens ]. |
|
4285 |
]. |
|
4286 |
||
4287 |
mustBeMethod ifTrue:[ |
|
4288 |
"/ only cache parsed methods |
|
4289 |
tree notNil ifTrue:[ |
|
4290 |
LastSource := source. |
|
4291 |
LastParseTree := tree. |
|
4292 |
LastScanTokens := tokens. |
|
4293 |
]. |
|
4294 |
] ifFalse:[ |
|
4295 |
(tree isNil or:[firstIntersectingNode isNil]) ifTrue:[ |
|
4296 |
"/ try as an expression |
|
4297 |
tree := parserClass |
|
4298 |
parseExpression: source |
|
4299 |
setup:[:p | |
|
4300 |
parser := p. |
|
4301 |
p rememberNodes:true. |
|
4302 |
p rememberTokens:true. |
|
4303 |
p nodeGenerationCallback:nodeGenerationHook |
|
4304 |
] |
|
4305 |
onError: onErrorBlock. |
|
4306 |
parser notNil ifTrue:[ tokens := parser rememberedTokens ]. |
|
4307 |
]. |
|
4308 |
]. |
|
4309 |
lastIntersectingNode notNil ifTrue:[ |
|
5658 | 4310 |
self debuggingCodeFor:#DWIM is:[ |
4934 | 4311 |
Transcript show:'last: '; showCR:lastIntersectingNode. |
4312 |
]. |
|
4313 |
^ lastIntersectingNode |
|
4314 |
]. |
|
4315 |
"/ firstIntersectingNode notNil ifTrue:[ ^ firstIntersectingNode ]. |
|
4238 | 4316 |
]. |
4317 |
||
4367 | 4318 |
bestNode := self findNodeForInterval:interval inParseTree:tree. |
5658 | 4319 |
self debuggingCodeFor:#DWIM is:[ |
4934 | 4320 |
Transcript show:'best: '; showCR:bestNode. |
4367 | 4321 |
]. |
4322 |
^ bestNode |
|
4238 | 4323 |
|
4324 |
"Created: / 16-09-2011 / 14:52:08 / cg" |
|
4407
59eecb68b4fb
class: DoWhatIMeanSupport
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
4392
diff
changeset
|
4325 |
"Modified: / 18-09-2013 / 16:47:13 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
5040 | 4326 |
"Modified (comment): / 01-05-2016 / 10:05:10 / cg" |
5911 | 4327 |
"Modified: / 22-09-2018 / 09:40:57 / Claus Gittinger" |
4238 | 4328 |
! |
4329 |
||
4330 |
findNodeForInterval:interval inParseTree:parseTree |
|
4331 |
|node| |
|
4332 |
||
4333 |
interval isEmpty ifTrue: [^ nil]. |
|
4334 |
parseTree isNil ifTrue:[^ nil]. |
|
4335 |
||
4336 |
node := parseTree whichNodeIsContainedBy:interval. |
|
4337 |
node isNil ifTrue:[ |
|
5542 | 4338 |
node := parseTree whichNodeIntersects:interval. |
4339 |
node isNil ifTrue: [ |
|
4340 |
node := self findNodeIn:parseTree forInterval:interval |
|
4341 |
]. |
|
4238 | 4342 |
]. |
4343 |
^ node |
|
4344 |
||
4345 |
"Modified: / 10-11-2006 / 13:13:58 / cg" |
|
4346 |
! |
|
4347 |
||
5272 | 4348 |
findNodeIn:aTree forInterval:anInterval |
4349 |
^ self class findNodeIn:aTree forInterval:anInterval |
|
4238 | 4350 |
|
4351 |
"Modified: / 20-11-2006 / 12:31:12 / cg" |
|
4352 |
! |
|
4353 |
||
4421 | 4354 |
insertAdditonalStuffAfterSelector:chosenCompletion |
4355 |
|optionalExtraSpace| |
|
4356 |
||
4548 | 4357 |
optionalExtraSpace := (codeView characterAfterCursor isSeparator) |
5080 | 4358 |
ifTrue:[''] |
4359 |
ifFalse:[' ']. |
|
4421 | 4360 |
|
4361 |
( |
|
5080 | 4362 |
#( |
4363 |
'ifTrue:' 'ifFalse:' 'ifTrue:ifFalse:' 'ifFalse:ifTrue:' |
|
4364 |
'and:' 'or:' 'timesRepeat:' 'whileTrue:' 'whileFalse:' |
|
4365 |
) includes:chosenCompletion |
|
4421 | 4366 |
) ifTrue:[ |
5080 | 4367 |
codeView insertStringAtCursor:('[',optionalExtraSpace,']'). |
4368 |
"/ codeView cursorLeft:1+extra size. |
|
4421 | 4369 |
]. |
4370 |
( |
|
5080 | 4371 |
#( |
4372 |
'collect:' 'select:' 'reject:' 'do:' |
|
4373 |
) includes:chosenCompletion |
|
4421 | 4374 |
) ifTrue:[ |
5080 | 4375 |
codeView insertStringAtCursor:('[:each | ]',optionalExtraSpace). |
4376 |
codeView cursorLeft:1+optionalExtraSpace size. |
|
4421 | 4377 |
]. |
4378 |
( |
|
5080 | 4379 |
#( |
4380 |
'contains:' 'findFirst:' 'detect:' |
|
4381 |
) includes:chosenCompletion |
|
4421 | 4382 |
) ifTrue:[ |
5080 | 4383 |
codeView insertStringAtCursor:('[:some | ]',optionalExtraSpace). |
4384 |
codeView cursorLeft:1+optionalExtraSpace size. |
|
4421 | 4385 |
]. |
4386 |
( |
|
5080 | 4387 |
#( |
4388 |
'remove:ifAbsent:' 'detect:ifNone:' |
|
4389 |
) includes:chosenCompletion |
|
4421 | 4390 |
) ifTrue:[ |
5080 | 4391 |
codeView insertStringAtCursor:('[]',optionalExtraSpace). |
4392 |
codeView cursorLeft:1+optionalExtraSpace size. |
|
4421 | 4393 |
]. |
4394 |
! |
|
4395 |
||
5043 | 4396 |
messagesSentTo:varName in:aTree |
5080 | 4397 |
|messagesToReceiver collector| |
4398 |
||
4399 |
collector := |
|
4400 |
[:node | |
|
5043 | 4401 |
(node isMessage |
4402 |
and:[node receiver isVariable |
|
4403 |
and:[node receiver name = varName]]) ifTrue:[ |
|
4404 |
messagesToReceiver add:(node selector) |
|
4405 |
]. |
|
4406 |
]. |
|
5080 | 4407 |
|
4408 |
"/ collect messages sent |
|
4409 |
messagesToReceiver := Set new. |
|
4410 |
"/ remembered nodes is nonNil if parser aborted with error |
|
4411 |
rememberedNodes notNil ifTrue:[ |
|
4412 |
rememberedNodes do:collector. |
|
5043 | 4413 |
] ifFalse:[ |
5080 | 4414 |
tree allMessageNodesDo:collector. |
5043 | 4415 |
]. |
4416 |
^ messagesToReceiver |
|
4417 |
! |
|
4418 |
||
4238 | 4419 |
old_askUserForCompletion:what for:codeView from:allTheBest |
4420 |
|list resources choice lastChoice| |
|
4421 |
||
4422 |
allTheBest isEmpty ifTrue:[ ^ nil ]. |
|
4423 |
allTheBest size == 1 ifTrue:[ ^ allTheBest first ]. |
|
4424 |
||
4425 |
list := allTheBest. |
|
4426 |
LastChoices notNil ifTrue:[ |
|
5542 | 4427 |
lastChoice := LastChoices at:what ifAbsent:nil. |
4428 |
lastChoice notNil ifTrue:[ |
|
4429 |
"/ move tha last choice to the top of the list, if it is in. |
|
4430 |
(list includes: lastChoice) ifTrue:[ |
|
4431 |
(list indexOf: lastChoice) < 10 ifTrue:[ |
|
4432 |
list := {lastChoice allBold } , (list copyWithout:lastChoice). |
|
4433 |
] |
|
4434 |
] |
|
4435 |
]. |
|
4238 | 4436 |
]. |
4437 |
||
4438 |
list size < 30 ifTrue:[ |
|
5542 | 4439 |
|menu idx exitKey| |
4440 |
||
4441 |
menu := PopUpMenu labels:list. |
|
4442 |
menu hideOnKeyFilter:[:key | |hide| |
|
4443 |
hide := ( #( CursorDown CursorUp Escape Return ) includes: key) not. |
|
4444 |
hide ifTrue:[ |
|
4445 |
exitKey := key. |
|
4446 |
]. |
|
4447 |
hide]. |
|
4448 |
menu memorizeLastSelection:3 "sigh, not 1 because of heading!!". |
|
4449 |
idx := menu startUpWithHeading:'Choose ',what. |
|
4450 |
idx == 0 ifTrue:[ |
|
4451 |
exitKey notNil ifTrue:[ |
|
4452 |
codeView keyPress:exitKey x:0 y:0. |
|
4453 |
]. |
|
4454 |
^ nil |
|
4455 |
]. |
|
4456 |
choice := list at:idx. |
|
4238 | 4457 |
] ifFalse:[ |
5542 | 4458 |
resources := codeView application isNil |
4459 |
ifTrue:[ codeView resources] |
|
4460 |
ifFalse:[ codeView application resources ]. |
|
4461 |
||
4462 |
choice := Dialog |
|
4463 |
choose:(resources string:'Choose ',what) |
|
4464 |
fromList:list |
|
4465 |
lines:20 |
|
4466 |
initialSelection:(list firstIfEmpty:nil) |
|
4467 |
title:(resources string:'Code completion'). |
|
4468 |
choice isNil ifTrue:[^ nil]. |
|
4238 | 4469 |
]. |
4301 | 4470 |
choice := choice string. |
4238 | 4471 |
|
4472 |
LastChoices isNil ifTrue:[ |
|
5542 | 4473 |
LastChoices := Dictionary new. |
4238 | 4474 |
]. |
4475 |
LastChoices at:what put:choice. |
|
4476 |
^ choice |
|
4477 |
||
4478 |
"Created: / 16-02-2010 / 09:38:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
4346 | 4479 |
"Modified: / 28-08-2013 / 15:28:01 / cg" |
4238 | 4480 |
! |
4481 |
||
5043 | 4482 |
sortSelectors:list forSelector:selector lcSelector:lcSelector |
4483 |
list sort: [:a :b | |
|
4484 |
|aBeforeB| |
|
4485 |
||
5613 | 4486 |
"/ (a startsWith:'sho') ifTrue:[ |
4487 |
"/ (b startsWith:'sho') ifTrue:[ |
|
4488 |
"/ self halt. |
|
4489 |
"/ ]. |
|
4490 |
"/ ]. |
|
4491 |
||
5043 | 4492 |
(a startsWith:selector) ifTrue:[ |
5613 | 4493 |
(b startsWith:selector) ifTrue:[ |
4494 |
aBeforeB := (a size < b size) or:[a < b] |
|
4495 |
] ifFalse:[ |
|
5043 | 4496 |
aBeforeB := true |
5613 | 4497 |
] |
5043 | 4498 |
] ifFalse:[ |
4499 |
(b startsWith:selector) ifTrue:[ |
|
4500 |
aBeforeB := false |
|
4501 |
] |
|
4502 |
]. |
|
4503 |
aBeforeB isNil ifTrue:[ |
|
4504 |
aBeforeB := a asLowercase < b asLowercase. |
|
4505 |
(a asLowercase startsWith:lcSelector) ifTrue:[ |
|
4506 |
(b asLowercase startsWith:lcSelector) ifFalse:[ |
|
4507 |
aBeforeB := true |
|
4508 |
] |
|
4509 |
] ifFalse:[ |
|
4510 |
(b asLowercase startsWith:lcSelector) ifTrue:[ |
|
4511 |
aBeforeB := false |
|
4512 |
] |
|
4513 |
]. |
|
4514 |
]. |
|
4515 |
aBeforeB |
|
4516 |
]. |
|
5613 | 4517 |
|
4518 |
"Modified: / 30-09-2017 / 14:10:06 / cg" |
|
5043 | 4519 |
! |
4520 |
||
5040 | 4521 |
sortUsefulSelectorsIn:selectorList |
4522 |
"/ cosmetics: |
|
4523 |
"/ ifTrue / whileTrue should come before ifFalse/whileFalse |
|
4524 |
#( |
|
4525 |
ifTrue: ifFalse: |
|
4526 |
ifTrue:ifFalse: ifFalse:ifTrue: |
|
4527 |
whileTrue: whileFalse: |
|
4528 |
whileTrue whileFalse |
|
4529 |
whileTrue: whileTrue |
|
4530 |
whileFalse: whileFalse |
|
4531 |
new: basicNew: |
|
4532 |
new basicNew |
|
4533 |
) pairWiseDo:[:sel1 :sel2 | |
|
4534 |
|idx1 idx2| |
|
4535 |
||
4536 |
(idx1 := selectorList indexOf:sel1) ~~ 0 ifTrue:[ |
|
4537 |
(idx2 := selectorList indexOf:sel2) ~~ 0 ifTrue:[ |
|
4538 |
idx1 > idx2 ifTrue:[ |
|
4539 |
selectorList swap:idx1 with:idx2 |
|
4540 |
] |
|
4541 |
] |
|
4542 |
]. |
|
4543 |
]. |
|
4544 |
||
4545 |
"Created: / 01-05-2016 / 17:48:02 / cg" |
|
4546 |
! |
|
4547 |
||
5044 | 4548 |
splitSelectorList:list by:condition |
4549 |
|part1 part2 newList| |
|
4550 |
||
4551 |
part1 := list select:condition. |
|
4552 |
part2 := list reject:condition. |
|
4553 |
part1 isEmpty ifTrue:[ |
|
4554 |
newList := part2. |
|
4555 |
] ifFalse:[ |
|
4556 |
part2 isEmpty ifTrue:[ |
|
4557 |
newList := part1. |
|
4558 |
] ifFalse:[ |
|
4559 |
newList := part1 , part2. |
|
4560 |
] |
|
4561 |
]. |
|
4562 |
^ newList |
|
4563 |
! |
|
4564 |
||
4238 | 4565 |
treeForCode:source allowErrors:allowErrors |
4566 |
|tree| |
|
4567 |
||
4568 |
source = LastSource ifTrue:[ |
|
5542 | 4569 |
tree := LastParseTree. |
4238 | 4570 |
] ifFalse:[ |
5542 | 4571 |
tree := RBParser |
4572 |
parseMethod:source |
|
4573 |
onError: [:str :err :nodesSoFar :parserOrNil| |
|
4574 |
allowErrors ifTrue:[ |
|
4575 |
"/ parserOrNil isNil if raised by the scanner |
|
4576 |
parserOrNil notNil ifTrue:[ |
|
4577 |
^ parserOrNil currentMethodNode |
|
4578 |
] |
|
4579 |
]. |
|
4580 |
^ nil |
|
4581 |
] |
|
4582 |
proceedAfterError:false |
|
4583 |
rememberNodes:true. |
|
4584 |
||
4585 |
tree notNil ifTrue:[ |
|
4586 |
LastSource := source. |
|
4587 |
LastParseTree := tree. |
|
4588 |
] |
|
4238 | 4589 |
]. |
4590 |
^ tree |
|
4591 |
||
4592 |
"Modified: / 13-01-2012 / 11:54:30 / cg" |
|
4352 | 4593 |
! |
4594 |
||
4524 | 4595 |
tryCodeCompletionWithSource:source nodeInterval:interval at:characterPositionOfCursor mustBeExpression:mustBeExpression into:actionBlock |
4548 | 4596 |
"this is tried multiple times; |
4931 | 4597 |
first with cursor line only |
4598 |
then with the source copied up to the cursor position, |
|
4599 |
then with the full source. |
|
4381 | 4600 |
Either one may give better results (for example, when completing |
4352 | 4601 |
after a keyword selector, and the remaining code would lead to a syntactically |
4602 |
legal, but stupid message send to be parsed... |
|
4603 |
(which happens often after inserting)" |
|
4604 |
||
5040 | 4605 |
|node nodeParent checkedNode characterBeforeCursor| |
4352 | 4606 |
|
5505 | 4607 |
characterPositionOfCursor < 1 ifTrue:[^ self]. |
4608 |
||
4352 | 4609 |
"/ this is too naive and stupid; if there is a syntactic error, |
4610 |
"/ we will not find a node for a long time (stepping back more and more, |
|
4611 |
"/ until reaching the beginning). This leads to a thousand and more times reparsing |
|
4612 |
"/ without any progress. |
|
4613 |
"/ TODO: do it vice-versa, in that the parser does a callOut for every node generated |
|
4614 |
"/ as it parses the code. Stop, when the interval is hit. |
|
4615 |
"/ that will also work for syntactic incorrect source code. |
|
4524 | 4616 |
(mustBeExpression not and:[methodOrNil notNil or:[classOrNil notNil]]) ifTrue:[ |
4931 | 4617 |
node := self findNodeForInterval:interval in:source allowErrors:true mustBeMethod:true. |
5040 | 4618 |
]. |
4352 | 4619 |
node isNil ifTrue:[ |
4931 | 4620 |
node := self findNodeForInterval:interval in:source allowErrors:true mustBeMethod:false mustBeExpression:true. |
4621 |
node isNil ifTrue:[ |
|
4622 |
"/ Transcript showCR:'No parseNode found (syntax error before or in comment?)'. |
|
4623 |
self information:'No parseNode found (syntax error before or in comment?)'. |
|
4624 |
^ self. |
|
4625 |
]. |
|
4352 | 4626 |
]. |
5900 | 4627 |
|
4628 |
"/ if we are in a partial string, |
|
4629 |
"/ try the special string-expansion hook (hook for expecco) |
|
4630 |
partialString notNil ifTrue:[ |
|
4631 |
self codeCompletionForLiteralString:partialString node:node in:source considerAll:false into:actionBlock. |
|
4632 |
^ self. |
|
4633 |
]. |
|
4634 |
||
5106 | 4635 |
"/ (source startsWith:'sel') ifTrue:[self halt]. |
4544 | 4636 |
nodeParent := node parent. |
4352 | 4637 |
|
4638 |
(node isVariable |
|
4544 | 4639 |
and:[ nodeParent notNil |
4640 |
and:[ nodeParent isMessage |
|
4368 | 4641 |
and:[ node stop < (characterPositionOfCursor-1) ]]]) ifTrue:[ |
4931 | 4642 |
node := nodeParent. |
4643 |
nodeParent := node parent. |
|
4352 | 4644 |
]. |
5508 | 4645 |
(characterPositionOfCursor-1 max:1) > source size ifTrue:[^ self]. |
4525 | 4646 |
characterBeforeCursor := source at:(characterPositionOfCursor-1 max:1). "/ codeView characterBeforeCursor. |
4352 | 4647 |
characterBeforeCursor isNil ifTrue:[ "at begin of line" ^ self]. |
4358 | 4648 |
characterBeforeCursor == $. ifTrue:[ "at end of statement" ^ self]. |
5040 | 4649 |
|
4352 | 4650 |
node isVariable ifTrue:[ |
5040 | 4651 |
|nodeIsInTemporaries nodeIsInBlockArguments nodeIsInMethodArguments | |
4652 |
||
4931 | 4653 |
nodeIsInTemporaries := |
4654 |
nodeParent notNil |
|
4655 |
and:[ nodeParent isSequence |
|
4656 |
and:[ nodeParent temporaries notEmptyOrNil |
|
5040 | 4657 |
and:[ node stop <= nodeParent temporaries last stop ]]]. |
4658 |
||
4659 |
nodeIsInBlockArguments := |
|
4660 |
node blockScope notNil |
|
4661 |
and:[ node blockScope arguments notEmptyOrNil |
|
4662 |
and:[ node stop <= node blockScope arguments last stop ]]. |
|
4663 |
||
4664 |
(nodeIsInBlockArguments not and:[rememberedScopeNodes notNil]) ifTrue:[ |
|
4665 |
"/ sigh - parent (and therefore blockScope) is unknown if parser has error |
|
4666 |
nodeIsInBlockArguments := |
|
4667 |
rememberedScopeNodes |
|
4668 |
contains:[:scope | |
|
4669 |
(scope isMethod or:[scope isBlock]) |
|
4670 |
and:[scope arguments notEmpty |
|
4671 |
and:[scope arguments first start <= node start |
|
4672 |
and:[scope arguments last stop >= node stop]]]. |
|
4673 |
]. |
|
4674 |
]. |
|
5106 | 4675 |
nodeIsInTemporaries ifTrue:[ self codeCompletionForTempVariable:node into:actionBlock. ^ self ]. |
4676 |
nodeIsInBlockArguments ifTrue:[ self codeCompletionForBlockArgument:node into:actionBlock. ^ self ]. |
|
5040 | 4677 |
|
4678 |
"/ for variable completion, cursor must be right after the node |
|
4679 |
codeView characterPositionOfCursor = (node stop + 1) ifTrue:[ |
|
4680 |
codeView characterBeforeCursor ~= Character space ifTrue:[ |
|
4931 | 4681 |
self codeCompletionForVariable:node into:actionBlock. |
5035 | 4682 |
^ self. |
5040 | 4683 |
]. |
5043 | 4684 |
]. |
5523 | 4685 |
"/ if right after a global, which is a class, look for implemented instance creation methods; offer them |
4686 |
node isGlobal ifTrue:[ |
|
4687 |
|class instCreators selectors| |
|
4688 |
||
4689 |
(class := Smalltalk classNamed:node name) notNil ifTrue:[ |
|
4690 |
(instCreators := class theMetaclass methodsInCategory:'instance creation') notEmpty ifTrue:[ |
|
4691 |
selectors := instCreators collect:[:m | m selector]. |
|
4692 |
actionBlock |
|
4693 |
value:selectors |
|
4694 |
value:(selectors collect:[:sel | self editActionToInsert:sel]) |
|
4695 |
value:'instance creation'. |
|
4696 |
]. |
|
4697 |
]. |
|
4698 |
] |
|
5040 | 4699 |
]. |
4700 |
||
5035 | 4701 |
false ifTrue:[ |
4702 |
codeView characterPositionOfCursor = (node stop + 2) ifTrue:[ |
|
5040 | 4703 |
|classes cls| |
5035 | 4704 |
"/ after a variable; |
4705 |
"/ offer local messages, if receiver type is known |
|
4706 |
classes := (self classesOfNode:node). |
|
4707 |
classes notEmptyOrNil ifTrue:[ |
|
4708 |
classes size > 1 ifTrue:[ |
|
4709 |
cls := classes anElement. |
|
4710 |
] ifFalse:[ |
|
4711 |
cls := Behavior commonSuperclassOf:classes. |
|
4712 |
] |
|
4713 |
]. |
|
4714 |
cls notNil ifTrue:[ |
|
4715 |
|clsSelectors moreSelectors| |
|
4716 |
||
4717 |
"/ completion in a message-send |
|
4718 |
clsSelectors := cls methodDictionary keys. "/ Parser findBest:50 selectorsFor:'' in:cls forCompletion:true. |
|
4719 |
clsSelectors size < 30 ifTrue:[ |
|
4720 |
cls superclass notNil ifTrue:[ |
|
4721 |
moreSelectors := cls superclass methodDictionary keys. |
|
4722 |
clsSelectors size + moreSelectors size < 30 ifTrue:[ |
|
4723 |
clsSelectors := clsSelectors , moreSelectors. |
|
4724 |
]. |
|
4725 |
]. |
|
4726 |
]. |
|
4727 |
"/ self codeCompletionForMessage:checkedNode into:actionBlock. |
|
4728 |
actionBlock value:clsSelectors value:nil value:nil. |
|
4729 |
^ self. |
|
4730 |
] |
|
4731 |
] |
|
4732 |
]. |
|
4367 | 4733 |
|
4352 | 4734 |
node isLiteral ifTrue:[ |
4931 | 4735 |
"/ however, user may want to complete a symbol inside a literal array!! |
4736 |
node value isArray ifTrue:[ |
|
4737 |
node token isLiteralArray ifTrue:[ |
|
4738 |
|elementBeforeCursor searcher| |
|
4739 |
||
4740 |
elementBeforeCursor := node token value detect:[:anElementToken | characterPositionOfCursor == (anElementToken stop + 1)] ifNone:nil. |
|
4741 |
elementBeforeCursor isNil ifTrue:[ |
|
4742 |
searcher := |
|
4743 |
[:tok :check | |
|
4744 |
tok isLiteralArray ifTrue:[ |
|
4745 |
tok value inject:nil into:[:found :el | found ifNil:[searcher value:el value:check]] |
|
4746 |
] ifFalse:[ |
|
4747 |
(check value:tok) ifTrue:[tok] ifFalse:[nil] |
|
4748 |
] |
|
4749 |
]. |
|
4750 |
elementBeforeCursor := searcher value:node token value:[:anElementToken | characterPositionOfCursor == (anElementToken stop)]. |
|
4751 |
]. |
|
4752 |
(elementBeforeCursor notNil and:[ elementBeforeCursor value isSymbol ]) ifTrue:[ |
|
4753 |
self codeCompletionForLiteralSymbol:nil element:elementBeforeCursor considerAll:true into:actionBlock. |
|
4754 |
^ self. |
|
4755 |
]. |
|
4756 |
]. |
|
4757 |
]. |
|
4758 |
||
4759 |
"/ cursor must be right after the literal |
|
4760 |
characterPositionOfCursor == (node stop + 1) ifFalse:[ |
|
4761 |
^ self |
|
4762 |
]. |
|
4763 |
||
4764 |
node value isSymbol ifTrue:[ |
|
4765 |
self codeCompletionForLiteralSymbol:node element:nil considerAll:false into:actionBlock. |
|
4766 |
^ self. |
|
4767 |
]. |
|
4768 |
||
4769 |
"/ huh - completing strings, numbers or what? |
|
4770 |
(nodeParent notNil |
|
4771 |
and:[ nodeParent isMessage |
|
4772 |
and:[ nodeParent isKeyword ]]) |
|
4773 |
ifFalse:[ |
|
4774 |
^ self |
|
4775 |
]. |
|
4776 |
"/ no, move up and try completing the outer keyword message (next arg) |
|
4777 |
node := nodeParent. |
|
4778 |
nodeParent := node parent. |
|
4352 | 4779 |
]. |
4780 |
||
5050 | 4781 |
Verbose == true ifTrue:[ |
4782 |
Transcript show:'node is '. |
|
4783 |
Error ignoreIn:[ Transcript show:node ]. |
|
4784 |
Transcript cr. |
|
4785 |
]. |
|
4786 |
||
5080 | 4787 |
"/ if in a keyword-argument position... |
4788 |
node isMessage ifTrue:[ |
|
4789 |
"/ where are we? |
|
5379 | 4790 |
node selectorString last == $: "node selector isSymbol" ifTrue:[ |
5080 | 4791 |
characterBeforeCursor == $: ifTrue:[ |
4792 |
|argIdx senders implementors receiverClasses selectorUpToCursor implementorOfSelectorUpToCursor| |
|
4793 |
"/ about to enter an argument? |
|
4794 |
argIdx := node selectorParts keysAndValuesDetectKey:[:idx :part | |
|
4795 |
part stop == (codeView characterPositionOfCursor-1). |
|
4796 |
] ifNone:nil. |
|
4797 |
argIdx notNil ifTrue:[ |
|
4798 |
selectorUpToCursor := ((node selectorParts collect:#value) copyTo:argIdx) asStringWith:''. |
|
4799 |
implementors := Set new. |
|
4800 |
"/ find senders of this message, and see if they call it with a block argument |
|
4801 |
"/ this takes too long for a completion; |
|
4802 |
"/ Smalltalk allClassesDo:[:cls | |
|
4803 |
"/ cls instAndClassMethodsDo:[:m | |
|
4804 |
"/ (m sendsMessageForWhich:[:sel | sel startsWith:node selector]) ifTrue:[ |
|
4805 |
"/ senders add:m |
|
4806 |
"/ ]. |
|
4807 |
"/ ] |
|
4808 |
"/ ]. |
|
4809 |
"/ therefore, restrict to a max. of 5 classes |
|
4810 |
receiverClasses := self classesOfNode:node receiver. |
|
4811 |
(receiverClasses notEmptyOrNil and:[receiverClasses size <= 5]) ifTrue:[ |
|
4812 |
receiverClasses do:[:eachPossibleReceiverClass | |
|
4813 |
eachPossibleReceiverClass withAllSuperclassesDo:[:cls | |
|
4814 |
cls methodDictionary keysAndValuesDo:[:sel :mthd | |
|
4815 |
(sel startsWith:selectorUpToCursor) ifTrue:[ |
|
4816 |
implementors add:mthd. |
|
4817 |
(sel = selectorUpToCursor) ifTrue:[ |
|
4818 |
implementorOfSelectorUpToCursor := implementorOfSelectorUpToCursor ? mthd |
|
4819 |
]. |
|
4820 |
]. |
|
4821 |
]. |
|
4822 |
] |
|
4823 |
] |
|
4824 |
]. |
|
4825 |
implementorOfSelectorUpToCursor notNil ifTrue:[ |
|
4826 |
|tree argName| |
|
4827 |
||
5123 | 4828 |
SourceCodeManagerError handle:[:ex | |
4829 |
] do:[ |
|
5392 | 4830 |
tree := implementorOfSelectorUpToCursor parseTree. |
5462 | 4831 |
(tree notNil and:[tree argumentNames size >= argIdx]) ifTrue:[ |
5392 | 4832 |
argName := tree argumentNames at:argIdx. |
4833 |
(argName includesString:'block' caseSensitive:false) ifTrue:[ |
|
4834 |
actionBlock value:{'[ "',argName,'" ]'} |
|
4835 |
value:{ self editActionToInsert:('[ "',argName,'" ]') } |
|
4836 |
value:'block argument' |
|
4837 |
]. |
|
5123 | 4838 |
]. |
4839 |
]. |
|
5080 | 4840 |
] ifFalse:[ |
4841 |
implementors notEmpty ifTrue:[ |
|
5082 | 4842 |
self breakPoint:#cg. |
5080 | 4843 |
]. |
4844 |
]. |
|
4845 |
]. |
|
4846 |
]. |
|
5528 | 4847 |
]. |
5080 | 4848 |
]. |
4849 |
||
5043 | 4850 |
(node isVariable or:[node isBlock and:[node stop notNil]]) ifTrue:[ |
4851 |
(characterPositionOfCursor == (node stop + 1) |
|
4852 |
"/ hack (spaces at end of line) |
|
4853 |
or:[characterPositionOfCursor == (node stop)]) ifTrue:[ |
|
4854 |
codeView characterBeforeCursor == Character space ifTrue:[ |
|
4855 |
self codeCompletionForMessageTo:node into:actionBlock. |
|
4856 |
^ self |
|
4857 |
]. |
|
4858 |
]. |
|
4859 |
]. |
|
5040 | 4860 |
|
5114 | 4861 |
"/ characterBeforeCursor == $) ifTrue:[self halt]. |
5109 | 4862 |
|
4933 | 4863 |
"/ move outward, until we find a message-send node, |
4864 |
"/ or the method's selector pattern node. |
|
4352 | 4865 |
checkedNode := node. |
4866 |
[checkedNode notNil] whileTrue:[ |
|
5322 | 4867 |
(characterPositionOfCursor <= ((checkedNode stop ? source size)+1)) ifTrue:[ |
5041 | 4868 |
"/ Transcript show:'T: '; showCR:node. |
5048 | 4869 |
"/ Transcript showCR:('Inside a ',(checkedNode className)). |
5043 | 4870 |
"/ self information:('Inside a ',(checkedNode className)). |
5040 | 4871 |
(checkedNode isMessage |
4872 |
and:[characterPositionOfCursor < (checkedNode selectorParts first start)]) ifTrue:[ |
|
4873 |
self codeCompletionForMessageTo:checkedNode receiver into:actionBlock. |
|
4874 |
^ self |
|
4875 |
] |
|
4876 |
||
4931 | 4877 |
]. |
4878 |
||
4879 |
checkedNode isMessage ifTrue:[ |
|
4880 |
"/ completion in a message-send |
|
5040 | 4881 |
"/ Transcript showCR:'codeCompletionForMessage'. |
4931 | 4882 |
self codeCompletionForMessage:checkedNode into:actionBlock. |
4883 |
^ self |
|
4884 |
]. |
|
4885 |
checkedNode isMethod ifTrue:[ |
|
4886 |
"/ completion in a method's selector pattern |
|
5040 | 4887 |
"/ Transcript showCR:'codeCompletionForMethodSpec'. |
4931 | 4888 |
self codeCompletionForMethodSpec:checkedNode into:actionBlock. |
4889 |
^ self. |
|
4890 |
]. |
|
5040 | 4891 |
|
4892 |
||
4931 | 4893 |
checkedNode := checkedNode parent. |
4352 | 4894 |
]. |
4895 |
||
5040 | 4896 |
"/ Transcript showCR:'Node is neither variable nor message'. |
4352 | 4897 |
self information:'Node is neither variable nor message.'. |
4898 |
||
4899 |
"Modified: / 04-07-2006 / 18:48:26 / fm" |
|
5528 | 4900 |
"Modified: / 08-04-2017 / 16:01:14 / cg" |
5900 | 4901 |
"Modified: / 29-08-2018 / 16:43:14 / Claus Gittinger" |
5040 | 4902 |
! |
4903 |
||
5070 | 4904 |
withoutSelectorsUnlikelyFor:aClass from:selectorsArg forPartial:partialSelector |
4905 |
"some heuristics; |
|
4906 |
as best selectors has been chosen by implemented methods for aClass, |
|
4907 |
some of them should be filtered (for example, at:/at:put:, which are |
|
4908 |
found in object, but only make sense for variable objects or those which do |
|
4909 |
implement at:put: themself. |
|
4910 |
I have currently no better idea than hardcoding stuff I found irritating..." |
|
4911 |
||
4912 |
|selectors noNilChecks noIsXXXChecks noNoXXXChecks noBecome |
|
5112 | 4913 |
noIndexedSetters noIndexedGetters noSizeQueries docSelectors| |
5070 | 4914 |
|
4915 |
aClass isNil ifTrue:[ ^ selectorsArg ]. |
|
4916 |
||
4917 |
noNilChecks := noIsXXXChecks := noNoXXXChecks := noBecome := false. |
|
4918 |
noIndexedSetters := noIndexedGetters := noSizeQueries := false. |
|
4919 |
||
4920 |
selectors := (selectorsArg ? #()) asOrderedCollection. |
|
4921 |
||
4922 |
self tracePoint:#cg message:aClass. |
|
4923 |
||
5112 | 4924 |
aClass isMeta ifTrue:[ |
4925 |
docSelectors := #(copyright documentation examples |
|
4926 |
version version_CVS version_SVN version_HG). |
|
4927 |
selectors := selectors reject:[:sel | docSelectors includes:sel]. |
|
4928 |
]. |
|
4929 |
||
5070 | 4930 |
"/ actually meaning booleans here |
4931 |
(aClass == True or:[aClass == False]) ifTrue:[ |
|
4932 |
noNilChecks := noBecome := true. |
|
4933 |
(partialSelector startsWith:'is') ifFalse:[ noIsXXXChecks := true ]. |
|
4934 |
(partialSelector startsWith:'no') ifFalse:[ noNoXXXChecks := true ]. |
|
4935 |
]. |
|
4936 |
||
4937 |
(aClass includesBehavior: ArithmeticValue) ifTrue:[ noNilChecks := true ]. |
|
4938 |
(aClass includesBehavior: Symbol) ifTrue:[ noNilChecks := noBecome := noIndexedSetters := true ]. |
|
4939 |
(aClass includesBehavior: Number) ifTrue:[ noBecome := true ]. |
|
4940 |
(aClass includesBehavior: Block) ifTrue:[ noNilChecks := noIsXXXChecks := noNoXXXChecks := noBecome := true ]. |
|
4941 |
||
4942 |
(aClass isMeta) ifTrue:[ |
|
4943 |
noNilChecks := noBecome := true. |
|
4944 |
"/ remove messages which are only defined in Object and non-meta classes. |
|
4945 |
selectors := selectors reject: |
|
4946 |
[:sel | |
|
4947 |
(Object implements:sel) |
|
4948 |
and:[ (Smalltalk allImplementorsOf:sel) conform:[:impl | impl isMeta not]] |
|
4949 |
]. |
|
4950 |
]. |
|
4951 |
||
4952 |
aClass isVariable ifFalse:[ |
|
4953 |
noIndexedGetters := noIndexedSetters := noSizeQueries := true. |
|
4954 |
]. |
|
4955 |
||
4956 |
noIndexedSetters ifTrue:[ |
|
4957 |
#( #'at:put:' #'basicAt:put:') do:[:indexAccessSelector | |
|
4958 |
(aClass whichClassIncludesSelector:indexAccessSelector) == Object ifTrue:[ |
|
4959 |
selectors := selectors copyWithout:indexAccessSelector. |
|
4960 |
]. |
|
4961 |
]. |
|
4962 |
]. |
|
4963 |
noIndexedGetters ifTrue:[ |
|
4964 |
#( #'at:' #'basicAt:') do:[:indexAccessSelector | |
|
4965 |
(aClass whichClassIncludesSelector:indexAccessSelector) == Object ifTrue:[ |
|
4966 |
selectors := selectors copyWithout:indexAccessSelector. |
|
4967 |
]. |
|
4968 |
]. |
|
4969 |
]. |
|
4970 |
noSizeQueries ifTrue:[ |
|
4971 |
#( #size #basicSize ) do:[:indexAccessSelector | |
|
4972 |
(aClass whichClassIncludesSelector:indexAccessSelector) == Object ifTrue:[ |
|
4973 |
selectors := selectors copyWithout:indexAccessSelector. |
|
4974 |
]. |
|
4975 |
]. |
|
4976 |
]. |
|
4977 |
||
4978 |
noNilChecks ifTrue:[ |
|
4979 |
selectors removeAllFoundIn:#( |
|
4980 |
'isNil' 'notNil' |
|
4981 |
'ifNil:' 'ifNotNil:' 'ifNil:ifNotNil:' 'ifNotNil:ifNil:' 'ifNotNilDo:' |
|
4982 |
'ifEmpty:' 'ifNotEmpty:' 'ifNotEmptyDo:' 'ifEmpty:ifNotEmpty:' |
|
4983 |
'ifEmpty:ifNotEmptyDo:' 'ifNotEmptyDo:ifEmpty:' 'ifEmptyDo:ifNotEmpty:' |
|
4984 |
). |
|
4985 |
]. |
|
4986 |
noIsXXXChecks ifTrue:[ |
|
4987 |
"/ get rid of all isXXX selectors |
|
4988 |
selectors := selectors reject:[:sel | sel startsWith:'is']. |
|
4989 |
]. |
|
4990 |
noNoXXXChecks ifTrue:[ |
|
4991 |
"/ get rid of all notXXX selectors |
|
4992 |
selectors := selectors reject:[:sel | sel startsWith:'no']. |
|
4993 |
]. |
|
4994 |
noBecome ifTrue:[ |
|
4995 |
"/ get rid of all become* selectors |
|
4996 |
selectors := selectors reject:[:sel | sel startsWith:'become']. |
|
4997 |
selectors remove:#oneWayBecome: ifAbsent:[]. |
|
4998 |
selectors := selectors reject:[:sel | sel startsWith:'changeClassTo']. |
|
4999 |
]. |
|
5000 |
||
5001 |
"/ actually: directly implemented selectors are more likely, so move them to top |
|
5002 |
selectors := (selectors select:[:sel | aClass implements:sel]) |
|
5003 |
, |
|
5004 |
(selectors reject:[:sel | aClass implements:sel]). |
|
5005 |
||
5006 |
^ selectors |
|
5007 |
! ! |
|
5008 |
||
4238 | 5009 |
!DoWhatIMeanSupport methodsFor:'code completion-helpers-old'! |
5010 |
||
5011 |
codeCompletionForLiteralSymbol:node inClass:classOrNil codeView:codeView |
|
5012 |
|sym possibleCompletions best start stop oldLen newLen oldVar| |
|
5013 |
||
5014 |
sym := node value. |
|
5015 |
possibleCompletions := OrderedCollection new. |
|
5016 |
||
5017 |
Symbol allInstancesDo:[:existingSym | |
|
5542 | 5018 |
(existingSym startsWith:sym) ifTrue:[ |
5019 |
(existingSym = sym) ifFalse:[ |
|
5020 |
possibleCompletions add:existingSym |
|
5021 |
]. |
|
5022 |
]. |
|
4238 | 5023 |
]. |
5024 |
possibleCompletions sort. |
|
5025 |
||
5026 |
best := possibleCompletions longestCommonPrefix. |
|
5027 |
(best = sym or:[(possibleCompletions includes:best) not]) ifTrue:[ |
|
5542 | 5028 |
best := self askUserForCompletion:'symbol literal' for:codeView at: node start from:possibleCompletions. |
5029 |
best isNil ifTrue:[^ self]. |
|
4238 | 5030 |
]. |
5031 |
||
5032 |
"/ self showInfo:best. |
|
5033 |
||
5034 |
start := node start. |
|
5035 |
stop := node stop. |
|
5036 |
(codeView characterAtCharacterPosition:start) == $# ifTrue:[ |
|
5542 | 5037 |
start := start + 1. |
4238 | 5038 |
]. |
5039 |
(codeView characterAtCharacterPosition:start) == $' ifTrue:[ |
|
5542 | 5040 |
start := start + 1. |
5041 |
stop := stop - 1. |
|
4238 | 5042 |
]. |
5043 |
||
5044 |
oldVar := (codeView textFromCharacterPosition:start to:stop) asString string withoutSeparators. |
|
5045 |
||
5046 |
codeView |
|
5542 | 5047 |
undoableDo:[ codeView replaceFromCharacterPosition:start to:stop with:best ] |
5048 |
info:'Completion'. |
|
4238 | 5049 |
|
5050 |
(best startsWith:oldVar) ifTrue:[ |
|
5542 | 5051 |
oldLen := stop - start + 1. |
5052 |
newLen := best size. |
|
5053 |
codeView selectFromCharacterPosition:start+oldLen to:start+newLen-1. |
|
5054 |
codeView dontReplaceSelectionOnInput |
|
4238 | 5055 |
]. |
5056 |
||
5057 |
"Modified: / 16-02-2010 / 10:15:05 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
5058 |
"Modified (format): / 03-07-2011 / 15:58:45 / cg" |
|
5059 |
! |
|
5060 |
||
4345 | 5061 |
codeCompletionForMessage:node inClass:classOrNil instance:instanceOrNil context:contextOrNil codeView:codeView |
5044 | 5062 |
"going to be OBSOLETE" |
5063 |
||
4548 | 5064 |
|selector |
4547 | 5065 |
bestSelectors selector2 bestSelectors2 bestSelectorsFromRB allBest best numArgs |
4548 | 5066 |
newParts nSelParts oldLen newLen selectorParts |
4345 | 5067 |
findBest parentNode selectorInBest selector2InBest2 |
5044 | 5068 |
parser selectorsSentInCode selectorsImplementedInClass |
4346 | 5069 |
varName rbTypes rbType tryParent parentIsKeywordMessage parentIsBinaryMessage rcvrClass| |
4345 | 5070 |
|
5071 |
RefactoryTyper notNil ifTrue:[ |
|
4710 | 5072 |
"/ refactory package also provides a (very limited) typer; |
5073 |
"/ ask it for its oppinion as well (temporary - will vanish, once we have a better typer) |
|
5074 |
(node receiver isVariable) ifTrue:[ |
|
5075 |
varName := node receiver name. |
|
5076 |
varName isUppercaseFirst ifTrue:[ |
|
5077 |
] ifFalse:[ |
|
5078 |
tree := RBParser |
|
5079 |
parseMethod:codeView contents string |
|
5080 |
onError:[:aString :pos | nil]. |
|
5081 |
tree notNil ifTrue:[ |
|
5082 |
rbTypes := RefactoryTyper |
|
5083 |
classesFor: varName |
|
5084 |
in: tree |
|
5085 |
model: nil |
|
5086 |
ignoredSelectors:(Array with:node selector). |
|
6013 | 5087 |
rbTypes size ~~ 0 ifTrue:[ |
4710 | 5088 |
rbTypes size > 1 ifTrue:[ |
5089 |
rbTypes remove:ProtoObject ifAbsent:[]. |
|
5090 |
rbTypes remove:Autoload ifAbsent:[]. |
|
5091 |
rbTypes remove:ObsoleteObject ifAbsent:[]. |
|
5092 |
]. |
|
5093 |
rbTypes size == 1 ifTrue:[ |
|
5094 |
rbType := rbTypes first. |
|
5095 |
] ifFalse:[ |
|
5096 |
rbType := Class commonSuperclassOf:rbTypes |
|
5097 |
]. |
|
5098 |
(rbType notNil "and:[rbType ~= Object]") ifTrue:[ |
|
5099 |
bestSelectorsFromRB := Parser findBest:30 selectorsFor:node selector in:rbType forCompletion:true. |
|
5100 |
]. |
|
5101 |
] |
|
5102 |
] |
|
5103 |
] |
|
5104 |
]. |
|
4345 | 5105 |
]. |
5106 |
||
5107 |
classOrNil notNil ifTrue:[ |
|
4710 | 5108 |
parser := Parser parseMethod:codeView contents string in:classOrNil ignoreErrors:true ignoreWarnings:true. |
5109 |
selectorsSentInCode := parser messagesSent. |
|
4548 | 5110 |
]. |
4345 | 5111 |
classOrNil notNil ifTrue:[ |
4936 | 5112 |
selectorsImplementedInClass := Set new. |
5113 |
classOrNil withAllSuperclassesDo:[:cls | |
|
5114 |
cls theNonMetaclass ~~ Object ifTrue:[ |
|
5115 |
selectorsImplementedInClass addAll:cls selectors. |
|
5116 |
] |
|
5117 |
] |
|
4345 | 5118 |
]. |
5119 |
||
5120 |
findBest := [:node :selector | |
|
5035 | 5121 |
|srchClasses bestSelectors bestPrefixes| |
4710 | 5122 |
|
4933 | 5123 |
codeView withCursor:(Cursor questionMark) do:[ |
5035 | 5124 |
srchClasses := self classesOfNode:node receiver. |
5125 |
srchClasses notNil ifTrue:[ |
|
5126 |
bestSelectors := Set new. |
|
5127 |
srchClasses do:[:each | |
|
5128 |
bestSelectors addAll:(Parser findBest:30 selectorsFor:selector in:each forCompletion:true). |
|
5129 |
] |
|
4710 | 5130 |
] ifFalse:[ |
5131 |
bestSelectors := Parser findBest:30 selectorsFor:selector in:nil forCompletion:true. |
|
5132 |
]. |
|
5133 |
]. |
|
5134 |
||
5135 |
(bestSelectors includes:selector) ifTrue:[ |
|
5136 |
bestSelectors := bestSelectors select:[:sel | sel size > selector size]. |
|
5137 |
]. |
|
5138 |
bestSelectors |
|
4345 | 5139 |
]. |
5140 |
||
5141 |
selector := node selector. |
|
5142 |
bestSelectors := findBest value:node value:selector. |
|
5143 |
||
5144 |
parentNode := node parent. |
|
5145 |
||
5146 |
parentIsKeywordMessage := |
|
4710 | 5147 |
(parentNode notNil |
5148 |
and:[ parentNode isMessage |
|
5038 | 5149 |
and:[ parentNode selector isKeyword]]). |
4346 | 5150 |
parentIsBinaryMessage := |
4710 | 5151 |
(parentNode notNil |
5152 |
and:[ parentNode isMessage |
|
5153 |
and:[ parentNode selector isBinarySelector]]). |
|
4345 | 5154 |
|
5155 |
tryParent := false. |
|
5471 | 5156 |
"/ if it's a unary message AND the parent is a keyword node, look for parent completion too. |
4548 | 5157 |
(node selector isUnarySelector and:[ parentIsKeywordMessage ]) ifTrue:[ |
4710 | 5158 |
tryParent := true. |
4345 | 5159 |
] ifFalse:[ |
4710 | 5160 |
"/ if the parent is an instance creation message, take that as lookup class. |
5161 |
(node isMessage |
|
5162 |
and:[ node receiver isMessage |
|
5163 |
and:[ node receiver receiver isVariable |
|
5164 |
and:[ node receiver receiver name isUppercaseFirst |
|
5165 |
and:[ #(new new:) includes:(selector2 := node receiver selector) ]]]]) ifTrue:[ |
|
5166 |
rcvrClass := Smalltalk classNamed:(node receiver receiver name). |
|
5167 |
"/ srchClass2 := self lookupClassForMessage:parentNode inClass:classOrNil. |
|
5168 |
bestSelectors := Parser findBest:30 selectorsFor:selector in:rcvrClass forCompletion:true. |
|
5169 |
] ifFalse:[ |
|
5170 |
"/ also, if nothing was found |
|
5171 |
(bestSelectors isEmpty |
|
5172 |
and:[ parentNode notNil |
|
5173 |
and:[ parentNode isMessage ]]) ifTrue:[ |
|
5174 |
"/ srchClass2 := self lookupClassForMessage:parentNode inClass:classOrNil. |
|
5175 |
"/ selector2 := parentNode selector. |
|
5176 |
"/ selector2 := selector2,selector. |
|
5177 |
bestSelectors := findBest value:parentNode value:selector. |
|
5178 |
] |
|
5179 |
] |
|
4345 | 5180 |
]. |
5181 |
tryParent ifTrue:[ |
|
4710 | 5182 |
selector2 := parentNode selector,selector. |
5183 |
bestSelectors2 := findBest value:parentNode value:selector2. |
|
4345 | 5184 |
]. |
4346 | 5185 |
bestSelectorsFromRB notEmptyOrNil ifTrue:[ |
4710 | 5186 |
bestSelectors := bestSelectorsFromRB , (bestSelectors reject:[:sel | bestSelectorsFromRB includes:sel]). |
4346 | 5187 |
]. |
4345 | 5188 |
|
5189 |
"/ if the parent is a keyword selector, the child cannot |
|
4346 | 5190 |
(parentIsKeywordMessage or:[parentIsBinaryMessage]) ifTrue:[ |
5038 | 5191 |
bestSelectors := bestSelectors reject:[:sel | sel isKeyword] |
4345 | 5192 |
]. |
5193 |
||
5194 |
bestSelectors2 isEmptyOrNil ifTrue:[ |
|
4710 | 5195 |
allBest := bestSelectors. |
4345 | 5196 |
] ifFalse:[ |
4710 | 5197 |
bestSelectors isEmptyOrNil ifTrue:[ |
5198 |
allBest := bestSelectors2 |
|
5199 |
] ifFalse:[ |
|
5200 |
selectorInBest := (bestSelectors contains:[:sel | sel asLowercase startsWith:selector asLowercase]). |
|
5201 |
selector2InBest2 := (bestSelectors2 contains:[:sel | sel asLowercase startsWith:selector2 asLowercase]). |
|
5202 |
||
5203 |
(selectorInBest not and:[ selector2InBest2 ]) ifTrue:[ |
|
5204 |
"/ selector2 is more likely |
|
5205 |
allBest := bestSelectors2 |
|
5206 |
] ifFalse:[ |
|
5207 |
(selectorInBest and:[ selector2InBest2 not ]) ifTrue:[ |
|
5208 |
"/ selector more likely |
|
5209 |
allBest := bestSelectors |
|
5210 |
] ifFalse:[ |
|
5211 |
"/ assume same likelyness |
|
5212 |
||
5213 |
allBest := bestSelectors isEmpty |
|
5214 |
ifTrue:[ bestSelectors2 ] |
|
5215 |
ifFalse:[ bestSelectors , #(nil) , bestSelectors2 ]. |
|
5216 |
] |
|
5217 |
]. |
|
5218 |
]. |
|
4345 | 5219 |
]. |
5220 |
||
5221 |
allBest isEmptyOrNil ifTrue:[ ^ self ]. |
|
5222 |
||
5223 |
selectorsImplementedInClass notNil ifTrue:[ |
|
4710 | 5224 |
"/ the ones implemented in the receiver class are moved to the top of the list. |
5044 | 5225 |
allBest := self |
5226 |
splitSelectorList:allBest |
|
5227 |
by:[:sel | selectorsImplementedInClass includes:sel]. |
|
4345 | 5228 |
]. |
5229 |
selectorsSentInCode notNil ifTrue:[ |
|
4710 | 5230 |
"/ the ones already sent in the code are moved to the top of the list. |
5044 | 5231 |
allBest := self |
5232 |
splitSelectorList:allBest |
|
5233 |
by:[:sel | selectorsSentInCode includes:sel]. |
|
4345 | 5234 |
]. |
5235 |
||
5236 |
"/ the ones which are a prefix are moved towards the top of the list |
|
5044 | 5237 |
allBest := self |
5238 |
splitSelectorList:allBest |
|
5239 |
by:[:sel | sel notNil and:[sel startsWith:selector]]. |
|
4345 | 5240 |
|
5241 |
best := allBest first. |
|
5242 |
allBest size > 1 ifTrue:[ |
|
4710 | 5243 |
"allBest size < 20 ifTrue:[ |
5244 |
|idx| |
|
5245 |
||
5246 |
idx := (PopUpMenu labels:allBest) startUp. |
|
5247 |
idx == 0 ifTrue:[ ^ self]. |
|
5248 |
best := allBest at:idx. |
|
5249 |
] ifFalse:[ |
|
5250 |
allBest remove:nil ifAbsent:[]. |
|
5251 |
best := Dialog request:'Matching selectors:' initialAnswer:best list:allBest. |
|
5252 |
||
5253 |
]." |
|
5254 |
allBest remove:nil ifAbsent:[]. |
|
5255 |
best := self askUserForCompletion:('Selector for "%1"' bindWith:selector) for:codeView at: node selectorParts first start from:allBest. |
|
5256 |
best isEmptyOrNil ifTrue:[^ self]. |
|
5257 |
best = '-' ifTrue:[^ self]. |
|
4345 | 5258 |
]. |
5259 |
||
4547 | 5260 |
"/ srchClass notNil ifTrue:[ |
5261 |
"/ implClass := srchClass whichClassIncludesSelector:best. |
|
5262 |
"/ ] ifFalse:[ |
|
5263 |
"/ implClass := Smalltalk allClasses select:[:cls | (cls includesSelector:best) or:[cls class includesSelector:best]]. |
|
5264 |
"/ implClass size == 1 ifTrue:[ |
|
5265 |
"/ implClass := implClass first. |
|
5266 |
"/ ] ifFalse:[ |
|
5267 |
"/ implClass := nil |
|
5268 |
"/ ] |
|
5269 |
"/ ]. |
|
5270 |
"/ |
|
5271 |
"/ info := best storeString. |
|
5272 |
"/ implClass notNil ifTrue:[ |
|
6082 | 5273 |
"/ info := implClass name , ' » ' , info. |
4547 | 5274 |
"/ ]. |
5275 |
"/ self information:info. |
|
4345 | 5276 |
|
5277 |
best ~= selector ifTrue:[ |
|
4710 | 5278 |
numArgs := best numArgs. |
5279 |
||
5280 |
(bestSelectors2 notEmptyOrNil and:[bestSelectors2 includes:best]) ifTrue:[ |
|
5281 |
selectorParts := parentNode selectorParts , node selectorParts. |
|
5282 |
] ifFalse:[ |
|
5283 |
selectorParts := node selectorParts. |
|
5284 |
]. |
|
5285 |
nSelParts := selectorParts size. |
|
5286 |
||
5287 |
newParts := best asCollectionOfSubstringsSeparatedBy:$:. |
|
5288 |
newParts := newParts select:[:part | part size > 0]. |
|
5289 |
||
5290 |
codeView |
|
5291 |
undoableDo:[ |
|
5292 |
|newCursorPosition stop| |
|
5293 |
||
5294 |
numArgs > nSelParts ifTrue:[ |
|
5295 |
stop := selectorParts last stop. |
|
5296 |
||
5297 |
"/ append the rest ... |
|
5298 |
numArgs downTo:nSelParts+1 do:[:idx | |
|
5299 |
|newPart| |
|
5300 |
||
5301 |
newPart := newParts at:idx. |
|
5302 |
(best endsWith:$:) ifTrue:[ |
|
5303 |
newPart := newPart , ':' |
|
5304 |
]. |
|
5305 |
||
5306 |
(codeView characterAtCharacterPosition:stop) == $: ifFalse:[ |
|
5307 |
newPart := ':' , newPart. |
|
5308 |
]. |
|
5309 |
newPart := (codeView characterAtCharacterPosition:stop) asString , newPart. |
|
5310 |
||
5311 |
codeView replaceFromCharacterPosition:stop to:stop with:newPart. |
|
5312 |
newCursorPosition := stop + newPart size. |
|
5313 |
] |
|
5314 |
]. |
|
5315 |
||
5316 |
(nSelParts min:newParts size) downTo:1 do:[:idx | |
|
5317 |
|newPart oldPartialToken start stop nextChar| |
|
5318 |
||
5319 |
newPart := newParts at:idx. |
|
5320 |
oldPartialToken := selectorParts at:idx. |
|
5321 |
start := oldPartialToken start. |
|
5322 |
stop := oldPartialToken stop. |
|
5323 |
||
5038 | 5324 |
(best isKeyword) ifTrue:[ |
4710 | 5325 |
(oldPartialToken value endsWith:$:) ifTrue:[ |
5326 |
newPart := newPart , ':' |
|
5327 |
] ifFalse:[ |
|
5328 |
(codeView characterAtCharacterPosition:stop+1) == $: ifFalse:[ |
|
5329 |
newPart := newPart , ':' |
|
5330 |
] |
|
5331 |
] |
|
5332 |
] ifFalse:[ |
|
5333 |
(codeView characterAtCharacterPosition:stop) == $: ifTrue:[ |
|
5334 |
newPart := newPart , ':' |
|
5335 |
] ifFalse:[ |
|
5336 |
nextChar := codeView characterAtCharacterPosition:stop+1. |
|
5337 |
nextChar isSeparator ifFalse:[ |
|
5338 |
(').' includes:nextChar) ifFalse:[ |
|
5339 |
newPart := newPart , ' ' |
|
5340 |
]. |
|
5341 |
] |
|
5342 |
] |
|
4345 | 5343 |
"/ codeView replaceFromCharacterPosition:start to:stop with:(newPart , ':'). |
5344 |
"/ ] ifFalse:[ |
|
5345 |
"/ codeView replaceFromCharacterPosition:start to:stop with:newPart. |
|
4710 | 5346 |
]. |
5347 |
||
5348 |
codeView replaceFromCharacterPosition:start to:stop with:newPart. |
|
5349 |
||
5350 |
"/ codeView selectFromCharacterPosition:start+oldLen to:start+newLen-1. |
|
5351 |
newCursorPosition isNil ifTrue:[ |
|
5352 |
oldLen := stop - start + 1. |
|
5353 |
newLen := newPart size. |
|
5354 |
||
5355 |
newCursorPosition := stop + (newLen-oldLen) |
|
5356 |
]. |
|
5357 |
]. |
|
5358 |
||
5359 |
codeView cursorToCharacterPosition:newCursorPosition. |
|
5360 |
codeView cursorRight. "/ avoid going to the next line !! |
|
5361 |
||
5362 |
((best endsWith:':') and:[numArgs == 1]) ifTrue:[ |
|
5363 |
|impls impl| |
|
5364 |
||
5365 |
"/ see if it expects a block argument (heuristic) |
|
5366 |
best := best asSymbol. |
|
5367 |
(node notNil |
|
5368 |
and:[classOrNil notNil |
|
5369 |
and:[node receiver isSelf]]) ifTrue:[ |
|
5370 |
(impl := classOrNil whichClassImplements:best) isNil ifTrue:[ |
|
5371 |
impls := #(). |
|
5687 | 5372 |
Screen current beepInEditor. |
4710 | 5373 |
] ifFalse:[ |
5374 |
impls := { impl } |
|
5375 |
] |
|
5376 |
] ifFalse:[ |
|
5377 |
impls := Smalltalk allImplementorsOf:best. |
|
5378 |
]. |
|
5379 |
(impls contains:[:cls | |
|
5380 |
|argName| |
|
5381 |
||
5382 |
argName := ((cls compiledMethodAt:best) methodArgAndVarNames at:1) asLowercase. |
|
5383 |
(argName includesString:'block') or:[ (argName includesString:'action')]] |
|
5384 |
) ifTrue:[ |
|
5385 |
codeView insertStringAtCursor:'[' |
|
5386 |
]. |
|
5387 |
]. |
|
5388 |
codeView dontReplaceSelectionOnInput. |
|
5389 |
] |
|
5390 |
info:'Completion'. |
|
4345 | 5391 |
]. |
5392 |
||
5393 |
"Created: / 10-11-2006 / 13:18:27 / cg" |
|
5394 |
"Modified: / 16-02-2010 / 10:33:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
5038 | 5395 |
"Modified: / 30-04-2016 / 18:21:21 / cg" |
5471 | 5396 |
"Modified (comment): / 13-02-2017 / 20:37:55 / cg" |
6013 | 5397 |
"Modified: / 01-03-2019 / 14:53:00 / Claus Gittinger" |
4345 | 5398 |
! |
5399 |
||
4238 | 5400 |
codeCompletionForVariable:node inClass:classOrNil codeView:codeView |
4277 | 5401 |
|parent nonMetaClass crsrPos nm |
4238 | 5402 |
allVariables allDistances best nodeVal |
5403 |
char start stop oldLen newLen oldVar |
|
4485 | 5404 |
getDistanceComputeBlockWithWeight addWithFactorBlock allTheBest bestAssoc |
4238 | 5405 |
globalFactor localFactor selectorOfMessageToNode tree implementors argIdx namesUsed kwPart| |
5406 |
||
5407 |
classOrNil notNil ifTrue:[ |
|
5253 | 5408 |
nonMetaClass := classOrNil theNonMetaclass. |
4238 | 5409 |
]. |
5410 |
||
5411 |
nm := node name. |
|
5412 |
||
5413 |
"/ if we are behind the variable and a space has already been entered, |
|
5414 |
"/ the user is probably looking for a message selector. |
|
5415 |
"/ If the variable represents a global, present its instance creation messages |
|
5416 |
crsrPos := codeView characterPositionOfCursor. |
|
5417 |
char := codeView characterAtCharacterPosition:crsrPos-1. |
|
5418 |
char isSeparator ifTrue:[ |
|
5253 | 5419 |
classOrNil isNil ifTrue:[ |
5420 |
nodeVal := Smalltalk at:nm asSymbol. |
|
5421 |
] ifFalse:[ |
|
5422 |
nodeVal := classOrNil topNameSpace at:nm asSymbol ifAbsent:[Smalltalk at:nm asSymbol]. |
|
5423 |
]. |
|
5424 |
nodeVal isBehavior ifTrue:[ |
|
5425 |
|methods menu exitKey idx| |
|
5426 |
||
5427 |
methods := nodeVal class methodDictionary values |
|
6022 | 5428 |
select:[:m | |
5429 |
|cat| |
|
5430 |
cat := m category. |
|
5431 |
cat notNil |
|
5432 |
and:[cat sameAs:'instance creation'] |
|
5253 | 5433 |
]. |
5434 |
||
5435 |
menu := PopUpMenu labels:(methods collect:[:each | each selector]). |
|
5436 |
menu hideOnKeyFilter:[:key | |hide| |
|
5437 |
hide := ( #( CursorDown CursorUp Escape Return ) includes: key) not. |
|
5438 |
hide ifTrue:[ |
|
5439 |
exitKey := key. |
|
5440 |
]. |
|
5441 |
hide]. |
|
5442 |
||
5443 |
idx := menu startUp. |
|
5444 |
idx == 0 ifTrue:[ |
|
5445 |
exitKey notNil ifTrue:[ |
|
5446 |
codeView keyPress:exitKey x:0 y:0. |
|
5447 |
]. |
|
5448 |
^ self |
|
5449 |
]. |
|
5450 |
best := (methods at:idx) selector. |
|
5451 |
codeView |
|
5452 |
undoableDo:[ |
|
5453 |
codeView insertString:best atCharacterPosition:crsrPos. |
|
5454 |
codeView cursorToCharacterPosition:crsrPos+best size. |
|
5455 |
] |
|
5456 |
info:'completion'. |
|
5457 |
^ self. |
|
5458 |
]. |
|
4238 | 5459 |
]. |
5460 |
||
4277 | 5461 |
((parent := node parent) notNil and:[parent isMessage]) ifTrue:[ |
5253 | 5462 |
node == parent receiver ifTrue:[ |
5463 |
selectorOfMessageToNode := parent selector |
|
5464 |
] |
|
4238 | 5465 |
]. |
5466 |
||
5467 |
getDistanceComputeBlockWithWeight := |
|
5253 | 5468 |
[:weight | |
5469 |
[:each | |
|
5470 |
|dist factor| |
|
5471 |
||
5472 |
dist := each spellAgainst:nm. |
|
5473 |
factor := 1. |
|
5474 |
||
5475 |
(each startsWith:nm) ifTrue:[ |
|
5476 |
factor := 6 * nm size. |
|
5477 |
] ifFalse:[ |
|
5478 |
(each asLowercase startsWith:nm asLowercase) ifTrue:[ |
|
5479 |
factor := 4 * nm size. |
|
5360 | 5480 |
] ifFalse:[ |
5481 |
(each endsWith:nm) ifTrue:[ |
|
5482 |
factor := 3 * nm size. |
|
5483 |
] ifFalse:[ |
|
5484 |
(each asLowercase endsWith:nm asLowercase) ifTrue:[ |
|
5485 |
factor := 2 * nm size. |
|
5551 | 5486 |
] |
5487 |
] |
|
5488 |
] |
|
5253 | 5489 |
]. |
5490 |
dist := dist + (weight*factor). |
|
5491 |
||
5492 |
each -> (dist * weight) |
|
5493 |
] |
|
5494 |
]. |
|
4238 | 5495 |
|
5496 |
addWithFactorBlock := |
|
6022 | 5497 |
[:eachNames :factor | |
5498 |
|namesToAdd| |
|
5499 |
||
5253 | 5500 |
namesToAdd := eachNames select:[:nameToAdd | nameToAdd ~= nm ]. |
5501 |
namesToAdd := namesToAdd reject:[:each | allVariables includes:each ]. |
|
5502 |
allVariables addAll:namesToAdd. |
|
5503 |
allDistances addAll:(namesToAdd collect:(getDistanceComputeBlockWithWeight value:factor)). |
|
5504 |
]. |
|
4238 | 5505 |
|
5506 |
nm isUppercaseFirst ifTrue:[ |
|
5253 | 5507 |
globalFactor := 2. "/ favour globals |
5508 |
localFactor := 1. |
|
4238 | 5509 |
] ifFalse:[ |
5253 | 5510 |
globalFactor := 1. "/ favour locals |
5511 |
localFactor := 2. |
|
4238 | 5512 |
]. |
5513 |
||
5514 |
allVariables := OrderedCollection new. |
|
5515 |
allDistances := OrderedCollection new. |
|
5516 |
||
4308 | 5517 |
"/ are we in the method's selector spec ? |
4548 | 5518 |
((parent := node parent) notNil |
4277 | 5519 |
and:[parent isMethod |
5520 |
and:[parent arguments includes:node]]) ifTrue:[ |
|
5253 | 5521 |
"/ now that's cool: look how the naem of this argument is in other implementations |
5522 |
"/ of this method, and take that as a basis of the selection |
|
5523 |
||
5524 |
implementors := SystemBrowser |
|
5525 |
findImplementorsOf:(parent selector) |
|
5526 |
in:(Smalltalk allClasses) |
|
5527 |
ignoreCase:false. |
|
5528 |
"/ which argument is it |
|
5529 |
argIdx := parent arguments indexOf:node. |
|
5530 |
implementors size > 50 ifTrue:[ |
|
5531 |
implementors := implementors asOrderedCollection copyTo:50. |
|
5532 |
]. |
|
5533 |
namesUsed := implementors |
|
5534 |
collect:[:eachImplementor | |
|
5535 |
|parseTree| |
|
5536 |
parseTree := eachImplementor parseTree. |
|
6013 | 5537 |
(parseTree notNil and:[parseTree arguments size ~~ 0]) |
5253 | 5538 |
ifFalse:nil |
5539 |
ifTrue:[ (parseTree arguments at:argIdx) name] ] |
|
5540 |
thenSelect:[:a | a notNil] as:Set. |
|
5541 |
||
5542 |
addWithFactorBlock value:namesUsed value:(2 * localFactor). |
|
5543 |
||
5544 |
classOrNil notNil ifTrue:[ |
|
5545 |
"/ also, look for the keyword before the argument, |
|
5546 |
"/ and see if there is such an instVar |
|
5547 |
"/ if so, add it with -Arg |
|
5548 |
parent selector isKeyword ifTrue:[ |
|
5549 |
kwPart := parent selector keywords at:argIdx. |
|
5550 |
(classOrNil allInstVarNames includes:(kwPart copyButLast:1)) ifTrue:[ |
|
5551 |
addWithFactorBlock |
|
5552 |
value:(classOrNil allInstVarNames collect:[:nm| nm,'Arg']) |
|
5553 |
value:(1 * localFactor). |
|
5554 |
]. |
|
5555 |
]. |
|
5556 |
] |
|
4238 | 5557 |
] ifFalse:[ |
5253 | 5558 |
classOrNil notNil ifTrue:[ |
5559 |
"/ locals in the block/method |
|
5560 |
|names| |
|
5561 |
||
5562 |
names := node allVariablesOnScope. |
|
5563 |
"/ if there were no variables (due to a parse error) |
|
5564 |
"/ do another parse and see what we have |
|
5565 |
names isEmpty ifTrue:[ |
|
5566 |
tree := self treeForCode:(codeView contentsAsString string) allowErrors:true. |
|
5567 |
"/ better if we already have a body (include locals then) |
|
5568 |
"/ otherwise, only the arguments are considered |
|
5569 |
tree notNil ifTrue:[ |
|
5570 |
names := (tree body ? tree) allVariablesOnScope. |
|
5571 |
] |
|
5572 |
]. |
|
5573 |
||
5574 |
addWithFactorBlock value:names value:(4 * localFactor). |
|
5575 |
||
5576 |
"/ instance variables |
|
5577 |
addWithFactorBlock value:classOrNil instVarNames value:(3 * localFactor). |
|
5578 |
||
5579 |
"/ inherited instance variables |
|
5580 |
classOrNil superclass notNil ifTrue:[ |
|
5581 |
addWithFactorBlock value:classOrNil superclass allInstVarNames value:(2.5 * localFactor). |
|
5582 |
]. |
|
5583 |
]. |
|
5584 |
||
5585 |
selectorOfMessageToNode notNil ifTrue:[ |
|
5586 |
|names responders nonResponders| |
|
5587 |
||
5551 | 5588 |
"/ responding to that message |
5253 | 5589 |
|
5590 |
classOrNil notNil ifTrue:[ |
|
5591 |
"/ private classes |
|
5592 |
addWithFactorBlock value:(nonMetaClass privateClasses collect:[:cls | cls nameWithoutPrefix]) |
|
5593 |
value:(1.75 * globalFactor). |
|
5594 |
||
5595 |
"/ class variables |
|
5596 |
names := nonMetaClass classVarNames. |
|
5597 |
responders := names select:[:classVar | (nonMetaClass classVarAt:classVar) respondsTo:selectorOfMessageToNode]. |
|
5598 |
nonResponders := names reject:[:classVar | (nonMetaClass classVarAt:classVar) respondsTo:selectorOfMessageToNode]. |
|
5599 |
||
5600 |
addWithFactorBlock value:responders value:(1.5 * globalFactor). |
|
5601 |
addWithFactorBlock value:nonResponders value:(0.5 * 1.5 * globalFactor). |
|
5602 |
||
5603 |
"/ superclass var names |
|
5604 |
nonMetaClass allSuperclassesDo:[:superClass | |
|
5605 |
names := superClass classVarNames. |
|
5606 |
responders := names select:[:classVar | (superClass classVarAt:classVar) respondsTo:selectorOfMessageToNode]. |
|
5607 |
nonResponders := names reject:[:classVar | (superClass classVarAt:classVar) respondsTo:selectorOfMessageToNode]. |
|
5608 |
||
5609 |
addWithFactorBlock value:responders value:(1 * globalFactor). |
|
5610 |
addWithFactorBlock value:nonResponders value:(0.5 * 1 * globalFactor). |
|
5611 |
]. |
|
5612 |
||
5613 |
"/ namespace vars |
|
5614 |
classOrNil nameSpace ~~ Smalltalk ifTrue:[ |
|
5615 |
names := classOrNil topNameSpace keys. |
|
5616 |
names := names reject:[:nm | nm includes:$:]. |
|
5617 |
names := names select:[:nm | nm isUppercaseFirst ]. |
|
5618 |
responders := names select:[:nsVar | |c| c := classOrNil topNameSpace at:nsVar. c isBehavior not or:[c isLoaded and:[c respondsTo:selectorOfMessageToNode]]]. |
|
5619 |
nonResponders := names reject:[:nsVar | |c| c := classOrNil topNameSpace at:nsVar. c isBehavior not or:[c isLoaded and:[c respondsTo:selectorOfMessageToNode]]]. |
|
5620 |
addWithFactorBlock value:responders value:(1.5 * globalFactor). |
|
5621 |
addWithFactorBlock value:nonResponders value:(0.5 * 1.5 * globalFactor). |
|
5622 |
]. |
|
5623 |
]. |
|
5624 |
||
5625 |
"/ globals |
|
5626 |
names := Smalltalk keys. |
|
5627 |
"/ names := names reject:[:nm | nm includes:$:]. |
|
5628 |
names := names select:[:nm | nm isUppercaseFirst ]. |
|
5629 |
responders := names select:[:glblVar | |c| c := Smalltalk at:glblVar. c isBehavior not or:[c isLoaded and:[c respondsTo:selectorOfMessageToNode]]]. |
|
5630 |
nonResponders := names reject:[:glblVar | |c| c := Smalltalk at:glblVar. c isBehavior not or:[c isLoaded and:[c respondsTo:selectorOfMessageToNode]]]. |
|
5631 |
addWithFactorBlock value:responders value:(1.5 * globalFactor). |
|
5632 |
addWithFactorBlock value:nonResponders value:(0.5 * 1.5 * globalFactor). |
|
5633 |
||
5634 |
"/ pool variables |
|
5635 |
classOrNil theNonMetaclass sharedPoolNames do:[:poolName | |
|
5636 |
|pool| |
|
5637 |
||
5638 |
pool := Smalltalk at:poolName. |
|
5639 |
names := pool classVarNames. |
|
5640 |
names := names select:[:nm | nm isUppercaseFirst ]. |
|
5641 |
responders := names select:[:glblVar | |c| c := Smalltalk at:glblVar. c isBehavior not or:[c isLoaded and:[c respondsTo:selectorOfMessageToNode]]]. |
|
5642 |
nonResponders := names reject:[:glblVar | |c| c := Smalltalk at:glblVar. c isBehavior not or:[c isLoaded and:[c respondsTo:selectorOfMessageToNode]]]. |
|
5643 |
addWithFactorBlock value:responders value:(2.5 * globalFactor). |
|
5644 |
addWithFactorBlock value:nonResponders value:(0.5 * 2.5 * globalFactor). |
|
5645 |
]. |
|
5646 |
] ifFalse:[ |
|
5647 |
|names| |
|
5648 |
||
5649 |
classOrNil notNil ifTrue:[ |
|
5650 |
"/ private classes |
|
5651 |
addWithFactorBlock value:(nonMetaClass privateClasses collect:[:cls | cls nameWithoutPrefix]) |
|
5652 |
value:(1.75 * globalFactor). |
|
5653 |
||
5654 |
"/ class variables |
|
5655 |
addWithFactorBlock value:nonMetaClass classVarNames value:(2.0 * globalFactor). |
|
5656 |
classOrNil superclass notNil ifTrue:[ |
|
5657 |
addWithFactorBlock value:nonMetaClass superclass allClassVarNames value:(2.0 * globalFactor). |
|
5658 |
]. |
|
5659 |
||
5660 |
"/ namespace vars |
|
5661 |
classOrNil nameSpace ~~ Smalltalk ifTrue:[ |
|
5662 |
names := classOrNil nameSpace isNameSpace ifTrue:[classOrNil nameSpace keys] ifFalse:[classOrNil nameSpace privateClasses collect:[:c | c nameWithoutPrefix]]. |
|
5663 |
names := names select:[:nm | nm isUppercaseFirst ]. |
|
5664 |
addWithFactorBlock value:names value:(1.5 * globalFactor). |
|
5665 |
]. |
|
5666 |
||
5667 |
"/ pool variables |
|
5668 |
classOrNil theNonMetaclass sharedPoolNames do:[:poolName | |
|
5669 |
|pool| |
|
5670 |
||
5671 |
pool := Smalltalk at:poolName. |
|
5672 |
names := pool classVarNames. |
|
5673 |
addWithFactorBlock value:names value:(2.5 * globalFactor). |
|
5674 |
]. |
|
5675 |
]. |
|
5676 |
||
5677 |
"/ globals |
|
5678 |
names := Smalltalk keys. |
|
5679 |
names := names select:[:nm | nm isUppercaseFirst ]. |
|
5680 |
addWithFactorBlock value:names value:(1.5 * globalFactor). |
|
5681 |
]. |
|
5682 |
||
5683 |
"/ pseudos - assuming that thisContext is seldom used. |
|
5684 |
"/ also assuming, that nil is short so its usually typed in. |
|
5685 |
addWithFactorBlock value:#('self') value:(2.5 * localFactor). |
|
5686 |
addWithFactorBlock value:#('nil') value:(0.5 * localFactor). |
|
5687 |
addWithFactorBlock value:#('super' 'false') value:(2 * localFactor). |
|
5688 |
addWithFactorBlock value:#('thisContext') value:(1 * localFactor). |
|
5689 |
addWithFactorBlock value:#('true') value:(1 * localFactor). |
|
5690 |
addWithFactorBlock value:#('false') value:(1 * localFactor). |
|
4238 | 5691 |
]. |
5692 |
||
5693 |
allDistances isEmpty ifTrue:[^ self]. |
|
5694 |
bestAssoc := allDistances at:1. |
|
5695 |
bestAssoc := allDistances inject:bestAssoc into:[:el :best | el value > best value |
|
5253 | 5696 |
ifTrue:[el] |
5697 |
ifFalse:[best] |
|
5698 |
]. |
|
4548 | 5699 |
|
5700 |
allDistances sort:[:a :b | |
|
5253 | 5701 |
a value > b value ifTrue:[ |
5702 |
true |
|
5703 |
] ifFalse:[ |
|
5704 |
a value = b value ifTrue:[ |
|
5705 |
a key < b key |
|
5706 |
] ifFalse:[ |
|
5707 |
false |
|
5708 |
] |
|
5709 |
] |
|
5710 |
]. |
|
4340 | 5711 |
((allTheBest := allDistances) count:[:entry | entry value]) > 20 ifTrue:[ |
5253 | 5712 |
allTheBest := allDistances select:[:entry | entry value >= (bestAssoc value * 0.5)]. |
5713 |
allTheBest size > 15 ifTrue:[ |
|
5714 |
allTheBest := allDistances select:[:entry | entry value >= (bestAssoc value * 0.8)]. |
|
5715 |
]. |
|
4238 | 5716 |
]. |
5717 |
||
4524 | 5718 |
start := node start. |
5719 |
stop := node stop. |
|
5720 |
||
4548 | 5721 |
best := self askUserForCompletion:('Variable for "%1"' bindWith:node name) |
5253 | 5722 |
for:codeView at: start |
5723 |
from:(allTheBest collect:[:assoc | assoc key]). |
|
4238 | 5724 |
best isNil ifTrue:[^ self]. |
5725 |
||
5726 |
"/ self showInfo:best. |
|
5727 |
||
5728 |
oldVar := (codeView textFromCharacterPosition:start to:stop) asString string withoutSeparators. |
|
5729 |
||
5730 |
codeView |
|
5253 | 5731 |
undoableDo:[ codeView replaceFromCharacterPosition:start to:stop with:best ] |
5732 |
info:'Completion'. |
|
4238 | 5733 |
|
5734 |
(best startsWith:oldVar) ifTrue:[ |
|
5253 | 5735 |
oldLen := stop - start + 1. |
5736 |
newLen := best size. |
|
5737 |
codeView selectFromCharacterPosition:start+oldLen to:start+newLen-1. |
|
5738 |
codeView dontReplaceSelectionOnInput |
|
4238 | 5739 |
]. |
5740 |
||
5741 |
"Created: / 10-11-2006 / 13:16:33 / cg" |
|
5742 |
"Modified: / 16-02-2010 / 10:13:13 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
4346 | 5743 |
"Modified: / 28-08-2013 / 15:37:28 / cg" |
5551 | 5744 |
"Modified (format): / 12-06-2017 / 12:37:31 / mawalch" |
6022 | 5745 |
"Modified (format): / 03-03-2019 / 22:53:58 / Claus Gittinger" |
4238 | 5746 |
! ! |
5747 |
||
5109 | 5748 |
!DoWhatIMeanSupport methodsFor:'helpers-naive type inference'! |
5749 |
||
5750 |
addClassesFromAssignmentTo:varName in:aTree to:setOfTypes |
|
5751 |
"/ assignments... |
|
5752 |
||
5753 |
aTree allAssignmentNodesDo:[:eachAssignmentNode | |
|
5754 |
|leftSide| |
|
5755 |
||
5756 |
leftSide := eachAssignmentNode variable. |
|
5757 |
leftSide name = varName ifTrue:[ |
|
5758 |
self |
|
5759 |
addClassesOfExpression:eachAssignmentNode value |
|
5760 |
inClass:classOrNil to:setOfTypes. |
|
5761 |
] |
|
5762 |
]. |
|
5763 |
^ setOfTypes. |
|
5764 |
! |
|
5765 |
||
5766 |
addClassesFromMessagesSentTo:varNode in:aTree to:setOfTypes |
|
5767 |
"/ from the set of messages sent at other places, |
|
5768 |
"/ try to find classes, which respond to all those. |
|
5769 |
||
5770 |
|varName allSelectors candidates| |
|
5771 |
||
5772 |
varName := varNode name. |
|
5773 |
||
5774 |
allSelectors := IdentitySet new. |
|
5775 |
aTree allMessageNodesDo:[:eachMessageNode | |
|
5776 |
|rcvr| |
|
5777 |
||
5778 |
rcvr := eachMessageNode receiver. |
|
5779 |
(rcvr isVariable and:[rcvr name = varName]) ifTrue:[ |
|
5780 |
allSelectors add:eachMessageNode selector |
|
5781 |
] |
|
5782 |
]. |
|
5783 |
||
5324 | 5784 |
"/ the selector being completed must be ignored here |
5109 | 5785 |
(varNode parent notNil and:[varNode parent isMessage]) ifTrue:[ |
5786 |
allSelectors remove:(varNode parent selector) ifAbsent:[]. |
|
5787 |
]. |
|
5788 |
||
5789 |
"/ now look for classes which implement all of them |
|
5790 |
candidates := SystemBrowser findRespondersOfAll:allSelectors in:nil ignoreCase:false. |
|
5791 |
candidates remove:Object ifAbsent:[]. |
|
5792 |
setOfTypes addAll:candidates. |
|
5793 |
^ setOfTypes. |
|
5794 |
! |
|
5795 |
||
5796 |
addClassesOfBlockVarForWellknownBlocks:variableNode inScope:blockScope to:setOfTypes |
|
5797 |
|blockParent blockParentSelector exNode isHandler| |
|
5798 |
||
5799 |
blockParent := blockScope parent. |
|
5800 |
(blockParent notNil and:[blockParent isMessage]) ifFalse:[^ setOfTypes]. |
|
5801 |
||
5802 |
blockParentSelector := blockParent selector. |
|
5803 |
||
5804 |
"/ if the parent of the block is an enumeration message, and the receiver is known, |
|
5805 |
"/ we know the type of argument. |
|
5806 |
( #(do: keysAndValuesDo: select: collect:) includes:blockParent selector) ifTrue:[ |
|
5807 |
|collection| |
|
5808 |
||
5809 |
collection := self valueOfNode:blockParent receiver. |
|
5810 |
collection notNil ifTrue:[ |
|
5811 |
(collection isKindOf:Collection) ifTrue:[ |
|
5812 |
collection notEmpty ifTrue:[ |
|
5813 |
|someElement| |
|
5814 |
someElement := collection anElement. |
|
5815 |
setOfTypes add:someElement class. |
|
5816 |
^ setOfTypes |
|
5817 |
]. |
|
5818 |
]. |
|
5819 |
]. |
|
5820 |
]. |
|
5821 |
||
5822 |
"/ because we type-in those so often, it is great to get |
|
5823 |
"/ better info on the ex parameter... (and it's a low hanging fruit) |
|
5824 |
( blockParentSelector == #handle:do: ) ifTrue:[ |
|
5825 |
exNode := blockParent receiver. |
|
5826 |
isHandler := (blockScope == (blockParent arguments at:1)). |
|
5827 |
]. |
|
5828 |
( blockParentSelector == #on:do: ) ifTrue:[ |
|
5133 | 5829 |
"/ self halt. |
5109 | 5830 |
exNode := blockParent arg1. |
5831 |
isHandler := (blockScope == (blockParent arguments at:2)). |
|
5832 |
]. |
|
5833 |
exNode notNil ifTrue:[ |
|
5834 |
|cls exClass| |
|
5835 |
||
5836 |
((cls := (self valueOfNode:exNode)) notNil and:[cls isBehavior]) ifTrue:[ |
|
5837 |
exClass := cls |
|
5838 |
] ifFalse:[ |
|
5839 |
exClass := Exception |
|
5840 |
]. |
|
5841 |
setOfTypes add:exClass. |
|
5842 |
^ self |
|
5843 |
]. |
|
5844 |
^ setOfTypes |
|
5845 |
! |
|
5846 |
||
5606 | 5847 |
addClassesOfExpression:expr inClass:classOrNil to:setOfPossibleClasses |
5803 | 5848 |
|cls exprVal varName constraints| |
5606 | 5849 |
|
5980 | 5850 |
classResolverHook notNil ifTrue:[ |
5851 |
setOfPossibleClasses addAll:((classResolverHook value:expr) ? #()) |
|
5852 |
]. |
|
5853 |
||
5606 | 5854 |
(expr isVariable "and:[expr name = 'secondsOrNil']") ifTrue:[ |
5855 |
"/ see if contained inside an isXXX ifTrue; |
|
5856 |
"/ then, we know a lot more... |
|
5857 |
expr parent notNil ifTrue:[ |
|
5858 |
constraints := self extractConstraintsFor:expr inClass:classOrNil. |
|
5859 |
constraints notEmpty ifTrue:[ |
|
5860 |
setOfPossibleClasses addAll:constraints. |
|
5861 |
^ setOfPossibleClasses. |
|
5862 |
]. |
|
5863 |
]. |
|
5864 |
]. |
|
5865 |
||
5109 | 5866 |
expr isLiteral ifTrue:[ |
5867 |
exprVal := expr value. |
|
5502 | 5868 |
cls := exprVal class. |
5109 | 5869 |
(exprVal isArray or:[ exprVal isByteArray or:[ exprVal isString ]]) ifTrue:[ |
5870 |
exprVal isImmutable ifTrue:[ |
|
5606 | 5871 |
setOfPossibleClasses add:cls mutableClass. |
5872 |
^ setOfPossibleClasses. |
|
5109 | 5873 |
] |
5874 |
]. |
|
5606 | 5875 |
setOfPossibleClasses add:cls. |
5876 |
^ setOfPossibleClasses. |
|
5502 | 5877 |
]. |
5878 |
||
5109 | 5879 |
expr isBlock ifTrue:[ |
5606 | 5880 |
setOfPossibleClasses add:Block. |
5881 |
^ setOfPossibleClasses. |
|
5109 | 5882 |
]. |
5883 |
||
5453 | 5884 |
thisContext isRecursive ifTrue:[ |
5885 |
|count| |
|
5886 |
count := 0. |
|
5887 |
thisContext withAllSendersDo:[:c | (c selector == thisContext selector) ifTrue:[count := count + 1 ]]. |
|
5888 |
count > 10 ifTrue:[ |
|
5606 | 5889 |
^ setOfPossibleClasses |
5453 | 5890 |
]. |
5891 |
]. |
|
5131 | 5892 |
|
5109 | 5893 |
expr isVariable ifTrue:[ |
5894 |
varName := expr name. |
|
5803 | 5895 |
self addClassesOfVariable:varName inExpression:expr inClass:classOrNil to:setOfPossibleClasses. |
5896 |
^ setOfPossibleClasses |
|
5897 |
]. |
|
5898 |
"/ we might get a degenerated / incomplete expression; |
|
5899 |
"/ especially in a workspace, where a variable is interpreted |
|
5900 |
"/ as a selector (when a period is missing before) |
|
5901 |
expr isMessage ifTrue:[ |
|
5902 |
expr selector isUnarySelector ifTrue:[ |
|
5903 |
varName := expr selector. |
|
5904 |
self addClassesOfVariable:varName inExpression:expr inClass:classOrNil to:setOfPossibleClasses. |
|
5109 | 5905 |
]. |
5803 | 5906 |
]. |
5907 |
||
5109 | 5908 |
(exprVal := self valueOfNode:expr) notNil ifTrue:[ |
5909 |
"/ knowing the value is always great!! |
|
5606 | 5910 |
setOfPossibleClasses add:exprVal class. |
5911 |
^ setOfPossibleClasses. |
|
5109 | 5912 |
]. |
5913 |
||
5914 |
expr isMessage ifTrue:[ |
|
5606 | 5915 |
self addClassesOfMessage:expr inClass:classOrNil to:setOfPossibleClasses. |
5916 |
^ setOfPossibleClasses |
|
5502 | 5917 |
]. |
5109 | 5918 |
|
5606 | 5919 |
^ setOfPossibleClasses |
5920 |
||
5502 | 5921 |
"Modified: / 24-02-2017 / 14:41:00 / stefan" |
5606 | 5922 |
"Modified: / 15-09-2017 / 10:52:40 / cg" |
5980 | 5923 |
"Modified: / 27-12-2018 / 12:16:28 / Claus Gittinger" |
5109 | 5924 |
! |
5925 |
||
5926 |
addClassesOfInstVarNamed:varName inClass:aClass to:setOfTypes |
|
5927 |
|instIndex type| |
|
5928 |
||
5929 |
instIndex := aClass instVarIndexFor:varName. |
|
5930 |
||
5931 |
"/ ask the class |
|
5932 |
(type := aClass typeOfInstVarNamed:varName) notNil ifTrue:[ |
|
5933 |
type isCollection ifTrue:[ |
|
5934 |
setOfTypes addAll:type. |
|
5935 |
] ifFalse:[ |
|
5936 |
setOfTypes add:type. |
|
5937 |
]. |
|
5938 |
^ setOfTypes |
|
5939 |
]. |
|
5940 |
||
5941 |
"/ look for instances |
|
5942 |
aClass allSubInstancesDo:[:i | |
|
5943 |
|varClass| |
|
5944 |
varClass := (i instVarAt:instIndex) class. |
|
5945 |
setOfTypes add:varClass. |
|
5946 |
]. |
|
5947 |
||
5948 |
"/ look for assignments in code |
|
5949 |
aClass withAllSubclassesDo:[:eachClass | |
|
5950 |
eachClass methodDictionary do:[:m | |
|
5951 |
|tree code visitor| |
|
5952 |
||
5953 |
"/ quick check |
|
5125 | 5954 |
SourceCodeManagerError handle:[:ex | |
5955 |
] do:[ |
|
5956 |
code := m source. |
|
5957 |
]. |
|
5109 | 5958 |
(code notNil and:[code includesString:varName]) ifTrue:[ |
5959 |
tree := Parser parse:code class:eachClass. |
|
5960 |
(tree notNil and:[tree ~~ #Error]) ifTrue:[ |
|
5961 |
visitor := PluggableParseNodeVisitor new. |
|
5962 |
visitor |
|
5963 |
actionForNodeClass:AssignmentNode |
|
5964 |
put:[:node | |
|
5965 |
|val expr exprSelector| |
|
5966 |
||
5967 |
node variable name = varName ifTrue:[ |
|
5968 |
expr := node expression. |
|
5969 |
"/ only look for wellknown types on the right side. |
|
5970 |
expr isLiteral ifTrue:[ |
|
5971 |
val := expr evaluate. |
|
5972 |
val isArray ifTrue:[ |
|
5973 |
setOfTypes add:Array |
|
5974 |
] ifFalse:[ |
|
5975 |
setOfTypes add:val class |
|
5976 |
]. |
|
5977 |
] ifFalse:[ |
|
5978 |
expr isMessage ifTrue:[ |
|
5979 |
exprSelector := expr selector. |
|
5980 |
( #(+ - * /) includes:exprSelector ) ifTrue:[ |
|
5981 |
setOfTypes add:Number |
|
5982 |
] ifFalse:[ |
|
5621 | 5983 |
( #(// size basicSize position) includes:exprSelector ) ifTrue:[ |
5109 | 5984 |
setOfTypes add:Integer |
5985 |
] ifFalse:[ |
|
5986 |
( #(copy shallowCopy) includes:exprSelector ) ifTrue:[ |
|
5987 |
] ifFalse:[ |
|
5988 |
( #(new new: basicNew basicNew:) includes:exprSelector ) ifTrue:[ |
|
5989 |
expr receiver isGlobal ifTrue:[ |
|
5990 |
setOfTypes add:expr receiver evaluate |
|
5991 |
]. |
|
5992 |
] ifFalse:[ |
|
5660 | 5993 |
self breakPoint:#DWIM. |
5109 | 5994 |
] |
5995 |
] |
|
5996 |
] |
|
5997 |
] |
|
5998 |
]. |
|
5999 |
]. |
|
6000 |
]. |
|
6001 |
true "/ yes - visit subnodes |
|
6002 |
]. |
|
6003 |
visitor visit:tree. |
|
6004 |
]. |
|
6005 |
] |
|
6006 |
] |
|
6007 |
]. |
|
6008 |
^ setOfTypes |
|
5621 | 6009 |
|
6010 |
"Modified: / 11-10-2017 / 13:58:36 / cg" |
|
5109 | 6011 |
! |
6012 |
||
6013 |
addClassesOfMessage:expr inClass:classOrNil to:setOfTypes |
|
6014 |
|valClass |
|
6015 |
msgSelector msgReceiver msgArg1 |
|
6016 |
receiverClasses receiverClass |
|
6017 |
arg1Classes arg1Value mthd| |
|
6018 |
||
6019 |
msgSelector := expr selector. |
|
6020 |
||
6021 |
"/ heuristic: quickly assume boolean for some: |
|
6022 |
( |
|
6023 |
#( |
|
6024 |
isNil notNil isEmpty isEmptyOrNil notEmpty notEmptyOrNil |
|
6025 |
> >= < <= = == ~ ~= |
|
6026 |
knownAsSymbol |
|
6027 |
isMeta |
|
6028 |
includes: contains: |
|
6029 |
not and: or: |
|
6030 |
exists atEnd positive negative odd even |
|
6031 |
) includes:msgSelector |
|
6032 |
) ifTrue:[ |
|
6033 |
setOfTypes add:True. "/ use True, because Boolean does not include the full protocol |
|
6034 |
^ setOfTypes |
|
6035 |
]. |
|
6036 |
||
6037 |
msgReceiver := expr receiver. |
|
6038 |
||
6039 |
"/ some hardwired knowledge here |
|
6040 |
receiverClasses := self classesOfNode:msgReceiver. |
|
6041 |
receiverClass := receiverClasses size == 1 ifTrue:[receiverClasses anElement] ifFalse:[nil]. |
|
6042 |
||
6043 |
receiverClass notNil ifTrue:[ |
|
6044 |
"/ follow Smalltalk at: to see what is there |
|
6045 |
receiverClass == Smalltalk ifTrue:[ |
|
6046 |
msgSelector == #at: ifTrue:[ |
|
6047 |
msgArg1 := expr arg1. |
|
6048 |
msgArg1 isLiteralSymbol ifTrue:[ |
|
6049 |
arg1Value := Smalltalk at:msgArg1 value. |
|
6050 |
arg1Value notNil ifTrue:[ |
|
6051 |
setOfTypes add:arg1Value class. |
|
6052 |
^ setOfTypes. |
|
6053 |
] |
|
6054 |
] |
|
6055 |
]. |
|
6056 |
]. |
|
6057 |
||
6058 |
"/ usually return something of the receiver's type |
|
6059 |
( #(copy shallowCopy copyWith: , ) includes:msgSelector ) ifTrue:[ |
|
6060 |
setOfTypes addAll:receiverClasses. |
|
6061 |
^ setOfTypes. |
|
6062 |
]. |
|
6063 |
||
6064 |
( msgSelector == #class ) ifTrue:[ |
|
6065 |
setOfTypes add:(receiverClass class). |
|
6066 |
^ setOfTypes |
|
6067 |
]. |
|
6068 |
||
6069 |
receiverClass isBehavior ifTrue:[ |
|
6070 |
( #(compiledMethodAt:) includes:msgSelector) ifTrue:[ |
|
6071 |
setOfTypes add:Method. |
|
6072 |
^ setOfTypes |
|
6073 |
]. |
|
6074 |
( #(superclass) includes:msgSelector) ifTrue:[ |
|
6075 |
receiverClass isMeta ifTrue:[ |
|
6076 |
setOfTypes add:Metaclass. |
|
6077 |
] ifFalse:[ |
|
6078 |
setOfTypes add:Class. |
|
6079 |
]. |
|
6080 |
^ setOfTypes |
|
6081 |
]. |
|
6082 |
( #(theNonMetaclass theMetaclass) includes:msgSelector) ifTrue:[ |
|
6083 |
receiverClass isMeta ifTrue:[ |
|
6084 |
setOfTypes add:Metaclass. |
|
6085 |
^ setOfTypes |
|
6086 |
]. |
|
5462 | 6087 |
setOfTypes add:(receiverClass perform:msgSelector asSymbol) class. |
5109 | 6088 |
^ setOfTypes |
6089 |
]. |
|
6090 |
||
6091 |
mthd := receiverClass lookupMethodFor:msgSelector. |
|
6092 |
receiverClass isMeta ifTrue:[ |
|
6093 |
( #( #'new' #'basicNew' #'new:' #'basicNew:' #'with:' #'with:with:') includes: msgSelector ) ifTrue:[ |
|
6094 |
setOfTypes add:receiverClass theNonMetaclass. |
|
6095 |
^ setOfTypes. |
|
6096 |
]. |
|
6097 |
"/ if that method sends one of new/basicNew/new:/basicNew:, assume it returns an instance of itself |
|
6098 |
mthd notNil ifTrue:[ |
|
5451 | 6099 |
( mthd sendsAnySelector:#( #'new' #'basicNew' #'new:' #'basicNew:' )) ifTrue:[ |
5109 | 6100 |
setOfTypes add:receiverClass theNonMetaclass. |
6101 |
^ setOfTypes |
|
6102 |
]. |
|
6103 |
]. |
|
6104 |
] ifFalse:[ |
|
6105 |
mthd notNil ifTrue:[ |
|
6106 |
(ParseTreeSearcher methodIsSetterMethod:mthd) ifTrue:[ |
|
6107 |
setOfTypes add:receiverClass. |
|
6108 |
^ setOfTypes |
|
6109 |
] |
|
6110 |
] |
|
6111 |
] |
|
6112 |
]. |
|
6113 |
]. |
|
6114 |
||
6115 |
((msgSelector startsWith:'as') |
|
6116 |
and:[ (valClass := Smalltalk classNamed:(msgSelector copyFrom:3)) notNil ] |
|
6117 |
) ifTrue:[ |
|
6118 |
setOfTypes add:valClass. |
|
6119 |
^ setOfTypes |
|
6120 |
]. |
|
6121 |
||
6122 |
((msgSelector startsWith:'is') |
|
6123 |
and:[ (valClass := Smalltalk classNamed:(msgSelector copyFrom:3)) notNil ] |
|
6124 |
) ifTrue:[ |
|
6125 |
setOfTypes add:True. "/ True - not boolean; it does not contain the full protocol (would not find ifTrue:) |
|
6126 |
^ setOfTypes. |
|
6127 |
]. |
|
6128 |
||
6129 |
#( |
|
6130 |
size SmallInteger |
|
6131 |
hash SmallInteger |
|
6132 |
identityHash SmallInteger |
|
6133 |
class Class |
|
6134 |
theMetaclass Metaclass |
|
6135 |
theNonMetaclass Class |
|
6136 |
fork Process |
|
6137 |
newProcess Process |
|
6138 |
) pairWiseDo:[:sel :clsName | |
|
6139 |
msgSelector == sel ifTrue:[ |
|
6140 |
setOfTypes add:(Smalltalk at:clsName). |
|
6141 |
^ setOfTypes. |
|
6142 |
]. |
|
6143 |
]. |
|
6144 |
||
6145 |
( #( bitAnd: bitOr: bitShift: rightShift: >> << highBit lowBit ) includes:msgSelector) ifTrue:[ |
|
6146 |
"/ assume integer |
|
6147 |
||
6148 |
setOfTypes add:Integer. |
|
6149 |
^ setOfTypes |
|
6150 |
]. |
|
6151 |
( #( + - * // \\ ) includes:msgSelector) ifTrue:[ |
|
6152 |
"/ assume numeric |
|
6153 |
||
6154 |
setOfTypes add:Number. |
|
6155 |
^ setOfTypes |
|
6156 |
]. |
|
6157 |
msgSelector == #/ ifTrue:[ |
|
6158 |
((receiverClasses ? #()) contains:[:cls | cls includesBehavior:Number]) ifTrue:[ |
|
6159 |
setOfTypes add:Number. |
|
6160 |
^ setOfTypes. |
|
6161 |
]. |
|
6162 |
msgArg1 := expr arguments at:1 ifAbsent:nil. |
|
6163 |
msgArg1 isNil ifTrue:[^ setOfTypes]. |
|
6164 |
arg1Classes := ((self classesOfNode:msgArg1) ? #()). |
|
6165 |
(arg1Classes contains:[:cls | cls includesBehavior:Number]) ifTrue:[ |
|
6166 |
setOfTypes add:Number. |
|
6167 |
^ setOfTypes |
|
6168 |
]. |
|
6169 |
]. |
|
6170 |
( #( construct: / ) includes:msgSelector) ifTrue:[ |
|
6171 |
((receiverClasses ? #()) contains:[:cls | cls includesBehavior:Filename]) ifTrue:[ |
|
6172 |
setOfTypes add:Filename. |
|
6173 |
^ setOfTypes |
|
6174 |
]. |
|
6175 |
]. |
|
6176 |
||
6177 |
^ setOfTypes |
|
5451 | 6178 |
|
5462 | 6179 |
"Modified: / 10-02-2017 / 10:39:41 / cg" |
5109 | 6180 |
! |
6181 |
||
5803 | 6182 |
addClassesOfVariable:varName inExpression:expr inClass:classOrNil to:setOfPossibleClasses |
5804 | 6183 |
"expr is either a variable-node or a message in which varName is the selector" |
6184 |
||
5803 | 6185 |
|varScope instVarClass classVarClass privateClass poolVarClass |
6186 |
sym topNameSpace exprVal| |
|
6187 |
||
6188 |
varName = 'self' ifTrue:[ |
|
6189 |
instanceOrNil notNil ifTrue:[ |
|
6190 |
setOfPossibleClasses add:(instanceOrNil class). |
|
6191 |
] ifFalse:[ |
|
6192 |
setOfPossibleClasses add:(classOrNil ? UndefinedObject). |
|
6193 |
]. |
|
6194 |
^ setOfPossibleClasses |
|
6195 |
]. |
|
6196 |
varName = 'super' ifTrue:[ |
|
6197 |
classOrNil isNil |
|
6198 |
ifTrue:[setOfPossibleClasses add:Object] |
|
6199 |
ifFalse:[setOfPossibleClasses add:classOrNil superclass]. |
|
6200 |
^ setOfPossibleClasses. |
|
6201 |
]. |
|
6202 |
varName = 'thisContext' ifTrue:[ |
|
6203 |
setOfPossibleClasses add:Context. |
|
6204 |
^ setOfPossibleClasses |
|
6205 |
]. |
|
6206 |
||
6207 |
varScope := expr whoDefines: varName. |
|
6208 |
(varScope notNil) ifTrue:[ |
|
5804 | 6209 |
expr isVariable ifTrue:[ |
6210 |
varScope isSequence ifTrue:[ |
|
6211 |
varScope := varScope parent. |
|
6212 |
]. |
|
6213 |
||
6214 |
(varScope isBlock) ifTrue:[ |
|
6215 |
self addClassesOfBlockVarForWellknownBlocks:expr inScope:varScope to:setOfPossibleClasses. |
|
6216 |
self addClassesFromAssignmentTo:varName in:varScope to:setOfPossibleClasses. |
|
6217 |
self addClassesFromMessagesSentTo:expr in:varScope to:setOfPossibleClasses. |
|
6218 |
^ setOfPossibleClasses |
|
6219 |
]. |
|
6220 |
(varScope isMethod) ifTrue:[ |
|
6221 |
self addClassesFromAssignmentTo:varName in:varScope to:setOfPossibleClasses. |
|
6222 |
self addClassesFromMessagesSentTo:expr in:varScope to:setOfPossibleClasses. |
|
6223 |
^ setOfPossibleClasses |
|
6224 |
]. |
|
5803 | 6225 |
]. |
6226 |
]. |
|
6227 |
||
6228 |
classOrNil notNil ifTrue:[ |
|
6229 |
"/ inst var |
|
6230 |
instVarClass := classOrNil whichClassDefinesInstVar:varName. |
|
6231 |
instVarClass notNil ifTrue:[ |
|
6232 |
setOfPossibleClasses addAll:(self classesOfInstVarNamed:varName inClass:instVarClass). |
|
6233 |
^ setOfPossibleClasses |
|
6234 |
]. |
|
6235 |
||
6236 |
"/ class vars |
|
6237 |
classVarClass := classOrNil theNonMetaclass whichClassDefinesClassVar:varName. |
|
6238 |
classVarClass notNil ifTrue:[ |
|
6239 |
"/ see what is currently there |
|
6240 |
setOfPossibleClasses add:(classVarClass classVarAt:varName asSymbol) class. |
|
6241 |
^ setOfPossibleClasses |
|
6242 |
]. |
|
6243 |
varName isUppercaseFirst ifTrue:[ |
|
6244 |
"/ private class |
|
6245 |
varName knownAsSymbol ifTrue:[ |
|
6246 |
privateClass := classOrNil theNonMetaclass privateClassesAt:varName asSymbol. |
|
6247 |
privateClass notNil ifTrue:[ |
|
6248 |
setOfPossibleClasses add:(privateClass theMetaclass). |
|
6249 |
^ setOfPossibleClasses |
|
6250 |
]. |
|
6251 |
]. |
|
6252 |
]. |
|
6253 |
"/ pool vars |
|
6254 |
poolVarClass := classOrNil theNonMetaclass whichPoolDefinesPoolVar:varName. |
|
6255 |
poolVarClass notNil ifTrue:[ |
|
6256 |
"/ see what is currently there |
|
6257 |
setOfPossibleClasses add:(poolVarClass classVarAt:varName asSymbol) class. |
|
6258 |
^ setOfPossibleClasses |
|
6259 |
]. |
|
6260 |
]. |
|
6261 |
||
6262 |
varName isUppercaseFirst ifTrue:[ |
|
6263 |
sym := varName asSymbolIfInterned. |
|
6264 |
sym notNil ifTrue:[ |
|
6265 |
(classOrNil isNil or:[(topNameSpace := classOrNil topNameSpace) isNil]) ifTrue:[ |
|
6266 |
topNameSpace := Smalltalk. |
|
6267 |
]. |
|
6268 |
exprVal := topNameSpace at:sym. |
|
6269 |
exprVal notNil ifTrue:[ |
|
6270 |
setOfPossibleClasses add:(exprVal class). |
|
6271 |
]. |
|
6036 | 6272 |
topNameSpace ~~ Smalltalk ifTrue:[ |
6273 |
exprVal := Smalltalk at:sym. |
|
6274 |
exprVal notNil ifTrue:[ |
|
6275 |
setOfPossibleClasses add:(exprVal class). |
|
6276 |
]. |
|
6277 |
]. |
|
5803 | 6278 |
]. |
6279 |
]. |
|
6280 |
^ setOfPossibleClasses |
|
6281 |
||
6282 |
"Created: / 13-06-2018 / 10:17:42 / Claus Gittinger" |
|
6036 | 6283 |
"Modified: / 18-03-2019 / 11:28:39 / Claus Gittinger" |
5803 | 6284 |
! |
6285 |
||
5109 | 6286 |
classOfNode:aNode |
6287 |
"returns the class of a receiver, if it is well-known. |
|
6288 |
Otherwise nil (either unknown, or multiple possibilities) |
|
6289 |
When showing possible completions for a message, |
|
5621 | 6290 |
it is a good idea to know what the receiver is." |
5109 | 6291 |
|
6292 |
| classes | |
|
6293 |
||
6294 |
classes := self classesOfNode:aNode. |
|
6295 |
classes size == 1 ifTrue:[ |
|
6296 |
^ classes anElement |
|
6297 |
]. |
|
5621 | 6298 |
"/ self breakPoint:#cg. |
5109 | 6299 |
^ nil |
5621 | 6300 |
|
6301 |
"Modified: / 10-10-2017 / 16:56:43 / cg" |
|
5109 | 6302 |
! |
6303 |
||
6304 |
classesFromAssignmentTo:varName in:aTree |
|
6305 |
^ self addClassesFromAssignmentTo:varName in:aTree to:IdentitySet new |
|
6306 |
! |
|
6307 |
||
6308 |
classesOfInstVarNamed:varName inClass:aClass |
|
6309 |
^ self addClassesOfInstVarNamed:varName inClass:aClass to:(IdentitySet new) |
|
6310 |
! |
|
6311 |
||
6312 |
classesOfNode:aNode |
|
6313 |
"returns the set of possible classes of a parsenode. |
|
6314 |
or nil if unknown. |
|
6315 |
When showing possible completions for a message, |
|
6316 |
it is a good idea to know what the kind receiver is." |
|
6317 |
||
5606 | 6318 |
|setOfPossibleClasses| |
6319 |
||
6320 |
setOfPossibleClasses := IdentitySet new. |
|
6321 |
self addClassesOfExpression:aNode inClass:classOrNil to:setOfPossibleClasses. |
|
6322 |
^ setOfPossibleClasses. |
|
5453 | 6323 |
|
6324 |
"Modified: / 05-02-2017 / 12:40:16 / cg" |
|
5606 | 6325 |
"Modified (format): / 15-09-2017 / 10:11:19 / cg" |
6326 |
! |
|
6327 |
||
6328 |
extractConstraintsFor:expr inClass:dummyClassOrNil |
|
6329 |
"see if expr is contained inside an isXXX ifTrue:[...] |
|
6330 |
then, we know a lot more... |
|
6331 |
For example, to expand possible messages for XXX in: |
|
6332 |
foo isString ifTrue:[ |
|
6333 |
f XXX |
|
6334 |
]. |
|
6335 |
we now have to care for instances for which isString returns true only" |
|
6336 |
||
6337 |
|node parentNode possibleClasses allImplementors condition classesReturningTrue classesReturningFalse| |
|
6338 |
||
6339 |
possibleClasses := Set new. |
|
6340 |
||
6341 |
node := expr. |
|
6342 |
[ |
|
6343 |
parentNode := node parent. |
|
6344 |
parentNode isNil ifTrue:[ |
|
6345 |
"/ due to the partial parse (being right in the middle of a parse), |
|
6346 |
"/ the parent may be undefined (for example, if closing bracket of a block was not yet entered) |
|
6347 |
"/ then the parse stopped and we have the partial parent message in the rememberedNodes list. |
|
6348 |
"/ try there; if found, continue there. If not, well, we might be really at the top. |
|
5608 | 6349 |
rememberedNodes notNil ifTrue:[ |
6350 |
rememberedNodes do:[:eachPossibleParentNode | |
|
5609 | 6351 |
(eachPossibleParentNode stop notNil and:[ node start notNil ]) ifTrue:[ |
5608 | 6352 |
eachPossibleParentNode stop <= node start ifTrue:[ |
6353 |
(parentNode isNil or:[parentNode stop < eachPossibleParentNode stop]) ifTrue:[ |
|
6354 |
parentNode := eachPossibleParentNode |
|
6355 |
]. |
|
5606 | 6356 |
]. |
6357 |
]. |
|
5608 | 6358 |
]. |
5612 | 6359 |
parentNode notNil ifTrue:[ rememberedNodes removeIdentical:parentNode ]. |
5606 | 6360 |
]. |
6361 |
]. |
|
6362 |
parentNode notNil |
|
6363 |
] whileTrue:[ |
|
6364 |
(parentNode isMessage |
|
6365 |
and:[#(ifTrue:) includes:parentNode selector]) |
|
6366 |
ifTrue:[ |
|
6367 |
(condition := parentNode receiver) isMessage ifTrue:[ |
|
6368 |
condition receiver isVariable ifTrue:[ |
|
6369 |
condition receiver name = expr name ifTrue:[ |
|
6370 |
"/ here, we have an if, sending some message to the same receiver variable |
|
6371 |
allImplementors := Smalltalk allImplementorsOf:condition selector. |
|
5633 | 6372 |
SourceCodeManagerError handle:[:ex | |
6373 |
] do:[ |
|
6374 |
classesReturningTrue := |
|
6375 |
allImplementors select:[:cls | |
|
6376 |
|mthd| |
|
6377 |
||
6378 |
mthd := cls compiledMethodAt:condition selector. |
|
6379 |
ParseTreeSearcher methodIsJustReturningTrue:mthd. |
|
6380 |
]. |
|
6381 |
||
6382 |
classesReturningTrue do:[:eachClass | |
|
6383 |
eachClass withAllSubclassesDo:[:eachSubClass | |
|
6384 |
possibleClasses add:eachSubClass |
|
6385 |
]. |
|
5606 | 6386 |
]. |
6387 |
]. |
|
6388 |
^ possibleClasses. |
|
6389 |
]. |
|
6390 |
]. |
|
6391 |
]. |
|
6392 |
]. |
|
6393 |
node := parentNode. |
|
6394 |
]. |
|
6395 |
^ possibleClasses |
|
6396 |
||
6397 |
"Created: / 15-09-2017 / 10:16:20 / cg" |
|
5633 | 6398 |
"Modified: / 05-11-2017 / 02:59:59 / cg" |
5109 | 6399 |
! |
6400 |
||
6401 |
isNonDestructive:aMessageNode whenSentTo:receiverValue |
|
6402 |
"return true, if it is safe to send aSelector to receiverValue |
|
6403 |
(i.e. has no side effects)" |
|
6404 |
||
6405 |
|selector method impl arg1Value| |
|
6406 |
||
6407 |
selector := aMessageNode selector. |
|
6408 |
impl := receiverValue class whichClassIncludesSelector:selector. |
|
5706 | 6409 |
impl isNil ifTrue:[ |
6410 |
^ false "/ don't know - but assume it is dangerous |
|
6411 |
]. |
|
5109 | 6412 |
|
6413 |
( #( |
|
6414 |
basicSize basicAt: |
|
6415 |
class theMetaclass theNonMetaclass ) includes:selector |
|
6416 |
) ifTrue:[ |
|
6417 |
^ true. |
|
6418 |
]. |
|
6419 |
||
6420 |
selector == #size ifTrue:[ |
|
6421 |
"/ mhm - be conservative; someone might have redefined #size |
|
6422 |
"/ more hardwired stuff. |
|
6423 |
((impl == Object) or:[(impl == String) or:[impl isSubclassOf:Collection]]) ifTrue:[ |
|
6424 |
^ true. |
|
6425 |
]. |
|
6426 |
]. |
|
6427 |
selector == #at: ifTrue:[ |
|
6428 |
"/ mhm - be conservative; someone might have redefined #at: and do something there |
|
6429 |
"/ more hardwired stuff. |
|
6430 |
arg1Value := self valueOfNode:(aMessageNode arg1). |
|
6431 |
arg1Value notNil ifTrue:[ |
|
6432 |
receiverValue == Smalltalk ifTrue:[ |
|
6433 |
^ arg1Value isSymbol |
|
6434 |
]. |
|
6435 |
||
6436 |
((impl == Object) or:[(impl == String) or:[(impl isSubclassOf:Collection)]]) ifTrue:[ |
|
6437 |
^ true. |
|
6438 |
]. |
|
6439 |
]. |
|
6440 |
]. |
|
6441 |
||
6442 |
selector argumentCount == 0 ifTrue:[ |
|
6443 |
"/ follow non-destructive accessors |
|
6444 |
method := receiverValue class lookupMethodFor:selector. |
|
6445 |
method notNil ifTrue:[ |
|
6446 |
(ParseTreeSearcher methodIsJustReturningSomething:method) ifTrue:[ |
|
6447 |
"/ we can savely call that method to get the current value |
|
6448 |
^ true. |
|
6449 |
]. |
|
6450 |
]. |
|
6451 |
]. |
|
6452 |
||
6453 |
^ false |
|
5512 | 6454 |
|
6455 |
"Modified: / 09-03-2017 / 10:58:50 / cg" |
|
5109 | 6456 |
! |
6457 |
||
6458 |
valueAndKindOfVariable:aVariableName |
|
6459 |
"when showing possible completions for a variable, |
|
6460 |
it is a good idea to know what the reveiver's value is. |
|
6461 |
Sigh - returns nil as value both if unknown AND if a real nil is there" |
|
6462 |
||
6463 |
|nodeVal con classInstVarClass classVarClass privateClass pool sym nameSpace topNameSpace| |
|
6464 |
||
6465 |
aVariableName isUppercaseFirst ifTrue:[ |
|
6466 |
classOrNil notNil ifTrue:[ |
|
6467 |
classOrNil isMeta ifTrue:[ |
|
6468 |
"/ class instVars |
|
6469 |
(classInstVarClass := classOrNil whichClassDefinesInstVar:aVariableName) notNil ifTrue:[ |
|
6470 |
nodeVal := classInstVarClass theNonMetaclass instVarNamed:aVariableName. |
|
6471 |
^ { nodeVal . #classInstVariable } |
|
6472 |
]. |
|
6473 |
]. |
|
6474 |
"/ class vars |
|
6475 |
(classVarClass := classOrNil theNonMetaclass whichClassDefinesClassVar:aVariableName) notNil ifTrue:[ |
|
6476 |
nodeVal := classVarClass classVarAt:aVariableName asSymbol. |
|
6477 |
^ { nodeVal . #classVariable } |
|
6478 |
]. |
|
6479 |
||
6480 |
privateClass := classOrNil theNonMetaclass privateClasses detect:[:cls | cls nameWithoutPrefix = aVariableName] ifNone:nil. |
|
6481 |
privateClass notNil ifTrue:[ |
|
6482 |
nodeVal := privateClass. |
|
6483 |
^ { nodeVal . #privateClass } |
|
6484 |
]. |
|
6485 |
pool := classOrNil theNonMetaclass whichPoolDefinesPoolVar:aVariableName. |
|
6486 |
pool notNil ifTrue:[ |
|
6487 |
nodeVal := pool classVarAt:aVariableName. |
|
6488 |
^ { nodeVal . #poolVariable } |
|
6489 |
]. |
|
5118 | 6490 |
(sym := aVariableName asSymbolIfInterned) notNil ifTrue:[ |
6491 |
((nameSpace := classOrNil nameSpace) notNil and:[nameSpace ~~ Smalltalk]) ifTrue:[ |
|
6492 |
nameSpace isNameSpace ifTrue:[ |
|
6493 |
nodeVal := nameSpace at:sym. |
|
6494 |
(nodeVal notNil or:[nameSpace includesKey:sym]) ifTrue:[ |
|
6495 |
^ { nodeVal . #nameSpaceVariable } |
|
6496 |
]. |
|
6497 |
]. |
|
6498 |
nodeVal := nameSpace privateClassNamed:sym. |
|
6499 |
(nodeVal notNil) ifTrue:[ |
|
6500 |
^ { nodeVal . #privateClass } |
|
6501 |
]. |
|
6502 |
]. |
|
6503 |
((topNameSpace := classOrNil topNameSpace) notNil |
|
6504 |
and:[topNameSpace ~~ nameSpace |
|
6505 |
and:[topNameSpace ~~ Smalltalk]]) ifTrue:[ |
|
6506 |
nodeVal := topNameSpace at:sym. |
|
6507 |
(nodeVal notNil or:[topNameSpace includesKey:sym]) ifTrue:[ |
|
6508 |
^ { nodeVal . #nameSpaceVariable } |
|
6509 |
]. |
|
6510 |
]. |
|
6511 |
]. |
|
5109 | 6512 |
]. |
6513 |
(sym := aVariableName asSymbolIfInterned) notNil ifTrue:[ |
|
6514 |
nodeVal := Smalltalk at:sym. |
|
6515 |
(nodeVal notNil or:[Smalltalk includesKey:sym]) ifTrue:[ |
|
6516 |
^ { nodeVal . #global } |
|
6517 |
] |
|
6518 |
]. |
|
6519 |
||
6520 |
"/ 'evaluate' the variable (like in a browser's codeView) |
|
6521 |
"/ mhmh - will we catch workspace vars then? |
|
5118 | 6522 |
Error ignoreIn:[ |
5109 | 6523 |
nodeVal := Parser new evaluate:aVariableName in:classOrNil receiver:classOrNil. |
6524 |
]. |
|
6525 |
nodeVal notNil ifTrue:[ |
|
6526 |
^ { nodeVal . #global } |
|
6527 |
]. |
|
6528 |
^ nil |
|
6529 |
]. |
|
6530 |
||
6531 |
aVariableName = 'self' ifTrue:[ |
|
6532 |
contextOrNil notNil ifTrue:[ |
|
6533 |
^ { contextOrNil receiver . #pseudoVar } |
|
6534 |
]. |
|
6535 |
(classOrNil notNil and:[classOrNil isMeta]) ifTrue:[ |
|
6536 |
"/ ^ { classOrNil . #pseudoVar } |
|
6537 |
^ { classOrNil theNonMetaclass . #pseudoVar } |
|
6538 |
]. |
|
6539 |
^ nil |
|
6540 |
]. |
|
6541 |
||
6542 |
contextOrNil notNil ifTrue:[ |
|
6543 |
"/ in the debugger, we know more |
|
6544 |
con := contextOrNil. |
|
6545 |
[ con notNil ] whileTrue:[ |
|
6546 |
"/ a local in the context? |
|
6547 |
((con argAndVarNames ? #()) includes:aVariableName) ifTrue:[ |
|
6548 |
nodeVal := con argsAndVars at:(con argAndVarNames indexOf:aVariableName) ifAbsent:nil. |
|
6549 |
nodeVal notNil ifTrue:[ |
|
6550 |
^ { nodeVal . #argument } |
|
6551 |
]. |
|
6552 |
]. |
|
6553 |
con := con home. |
|
6554 |
]. |
|
5370 | 6555 |
"/ an instvar? |
5109 | 6556 |
(contextOrNil receiver class allInstVarNames includes:aVariableName) ifTrue:[ |
5370 | 6557 |
contextOrNil receiver isProtoObject ifFalse:[ |
6558 |
nodeVal := contextOrNil receiver instVarNamed:aVariableName. |
|
6559 |
]. |
|
5109 | 6560 |
nodeVal notNil ifTrue:[ |
6561 |
^ { nodeVal . #instanceVariable } |
|
6562 |
]. |
|
6563 |
]. |
|
6564 |
]. |
|
6565 |
^ nil |
|
6566 |
||
6567 |
"Created: / 01-05-2016 / 12:40:05 / cg" |
|
6568 |
! |
|
6569 |
||
6570 |
valueOfNode:aNode |
|
6571 |
"when showing possible completions for a message, |
|
6572 |
it is a good idea to know what the reveiver's value is. |
|
6573 |
Sigh - returns nil both if unknown AND if a real nil is there." |
|
6574 |
||
5368 | 6575 |
|nodeSelector nodeReceiver receiverValue arg1Value| |
5109 | 6576 |
|
6577 |
aNode isLiteral ifTrue:[ |
|
6578 |
^ aNode value |
|
6579 |
]. |
|
6580 |
aNode isVariable ifTrue:[ |
|
5512 | 6581 |
aNode isSelf ifTrue:[ |
6582 |
instanceOrNil notNil ifTrue:[ |
|
6583 |
^ instanceOrNil |
|
6584 |
]. |
|
6585 |
]. |
|
5109 | 6586 |
^ self valueOfVariable:aNode name. |
6587 |
]. |
|
6588 |
||
6589 |
aNode isMessage ifTrue:[ |
|
6590 |
nodeSelector := aNode selector. |
|
6591 |
nodeReceiver := aNode receiver. |
|
5512 | 6592 |
|
5109 | 6593 |
"/ some hardwired knowledge here |
6594 |
classOrNil notNil ifTrue:[ |
|
6595 |
(nodeReceiver isSelf and:[nodeSelector = #'class']) ifTrue:[ |
|
6596 |
^ classOrNil |
|
6597 |
]. |
|
6598 |
]. |
|
6599 |
||
6600 |
receiverValue := self valueOfNode:nodeReceiver. |
|
6601 |
receiverValue notNil ifTrue:[ |
|
5368 | 6602 |
(self isNonDestructive:aNode whenSentTo:receiverValue) ifTrue:[ |
6603 |
[ |
|
6604 |
nodeSelector argumentCount == 1 ifTrue:[ |
|
6605 |
arg1Value := self valueOfNode:(aNode arg1). |
|
5109 | 6606 |
^ receiverValue perform: nodeSelector with: arg1Value. |
5368 | 6607 |
]. |
6608 |
^ receiverValue perform: nodeSelector. |
|
6609 |
] on:Error do:[ |
|
6610 |
^ nil |
|
6611 |
] |
|
5109 | 6612 |
]. |
6613 |
]. |
|
6614 |
]. |
|
6615 |
||
6616 |
^ nil |
|
6617 |
||
6618 |
"Created: / 28-08-2013 / 16:34:53 / cg" |
|
5512 | 6619 |
"Modified: / 09-03-2017 / 10:58:42 / cg" |
5109 | 6620 |
! |
6621 |
||
6622 |
valueOfVariable:aVariableName |
|
6623 |
"when showing possible completions for a variable, |
|
6624 |
it is a good idea to know what the reveiver's value is. |
|
6625 |
Sigh - returns nil both if unknown AND if a real nil is there." |
|
6626 |
||
6627 |
|valueAndKind| |
|
6628 |
||
6629 |
(valueAndKind := self valueAndKindOfVariable:aVariableName) notNil ifTrue:[ |
|
6630 |
self assert:valueAndKind isArray. |
|
6631 |
^ valueAndKind first. |
|
6632 |
]. |
|
6633 |
^ nil |
|
6634 |
||
5225 | 6635 |
"Modified: / 26-08-2016 / 14:16:16 / cg" |
5109 | 6636 |
! ! |
6637 |
||
5980 | 6638 |
!DoWhatIMeanSupport methodsFor:'setup'! |
6639 |
||
6640 |
classResolverHook:aBlock |
|
6641 |
"additional hook to allow for special class resolving. |
|
6642 |
If non-nil, this hook is called for expression nodes, |
|
6643 |
and may return a class or nil. |
|
6644 |
Used eg. with expecco to resolve pin types" |
|
6645 |
||
6646 |
classResolverHook := aBlock |
|
6647 |
||
6648 |
" |
|
6649 |
self new classResolverHook:[:expr | |
|
6650 |
(expr isVariable and:[expr name = 'foo']) ifTrue:[ |
|
6651 |
{ String } |
|
6652 |
] ifFalse:[ |
|
6653 |
nil |
|
6654 |
] |
|
6655 |
]. |
|
6656 |
||
6657 |
" |
|
6658 |
||
6659 |
"Created: / 27-12-2018 / 12:11:31 / Claus Gittinger" |
|
6660 |
! ! |
|
6661 |
||
3854
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6662 |
!DoWhatIMeanSupport::InputCompletionResult class methodsFor:'instance creation'! |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6663 |
|
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6664 |
bestName:bestNameArg matchingNames:matchingNamesArg |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6665 |
^ self with:bestNameArg with:matchingNamesArg |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6666 |
|
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6667 |
" |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6668 |
self bestName:123 matchingNames:345 |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6669 |
" |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6670 |
! ! |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6671 |
|
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6672 |
!DoWhatIMeanSupport::InputCompletionResult methodsFor:'accessing'! |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6673 |
|
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6674 |
bestName |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6675 |
^ self at:1 |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6676 |
! |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6677 |
|
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6678 |
matchingNames |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6679 |
^ self at:2 |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6680 |
! ! |
d7904ce12bb6
changed: #classnameCompletion:filter:inEnvironment:
Claus Gittinger <cg@exept.de>
parents:
3853
diff
changeset
|
6681 |
|
2376 | 6682 |
!DoWhatIMeanSupport class methodsFor:'documentation'! |
6683 |
||
4281 | 6684 |
version |
4784 | 6685 |
^ '$Header$' |
4281 | 6686 |
! |
6687 |
||
3761
6584390d427d
dont forget pool variables when looking for the best variable match
Claus Gittinger <cg@exept.de>
parents:
3751
diff
changeset
|
6688 |
version_CVS |
4784 | 6689 |
^ '$Header$' |
2376 | 6690 |
! ! |
4194 | 6691 |