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