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