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