author | Claus Gittinger <cg@exept.de> |
Wed, 23 Aug 2017 13:32:38 +0200 | |
changeset 4187 | a315b2137a68 |
parent 4177 | 43a98615134a |
child 4197 | 3ea2e07f886c |
permissions | -rw-r--r-- |
2 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
3810 | 3 |
All Rights Reserved |
2 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
1081 | 12 |
"{ Package: 'stx:libcomp' }" |
13 |
||
3548 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
141 | 16 |
Parser subclass:#Explainer |
3813 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'System-Compiler' |
|
2 | 21 |
! |
22 |
||
3866 | 23 |
Object variableSubclass:#ActionWithInfo |
24 |
instanceVariableNames:'block info' |
|
25 |
classVariableNames:'' |
|
26 |
poolDictionaries:'' |
|
27 |
privateIn:Explainer |
|
28 |
! |
|
29 |
||
396 | 30 |
!Explainer class methodsFor:'documentation'! |
2 | 31 |
|
20 | 32 |
copyright |
33 |
" |
|
34 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
3810 | 35 |
All Rights Reserved |
20 | 36 |
|
37 |
This software is furnished under a license and may be used |
|
38 |
only in accordance with the terms of that license and with the |
|
39 |
inclusion of the above copyright notice. This software may not |
|
40 |
be provided or otherwise made available to, or used by, any |
|
41 |
other person. No title to or ownership of the software is |
|
42 |
hereby transferred. |
|
43 |
" |
|
44 |
! |
|
45 |
||
2 | 46 |
documentation |
47 |
" |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
48 |
a very simple explainer - much more should be added... |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
49 |
|
3608 | 50 |
This one is involved, when the 'explain' menu function is used in a |
1728 | 51 |
codeView (popup or via CMD-E), or if the mouse button is clicked |
3608 | 52 |
on some code construct in the new browser (then a short explanaition |
53 |
is shown in the lower info area). |
|
263 | 54 |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
55 |
Most of the texts returned here are heuristically motivated, |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
56 |
based on the experience with beginners and their frequently asked questions. |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
57 |
|
3885 | 58 |
TODO: |
59 |
this is in an experimental stage; the naive type inferer replicates code |
|
60 |
found in DWIM support; it should use the code there. |
|
61 |
||
263 | 62 |
[author:] |
3608 | 63 |
Claus Gittinger |
2 | 64 |
" |
65 |
! ! |
|
66 |
||
396 | 67 |
!Explainer class methodsFor:'explaining'! |
2 | 68 |
|
2193
9416fc17ee62
pass the interval to the explainer
Claus Gittinger <cg@exept.de>
parents:
2191
diff
changeset
|
69 |
explainLiteralNode:node in:code forClass:cls short:short interval:intervalIfKnown |
3842 | 70 |
|expl literalValue literalsClass findInnerMost elementIndex codeOfCharacterBeforeCursor| |
1728 | 71 |
|
72 |
literalValue := node value. |
|
3842 | 73 |
literalsClass := literalValue class. |
74 |
expl := (self asClassLink:literalsClass name "allBold") , '-constant'. |
|
2954
0943337cfb73
changed: #explainLiteralNode:in:forClass:short:interval:
Claus Gittinger <cg@exept.de>
parents:
2940
diff
changeset
|
75 |
|
2316
f09794e008f8
changed: #explainLiteralNode:in:forClass:short:interval:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
76 |
(literalValue isInteger) ifTrue:[ |
3697 | 77 |
(literalValue ~~ 0 and:[literalValue ~~ 1]) ifTrue:[ |
3319 | 78 |
expl := expl , ' ('. |
3798 | 79 |
#(2 10 16) with:#('binary: ' 'decimal: ' 'hex: ') do:[:base :baseExpl | |
3319 | 80 |
|bStr| |
3203 | 81 |
|
3319 | 82 |
base ~= (node token radix ? 10) ifTrue:[ |
83 |
bStr := base==10 |
|
84 |
ifTrue:[literalValue printString] |
|
3798 | 85 |
ifFalse:[(literalValue printStringRadix:base) leftPaddedTo:2 with:$0]. "/ looks better: at least a size of 2 |
86 |
"/ oops - someone looks at a largeInteger |
|
87 |
bStr := bStr contractAtEndTo:40. |
|
3319 | 88 |
expl := expl , ' ' , baseExpl, bStr |
89 |
]. |
|
90 |
]. |
|
91 |
expl := expl , ' )'. |
|
3798 | 92 |
]. |
3319 | 93 |
^ expl. |
94 |
]. |
|
95 |
(literalValue isCharacter) ifTrue:[ |
|
96 |
expl := expl , ' ',literalValue storeString,' (code: ',literalValue codePoint printString,' hex: 16r',literalValue codePoint hexPrintString,')'. |
|
97 |
^ expl. |
|
2316
f09794e008f8
changed: #explainLiteralNode:in:forClass:short:interval:
Claus Gittinger <cg@exept.de>
parents:
2315
diff
changeset
|
98 |
]. |
2954
0943337cfb73
changed: #explainLiteralNode:in:forClass:short:interval:
Claus Gittinger <cg@exept.de>
parents:
2940
diff
changeset
|
99 |
|
0943337cfb73
changed: #explainLiteralNode:in:forClass:short:interval:
Claus Gittinger <cg@exept.de>
parents:
2940
diff
changeset
|
100 |
(node isLiteralArray and:[intervalIfKnown notNil]) ifTrue:[ |
3319 | 101 |
"/ see which element we have to explain |
102 |
findInnerMost := |
|
103 |
[:token | |
|
104 |
token value keysAndValuesDo:[:index :eachToken | |
|
3810 | 105 |
|selectorOrName selectorExplanation| |
2954
0943337cfb73
changed: #explainLiteralNode:in:forClass:short:interval:
Claus Gittinger <cg@exept.de>
parents:
2940
diff
changeset
|
106 |
|
3319 | 107 |
((eachToken start to:eachToken stop) intersect:intervalIfKnown) notEmpty ifTrue:[ |
108 |
elementIndex := index. "/ see below |
|
2954
0943337cfb73
changed: #explainLiteralNode:in:forClass:short:interval:
Claus Gittinger <cg@exept.de>
parents:
2940
diff
changeset
|
109 |
|
3319 | 110 |
eachToken value isArray ifTrue:[ |
111 |
findInnerMost value:eachToken. |
|
112 |
]. |
|
113 |
eachToken value isSymbol ifTrue:[ |
|
114 |
selectorOrName := eachToken value. |
|
115 |
selectorOrName isUppercaseFirst ifTrue:[ |
|
116 |
(Smalltalk includesKey:selectorOrName) ifTrue:[ |
|
117 |
^ expl, ' / ', (Explainer explainGlobal:selectorOrName inClass:cls short:short) |
|
118 |
]. |
|
119 |
]. |
|
3810 | 120 |
selectorExplanation := (Explainer explainSelector:selectorOrName inClass:cls short:short). |
121 |
selectorExplanation isNil ifTrue:[^ expl]. |
|
122 |
^ expl, ' / ', selectorExplanation. |
|
3319 | 123 |
]. |
124 |
]. |
|
125 |
] |
|
126 |
]. |
|
127 |
findInnerMost value:node token. |
|
2954
0943337cfb73
changed: #explainLiteralNode:in:forClass:short:interval:
Claus Gittinger <cg@exept.de>
parents:
2940
diff
changeset
|
128 |
]. |
0943337cfb73
changed: #explainLiteralNode:in:forClass:short:interval:
Claus Gittinger <cg@exept.de>
parents:
2940
diff
changeset
|
129 |
|
2955
d52a51930921
changed: #explainLiteralNode:in:forClass:short:interval:
Claus Gittinger <cg@exept.de>
parents:
2954
diff
changeset
|
130 |
(literalValue isArray or:[ literalValue isByteArray or:[ literalValue isString ]]) ifTrue:[ |
3319 | 131 |
literalValue size == 0 ifTrue:[ |
132 |
^ 'empty ' , expl |
|
133 |
]. |
|
134 |
elementIndex notNil ifTrue:[ |
|
135 |
^ expl , (' (size=%1; element=%2)' bindWith:literalValue size with:elementIndex) |
|
136 |
]. |
|
137 |
literalValue isString ifTrue:[ |
|
138 |
elementIndex := intervalIfKnown start - node token start. |
|
2955
d52a51930921
changed: #explainLiteralNode:in:forClass:short:interval:
Claus Gittinger <cg@exept.de>
parents:
2954
diff
changeset
|
139 |
|
3319 | 140 |
(elementIndex between:1 and:literalValue size) ifTrue:[ |
3601 | 141 |
codeOfCharacterBeforeCursor := (literalValue at:elementIndex) codePoint. |
142 |
^ expl , (' (size=%1; position=%2; codePoint=16r%3)' |
|
143 |
bindWith:literalValue size |
|
144 |
with:elementIndex |
|
145 |
with:codeOfCharacterBeforeCursor hexPrintString) |
|
3319 | 146 |
] |
147 |
]. |
|
148 |
^ expl , (' (size=%1)' bindWith:literalValue size) |
|
1728 | 149 |
]. |
2191
87e08ab150e2
improved explain (symbol in an array literal)
Claus Gittinger <cg@exept.de>
parents:
2190
diff
changeset
|
150 |
|
1728 | 151 |
^ expl |
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
152 |
|
3798 | 153 |
"Modified (format): / 30-04-2016 / 16:00:05 / cg" |
1728 | 154 |
! |
155 |
||
2193
9416fc17ee62
pass the interval to the explainer
Claus Gittinger <cg@exept.de>
parents:
2191
diff
changeset
|
156 |
explainMessageNode:node in:code forClass:cls short:short interval:intervalIfKnown |
1771 | 157 |
"answer a string which explains node" |
158 |
||
3203 | 159 |
|receiver nm srchClass selector selectorString implClass |
1728 | 160 |
boldSelectorString globalValue recClassSet |
3855 | 161 |
implMethod implMethodComment info definer |
1854 | 162 |
instances classesOfInstVars implementingClasses canBeNil |
3866 | 163 |
bestMatches hint redefiningClasses| |
1728 | 164 |
|
3538 | 165 |
selector := node buildSelectorString. |
3842 | 166 |
selectorString := selector printString contractTo:50. |
3548 | 167 |
selector := selector asSymbolIfInterned. "/ avoid creating new symbols. |
3810 | 168 |
selector isNil ifTrue:[ |
169 |
^ '%1 is NOWHERE impemented.' bindWith:boldSelectorString |
|
170 |
]. |
|
171 |
||
3859 | 172 |
selectorString := self asLink:selectorString to:(self actionToBrowseImplementorsOf:selector). |
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
173 |
boldSelectorString := selectorString "allBold". |
3810 | 174 |
|
3885 | 175 |
recClassSet := self guessPossibleClassesFor:(node receiver) in:code forClass:cls. |
1733 | 176 |
recClassSet size == 1 ifTrue:[ |
3209 | 177 |
srchClass := recClassSet first. |
178 |
"take care - Set cannot store nil!!" |
|
179 |
implementingClasses := (recClassSet collect:[:cls | cls whichClassIncludesSelector:selector]) asArray. |
|
1869 | 180 |
|
3209 | 181 |
(implementingClasses includes:nil) ifTrue:[ |
182 |
implementingClasses size > 1 ifTrue:[ |
|
183 |
^ 'possibly not understood: %1 (%2 other implementors)' |
|
184 |
bindWith:selector "allBold" |
|
185 |
with:(implementingClasses size - 1) |
|
186 |
]. |
|
1869 | 187 |
|
3209 | 188 |
(#('self' 'super' 'true' 'false' 'thisContext') includes:selector) ifTrue:[ |
3885 | 189 |
^ ('%1 is NOT understood here (missing period after previous statement?)' |
3854 | 190 |
bindWith:selector allBold) |
3209 | 191 |
]. |
3193 | 192 |
|
3854 | 193 |
hint := ''. |
3209 | 194 |
(recClassSet contains:[:cls | cls isMeta not and:[cls theMetaclass canUnderstand:selector]]) ifTrue:[ |
195 |
hint := '. But class understands it - did you mean "rcvr class ',selector,'..." ?'. |
|
196 |
]. |
|
197 |
bestMatches := Parser findBestSelectorsFor:selector in:srchClass. |
|
3927 | 198 |
bestMatches notEmptyOrNil ifTrue:[ |
3928 | 199 |
|guessedClass| |
200 |
guessedClass := srchClass whichClassIncludesSelector:bestMatches first. |
|
201 |
guessedClass notNil ifTrue:[ |
|
3927 | 202 |
^ ('%1 is NOT understood here (best guess is: "%2" from %3)' |
203 |
bindWith:selector allBold |
|
204 |
with:(bestMatches first "allBold") |
|
3928 | 205 |
with:guessedClass name) , hint. |
3927 | 206 |
]. |
3209 | 207 |
]. |
3927 | 208 |
^ ('%1 is NOT understood here' bindWith:selector allBold), hint |
3209 | 209 |
]. |
1733 | 210 |
]. |
211 |
||
212 |
implementingClasses isNil ifTrue:[ |
|
3209 | 213 |
receiver := node receiver. |
214 |
receiver isVariable ifTrue:[ |
|
215 |
nm := receiver name. |
|
216 |
nm = 'self' ifTrue:[ |
|
217 |
srchClass := cls |
|
218 |
]. |
|
219 |
nm = 'super' ifTrue:[ |
|
220 |
srchClass := cls superclass |
|
221 |
]. |
|
222 |
definer := receiver whoDefines:nm. |
|
223 |
definer isNil ifTrue:[ |
|
224 |
"/ not a local or argument |
|
225 |
(cls instanceVariableNames includes:nm) ifTrue:[ |
|
226 |
"/ ok - an instVar; see what values we find... |
|
4059 | 227 |
instances := cls allSubInstances select:[:eachInst| eachInst isProtoObject not]. |
3209 | 228 |
classesOfInstVars := instances collect:[:eachInst | (eachInst instVarNamed:nm) class] as:Set. |
229 |
canBeNil := (classesOfInstVars remove:UndefinedObject ifAbsent:[]) notNil. |
|
230 |
"take care - Set cannot store nil!!" |
|
231 |
implementingClasses := classesOfInstVars collect:[:cls | (cls whichClassIncludesSelector:selector) ? 0]. |
|
232 |
implementingClasses remove:0 ifAbsent:[]. |
|
233 |
] ifFalse:[ |
|
234 |
nm isUppercaseFirst ifTrue:[ |
|
235 |
nm knownAsSymbol ifTrue:[ |
|
236 |
globalValue := Smalltalk at:nm asSymbol. |
|
237 |
globalValue isClass ifTrue:[ |
|
238 |
srchClass := globalValue class. |
|
239 |
]. |
|
240 |
] |
|
241 |
]. |
|
242 |
]. |
|
243 |
]. |
|
244 |
]. |
|
1728 | 245 |
|
3209 | 246 |
receiver isLiteral ifTrue:[ |
247 |
srchClass := receiver value class |
|
248 |
]. |
|
1728 | 249 |
|
3209 | 250 |
srchClass notNil ifTrue:[ |
251 |
implClass := srchClass whichClassIncludesSelector:selector. |
|
252 |
implClass isNil ifTrue:[ |
|
253 |
^ '%1 is NOT understood here.' bindWith:boldSelectorString |
|
254 |
]. |
|
3866 | 255 |
redefiningClasses := srchClass allSubclasses select:[:cls | cls implements:selector]. |
256 |
] ifFalse:[ |
|
257 |
implementingClasses isNil ifTrue:[ |
|
258 |
implementingClasses := Smalltalk allImplementorsOf:selector |
|
259 |
]. |
|
3209 | 260 |
]. |
1728 | 261 |
]. |
262 |
||
3866 | 263 |
implClass isNil ifTrue:[ |
264 |
implementingClasses size == 1 ifTrue:[ |
|
265 |
implClass := implementingClasses anElement. |
|
266 |
] |
|
267 |
]. |
|
268 |
implClass notNil ifTrue:[ |
|
269 |
|clsName action1 action2| |
|
3855 | 270 |
|
3810 | 271 |
implMethod := implClass compiledMethodAt:selector. |
272 |
clsName := implClass name. |
|
3859 | 273 |
clsName := self asLink:clsName to:(self actionToBrowseClass:implClass selector:selector). |
4059 | 274 |
info := '%1 » %2' bindWith:clsName "allBold" with:selectorString. |
3866 | 275 |
info := self asLink:info to:(action1 := self actionToBrowseClass:implClass selector:selector info:nil). |
276 |
||
277 |
redefiningClasses size > 0 ifTrue:[ |
|
278 |
redefiningClasses size == 1 ifTrue:[ |
|
279 |
|redefiner| |
|
280 |
redefiner := redefiningClasses first. |
|
281 |
info := info,(' (redefined in %1)' |
|
282 |
bindWith:(self |
|
283 |
asLink:redefiner name |
|
284 |
to:(action2 := self actionToBrowseClass:redefiner selector:selector))). |
|
285 |
action2 info:((redefiner compiledMethodAt:selector) source) |
|
286 |
] ifFalse:[ |
|
287 |
info := info,' (redefined in ' |
|
288 |
,('%1 classes' |
|
289 |
bindWith:(self |
|
290 |
asLink:redefiningClasses size printString |
|
291 |
info:'Browse redefiners' |
|
292 |
to:(action2 := self actionToBrowseImplementorsOf:selector in:redefiningClasses))) |
|
293 |
,')'. |
|
294 |
action2 info:'Browse redefiners' |
|
295 |
]. |
|
296 |
]. |
|
297 |
||
3885 | 298 |
implMethodComment := self fetchDescriptionOfMethod:"fetchCommentOfMethod:"implMethod. |
3855 | 299 |
implMethodComment notNil ifTrue:[ |
3883 | 300 |
info := info , Character cr , implMethodComment. |
3866 | 301 |
action1 info:implMethod source. |
3855 | 302 |
]. |
303 |
^ info |
|
3842 | 304 |
"/ (srchClass isNil and:[(cls includesBehavior:implClass) not]) ifTrue:[ |
305 |
"/ "/ info := 'guess: ', info. |
|
306 |
"/ info := info , ' (guess)'. |
|
307 |
"/ ]. |
|
1728 | 308 |
] ifFalse:[ |
3209 | 309 |
info := Explainer explainSelector:selector inClass:cls short:short. |
1728 | 310 |
]. |
311 |
||
3798 | 312 |
"/ implementingClasses notEmptyOrNil ifTrue:[ |
313 |
"/ implMethods := implementingClasses collect:[:implClass | implClass compiledMethodAt:selector]. |
|
314 |
"/ implMethods size <= 8 ifTrue:[ |
|
315 |
"/ implMethods size < 4 ifTrue:[ |
|
316 |
"/ "/ show full comments |
|
317 |
"/ comments := implMethods |
|
318 |
"/ collect:[:implMethod | implMethod comment] |
|
319 |
"/ thenSelect:[:comment | comment notEmptyOrNil]. |
|
320 |
"/ ] ifFalse:[ |
|
321 |
"/ "/ show first lines one |
|
322 |
"/ comments := implMethods |
|
323 |
"/ collect:[:implMethod | (self fetchCommentOfMethod:implMethod)] |
|
324 |
"/ thenSelect:[:comment | comment notEmptyOrNil]. |
|
325 |
"/ ]. |
|
326 |
"/ comments := comments collect:[:each | each colorizeAllWith:(UserPreferences current commentColor) ]. |
|
327 |
"/ short ifTrue:[ |
|
328 |
"/ comments := comments collect:[:implMethodComment | implMethodComment firstLine] as:Set. |
|
329 |
"/ comments size == 1 ifTrue:[ |
|
330 |
"/ ^ info , ' ' , (comments first). |
|
331 |
"/ ]. |
|
332 |
"/ ^ info |
|
333 |
"/ ]. |
|
334 |
"/ info := info,'\'withCRs. |
|
335 |
"/ comments |
|
336 |
"/ with:implementingClasses |
|
337 |
"/ do:[:eachComment :eachClass | |
|
338 |
"/ info := info,'\comment in ',eachClass name,':\',(eachComment asStringCollection asString) |
|
339 |
"/ ]. |
|
340 |
"/ ]. |
|
341 |
"/ ]. |
|
1728 | 342 |
^ info |
1854 | 343 |
|
3798 | 344 |
"Modified: / 30-04-2016 / 17:08:11 / cg" |
1728 | 345 |
! |
346 |
||
2193
9416fc17ee62
pass the interval to the explainer
Claus Gittinger <cg@exept.de>
parents:
2191
diff
changeset
|
347 |
explainMethodNode:node in:code forClass:cls short:short interval:intervalIfKnown |
3203 | 348 |
|srchClass selector selectorString implClass |
3859 | 349 |
"sendingMethods numSendingMethods sendingClasses" | |
1728 | 350 |
|
3203 | 351 |
selector := node selector. |
3549 | 352 |
selector := selector asSymbolIfInterned. "/ avoid creating new symbols. |
3842 | 353 |
selectorString := selector printString contractTo:50. |
1728 | 354 |
|
355 |
(srchClass := cls superclass) notNil ifTrue:[ |
|
3549 | 356 |
implClass := srchClass whichClassIncludesSelector:selector. |
357 |
implClass notNil ifTrue:[ |
|
3855 | 358 |
^ '%1 overrides implementation in %2.' |
3859 | 359 |
bindWith:(self asLink:selectorString "allBold" to:(self actionToOpenMethodFinderFor:selector)) |
360 |
with:(self asLink:implClass name "allBold" to:(self actionToBrowseClass:implClass selector:selector)) |
|
3549 | 361 |
]. |
1728 | 362 |
]. |
2223
50b76baab48b
changed: #explainMethodNode:in:forClass:short:interval:
Claus Gittinger <cg@exept.de>
parents:
2214
diff
changeset
|
363 |
(cls includesSelector:selector) ifFalse:[ |
3859 | 364 |
^ '%1: a new method.' bindWith:selectorString "allBold" |
1728 | 365 |
]. |
366 |
"/ |
|
3203 | 367 |
"/ sendingMethods := SystemBrowser |
368 |
"/ allCallsOn:selector |
|
369 |
"/ in:(cls withAllSubclasses , cls allSubclasses) |
|
370 |
"/ ignoreCase:false |
|
1728 | 371 |
"/ match:false. |
372 |
"/ sendingMethods := sendingMethods select:[:eachMethod | eachMethod mclass notNil]. "/ remove unbound ones |
|
373 |
"/ |
|
374 |
"/ sendingClasses := (sendingMethods collect:[:eachMethod | eachMethod mclass]) asSet. |
|
375 |
"/ numSendingMethods := sendingMethods size. |
|
376 |
"/ numSendingMethods == 1 ifTrue:[ |
|
377 |
"/ sendingClasses first == cls ifTrue:[ |
|
3203 | 378 |
"/ ^ '%1: sent locally from %2.' |
1728 | 379 |
"/ bindWith:boldSelectorString |
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
380 |
"/ with:sendingMethods first selector "allBold" |
1728 | 381 |
"/ ]. |
3203 | 382 |
"/ ^ '%1: sent in hierarchy from %2 in %3.' |
1728 | 383 |
"/ bindWith:boldSelectorString |
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
384 |
"/ with:sendingMethods first selector "allBold" |
1728 | 385 |
"/ with:sendingClasses first name. |
386 |
"/ ]. |
|
387 |
"/ numSendingMethods == 2 ifTrue:[ |
|
388 |
"/ sendingClasses asIdentitySet size == 1 ifTrue:[ |
|
389 |
"/ sendingClasses first == cls ifTrue:[ |
|
3203 | 390 |
"/ ^ '%1: sent locally from %2 and %3.' |
1728 | 391 |
"/ bindWith:boldSelectorString |
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
392 |
"/ with:sendingMethods first selector "allBold" |
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
393 |
"/ with:sendingMethods second selector "allBold" |
1728 | 394 |
"/ ]. |
3203 | 395 |
"/ ^ '%1: sent in hierarchy from %2 and %3 in %4.' |
1728 | 396 |
"/ bindWith:boldSelectorString |
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
397 |
"/ with:sendingMethods first selector "allBold" |
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
398 |
"/ with:sendingMethods second selector "allBold" |
1728 | 399 |
"/ with:sendingClasses first name. |
400 |
"/ ]. |
|
401 |
"/ ]. |
|
402 |
"/ numSendingMethods == 0 ifTrue:[ |
|
3203 | 403 |
"/"/ ^ '%1: no sender found.' |
1728 | 404 |
"/"/ bindWith:boldSelectorString. |
405 |
"/ ^ nil |
|
406 |
"/ ]. |
|
3203 | 407 |
"/ ^ '%1: %2 sending methods in hierarchy.' |
1728 | 408 |
"/ bindWith:boldSelectorString |
409 |
"/ with:numSendingMethods. |
|
410 |
"/ |
|
411 |
"/ "/ the following is too expensive... |
|
412 |
"/"/ sendingMethods := SystemBrowser allCallsOn:selector in:Smalltalk allClasses. |
|
413 |
"/"/ numSendingMethods == 0 ifTrue:[ |
|
414 |
"/"/ ^ '%1: nowhere sent.' bindWith:boldSelectorString |
|
415 |
"/"/ ]. |
|
416 |
"/"/ numSendingMethods == 1 ifTrue:[ |
|
417 |
"/"/ sendingMethods first mclass == cls ifTrue:[ |
|
3203 | 418 |
"/"/ ^ '%1: only sent from %2.' |
1728 | 419 |
"/"/ bindWith:boldSelectorString |
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
420 |
"/"/ with:sendingMethods first selector "allBold" |
1728 | 421 |
"/"/ ]. |
3203 | 422 |
"/"/ ^ '%1: only sent from %2 in %3.' |
1728 | 423 |
"/"/ bindWith:boldSelectorString |
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
424 |
"/"/ with:sendingMethods first selector "allBold" |
1728 | 425 |
"/"/ with:sendingMethods first mclass name. |
426 |
"/"/ ]. |
|
427 |
"/"/ sendingClasses := (sendingMethods collect:[:eachMethod | eachMethod mclass]) asSet. |
|
428 |
"/"/ sendingClasses size == 1 ifTrue:[ |
|
429 |
"/"/ sendingClasses first == cls ifTrue:[ |
|
3203 | 430 |
"/"/ ^ '%1: locally sent from %2 methods.' |
1728 | 431 |
"/"/ bindWith:boldSelectorString |
432 |
"/"/ with:numSendingMethods |
|
433 |
"/"/ ]. |
|
3203 | 434 |
"/"/ ^ '%1 only sent from %2 methods in %3.' |
1728 | 435 |
"/"/ bindWith:boldSelectorString |
436 |
"/"/ with:numSendingMethods |
|
437 |
"/"/ with:sendingClasses first name. |
|
438 |
"/"/ ]. |
|
439 |
||
440 |
^ nil |
|
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
441 |
|
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
442 |
"Modified: / 09-10-2006 / 12:11:16 / cg" |
1728 | 443 |
! |
444 |
||
3203 | 445 |
explainNode:node in:code forClass:cls short:short |
2193
9416fc17ee62
pass the interval to the explainer
Claus Gittinger <cg@exept.de>
parents:
2191
diff
changeset
|
446 |
self explainNode:node in:code forClass:cls short:short interval:nil |
9416fc17ee62
pass the interval to the explainer
Claus Gittinger <cg@exept.de>
parents:
2191
diff
changeset
|
447 |
! |
9416fc17ee62
pass the interval to the explainer
Claus Gittinger <cg@exept.de>
parents:
2191
diff
changeset
|
448 |
|
9416fc17ee62
pass the interval to the explainer
Claus Gittinger <cg@exept.de>
parents:
2191
diff
changeset
|
449 |
explainNode:node in:code forClass:cls short:short interval:intervalIfKnown |
1728 | 450 |
node isVariable ifTrue:[ |
3810 | 451 |
^ self explainVariableNode:node in:code forClass:cls short:short interval:intervalIfKnown. |
1728 | 452 |
]. |
453 |
||
454 |
node isLiteral ifTrue:[ |
|
3810 | 455 |
^ self explainLiteralNode:node in:code forClass:cls short:short interval:intervalIfKnown |
1728 | 456 |
]. |
457 |
||
458 |
node isMessage ifTrue:[ |
|
3810 | 459 |
^ self explainMessageNode:node in:code forClass:cls short:short interval:intervalIfKnown |
1728 | 460 |
]. |
461 |
||
462 |
node isMethod ifTrue:[ |
|
3810 | 463 |
^ self explainMethodNode:node in:code forClass:cls short:short interval:intervalIfKnown |
1728 | 464 |
]. |
465 |
^ nil |
|
466 |
! |
|
467 |
||
2193
9416fc17ee62
pass the interval to the explainer
Claus Gittinger <cg@exept.de>
parents:
2191
diff
changeset
|
468 |
explainVariableNode:node in:code forClass:cls short:short interval:intervalIfKnown |
3842 | 469 |
|expl nm nmBold definingNode namePart argNode argClass argClassSet| |
1728 | 470 |
|
471 |
nm := node name. |
|
472 |
||
473 |
(#( 'self' 'super' 'thisContext' 'here') includes:nm) ifTrue:[ |
|
3810 | 474 |
^ Explainer explainPseudoVariable:nm in:cls short:short |
1728 | 475 |
]. |
476 |
||
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
477 |
nm notNil ifTrue:[ nmBold := nm "allBold" ]. |
1728 | 478 |
|
479 |
definingNode := node whoDefines:nm. |
|
480 |
definingNode notNil ifTrue:[ |
|
3810 | 481 |
namePart := '''' , nmBold , ''''. |
482 |
definingNode isMethod ifTrue:[ |
|
3842 | 483 |
argNode := definingNode arguments detect:[:arg | arg name = nm] ifNone:nil. |
484 |
argNode notNil ifTrue:[ |
|
3883 | 485 |
expl := namePart , ': a method argument.'. |
3842 | 486 |
|
3885 | 487 |
argClassSet := self guessPossibleClassesFor:argNode in:code forClass:cls. |
3842 | 488 |
argClassSet size == 1 ifTrue:[ |
489 |
argClass := argClassSet first. |
|
490 |
]. |
|
3810 | 491 |
]. |
492 |
]. |
|
493 |
expl isNil ifTrue:[ |
|
494 |
definingNode isBlock ifTrue:[ |
|
495 |
(definingNode arguments contains:[:arg | arg name = nm]) ifTrue:[ |
|
3883 | 496 |
expl := namePart , ': a block argument.' |
3810 | 497 |
]. |
498 |
]. |
|
499 |
]. |
|
500 |
expl isNil ifTrue:[ |
|
3885 | 501 |
| parentNode setOfTypes isLocal typesDescription | |
3810 | 502 |
|
503 |
(parentNode := definingNode parent) notNil ifTrue:[ |
|
504 |
(isLocal := parentNode isMethod) ifTrue:[ |
|
3883 | 505 |
expl := namePart , ': a method temporary.'. |
3810 | 506 |
] ifFalse:[ |
507 |
(isLocal := parentNode isBlock) ifTrue:[ |
|
3883 | 508 |
expl := namePart , ': a block temporary.'. |
3810 | 509 |
] |
510 |
]. |
|
511 |
isLocal ifTrue:[ |
|
512 |
setOfTypes := Set new. |
|
513 |
self addTypesAssignedToLocal:nm inTree:parentNode to:setOfTypes. |
|
3885 | 514 |
setOfTypes remove:UndefinedObject ifAbsent:[]. |
3810 | 515 |
typesDescription := self typeDescriptionFor:setOfTypes andSelector:nil. |
516 |
typesDescription notNil ifTrue:[ |
|
517 |
expl := expl,' (',typesDescription,')'. |
|
518 |
]. |
|
519 |
]. |
|
520 |
]. |
|
521 |
]. |
|
522 |
expl isNil ifTrue:[ |
|
3883 | 523 |
expl := namePart , ': temporary.' |
3810 | 524 |
]. |
525 |
(cls allInstanceVariableNames includes:nm) ifTrue:[ |
|
3883 | 526 |
expl := expl , ' (instance variable is hidden)' |
3810 | 527 |
]. |
528 |
^ expl. |
|
1728 | 529 |
]. |
1938 | 530 |
|
1728 | 531 |
^ Explainer explain:node name in:code forClass:cls short:short |
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
532 |
|
1938 | 533 |
"Modified: / 07-11-2006 / 12:22:09 / cg" |
1728 | 534 |
! |
535 |
||
3203 | 536 |
fetchCommentOfMethod:mthd |
3885 | 537 |
"retrieve the comment of a method |
538 |
(if possible and there is one; otherwise, return nil)" |
|
539 |
||
540 |
|methodSource methodComment lines maxNumLines| |
|
541 |
||
542 |
self withWaitCursorDo:[ |
|
543 |
SourceCodeManagerError handle:[:ex | |
|
544 |
] do:[ |
|
545 |
methodSource := mthd source. |
|
546 |
methodComment := mthd comment |
|
3883 | 547 |
]. |
3885 | 548 |
]. |
3883 | 549 |
|
3885 | 550 |
methodComment isEmptyOrNil ifTrue:[^ nil]. |
551 |
||
552 |
lines := methodComment asStringCollection. |
|
553 |
maxNumLines := 1. |
|
554 |
true ifTrue:[ |
|
555 |
methodComment := (lines copyToMax:maxNumLines) asString. |
|
556 |
maxNumLines := 5. |
|
557 |
] ifFalse:[ |
|
558 |
methodComment := lines first. |
|
559 |
methodComment := methodComment withoutSeparators. |
|
560 |
(methodComment endsWith:',') ifTrue:[ methodComment := methodComment copyButLast:1]. |
|
561 |
methodComment := methodComment withoutSeparators. |
|
562 |
]. |
|
563 |
(lines size > maxNumLines) ifTrue:[ |
|
564 |
methodComment := methodComment , '\...' withCRs |
|
3233 | 565 |
]. |
3885 | 566 |
^ (methodComment) withColor:(UserPreferences current commentColor). |
567 |
||
568 |
"Created: / 14-09-2006 / 14:11:58 / cg" |
|
569 |
"Modified (comment): / 30-04-2016 / 16:17:18 / cg" |
|
570 |
! |
|
571 |
||
572 |
fetchDescriptionOfMethod:mthd |
|
573 |
"retrieve a desription of a method |
|
574 |
(if possible and there is one; otherwise, return nil)" |
|
575 |
||
3888 | 576 |
|methodDecl methodSource methodComment lines maxNumLines firstIndent| |
3885 | 577 |
|
578 |
self withWaitCursorDo:[ |
|
579 |
SourceCodeManagerError handle:[:ex | |
|
580 |
] do:[ |
|
581 |
methodSource := mthd source. |
|
3918 | 582 |
mthd selector isNil ifTrue:[ |
583 |
methodDecl := '??? (method removed from class)'. |
|
584 |
] ifFalse:[ |
|
585 |
methodDecl := mthd methodDefinitionTemplate. |
|
586 |
]. |
|
3885 | 587 |
methodComment := mthd comment. |
588 |
]. |
|
589 |
]. |
|
590 |
methodComment isEmptyOrNil ifTrue:[ |
|
591 |
^ methodDecl |
|
3888 | 592 |
]. |
3891 | 593 |
lines := methodComment asStringCollection withoutLeadingAndTrailingBlankLines. |
594 |
lines isEmptyOrNil ifTrue:[ |
|
595 |
^ methodDecl |
|
596 |
]. |
|
597 |
||
3888 | 598 |
firstIndent := 0. |
3889 | 599 |
lines size > 1 ifTrue:[ |
3888 | 600 |
|line2| |
3889 | 601 |
line2 := lines at:2. |
3888 | 602 |
line2 notEmpty ifTrue:[ |
4153 | 603 |
firstIndent := (line2 indexOfNonSeparator - 1) max:0. |
3888 | 604 |
]. |
3885 | 605 |
]. |
3889 | 606 |
lines := lines |
3888 | 607 |
collect:[:line | |
608 |
|l| |
|
609 |
l := line withTabsExpanded. |
|
610 |
l indexOfNonSeparator > firstIndent ifTrue:[ |
|
611 |
l := l copyFrom:firstIndent+1 |
|
612 |
]. |
|
613 |
l |
|
614 |
]. |
|
3889 | 615 |
methodComment := lines asString. |
3888 | 616 |
|
3885 | 617 |
^ methodDecl , Character cr , |
618 |
(methodComment withColor:(UserPreferences current commentColor)). |
|
3855 | 619 |
|
3888 | 620 |
"/ methodComment isEmptyOrNil ifTrue:[^ nil]. |
621 |
"/ |
|
622 |
"/ lines := methodComment asStringCollection. |
|
623 |
"/ maxNumLines := 1. |
|
624 |
"/true ifTrue:[ |
|
625 |
"/ methodComment := (lines copyToMax:maxNumLines) asString. |
|
626 |
"/ maxNumLines := 5. |
|
627 |
"/] ifFalse:[ |
|
628 |
"/ methodComment := lines first. |
|
629 |
"/ methodComment := methodComment withoutSeparators. |
|
630 |
"/ (methodComment endsWith:',') ifTrue:[ methodComment := methodComment copyButLast:1]. |
|
631 |
"/ methodComment := methodComment withoutSeparators. |
|
632 |
"/]. |
|
633 |
"/ (lines size > maxNumLines) ifTrue:[ |
|
634 |
"/ methodComment := methodComment , '\...' withCRs |
|
635 |
"/ ]. |
|
636 |
"/ ^ (methodComment) withColor:(UserPreferences current commentColor). |
|
1909 | 637 |
|
638 |
"Created: / 14-09-2006 / 14:11:58 / cg" |
|
4153 | 639 |
"Modified (format): / 20-06-2017 / 10:29:13 / cg" |
1909 | 640 |
! |
641 |
||
3885 | 642 |
guessPossibleClassesFor:node in:code forClass:cls |
2191
87e08ab150e2
improved explain (symbol in an array literal)
Claus Gittinger <cg@exept.de>
parents:
2190
diff
changeset
|
643 |
"given a node of some code of a method in cls, |
3885 | 644 |
return a collection of possible types of the node." |
2191
87e08ab150e2
improved explain (symbol in an array literal)
Claus Gittinger <cg@exept.de>
parents:
2190
diff
changeset
|
645 |
|
2809
bbf42cc4d305
changed: #guessPossibleImplementorClassesFor:in:forClass:
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
646 |
|nm globalValue definer instances classesOfInstVars ns| |
1728 | 647 |
|
648 |
node isVariable ifTrue:[ |
|
3549 | 649 |
nm := node name. |
1728 | 650 |
|
3549 | 651 |
nm = #self ifTrue:[ |
652 |
^ cls withAllSubclasses |
|
653 |
]. |
|
654 |
nm = #here ifTrue:[ |
|
655 |
^ cls withAllSuperclasses |
|
656 |
]. |
|
657 |
nm = #super ifTrue:[ |
|
658 |
^ cls allSuperclasses |
|
659 |
]. |
|
1728 | 660 |
|
3549 | 661 |
definer := node whoDefines:nm. |
662 |
definer isNil ifTrue:[ |
|
663 |
"/ not a local or argument |
|
664 |
(cls instanceVariableNames includes:nm) ifTrue:[ |
|
665 |
"/ ok - an instVar; see what values we find... |
|
4059 | 666 |
instances := cls allSubInstances select:[:eachInst | eachInst isProtoObject not]. |
3549 | 667 |
classesOfInstVars := instances collect:[:eachInst | (eachInst instVarNamed:nm) class] as:Set. |
668 |
^ classesOfInstVars. |
|
669 |
]. |
|
670 |
(cls classVariableNames includes:nm) ifTrue:[ |
|
671 |
^ Array with:(cls classVarAt:nm asSymbol) class. |
|
672 |
]. |
|
673 |
nm isUppercaseFirst ifTrue:[ |
|
674 |
nm knownAsSymbol ifTrue:[ |
|
675 |
globalValue := Smalltalk at:nm asSymbol. |
|
676 |
globalValue isClass ifTrue:[ |
|
677 |
^ Array with:globalValue class. |
|
678 |
]. |
|
679 |
]. |
|
680 |
((ns := cls topNameSpace) notNil and:[ns ~~ Smalltalk]) ifTrue:[ |
|
681 |
nm knownAsSymbol ifTrue:[ |
|
682 |
globalValue := ns at:nm asSymbol. |
|
683 |
globalValue isClass ifTrue:[ |
|
684 |
^ Array with:globalValue class. |
|
685 |
]. |
|
686 |
]. |
|
687 |
] |
|
688 |
]. |
|
689 |
]. |
|
3885 | 690 |
definer isMethod ifTrue:[ |
691 |
|callers argNr setOfTypes| |
|
692 |
||
693 |
argNr := definer arguments findFirst:[:arg | arg name = nm]. |
|
694 |
setOfTypes := IdentitySet new. |
|
695 |
"/ see who calls this message; are there any calls with an obvious type? |
|
696 |
callers := SystemBrowser allCallsOn:definer selector. |
|
697 |
callers do:[:eachCaller | |
|
698 |
|tree| |
|
3940 | 699 |
(tree := eachCaller parseTree) notNil ifTrue:[ |
700 |
tree allMessageNodesDo:[:msg | |
|
701 |
|argExpr| |
|
702 |
||
703 |
msg selector = definer selector ifTrue:[ |
|
704 |
argExpr := (msg arguments at:argNr). |
|
705 |
self addTypeOfExpressionNode:argExpr forAssignmentTo:nil to:setOfTypes. |
|
706 |
]. |
|
707 |
]. |
|
708 |
]. |
|
3885 | 709 |
]. |
710 |
setOfTypes notEmpty ifTrue:[ |
|
711 |
^ setOfTypes. |
|
712 |
]. |
|
713 |
]. |
|
3549 | 714 |
^ nil |
1728 | 715 |
]. |
716 |
||
717 |
node isLiteral ifTrue:[ |
|
3549 | 718 |
^ Array with:(node value class) |
1728 | 719 |
]. |
720 |
||
2191
87e08ab150e2
improved explain (symbol in an array literal)
Claus Gittinger <cg@exept.de>
parents:
2190
diff
changeset
|
721 |
"/ node isMessage ifTrue:[ |
87e08ab150e2
improved explain (symbol in an array literal)
Claus Gittinger <cg@exept.de>
parents:
2190
diff
changeset
|
722 |
"/ recClassSet := self guessPossibleImplementorClassesFor:(node receiver) in:code forClass:cls. |
87e08ab150e2
improved explain (symbol in an array literal)
Claus Gittinger <cg@exept.de>
parents:
2190
diff
changeset
|
723 |
"/ recClassSet isNil ifTrue:[ ^ nil ]. |
87e08ab150e2
improved explain (symbol in an array literal)
Claus Gittinger <cg@exept.de>
parents:
2190
diff
changeset
|
724 |
"/ recClassSet isEmpty ifTrue:[ ^ nil ]. |
87e08ab150e2
improved explain (symbol in an array literal)
Claus Gittinger <cg@exept.de>
parents:
2190
diff
changeset
|
725 |
"/ |
87e08ab150e2
improved explain (symbol in an array literal)
Claus Gittinger <cg@exept.de>
parents:
2190
diff
changeset
|
726 |
"/ implSet := recClassSet collect:[:recClass | recClass whichClassIncludesSelector:node selector]. |
87e08ab150e2
improved explain (symbol in an array literal)
Claus Gittinger <cg@exept.de>
parents:
2190
diff
changeset
|
727 |
"/ "/ toDo: for each implementor, parse its method for possible return types. |
87e08ab150e2
improved explain (symbol in an array literal)
Claus Gittinger <cg@exept.de>
parents:
2190
diff
changeset
|
728 |
"/ ^ rsltClassSet. |
87e08ab150e2
improved explain (symbol in an array literal)
Claus Gittinger <cg@exept.de>
parents:
2190
diff
changeset
|
729 |
"/ ]. |
1731 | 730 |
^ nil |
2809
bbf42cc4d305
changed: #guessPossibleImplementorClassesFor:in:forClass:
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
731 |
|
bbf42cc4d305
changed: #guessPossibleImplementorClassesFor:in:forClass:
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
732 |
"Modified: / 07-02-2012 / 22:19:53 / cg" |
3885 | 733 |
! |
734 |
||
735 |
withWaitCursorDo:aBlock |
|
736 |
"with wait cursor; |
|
737 |
however: this class is in libcomp (should be in libtool) |
|
738 |
so check if WindowGroup (from libview) is present" |
|
739 |
||
740 |
|windowGroup wg| |
|
741 |
||
742 |
windowGroup := Smalltalk at:#WindowGroup. |
|
743 |
windowGroup notNil ifTrue:[ |
|
744 |
(wg := windowGroup activeGroup) notNil ifTrue:[ |
|
745 |
^ wg withWaitCursorDo:aBlock. |
|
746 |
]. |
|
747 |
]. |
|
748 |
^ aBlock value. |
|
1728 | 749 |
! ! |
750 |
||
751 |
!Explainer class methodsFor:'explaining-naive'! |
|
752 |
||
2 | 753 |
explain:someText in:source forClass:aClass |
6 | 754 |
"Given a source and a substring of it, return a string containing |
755 |
an explanation. |
|
49 | 756 |
This is just a q&d implementation - to be correct, it should use the parser, |
757 |
and explain from the parsetree (instead of doing string matches). |
|
758 |
This leads to some wrong explanations, for example if some string is |
|
839 | 759 |
used as selector within a string, or if a variable is named like a |
760 |
message selector. I.e. the explanation should be context sensitive. |
|
49 | 761 |
Also, there could be much more detailed explanations." |
2 | 762 |
|
2037
003d9b5a2788
explain shared pool variables
Claus Gittinger <cg@exept.de>
parents:
2031
diff
changeset
|
763 |
^ self explain:someText in:source forClass:aClass short:false |
003d9b5a2788
explain shared pool variables
Claus Gittinger <cg@exept.de>
parents:
2031
diff
changeset
|
764 |
|
003d9b5a2788
explain shared pool variables
Claus Gittinger <cg@exept.de>
parents:
2031
diff
changeset
|
765 |
"Modified: / 07-06-2007 / 11:34:05 / cg" |
1183 | 766 |
! |
767 |
||
768 |
explain:someText in:source forClass:aClass short:shortText |
|
769 |
"Given a source and a substring of it, return a string containing |
|
770 |
an explanation. |
|
771 |
This is just a q&d implementation - to be correct, it should use the parser, |
|
772 |
and explain from the parsetree (instead of doing string matches). |
|
773 |
This leads to some wrong explanations, for example if some string is |
|
774 |
used as selector within a string, or if a variable is named like a |
|
775 |
message selector. I.e. the explanation should be context sensitive. |
|
776 |
Also, there could be much more detailed explanations." |
|
777 |
||
3319 | 778 |
|explainer variables c string explanation tmp1 |
4187 | 779 |
spc sym sel stringText cls clsName val valString color| |
2 | 780 |
|
675 | 781 |
string := someText string withoutSeparators. |
2314
0c590d9937bd
changed: #explain:in:forClass:short:
Claus Gittinger <cg@exept.de>
parents:
2292
diff
changeset
|
782 |
string isEmpty ifTrue:[ ^ nil ]. |
0c590d9937bd
changed: #explain:in:forClass:short:
Claus Gittinger <cg@exept.de>
parents:
2292
diff
changeset
|
783 |
|
2214 | 784 |
stringText := string allBold. |
2414
4da315c5bcf4
changed: #explain:in:forClass:short:
Claus Gittinger <cg@exept.de>
parents:
2316
diff
changeset
|
785 |
"/ stringText := '''' , stringText , ''''. |
49 | 786 |
|
787 |
" |
|
788 |
ask parser for variable names |
|
789 |
" |
|
3070 | 790 |
ParseError catch:[ |
3319 | 791 |
explainer := self parseMethod:source in:aClass ignoreErrors:true ignoreWarnings:true. |
1357 | 792 |
]. |
3320 | 793 |
"/ bad leftover from a stupid return value from ancient times |
3319 | 794 |
explainer == #Error ifTrue:[ explainer := nil ]. |
795 |
||
796 |
(explainer notNil) ifTrue:[ |
|
3233 | 797 |
"look for variables" |
2 | 798 |
|
3319 | 799 |
variables := explainer methodVars. |
3233 | 800 |
(variables notNil and:[variables includes:string]) ifTrue:[ |
801 |
^ stringText , ' a method variable.' |
|
802 |
]. |
|
3319 | 803 |
variables := explainer methodArgs. |
3233 | 804 |
(variables notNil and:[variables includes:string]) ifTrue:[ |
805 |
^ stringText , ' a method argument.' |
|
806 |
] |
|
2 | 807 |
]. |
49 | 808 |
|
3319 | 809 |
explainer isNil ifTrue:[ |
810 |
explainer := self for:(ReadStream on:source) in:aClass |
|
2 | 811 |
]. |
812 |
||
2988 | 813 |
"instvars/class instVars" |
84 | 814 |
c := aClass whichClassDefinesInstVar:string. |
815 |
c notNil ifTrue:[ |
|
3233 | 816 |
c isMeta ifTrue:[ |
817 |
clsName := c theNonMetaclass name. |
|
818 |
shortText ifTrue:[ |
|
3866 | 819 |
clsName := self asClassLink:clsName. |
3233 | 820 |
stringText := stringText , ': a class instVar in ' , clsName |
821 |
] ifFalse:[ |
|
822 |
stringText := stringText, ': a class instance variable inherited from ' , clsName |
|
823 |
]. |
|
824 |
val := aClass theNonMetaclass instVarNamed:string. |
|
825 |
valString := self valueStringFor:val. |
|
826 |
^ stringText , ' (' , valString , ').' |
|
827 |
]. |
|
3798 | 828 |
^ self explainInstanceVariable:string inClass:c short:shortText. |
2 | 829 |
]. |
830 |
||
2986 | 831 |
string isWideString ifFalse:[ |
3233 | 832 |
"classvars" |
3319 | 833 |
c := explainer inWhichClassIsClassVar:string. |
3233 | 834 |
c notNil ifTrue:[ |
4187 | 835 |
val := c theNonMetaclass classVarAt:string. "/ Smalltalk at:(clsName , ':' , string) asSymbol. |
836 |
valString := self valueStringFor:val. |
|
837 |
||
838 |
val == true ifTrue:[ |
|
839 |
color := Color green darkened. |
|
840 |
] ifFalse:[ |
|
841 |
val == false ifTrue:[ |
|
842 |
color := Color red darkened |
|
843 |
] ifFalse:[ |
|
844 |
val == nil ifTrue:[ |
|
845 |
color := Color darkGrey |
|
846 |
]. |
|
847 |
]. |
|
848 |
]. |
|
849 |
color notNil ifTrue:[ |
|
850 |
stringText := stringText withColor:color. |
|
851 |
valString := valString withColor:color. |
|
852 |
]. |
|
853 |
||
3233 | 854 |
clsName := c name. |
855 |
shortText ifTrue:[ |
|
3866 | 856 |
clsName := self asClassLink:clsName. |
3233 | 857 |
stringText := stringText , ': a classVar in ' , clsName |
858 |
] ifFalse:[ |
|
859 |
stringText := stringText , ': a class variable in ' , clsName |
|
860 |
]. |
|
2315
45f09cea2063
changed: #explain:in:forClass:short:
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
861 |
|
3233 | 862 |
^ stringText , ' (' , valString , ').' |
863 |
]. |
|
1693 | 864 |
|
3233 | 865 |
"private classes" |
866 |
c := aClass theNonMetaclass. |
|
867 |
c privateClasses do:[:pClass | |
|
868 |
(pClass name = string |
|
869 |
or:[pClass nameWithoutPrefix = string]) ifTrue:[ |
|
3866 | 870 |
stringText := self asClassLink:pClass name. |
3233 | 871 |
stringText := stringText , ': a private class in ''' , c name , '''.'. |
872 |
shortText ifFalse:[ |
|
873 |
stringText := (stringText , '\\It is only visible locally.') withCRs |
|
874 |
]. |
|
875 |
^ stringText withCRs |
|
876 |
]. |
|
877 |
]. |
|
422 | 878 |
|
3233 | 879 |
aClass theNonMetaclass sharedPoolNames do:[:eachPoolName | |
3810 | 880 |
|sharedPool sharedPoolSym poolName| |
2037
003d9b5a2788
explain shared pool variables
Claus Gittinger <cg@exept.de>
parents:
2031
diff
changeset
|
881 |
|
3407 | 882 |
sharedPoolSym := string asSymbolIfInterned. |
883 |
sharedPoolSym notNil ifTrue:[ |
|
884 |
sharedPool := Smalltalk classNamed:eachPoolName. |
|
885 |
sharedPool notNil ifTrue:[ |
|
886 |
sharedPool isSharedPool ifFalse:[ |
|
887 |
^ 'oops - not a shared pool: ',eachPoolName |
|
888 |
]. |
|
889 |
(sharedPool includesKey:sharedPoolSym) ifTrue:[ |
|
3810 | 890 |
poolName := sharedPool name. |
3866 | 891 |
poolName := self asClassLink:eachPoolName. |
3810 | 892 |
stringText := stringText , ': a pool variable in ',poolName. |
3407 | 893 |
val := sharedPool at:sharedPoolSym. |
894 |
valString := self valueStringFor:val. |
|
895 |
^ stringText , ' (' , valString , ').' |
|
896 |
]. |
|
3233 | 897 |
]. |
898 |
]. |
|
899 |
]. |
|
2037
003d9b5a2788
explain shared pool variables
Claus Gittinger <cg@exept.de>
parents:
2031
diff
changeset
|
900 |
|
3233 | 901 |
"namespace & global variables" |
902 |
(spc := aClass nameSpace) notNil ifTrue:[ |
|
903 |
sym := (spc name , '::' , string) asSymbolIfInterned. |
|
904 |
sym notNil ifTrue:[ |
|
905 |
(cls := Smalltalk at:sym) isBehavior ifTrue:[ |
|
3866 | 906 |
stringText := self asClassLink:sym. |
3233 | 907 |
string := stringText , ': '. |
908 |
cls name = sym ifFalse:[ |
|
909 |
string := string , 'refers to ',cls name,', ' |
|
910 |
]. |
|
911 |
cls isSharedPool ifTrue:[ |
|
912 |
string := string , 'a sharedPool'. |
|
913 |
] ifFalse:[ |
|
914 |
string := string , 'a class'. |
|
915 |
]. |
|
3049 | 916 |
|
3233 | 917 |
string := string , ' in the ''' , spc name , ''' nameSpace'. |
918 |
string := string , ' {', cls category ,'}'. |
|
919 |
shortText ifFalse:[ |
|
920 |
string := (string |
|
921 |
, '\\It is only visible within this nameSpace.' |
|
922 |
, '\Access from the outside is possible' |
|
923 |
, '\by the special name ''' , spc name , '::' , string , '''.') withCRs |
|
924 |
]. |
|
925 |
^ string withCRs |
|
926 |
]. |
|
927 |
]. |
|
928 |
]. |
|
422 | 929 |
|
3866 | 930 |
"/ string knownAsSymbol ifTrue:[ |
3233 | 931 |
"globals & symbols" |
839 | 932 |
|
3319 | 933 |
explanation := self explainKnownSymbol:string inClass:aClass short:shortText. |
934 |
explanation notNil ifTrue:[ |
|
3233 | 935 |
string isBinarySelector ifTrue:[ |
936 |
"/ some are both known as syntax AND as selector (for example: #| ) |
|
937 |
tmp1 := self explainSyntax:string short:shortText. |
|
938 |
tmp1 notNil ifTrue:[ |
|
3319 | 939 |
^ tmp1 , '\\also:\\' withCRs , explanation |
3233 | 940 |
]. |
941 |
]. |
|
3319 | 942 |
^ explanation |
3233 | 943 |
]. |
944 |
||
945 |
"/ try with added colon ... |
|
946 |
sel := string , ':'. |
|
947 |
Symbol allInstancesDo:[:sym | |
|
948 |
(sym startsWith:sel) ifTrue:[ |
|
3319 | 949 |
explanation := self explainKnownSymbol:sym inClass:aClass short:shortText. |
950 |
explanation notNil ifTrue:[ ^ explanation]. |
|
3233 | 951 |
] |
952 |
]. |
|
3866 | 953 |
"/ ]. |
2 | 954 |
|
3233 | 955 |
"try for some obvious things" |
3319 | 956 |
explanation := self explainPseudoVariable:string in:aClass short:true. |
957 |
explanation notNil ifTrue:[ ^ explanation]. |
|
2315
45f09cea2063
changed: #explain:in:forClass:short:
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
958 |
]. |
2 | 959 |
|
960 |
"try syntax ..." |
|
961 |
||
3319 | 962 |
explanation := self explainSyntax:string short:shortText. |
963 |
explanation notNil ifTrue:[ ^ explanation]. |
|
20 | 964 |
|
1183 | 965 |
shortText ifTrue:[ |
3883 | 966 |
|selector| |
967 |
||
968 |
(selector := SystemBrowser extractSelectorFrom:string) notNil ifTrue:[ |
|
969 |
selector ~= string string ifTrue:[ |
|
970 |
^ self explain:selector in:source forClass:aClass short:shortText |
|
971 |
]. |
|
4177 | 972 |
]. |
973 |
someText size == 1 ifTrue:[ |
|
974 |
^ 'no explanation; character encoding: %1 (0x%2)' |
|
975 |
bindWith:someText first codePoint |
|
976 |
with:(someText first codePoint hexPrintString:4) |
|
3883 | 977 |
]. |
3233 | 978 |
^ 'no explanation' |
1183 | 979 |
]. |
980 |
||
3319 | 981 |
explainer isNil ifTrue:[ |
3233 | 982 |
^ 'parse error - no explanation' |
2 | 983 |
]. |
3319 | 984 |
^ 'Sorry, I cannot explain this (could not figure out what this is). |
985 |
Please try again with an individual token selected.' |
|
146
3d407ab8ac44
suppress messages on transcript when explaining
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
986 |
|
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
987 |
"Created: / 03-12-1995 / 12:47:37 / cg" |
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
988 |
"Modified: / 16-04-1997 / 12:46:11 / stefan" |
4187 | 989 |
"Modified: / 23-08-2017 / 12:44:12 / cg" |
2 | 990 |
! |
991 |
||
1188 | 992 |
explainGlobal:string inClass:aClass short:shortText |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
993 |
"return an explanation or nil" |
1183 | 994 |
|
2427 | 995 |
^ self explainGlobalOrPoolVariable:string inClass:aClass short:shortText |
996 |
||
997 |
"Modified: / 14-10-2010 / 11:33:17 / cg" |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
998 |
"Modified (comment): / 28-02-2012 / 10:45:58 / cg" |
2427 | 999 |
! |
1000 |
||
3878 | 1001 |
explainGlobalOrPoolVariable:varName inClass:aClass short:shortText |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1002 |
"return an explanation or nil" |
2427 | 1003 |
|
3879 | 1004 |
|template bindings |
3972 | 1005 |
sym explanation val pool valText doc| |
141 | 1006 |
|
3878 | 1007 |
"if not even known as key, it's definitely not a global" |
1008 |
sym := varName asSymbolIfInterned. |
|
796 | 1009 |
sym isNil ifTrue:[^ nil]. |
3879 | 1010 |
|
1011 |
template := nil. |
|
1012 |
bindings := Dictionary new. |
|
1013 |
||
2427 | 1014 |
"a pool variable?" |
1015 |
aClass notNil ifTrue:[ |
|
3319 | 1016 |
aClass theNonMetaclass sharedPoolNames do:[:eachPoolName | |
1017 |
pool := Smalltalk at:eachPoolName. |
|
1018 |
pool isNil ifTrue:[ |
|
3878 | 1019 |
"/ oops - on the fly we encountered a non existing pool... |
3319 | 1020 |
] ifFalse:[ |
3878 | 1021 |
(pool classVarNames includes:varName) ifTrue:[ |
3879 | 1022 |
template := '%(varName): pool variable in "%(poolName)"'. |
1023 |
bindings at:'poolName' put:eachPoolName. |
|
3319 | 1024 |
val := pool classVarAt:sym. |
1025 |
]. |
|
1026 |
]. |
|
1027 |
]. |
|
2427 | 1028 |
]. |
1029 |
||
3879 | 1030 |
bindings at:'varName' put:varName allBold. |
3810 | 1031 |
|
3879 | 1032 |
template isNil ifTrue:[ |
3319 | 1033 |
"try globals" |
3879 | 1034 |
(Smalltalk includesKey:sym) ifFalse:[ |
1035 |
^ '%(varName): an undefined variable.' bindWithArguments:bindings. |
|
1036 |
]. |
|
1037 |
template := '%(varName): global'. |
|
1038 |
val := Smalltalk at:sym. |
|
2427 | 1039 |
]. |
141 | 1040 |
|
1245 | 1041 |
val isBehavior ifTrue:[ |
3319 | 1042 |
"/ a class |
1043 |
val name = sym ifFalse:[ |
|
3879 | 1044 |
"/ an alias (such as OperatingSystem, Screen etc.) |
4040 | 1045 |
template := template,'. Refers to %(realName)'. |
3879 | 1046 |
bindings at:'realName' put:val name. |
3319 | 1047 |
]. |
1979 | 1048 |
|
3319 | 1049 |
(val isRealNameSpace) ifTrue:[ |
3879 | 1050 |
template := template,', a namespace.'. |
1051 |
^ template bindWithArguments:bindings. |
|
3319 | 1052 |
]. |
3879 | 1053 |
|
1054 |
"/ a real class |
|
1055 |
bindings at:'category' put:(val category ? 'uncategorized'). |
|
3319 | 1056 |
shortText ifTrue:[ |
3883 | 1057 |
template := template , ',' , (val isLoaded ifTrue:[' a'] ifFalse:[' an autoloaded']). |
3879 | 1058 |
template := template , (val isSharedPool ifTrue:[' pool'] ifFalse:[' class']). |
1059 |
template := template , ' in %(package) {%(category)}.'. |
|
1060 |
bindings at:'package' put:val package. |
|
4040 | 1061 |
bindings at:'varName' put:(self asClassLink:varName "val name"). |
3879 | 1062 |
^ template bindWithArguments:bindings |
3319 | 1063 |
]. |
1330 | 1064 |
|
3878 | 1065 |
val name = varName ifTrue:[ |
3322 | 1066 |
false ifTrue:[ |
1067 |
"/ TODO: generate a short documentation string (comment plus interface) |
|
1068 |
"/ and return { #html->htmlText . #text->regularText} |
|
3972 | 1069 |
"/ so caller can extract what he wants to see... |
3322 | 1070 |
^ (HTMLDocGenerator htmlDocOf:val). |
1071 |
]. |
|
3972 | 1072 |
explanation := varName , ' is '. |
3319 | 1073 |
explanation := explanation , (val isLoaded ifTrue:['a'] ifFalse:['an autoloaded']). |
1074 |
explanation := explanation , (val isSharedPool ifTrue:[' sharedPool'] ifFalse:[' class']). |
|
3972 | 1075 |
explanation := explanation , ' categorized as "' , val category , '"'. |
3319 | 1076 |
explanation := explanation , '\' withCRs , 'in the "' , val package , '" package.'. |
1077 |
(doc := val commentOrDocumentationString) notEmptyOrNil ifTrue:[ |
|
1078 |
doc := doc asStringCollection. |
|
1079 |
doc size > 25 ifTrue:[ |
|
1080 |
doc := doc copyTo:25. |
|
1081 |
doc add:''; add:' ... <more documentation cut off>'. |
|
1082 |
]. |
|
3864 | 1083 |
doc := doc asString withColor:(UserPreferences current commentColor). |
3319 | 1084 |
explanation := explanation,'\\' withCRs,val name,'''s documentation:\'withCRs, doc |
1085 |
]. |
|
1086 |
^ explanation. |
|
1087 |
]. |
|
1245 | 1088 |
|
3319 | 1089 |
explanation := explanation , ' |
1183 | 1090 |
|
3878 | 1091 |
' , varName , ' is bound to the class ' , val name , |
3972 | 1092 |
' in the "' , val category , '" category.'. |
3319 | 1093 |
^ explanation. |
141 | 1094 |
]. |
1095 |
||
2427 | 1096 |
valText := self valueStringFor:val. |
1097 |
||
1245 | 1098 |
shortText ifTrue:[ |
3881 | 1099 |
^ '%1: a global (%2)' |
1100 |
bindWith:varName |
|
1101 |
with:(self asLink:valText to:(self actionToBrowseClass:val class selector:nil)) |
|
1245 | 1102 |
]. |
3881 | 1103 |
|
3319 | 1104 |
explanation := explanation , ' |
1245 | 1105 |
|
2466
75b29e4a34c0
changed: #explainGlobalOrPoolVariable:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
1106 |
Its current value is "%1".' bindWith:valText. |
3319 | 1107 |
^ explanation. |
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
1108 |
|
2427 | 1109 |
"Created: / 14-10-2010 / 11:33:04 / cg" |
4040 | 1110 |
"Modified: / 16-11-2016 / 13:08:07 / cg" |
1188 | 1111 |
! |
1112 |
||
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1113 |
explainHereIn:aClass short:shortText |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1114 |
"return an explanation" |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1115 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1116 |
|className| |
1188 | 1117 |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1118 |
className := aClass name. |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1119 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1120 |
shortText ifTrue:[ |
3810 | 1121 |
^ '"here" - message lookup always starts in "%1" (always call local method).' bindWith:className |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1122 |
]. |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1123 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1124 |
^ 'like "self", "','here'allBold,'" refers to the object which received the message. |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1125 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1126 |
However, when sending a message to "here", the search for methods |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1127 |
implementing this message will start in the defining class (' , className , '), |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1128 |
instead of the receiver''s class (' , className , ' or subclass). |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1129 |
Thus, using "here", redefined methods will NOT be reached with a here-send and it is ensured, |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1130 |
that the local method is called.' |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1131 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1132 |
"Created: / 28-02-2012 / 10:44:55 / cg" |
1188 | 1133 |
! |
1134 |
||
3798 | 1135 |
explainInstanceVariable:instVarName inClass:aClass short:shortText |
3853 | 1136 |
|varNameInText classNameInText template stringText setOfTypes typesDescription| |
3798 | 1137 |
|
3853 | 1138 |
varNameInText := instVarName allBold. |
1139 |
classNameInText := aClass name. |
|
1140 |
||
3798 | 1141 |
shortText ifTrue:[ |
3883 | 1142 |
template := '%1: instVar in %2'. |
3878 | 1143 |
varNameInText := self |
1144 |
asLink:varNameInText |
|
1145 |
info:('Click to browse references to %1' bindWith:instVarName) |
|
1146 |
to:(self actionToBrowseInstvarRefsTo:instVarName inClass:aClass). |
|
3866 | 1147 |
classNameInText := self asClassLink:classNameInText. |
3798 | 1148 |
] ifFalse:[ |
1149 |
template := '%1: an instance variable in %2' |
|
1150 |
]. |
|
3853 | 1151 |
stringText := template bindWith:varNameInText with:classNameInText. |
3798 | 1152 |
|
1153 |
"/ look for instances |
|
1154 |
setOfTypes := IdentitySet new. |
|
1155 |
self addTypesOfInstvar:instVarName inClass:aClass fromExistingInstancesTo:setOfTypes. |
|
1156 |
"/ look for assignments |
|
3878 | 1157 |
aClass withAllSubclassesDo:[:cls | |
1158 |
self addTypesAssignedToInstvar:instVarName inClass:cls to:setOfTypes |
|
1159 |
]. |
|
1160 |
||
1161 |
"/ generate a list of up-to 3 links |
|
3810 | 1162 |
typesDescription := self typeDescriptionFor:setOfTypes andSelector:nil. |
1163 |
typesDescription notNil ifTrue:[ |
|
1164 |
stringText := stringText,' (',typesDescription,')' |
|
1165 |
]. |
|
3798 | 1166 |
^ stringText |
1167 |
! |
|
1168 |
||
1188 | 1169 |
explainKnownSymbol:string inClass:aClass |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1170 |
"return an explanation or nil" |
1188 | 1171 |
|
1172 |
^ self explainKnownSymbol:string inClass:aClass short:false |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1173 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1174 |
"Modified (comment): / 28-02-2012 / 10:45:40 / cg" |
1188 | 1175 |
! |
1176 |
||
1177 |
explainKnownSymbol:string inClass:aClass short:shortText |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1178 |
"return an explanation or nil" |
1188 | 1179 |
|
2471 | 1180 |
|sym expl| |
1188 | 1181 |
|
1182 |
sym := string asSymbolIfInterned. |
|
1183 |
sym isNil ifTrue:[^ nil]. |
|
1184 |
||
2427 | 1185 |
"try globals and pools" |
1188 | 1186 |
(Smalltalk includesKey:sym) ifTrue:[ |
3810 | 1187 |
expl := self explainGlobalOrPoolVariable:string inClass:aClass short:shortText. |
1188 |
expl notNil ifTrue:[^ expl]. |
|
1188 | 1189 |
]. |
1190 |
||
2471 | 1191 |
expl := self explainSelector:string inClass:aClass short:shortText. |
1192 |
expl notNil ifTrue:[^ expl]. |
|
1193 |
^ self explainGlobalOrPoolVariable:string inClass:aClass short:shortText. |
|
2427 | 1194 |
|
2471 | 1195 |
"Modified: / 18-01-2011 / 19:13:07 / cg" |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1196 |
"Modified (comment): / 28-02-2012 / 10:46:06 / cg" |
1188 | 1197 |
! |
1198 |
||
1199 |
explainPseudoVariable:string in:aClass |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1200 |
"return an explanation for the pseudoVariables self, super etc." |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1201 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1202 |
<resource: #obsolete> |
1188 | 1203 |
|
1204 |
^ self explainPseudoVariable:string in:aClass short:false |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1205 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1206 |
"Modified (format): / 28-02-2012 / 10:47:17 / cg" |
1188 | 1207 |
! |
1208 |
||
1209 |
explainPseudoVariable:string in:aClass short:shortText |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1210 |
"return an explanation for the pseudoVariables self, super etc." |
1188 | 1211 |
|
1212 |
(string = 'self') ifTrue:[ |
|
3810 | 1213 |
^ self explainSelfIn:aClass short:shortText |
1188 | 1214 |
]. |
1215 |
||
1216 |
(string = 'super') ifTrue:[ |
|
3810 | 1217 |
^ self explainSuperIn:aClass short:shortText |
1188 | 1218 |
]. |
1219 |
||
1220 |
(string = 'here') ifTrue:[ |
|
3810 | 1221 |
^ self explainHereIn:aClass short:shortText |
1188 | 1222 |
]. |
1223 |
||
1224 |
(string = 'thisContext') ifTrue:[ |
|
3810 | 1225 |
shortText ifTrue:[ |
1226 |
^ '''thisContext'' - the current stack frame as an object.' |
|
1227 |
]. |
|
1228 |
^ 'thisContext is a pseudo variable (i.e. it is built in). |
|
1188 | 1229 |
|
1230 |
ThisContext always refers to the context object for the currently executed method or |
|
1231 |
block (an instance of Context or BlockContext respectively). The calling chain and calling |
|
1232 |
receivers/selectors can be accessed via thisContext.' |
|
1233 |
]. |
|
1234 |
||
1235 |
(string = 'true') ifTrue:[ |
|
3810 | 1236 |
shortText ifTrue:[ |
1237 |
^ '''true'' - the truth and nothing but the truth.' |
|
1238 |
]. |
|
1239 |
^ 'true is a pseudo variable (i.e. it is built in). |
|
1188 | 1240 |
|
1241 |
True represents logical truth. It is the one and only instance of class True.' |
|
1242 |
]. |
|
1243 |
||
1244 |
(string = 'false') ifTrue:[ |
|
3810 | 1245 |
shortText ifTrue:[ |
1246 |
^ '''false'' - obvisously not true.' |
|
1247 |
]. |
|
1248 |
^ 'false is a pseudo variable (i.e. it is built in). |
|
1188 | 1249 |
|
1250 |
False represents logical falseness. It is the one and only instance of class False.' |
|
1251 |
]. |
|
1252 |
||
1253 |
(string = 'nil') ifTrue:[ |
|
3810 | 1254 |
shortText ifTrue:[ |
1255 |
^ '''nil'' - undefined, unknown, void or dont care.' |
|
1256 |
]. |
|
1257 |
^ 'nil is a pseudo variable (i.e. it is built in). |
|
1188 | 1258 |
|
1259 |
Nil is used for unitialized variables (among other uses). |
|
1260 |
Nil is the one and only instance of class UndefinedObject.' |
|
1261 |
]. |
|
1262 |
^ nil |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1263 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1264 |
"Modified (comment): / 28-02-2012 / 10:46:18 / cg" |
1188 | 1265 |
! |
1266 |
||
1267 |
explainSelector:string inClass:aClass short:shortText |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1268 |
"return an explanation or nil" |
1188 | 1269 |
|
3810 | 1270 |
|selector listOfImplementingClasses listOfImplementingClassNames listOfSimilarSelectors |
3203 | 1271 |
firstImplementingClassOfSimilar count tmp commonSuperClass s s2 |
4143 | 1272 |
firstImplementingClass |
3798 | 1273 |
firstImplementingClassName secondImplementingClassName thirdImplementingClassName |
1274 |
classProvidingComment |
|
1275 |
cm msg t check| |
|
1188 | 1276 |
|
3810 | 1277 |
selector := string asSymbolIfInterned. |
1278 |
selector isNil ifTrue:[^ nil]. |
|
1188 | 1279 |
|
4143 | 1280 |
"/ look who implements it |
3798 | 1281 |
listOfImplementingClassNames := Set new. |
3174 | 1282 |
listOfImplementingClasses := Set new. |
1283 |
listOfSimilarSelectors := Set new. |
|
2288
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1284 |
|
3203 | 1285 |
check := |
3233 | 1286 |
[:sel :mthd :cls | |
3810 | 1287 |
sel == selector ifTrue:[ |
3798 | 1288 |
listOfImplementingClasses add:cls. |
1289 |
listOfImplementingClassNames add:(cls name). |
|
3233 | 1290 |
firstImplementingClass isNil ifTrue:[ |
3798 | 1291 |
firstImplementingClass := cls. |
1292 |
firstImplementingClassName := cls name. |
|
3233 | 1293 |
] |
1294 |
] ifFalse:[ |
|
3810 | 1295 |
(sel startsWith:selector) ifTrue:[ |
3233 | 1296 |
listOfSimilarSelectors add:sel. |
1297 |
firstImplementingClassOfSimilar isNil ifTrue:[ |
|
1298 |
firstImplementingClassOfSimilar := cls |
|
1299 |
] |
|
1300 |
] |
|
1301 |
] |
|
1302 |
]. |
|
2288
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1303 |
|
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1304 |
Smalltalk allClassesDo:[:c| |
3233 | 1305 |
c methodDictionary keysAndValuesDo:[:sel :mthd | |
1306 |
check value:sel value:mthd value:c |
|
1307 |
]. |
|
1308 |
c class methodDictionary keysAndValuesDo:[:sel :mthd | |
|
1309 |
check value:sel value:mthd value:c class |
|
1310 |
]. |
|
141 | 1311 |
]. |
1312 |
||
3810 | 1313 |
(aClass canUnderstand:selector) ifTrue:[ |
1314 |
s2 := ('Instances of ''' , aClass name , ''' respond to #') , selector "allBold" , '.'. |
|
3233 | 1315 |
shortText ifFalse:[ |
1316 |
s2 := '\\' , s2 |
|
1317 |
, '\- inherited from ' withCRs |
|
3810 | 1318 |
, (aClass whichClassIncludesSelector:selector) name "allBold". |
3233 | 1319 |
]. |
3810 | 1320 |
firstImplementingClass := (aClass whichClassIncludesSelector:selector) |
835 | 1321 |
] ifFalse:[ |
3233 | 1322 |
s2 := ''. |
835 | 1323 |
]. |
1324 |
||
3798 | 1325 |
count := listOfImplementingClassNames size. |
141 | 1326 |
(count ~~ 0) ifTrue:[ |
3233 | 1327 |
" |
1328 |
for up-to 4 implementing classes, |
|
1329 |
list them |
|
1330 |
" |
|
3798 | 1331 |
listOfImplementingClassNames := listOfImplementingClassNames asOrderedCollection sort. |
3233 | 1332 |
shortText ifTrue:[ |
1333 |
tmp := ' is implemented in '. |
|
1334 |
] ifFalse:[ |
|
1335 |
tmp := ' is a selector implemented in '. |
|
1336 |
]. |
|
3881 | 1337 |
"/ s := string allBold. |
3855 | 1338 |
count > 1 ifTrue:[ |
3881 | 1339 |
s := self asLink:selector to:(self actionToOpenMethodFinderFor:selector). |
4143 | 1340 |
] ifFalse:[ |
3885 | 1341 |
s := self asLink:selector to:(self actionToBrowseClass:listOfImplementingClasses first selector:selector). |
3842 | 1342 |
]. |
4143 | 1343 |
|
3810 | 1344 |
shortText ifTrue:[ |
1345 |
|typesDescription| |
|
4143 | 1346 |
|
3810 | 1347 |
msg := s , tmp. |
3842 | 1348 |
typesDescription := self typeDescriptionFor:listOfImplementingClasses andSelector:selector wordBetween:'and'. |
3810 | 1349 |
typesDescription notNil ifTrue:[ |
3842 | 1350 |
msg := msg,typesDescription |
3233 | 1351 |
]. |
1352 |
] ifFalse:[ |
|
3810 | 1353 |
(count == 1) ifTrue:[ |
1354 |
t := firstImplementingClassName. |
|
1355 |
firstImplementingClass isMeta ifTrue:[ |
|
1356 |
t := 'the ' , t |
|
1357 |
]. |
|
1358 |
msg := s , tmp , t , '.'. |
|
3233 | 1359 |
shortText ifFalse:[ |
1360 |
msg := msg , s2. |
|
3810 | 1361 |
] |
3233 | 1362 |
] ifFalse:[ |
3810 | 1363 |
firstImplementingClassName := listOfImplementingClassNames at:1. |
1364 |
secondImplementingClassName := listOfImplementingClassNames at:2. |
|
1365 |
(count == 2) ifTrue:[ |
|
1366 |
msg := s,tmp,firstImplementingClassName,' and ',secondImplementingClassName,'.'. |
|
3233 | 1367 |
shortText ifFalse:[ |
1368 |
msg := msg , s2. |
|
1369 |
]. |
|
1370 |
] ifFalse:[ |
|
3810 | 1371 |
thirdImplementingClassName := listOfImplementingClassNames at:3. |
1372 |
(count == 3) ifTrue:[ |
|
1373 |
msg := s,tmp,firstImplementingClassName,',',secondImplementingClassName,' and ',thirdImplementingClassName,'.'. |
|
1374 |
shortText ifFalse:[ |
|
1375 |
msg := msg , s2. |
|
1376 |
]. |
|
1377 |
] ifFalse:[ |
|
1378 |
false "shortText" ifTrue:[ |
|
1379 |
msg := s , tmp , count printString , ' classes'. |
|
1380 |
commonSuperClass := Class commonSuperclassOf:listOfImplementingClassNames. |
|
1381 |
commonSuperClass notNil ifTrue:[ |
|
1382 |
(commonSuperClass == Object |
|
1383 |
and:[commonSuperClass includesSelector:selector]) ifTrue:[ |
|
1384 |
msg := msg , ' (including ' , 'Object' "allBold", ')' |
|
1385 |
] ifFalse:[ |
|
1386 |
(commonSuperClass ~= Object) ifTrue:[ |
|
1387 |
msg := msg , ' (under ' , commonSuperClass name, ')' |
|
1388 |
] |
|
1389 |
]. |
|
3492 | 1390 |
]. |
3810 | 1391 |
msg := msg , '.'. |
1392 |
^ msg |
|
3233 | 1393 |
]. |
1183 | 1394 |
|
3810 | 1395 |
" |
1396 |
if there are more, look for a common |
|
1397 |
superclass and show it ... |
|
1398 |
" |
|
1399 |
commonSuperClass := (Behavior commonSuperclassOf:listOfImplementingClasses) ? Object. |
|
1400 |
(commonSuperClass ~~ Object |
|
1401 |
and:[commonSuperClass ~~ Behavior |
|
1402 |
and:[commonSuperClass ~~ Class |
|
1403 |
and:[commonSuperClass ~~ ClassDescription]]]) ifTrue:[ |
|
1404 |
(listOfImplementingClasses includes:commonSuperClass) ifTrue:[ |
|
1405 |
msg := s . tmp , count printString , commonSuperClass name |
|
1406 |
, ' and redefined in ' , (count - 1) printString |
|
1407 |
, ' subclasses' |
|
1408 |
, s2. |
|
1409 |
firstImplementingClass := commonSuperClass |
|
1410 |
] ifFalse:[ |
|
1411 |
msg := s , tmp, count printString , ' subclasses of ' , commonSuperClass name , s2 |
|
1412 |
] |
|
3233 | 1413 |
] ifFalse:[ |
3810 | 1414 |
(commonSuperClass == Object |
1415 |
and:[commonSuperClass includesSelector:selector]) ifTrue:[ |
|
1416 |
shortText ifTrue:[ |
|
1417 |
msg := s , tmp , count printString , ' classes (incl. Object)'. |
|
1418 |
] ifFalse:[ |
|
1419 |
msg := s , tmp , count printString , ' classes. |
|
4143 | 1420 |
|
3203 | 1421 |
All objects seem to respond to that message, |
911
be6ce6482a7f
extract the methods comment and show it.
Claus Gittinger <cg@exept.de>
parents:
839
diff
changeset
|
1422 |
since there is an implementation in Object.' , s2. |
3810 | 1423 |
]. |
1424 |
firstImplementingClass := Object |
|
1425 |
] ifFalse:[ |
|
1426 |
((commonSuperClass == Behavior |
|
1427 |
or:[commonSuperClass == Class |
|
1428 |
or:[commonSuperClass == ClassDescription]]) |
|
1429 |
and:[commonSuperClass includesSelector:selector]) ifTrue:[ |
|
1430 |
shortText ifTrue:[ |
|
1431 |
msg := s , tmp , count printString , ' classes (incl. all classes)'. |
|
1432 |
] ifFalse:[ |
|
1433 |
msg := s , tmp , count printString , ' classes. |
|
913 | 1434 |
|
3203 | 1435 |
All classes seem to respond to that message, |
913 | 1436 |
since there is an implementation in ' , commonSuperClass name , '.' , s2. |
3810 | 1437 |
]. |
1438 |
firstImplementingClass := commonSuperClass |
|
1439 |
] ifFalse:[ |
|
1440 |
" |
|
1441 |
otherwise just give the number. |
|
1442 |
" |
|
1443 |
msg := s , tmp , count printString , ' classes.' , s2 |
|
1444 |
] |
|
3233 | 1445 |
] |
1446 |
] |
|
1447 |
] |
|
3810 | 1448 |
]. |
3233 | 1449 |
]. |
1450 |
]. |
|
3798 | 1451 |
"/ look for a comment... |
1452 |
count == 1 ifTrue:[ |
|
1453 |
classProvidingComment := firstImplementingClass. |
|
3233 | 1454 |
] ifFalse:[ |
3798 | 1455 |
commonSuperClass isNil ifTrue:[ |
1456 |
commonSuperClass := Behavior commonSuperclassOf:listOfImplementingClasses. |
|
1457 |
] . |
|
4100 | 1458 |
|
4143 | 1459 |
((listOfImplementingClasses includes:commonSuperClass) |
4100 | 1460 |
and:[ (commonSuperClass implements:selector) ] |
1461 |
) ifTrue:[ |
|
1462 |
classProvidingComment := commonSuperClass |
|
1463 |
] ifFalse:[ |
|
4143 | 1464 |
"/ avoid some obvious misleading paths... |
1465 |
classProvidingComment := { Number . Collection . Stream . View } |
|
4100 | 1466 |
detect:[:cls | (listOfImplementingClasses includes:cls) |
1467 |
or:[cls canUnderstand:selector] ] |
|
1468 |
ifNone:nil. |
|
1469 |
classProvidingComment notNil ifTrue:[ |
|
1470 |
classProvidingComment := classProvidingComment whichClassImplements:selector. |
|
4143 | 1471 |
] ifFalse:[ |
3810 | 1472 |
classProvidingComment := firstImplementingClass. |
4143 | 1473 |
[ |
4100 | 1474 |
classProvidingComment superclass notNil |
1475 |
and:[ classProvidingComment superclass implements:selector ] |
|
1476 |
] whileTrue:[ |
|
1477 |
classProvidingComment := classProvidingComment superclass |
|
1478 |
]. |
|
3810 | 1479 |
]. |
3798 | 1480 |
]. |
1481 |
]. |
|
1482 |
classProvidingComment notNil ifTrue:[ |
|
3885 | 1483 |
cm := self fetchDescriptionOfMethod:"fetchCommentOfMethod:"(classProvidingComment compiledMethodAt:selector). |
3798 | 1484 |
]. |
1485 |
cm isNil ifTrue:[ |
|
1486 |
"/ should: find the class with most subclasses from the list |
|
1487 |
(listOfImplementingClasses includes:Collection) ifTrue:[ |
|
1488 |
classProvidingComment := Collection |
|
1489 |
] ifFalse:[ |
|
1490 |
(listOfImplementingClasses includes:Stream) ifTrue:[ |
|
1491 |
classProvidingComment := Stream |
|
1492 |
] ifFalse:[ |
|
4143 | 1493 |
classProvidingComment := listOfImplementingClasses |
1494 |
detect:[:cls | |
|
3883 | 1495 |
|mthd| |
1496 |
(mthd := cls compiledMethodAt:selector) notNil |
|
3885 | 1497 |
and:[ (self fetchCommentOfMethod:mthd) notNil]] |
3883 | 1498 |
ifNone:nil. |
3798 | 1499 |
] |
1500 |
]. |
|
3883 | 1501 |
cm isNil ifTrue:[ |
1502 |
classProvidingComment notNil ifTrue:[ |
|
3885 | 1503 |
cm := self fetchDescriptionOfMethod:(classProvidingComment compiledMethodAt:selector). |
3883 | 1504 |
] |
4143 | 1505 |
]. |
3798 | 1506 |
]. |
1507 |
cm notNil ifTrue:[ |
|
4100 | 1508 |
cm := (' %1 says:\' withCRs bindWith:(self asClassLink:classProvidingComment name)),cm. |
4143 | 1509 |
|
3883 | 1510 |
"/ msg := msg,(msg last isSeparator ifTrue:[''] ifFalse:[' ']),cm |
1511 |
msg := msg,(Character cr),cm |
|
3233 | 1512 |
]. |
1513 |
^ msg |
|
141 | 1514 |
]. |
1515 |
||
3881 | 1516 |
"/ none implements it (typo?); |
3174 | 1517 |
count := listOfSimilarSelectors size. |
2288
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1518 |
(count ~~ 0) ifTrue:[ |
3233 | 1519 |
listOfSimilarSelectors := listOfSimilarSelectors asOrderedCollection sort. |
2288
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1520 |
|
3233 | 1521 |
(count == 1) ifTrue:[ |
3881 | 1522 |
|sel selLink clsLink implementors| |
1523 |
||
1524 |
sel := listOfSimilarSelectors first. |
|
1525 |
selLink := self asLink:sel to:(self actionToOpenMethodFinderFor:sel). |
|
1526 |
"/ clsLink := self asLink:firstImplementingClassOfSimilar name to:(self actionToBrowseClass:firstImplementingClassOfSimilar selector:sel). |
|
1527 |
implementors := Smalltalk allImplementorsOf:sel. |
|
1528 |
clsLink := self typeDescriptionFor:implementors andSelector:sel. |
|
1529 |
msg := selLink , ' is implemented in ' , clsLink , '.'. |
|
3233 | 1530 |
shortText ifFalse:[ |
1531 |
msg := msg , s2. |
|
1532 |
] |
|
1533 |
] ifFalse:[ |
|
3881 | 1534 |
|sel1 sel2| |
1535 |
sel1 := listOfSimilarSelectors first. |
|
1536 |
sel2 := listOfSimilarSelectors second. |
|
4143 | 1537 |
msg := 'similar selectors: %1, %2' |
1538 |
bindWith:(self asLink:sel1 to:(self actionToOpenMethodFinderFor:sel1)) |
|
3881 | 1539 |
with:(self asLink:sel2 to:(self actionToOpenMethodFinderFor:sel1)). |
3233 | 1540 |
count > 2 ifTrue:[ |
1541 |
msg := msg,'...'. |
|
1542 |
]. |
|
1543 |
]. |
|
1544 |
^ msg |
|
2288
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1545 |
]. |
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1546 |
|
141 | 1547 |
^ nil |
290
d1650b17b9e9
Change the name of a classes Metaclass from e.g. "SmallIntegerclass" to
Stefan Vogel <sv@exept.de>
parents:
263
diff
changeset
|
1548 |
|
1909 | 1549 |
"Modified: / 17-06-1996 / 17:09:30 / stefan" |
1550 |
"Created: / 23-03-1999 / 13:29:33 / cg" |
|
3804 | 1551 |
"Modified: / 01-05-2016 / 15:42:24 / cg" |
4143 | 1552 |
"Modified: / 22-05-2017 / 11:35:31 / mawalch" |
141 | 1553 |
! |
1554 |
||
1183 | 1555 |
explainSelfIn:aClass short:shortText |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1556 |
"return an explanation" |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1557 |
|
3883 | 1558 |
|subClasses subNames selfString className nSubClasses |
1559 |
classLink classLink2 subclassesLink| |
|
1198 | 1560 |
|
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
1561 |
selfString := '''' , 'self' "allBold" , ''''. |
6 | 1562 |
|
3883 | 1563 |
subClasses := aClass allSubclasses. |
1564 |
nSubClasses := subClasses size. |
|
1244 | 1565 |
|
1566 |
aClass isMeta ifTrue:[ |
|
3810 | 1567 |
className := aClass theNonMetaclass name. |
3883 | 1568 |
subNames := subClasses collect:[:c | c theNonMetaclass name]. |
1569 |
shortText ifTrue:[ |
|
1570 |
classLink := (self asClassLink:className). |
|
1571 |
nSubClasses == 0 ifTrue:[ |
|
1572 |
^ selfString , (' - the %1 class.' bindWith:classLink) |
|
1573 |
]. |
|
1574 |
nSubClasses == 1 ifTrue:[ |
|
1575 |
classLink2 := self asClassLink:subNames first. |
|
1576 |
^ selfString , (' - the %1- or %2 class.' bindWith:classLink with:classLink2) |
|
1577 |
]. |
|
1578 |
subclassesLink := self |
|
1579 |
asLink:('%1 subclasses' bindWith:nSubClasses) |
|
1580 |
info:('Click to browse subclasses') |
|
1581 |
to:(self actionToBrowseClasses:subClasses). |
|
1582 |
||
1583 |
^ selfString , (' - the %1 class or one of its %2.' bindWith:classLink with:subclassesLink) |
|
1584 |
]. |
|
1585 |
||
3810 | 1586 |
nSubClasses == 0 ifTrue:[ |
1587 |
^ selfString , 'refers to the object which received the message. |
|
1244 | 1588 |
|
1589 |
In this case, it will be the ' , className , '-class itself.' |
|
3810 | 1590 |
]. |
1244 | 1591 |
|
3810 | 1592 |
nSubClasses <= 5 ifTrue:[ |
1593 |
^ selfString , ' refers to the object which received the message. |
|
1244 | 1594 |
|
1595 |
In this case, it will be the ' , className , '-class |
|
1596 |
or one of its subclasses: |
|
1597 |
||
1598 |
' , subNames asStringCollection asString |
|
3810 | 1599 |
]. |
1244 | 1600 |
|
3810 | 1601 |
^ selfString , ' refers to the object which received the message. |
1244 | 1602 |
|
1603 |
In this case, it will be the ' , className , '-class |
|
1604 |
or one of its ' , nSubClasses printString , ' subclasses.' |
|
1605 |
]. |
|
1606 |
||
3883 | 1607 |
subClasses := aClass allSubclasses. |
1608 |
subNames := subClasses collect:[:c | c theNonMetaclass name]. |
|
1244 | 1609 |
className := aClass name. |
3883 | 1610 |
shortText ifTrue:[ |
1611 |
classLink := self asClassLink:className. |
|
1612 |
nSubClasses == 0 ifTrue:[ |
|
1613 |
^ selfString , (' - an instance of %1.' bindWith:classLink) |
|
1614 |
]. |
|
1615 |
nSubClasses == 1 ifTrue:[ |
|
1616 |
classLink2 := self asClassLink:subNames first. |
|
1617 |
^ selfString , (' - an instance of %1 or %2.' bindWith:classLink with:classLink2) |
|
1618 |
]. |
|
1619 |
subclassesLink := self |
|
1620 |
asLink:('%1 subclasses' bindWith:nSubClasses) |
|
1621 |
info:('Click to browse subclasses') |
|
1622 |
to:(self actionToBrowseClasses:subClasses). |
|
1623 |
^ selfString , (' - an instance of %1 or one of its %2.' bindWith:classLink with:subclassesLink) |
|
1624 |
]. |
|
1625 |
||
1244 | 1626 |
nSubClasses == 0 ifTrue:[ |
3810 | 1627 |
^ selfString , 'refers to the object which received the message. |
6 | 1628 |
|
1244 | 1629 |
In this case, it will be an instance of ' , className , '.' |
6 | 1630 |
]. |
1631 |
||
1244 | 1632 |
nSubClasses <= 5 ifTrue:[ |
3810 | 1633 |
^ selfString , ' refers to the object which received the message. |
6 | 1634 |
|
1244 | 1635 |
In this case, it will be an instance of ' , className , ' |
6 | 1636 |
or one of its subclasses: |
1637 |
||
1256 | 1638 |
' , subNames asStringCollection asString |
6 | 1639 |
]. |
1640 |
||
1198 | 1641 |
^ selfString , ' refers to the object which received the message. |
6 | 1642 |
|
1244 | 1643 |
In this case, it will be an instance of ' , className , ' |
1644 |
or one of its ' , nSubClasses printString , ' subclasses.' |
|
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
1645 |
|
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
1646 |
"Modified: / 09-10-2006 / 12:11:44 / cg" |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1647 |
"Modified (comment): / 28-02-2012 / 10:47:06 / cg" |
6 | 1648 |
! |
1649 |
||
1188 | 1650 |
explainSuperIn:aClass short:shortText |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1651 |
"return an explanation" |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1652 |
|
1352 | 1653 |
|superName| |
1244 | 1654 |
|
1655 |
superName := aClass superclass name. |
|
1188 | 1656 |
|
1657 |
shortText ifTrue:[ |
|
3842 | 1658 |
^ '"super" - message lookup starts in superclass "%1" (call redefined method).' bindWith:(self asClassLink:superName) |
1188 | 1659 |
]. |
1660 |
||
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1661 |
^ 'like "self", "','super'allBold,'" refers to the object which received the message. |
6 | 1662 |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1663 |
However, when sending a message to "super", the search for methods |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1664 |
implementing this message will start in the superclass (' , superName , '), |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1665 |
instead of the receiver''s class (' , aClass name , ' or subclass). |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1666 |
Thus, using "super", a redefined method can call the original (redefined) method of its superclass.' |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1667 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1668 |
"Modified (comment): / 28-02-2012 / 10:47:03 / cg" |
6 | 1669 |
! |
2 | 1670 |
|
49 | 1671 |
explainSyntax:string |
1672 |
"try syntax ...; return explanation or nil" |
|
1673 |
||
1183 | 1674 |
^ self explainSyntax:string short:false |
1675 |
! |
|
1676 |
||
1677 |
explainSyntax:string short:shortText |
|
3842 | 1678 |
"try syntax ...; return explanation or nil. |
1679 |
This is meant for beginners..." |
|
1183 | 1680 |
|
3883 | 1681 |
|fullMsg stringWithoutSeparators| |
3881 | 1682 |
|
49 | 1683 |
((string = ':=') or:[string = '_']) ifTrue:[ |
3881 | 1684 |
fullMsg := '<variable> := <expression> |
49 | 1685 |
|
3233 | 1686 |
":=" and "_" (which is left-arrow in some fonts) mean assignment. |
3464 | 1687 |
The variable is bound to (i.e. points to) the value of <expression>. |
3881 | 1688 |
The "_" form is historic and should not be used with new code.'. |
1689 |
||
1690 |
shortText ifTrue:[ |
|
1691 |
string = '_' ifTrue:[ |
|
1692 |
^ (self |
|
1693 |
asLink:'"_"' |
|
1694 |
info:fullMsg |
|
1695 |
to:nil),' - old style for assignment. Consider changing to ":=".' |
|
1696 |
]. |
|
1697 |
^ (self |
|
1698 |
asLink:'":="' |
|
1699 |
info:fullMsg |
|
1700 |
to:nil),' - assign to variable on the left (syntax).' |
|
1701 |
]. |
|
1702 |
^ fullMsg |
|
49 | 1703 |
]. |
1704 |
||
1705 |
(string = '^') ifTrue:[ |
|
3233 | 1706 |
shortText ifTrue:[ |
3607 | 1707 |
^ '"^" - return value from method (syntax)'. |
3233 | 1708 |
]. |
1709 |
^ '^ <expression> |
|
49 | 1710 |
|
3319 | 1711 |
A return statement evaluates <expression> and returns its value from the method. |
1712 |
A return inside a block exits the method where the block is defined (not only the block).' |
|
49 | 1713 |
]. |
1714 |
||
1715 |
(string = ';') ifTrue:[ |
|
3233 | 1716 |
shortText ifTrue:[ |
3607 | 1717 |
^ '";" - a cascade expression (syntax)'. |
3233 | 1718 |
]. |
1719 |
^ '<expression> ; selector1 ; .... ; selectorN |
|
49 | 1720 |
|
3203 | 1721 |
a cascade expression; evaluate expression, and send messages |
1722 |
<selector1> ... <selectorN> to the first expression''s receiver. |
|
49 | 1723 |
Returns the value of the last send. The cascade sends may also have arguments.' |
1724 |
]. |
|
1725 |
||
1726 |
(string = '|') ifTrue:[ |
|
3233 | 1727 |
shortText ifTrue:[ |
3607 | 1728 |
^ '"|" - local variables (syntax)'. |
3233 | 1729 |
]. |
1730 |
^ '| locals | or: [:arg | statements] |
|
49 | 1731 |
|
3233 | 1732 |
"|" is used to mark a local variable declaration or separates arguments |
49 | 1733 |
from the statements in a block. Notice, that in a block-argument declaration |
3319 | 1734 |
these arg nams must be prefixed by a colon character. |
3233 | 1735 |
"|" is also a selector understood by Booleans.' |
49 | 1736 |
]. |
1737 |
||
3319 | 1738 |
(string startsWith:'$' ) ifTrue:[ |
1739 |
shortText ifTrue:[ |
|
3842 | 1740 |
^ '"$x" - ',(self asClassLink:'Character'),' literal (syntax)'. |
3319 | 1741 |
]. |
1742 |
^ 'is a Character literal constant. |
|
1743 |
||
1744 |
Character objects represent indivdual text cheracters in Unicode encoding. |
|
1745 |
For example, $a is the character "a" with an encoding of 97 "hex: 16r61". |
|
1746 |
Smalltalk/X supports unicode and uses this encoding internally for all characters |
|
1747 |
and strings. Notice, that not all Smalltalk implementations allow non-ascii (i.e. codes > 255).' |
|
1748 |
]. |
|
1749 |
||
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1750 |
(string startsWith:'#' ) ifTrue:[ |
3233 | 1751 |
(string startsWith:'#(' ) ifTrue:[ |
1752 |
shortText ifTrue:[ |
|
3842 | 1753 |
^ '"#(..)" - ',(self asClassLink:'Array'),' literal (syntax)'. |
3233 | 1754 |
]. |
3607 | 1755 |
^ 'is a constant Array (literal). |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1756 |
|
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1757 |
The array-object is created at compilation time and a reference to this is |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1758 |
used at execution time (thus, the same object is referred to every time). |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1759 |
The elements of a constant Array must be numbers, strings, symbols, nil, true or false, |
3194 | 1760 |
other array constants or byte-arrays. |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1761 |
(notice, that not all Smalltalk implementations allow true, false and nil as |
3194 | 1762 |
element in an Array-constant).' |
3233 | 1763 |
]. |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1764 |
|
3233 | 1765 |
(string startsWith:'#[') ifTrue:[ |
1766 |
shortText ifTrue:[ |
|
3842 | 1767 |
^ '"#[..]" - ',(self asClassLink:'ByteArray'),' literal (syntax)'. |
3233 | 1768 |
]. |
3607 | 1769 |
^ 'is a constant ByteArray (literal). |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1770 |
|
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1771 |
The elements of a constant ByteArray must be Integer constants in the range |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1772 |
0 .. 255. |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1773 |
(notice, that not all Smalltalk implementations support constant ByteArrays).' |
3233 | 1774 |
]. |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1775 |
|
3233 | 1776 |
(string startsWith:'#''') ifTrue:[ |
1777 |
shortText ifTrue:[ |
|
3842 | 1778 |
^ '"#''..''" - ',(self asClassLink:'Symbol'),' literal (syntax)'. |
3233 | 1779 |
]. |
1780 |
^ 'is a constant symbol containing non-alphanumeric characters. |
|
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1781 |
|
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1782 |
Symbols are unique strings, meaning that there exists |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1783 |
exactly one instance of a given symbol. Therefore symbols can |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1784 |
be compared using == (identity compare) in addition to = (contents compare). |
3194 | 1785 |
Beside this, Symbols behave mostly like Strings but are immutable.' |
3233 | 1786 |
]. |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1787 |
|
3233 | 1788 |
shortText ifTrue:[ |
3842 | 1789 |
^ '"#.." - ',(self asClassLink:'Symbol'),' literal (syntax)'. |
3233 | 1790 |
]. |
3319 | 1791 |
^ 'is a constant symbol. |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1792 |
|
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1793 |
Symbols are unique strings, meaning that there exists |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1794 |
exactly one instance of a given symbol. Therefore symbols can |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1795 |
be compared using == (identity compare) in addition to = (contents compare). |
3194 | 1796 |
Beside this, Symbols behave mostly like Strings but are immutable.' |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1797 |
]. |
49 | 1798 |
((string startsWith:'(') or:[string endsWith:')']) ifTrue:[ |
3233 | 1799 |
shortText ifTrue:[ |
1800 |
^ ''. |
|
1801 |
]. |
|
1802 |
^ '( <expression> ) |
|
49 | 1803 |
|
3194 | 1804 |
expression grouping. Without them, expressions are evaluated left to right, |
1805 |
with unary messages preceeding binary messages, preceeding keyword mesages.' |
|
49 | 1806 |
]. |
1807 |
||
1808 |
((string startsWith:'[') or:[string endsWith:']']) ifTrue:[ |
|
3233 | 1809 |
shortText ifTrue:[ |
3842 | 1810 |
^ '"[..]" - a ',(self asClassLink:'Block'),' (aka lambda/closure for experts)'. |
3233 | 1811 |
]. |
1812 |
^ '[:arg1 .. :argN | statements] |
|
49 | 1813 |
|
3203 | 1814 |
defines a block. |
3194 | 1815 |
Blocks represent pieces of executable code. The definition of a block does |
3203 | 1816 |
not evaluate it, instead a reference to the blocks computation is created. |
3194 | 1817 |
The block can be evaluated later, by sending it a value/value: message |
1818 |
(it can be even evaluated multiple times). |
|
1819 |
Blocks are often passed as arguments to Booleans (i.e. "bool ifTrue:[...]"), |
|
1820 |
other blocks (i.e. "[...] whileTrue: [...]"]) or collections (i.e. "coll do:[...]"). |
|
1821 |
Blocks are also often used as callbacks from UI components or as exception handlers.' |
|
49 | 1822 |
]. |
1823 |
||
2286
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1824 |
((string startsWith:'{') or:[string endsWith:'}']) ifTrue:[ |
3233 | 1825 |
shortText ifTrue:[ |
3842 | 1826 |
^ '"{..}" ',(self asClassLink:'Array'),' instantiation (syntax)'. |
3233 | 1827 |
]. |
1828 |
^ '{ <expr1>. .. <exprN> } |
|
2286
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1829 |
|
3319 | 1830 |
This is syntactic sugar for "Array with:<expr1> .. with:<exprN>". |
1831 |
||
2286
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1832 |
A new array is created with N elements initialized from the N expressions. |
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1833 |
The expressions are separated by a period statement separator. |
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1834 |
The expressions are evaluated at execution time, and a new array is always created |
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1835 |
when executed (as opposed to an array literal, which is computed only once at |
3233 | 1836 |
compilation time) and which is shared. |
3319 | 1837 |
Notice that this construct is not supported by all Smalltalk dialects, so its use |
1838 |
makes your program somewhat dialect specific.' |
|
2286
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1839 |
]. |
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1840 |
|
49 | 1841 |
(string = ':') ifTrue:[ |
3233 | 1842 |
shortText ifTrue:[ |
1843 |
^ ''. |
|
1844 |
]. |
|
1845 |
^ 'colons have different meaning depending on context: |
|
49 | 1846 |
|
1847 |
1) they separate keyword-parts in symbols and keyword-messages as in: |
|
1848 |
||
1849 |
#at:put: a constant keyword symbol |
|
1850 |
||
1851 |
rec at:index put:value sends the #at:put: message to rec, |
|
3233 | 1852 |
passing index and value as arguments. |
49 | 1853 |
|
1854 |
2) within block-argument declarations as in: |
|
1855 |
||
1856 |
[:arg1 :arg2 | statements] |
|
834 | 1857 |
|
1858 |
3) within an identifier, they separate the nameSpace part from |
|
1859 |
the name part; as in: |
|
1860 |
||
1861 |
Smalltalk::Array - the Array class in the Smalltalk nameSpace. |
|
1862 |
Foo::Array - the Array class in the Foo nameSpace. |
|
49 | 1863 |
' |
1864 |
]. |
|
1865 |
||
1866 |
(string = '.') ifTrue:[ |
|
3233 | 1867 |
^ 'statement. "<- period here" |
49 | 1868 |
statement |
1869 |
||
1870 |
within a method or block, individual statements are separated by periods. |
|
1871 |
' |
|
1872 |
]. |
|
1873 |
||
3883 | 1874 |
stringWithoutSeparators := string withoutSeparators. |
1875 |
||
1876 |
(stringWithoutSeparators startsWith:'"') ifTrue:[ |
|
1877 |
(stringWithoutSeparators startsWith:'"/') ifTrue:[ |
|
3464 | 1878 |
shortText ifTrue:[ |
3607 | 1879 |
^ 'an end-of-line comment (syntax)'. |
3464 | 1880 |
]. |
1881 |
^ '"/ comment |
|
3193 | 1882 |
EOL (end-of-line) comment |
1883 |
||
1884 |
anything up to the end of line is a comment and ignored (but not inside a string). |
|
3233 | 1885 |
Notice that EOL-comments are only supported by Smalltalk/X (i.e. non-portable). |
3193 | 1886 |
' |
3464 | 1887 |
]. |
3883 | 1888 |
(stringWithoutSeparators startsWith:'"<<') ifTrue:[ |
3464 | 1889 |
shortText ifTrue:[ |
4175 | 1890 |
^ 'a token delimited comment; up to line starting with the token after ''<<''.'. |
3464 | 1891 |
]. |
1892 |
^ '"<<TOKEN |
|
1893 |
... |
|
1894 |
TOKEN |
|
1895 |
||
1896 |
Token delimited comment |
|
1897 |
||
4175 | 1898 |
anything up to a line which starts with the token is a comment and ignored (but not inside a string). |
3464 | 1899 |
Notice that Token-comments are only supported by Smalltalk/X (i.e. non-portable). |
1900 |
' |
|
1901 |
]. |
|
1902 |
||
3233 | 1903 |
shortText ifTrue:[ |
3607 | 1904 |
^ 'a comment (syntax)'. |
3233 | 1905 |
]. |
1906 |
^ '" comment ... " |
|
3193 | 1907 |
comment |
1908 |
||
1909 |
anything between double quotes is a comment and ignored (but not inside a string). |
|
1910 |
' |
|
1911 |
]. |
|
1912 |
||
49 | 1913 |
"/ is it a symbol without hash-character ? |
1914 |
"/ |
|
796 | 1915 |
"/ string knownAsSymbol ifTrue:[ |
1916 |
"/ ^ 'is nothing, but #' , string , ' is known as a symbol. |
|
1917 |
"/ |
|
1918 |
"/Symbols are unique strings, meaning that there exists |
|
1919 |
"/exactly one instance of a given symbol. Therefore symbols can |
|
1920 |
"/be compared using == (identity compare) in addition to = (contents compare). |
|
1921 |
"/Beside this, Symbols behave mostly like Strings.' |
|
1922 |
"/ ]. |
|
49 | 1923 |
|
3883 | 1924 |
((stringWithoutSeparators startsWith:'<') and:[stringWithoutSeparators endsWith:'>']) ifTrue:[ |
1925 |
shortText ifTrue:[ |
|
1926 |
(stringWithoutSeparators includesString:'resource:') ifTrue:[ |
|
1927 |
^ 'a ',(self |
|
1928 |
asLink:'resource annotation' |
|
1929 |
info:fullMsg |
|
1930 |
to:(self actionToBrowseClass:Annotation)),' (syntax)' |
|
1931 |
]. |
|
1932 |
^ 'an ',(self |
|
1933 |
asLink:'annotation / pragma' |
|
1934 |
info:fullMsg |
|
1935 |
to:(self actionToBrowseClass:Annotation)),' (syntax)' |
|
1936 |
]. |
|
1937 |
]. |
|
1938 |
||
49 | 1939 |
^ nil |
796 | 1940 |
|
4175 | 1941 |
"Modified: / 28-07-2017 / 10:32:11 / cg" |
2214 | 1942 |
! |
1943 |
||
3810 | 1944 |
typeDescriptionFor:setOfTypes andSelector:selectorOrNil |
1945 |
"up to 3 types are shown by name; more are simply counted" |
|
1946 |
||
3842 | 1947 |
^ self typeDescriptionFor:setOfTypes andSelector:selectorOrNil wordBetween:'or' |
1948 |
! |
|
1949 |
||
1950 |
typeDescriptionFor:setOfTypes andSelector:selectorOrNil wordBetween:wordbetween |
|
1951 |
"up to 3 types are shown by name; more are simply counted" |
|
1952 |
||
3810 | 1953 |
|types numTypes |
1954 |
type1 type2 type3 |
|
3855 | 1955 |
nm1 nm2 nm3 link| |
3810 | 1956 |
|
1957 |
"/ reduce... |
|
1958 |
self compressSetOfTypes:setOfTypes. |
|
1959 |
setOfTypes isEmpty ifTrue:[^ nil]. |
|
1960 |
||
1961 |
types := setOfTypes asOrderedCollection. |
|
1962 |
types sortBySelector:#name. |
|
1963 |
||
1964 |
"/ now make this a nice string |
|
1965 |
numTypes := types size. |
|
1966 |
type1 := types first. |
|
3859 | 1967 |
nm1 := self asLink:type1 name to:(self actionToBrowseClass:type1 selector:selectorOrNil). |
3810 | 1968 |
numTypes == 1 ifTrue:[ |
1969 |
^ nm1 |
|
1970 |
]. |
|
3842 | 1971 |
|
3810 | 1972 |
type2 := types second. |
3859 | 1973 |
nm2 := self asLink:type2 name to:(self actionToBrowseClass:type2 selector:selectorOrNil). |
3810 | 1974 |
numTypes == 2 ifTrue:[ |
3842 | 1975 |
^ nm1,' ',wordbetween,' ',nm2 |
3810 | 1976 |
]. |
1977 |
type3 := types third. |
|
3859 | 1978 |
nm3 := self asLink:type3 name to:(self actionToBrowseClass:type3 selector:selectorOrNil). |
3810 | 1979 |
numTypes == 3 ifTrue:[ |
3842 | 1980 |
^ nm1,', ',nm2,' ',wordbetween,' ',nm3 |
3810 | 1981 |
]. |
3855 | 1982 |
link := self actionToBrowseClasses:types. |
1983 |
"/ selectorOrNil notNil ifTrue:[ |
|
1984 |
"/ link := self actionToOpenMethodFinderFor:selectorOrNil. |
|
1985 |
"/ ]. |
|
3859 | 1986 |
^ self asLink:('%1 classes' bindWith:numTypes) to:link. |
3810 | 1987 |
! |
1988 |
||
2214 | 1989 |
valueStringFor:aValue |
1990 |
|valString| |
|
1991 |
||
1992 |
"/ only show the value, if it is really short... |
|
3203 | 1993 |
(aValue isBoolean |
3708 | 1994 |
or:[aValue isNil |
1995 |
or:[aValue isString |
|
1996 |
or:[aValue isNumber |
|
1997 |
or:[aValue isPoint |
|
1998 |
or:[aValue isCharacter |
|
1999 |
]]]]]) ifTrue:[ |
|
2000 |
^ aValue storeString. |
|
2226 | 2001 |
]. |
2427 | 2002 |
|
2226 | 2003 |
(aValue isColor) ifTrue:[ |
3708 | 2004 |
^ 'Color: ',(' ' emphasizeAllWith:(#backgroundColor->aValue)). |
2226 | 2005 |
]. |
2006 |
||
3203 | 2007 |
((aValue isKindOf:GUID) |
2008 |
or:[aValue isUUID]) ifTrue:[ |
|
3708 | 2009 |
valString := aValue printString. |
2427 | 2010 |
]. |
2011 |
||
3203 | 2012 |
(valString isEmptyOrNil |
2899
8b8701a65441
Avoid #isKindOf: where possible
Stefan Vogel <sv@exept.de>
parents:
2828
diff
changeset
|
2013 |
or:[ valString size > 50 |
8b8701a65441
Avoid #isKindOf: where possible
Stefan Vogel <sv@exept.de>
parents:
2828
diff
changeset
|
2014 |
or:[ valString includes:Character cr ]]) ifTrue:[ |
3708 | 2015 |
valString := aValue classNameWithArticle. |
2214 | 2016 |
]. |
2017 |
||
2018 |
^ valString |
|
2427 | 2019 |
|
2020 |
"Modified: / 14-10-2010 / 11:57:52 / cg" |
|
141 | 2021 |
! ! |
49 | 2022 |
|
3798 | 2023 |
!Explainer class methodsFor:'naive type inferer'! |
2024 |
||
2025 |
addTypeOfExpressionNode:expr forAssignmentTo:varName to:setOfTypes |
|
3842 | 2026 |
"pick up low hanging type information. |
2027 |
This is far from being complete, but often gives a hint good enough for code completion |
|
2028 |
and info in the browser." |
|
2029 |
||
2030 |
|val valClass| |
|
3798 | 2031 |
|
2032 |
"/ only look for wellknown types on the right side. |
|
3810 | 2033 |
expr isLiteral ifTrue:[ |
2034 |
val := expr value. |
|
3801 | 2035 |
valClass := val class. |
3798 | 2036 |
val isImmutable ifTrue:[ |
3801 | 2037 |
valClass := [ valClass mutableClass ] on:Error do:[ valClass ]. |
3798 | 2038 |
]. |
2039 |
self rememberType:valClass in:setOfTypes. |
|
3826 | 2040 |
^ setOfTypes. |
3798 | 2041 |
]. |
2042 |
||
2043 |
expr isMessage ifTrue:[ |
|
3842 | 2044 |
self addTypeOfMessageNode:expr forAssignmentTo:varName to:setOfTypes. |
2045 |
^ setOfTypes. |
|
3798 | 2046 |
]. |
3855 | 2047 |
|
3826 | 2048 |
^ setOfTypes |
2049 |
||
3798 | 2050 |
"Created: / 30-04-2016 / 15:28:59 / cg" |
3801 | 2051 |
"Modified: / 30-04-2016 / 20:17:35 / cg" |
3798 | 2052 |
! |
2053 |
||
3842 | 2054 |
addTypeOfMessageNode:expr forAssignmentTo:varName to:setOfTypes |
2055 |
"pick up low hanging type information. |
|
2056 |
This is far from being complete, but often gives a hint good enough for code completion |
|
2057 |
and info in the browser." |
|
2058 |
||
2059 |
|valClass msgSelector msgReceiver| |
|
2060 |
||
2061 |
msgSelector := expr selector. |
|
2062 |
msgReceiver := expr receiver. |
|
2063 |
||
2064 |
msgSelector == #? ifTrue:[ |
|
2065 |
self addTypeOfExpressionNode:(expr arg1) forAssignmentTo:varName to:setOfTypes. |
|
2066 |
^ setOfTypes |
|
2067 |
]. |
|
2068 |
||
2069 |
"/ really really only very low hanging fruit... |
|
2070 |
"/ ignore #/ here, because of filename |
|
2071 |
( #(+ - *) includes:msgSelector ) ifTrue:[ |
|
3885 | 2072 |
true "(msgReceiver isLiteralNumber or:[expr arg1 isLiteralNumber])" ifTrue:[ |
2073 |
self rememberType:Number in:setOfTypes. |
|
2074 |
^ setOfTypes. |
|
2075 |
]. |
|
2076 |
"/ "/ ignore foo := foo OP expr |
|
2077 |
"/ "/ ignore foo := expr OP foo |
|
2078 |
"/ (msgReceiver isVariable and:[msgReceiver name = varName]) ifFalse:[ |
|
2079 |
"/ (expr arg1 isVariable and:[expr arg1 name = varName]) ifFalse:[ |
|
2080 |
"/ self rememberType:Number in:setOfTypes. |
|
2081 |
"/ ] |
|
2082 |
"/ ]. |
|
3842 | 2083 |
^ setOfTypes. |
2084 |
]. |
|
2085 |
||
2086 |
( #(// size hash identityHash) includes:msgSelector ) ifTrue:[ |
|
2087 |
self rememberType:Integer in:setOfTypes. |
|
2088 |
^ setOfTypes. |
|
2089 |
]. |
|
2090 |
||
2091 |
( #(next next:) includes:msgSelector ) ifTrue:[ |
|
2092 |
|rcvrTypes| |
|
2093 |
||
2094 |
rcvrTypes := self addTypeOfExpressionNode:msgReceiver forAssignmentTo:nil to:Set new. |
|
2095 |
rcvrTypes notEmpty ifTrue:[ |
|
3885 | 2096 |
self breakPoint:#cg. |
3842 | 2097 |
self rememberType:Character in:setOfTypes. |
2098 |
]. |
|
2099 |
^ setOfTypes. |
|
2100 |
]. |
|
2101 |
||
2102 |
( msgSelector startsWith:'as') ifTrue:[ |
|
2103 |
valClass := Smalltalk classNamed:(msgSelector copyFrom:3). |
|
2104 |
valClass notNil ifTrue:[ |
|
2105 |
self rememberType:valClass in:setOfTypes. |
|
2106 |
^ setOfTypes. |
|
2107 |
]. |
|
2108 |
]. |
|
2109 |
||
2110 |
( #(copy shallowCopy) includes:msgSelector ) ifTrue:[ |
|
2111 |
msgReceiver isLiteral ifTrue:[ |
|
2112 |
self addTypeOfExpressionNode:msgReceiver forAssignmentTo:varName to:setOfTypes. |
|
2113 |
]. |
|
2114 |
^ setOfTypes |
|
2115 |
]. |
|
2116 |
||
2117 |
msgReceiver isGlobal ifTrue:[ |
|
3853 | 2118 |
|instCreatorMessages globalValue implMethod| |
3842 | 2119 |
|
3853 | 2120 |
instCreatorMessages := #(new new: basicNew basicNew:). |
2121 |
||
3842 | 2122 |
globalValue := msgReceiver value. |
2123 |
globalValue isBehavior ifTrue:[ |
|
3853 | 2124 |
( instCreatorMessages includes:msgSelector ) ifTrue:[ |
3842 | 2125 |
self rememberType:globalValue in:setOfTypes. |
2126 |
^ setOfTypes. |
|
2127 |
]. |
|
3853 | 2128 |
implMethod := globalValue class lookupMethodFor:msgSelector. |
2129 |
"/ mhmh - fuzzy; if the implementing message sends any of the above to itself... |
|
2130 |
"/ assume it is returning it. |
|
2131 |
implMethod isNil ifTrue:[ |
|
2132 |
"/ will not be understood |
|
3857 | 2133 |
self breakPoint:#cg. |
3853 | 2134 |
^ setOfTypes. |
2135 |
]. |
|
2136 |
(implMethod messagesSentToSelf includesAny:instCreatorMessages) ifTrue:[ |
|
3857 | 2137 |
self breakPoint:#cg. |
3853 | 2138 |
self rememberType:globalValue in:setOfTypes. |
2139 |
^ setOfTypes. |
|
2140 |
]. |
|
2141 |
"/ very fuzzy - if the implementing method is in the "instance creation" category... |
|
2142 |
((implMethod category ? '') startsWith:'instance creation') ifTrue:[ |
|
3857 | 2143 |
self breakPoint:#cg. |
3853 | 2144 |
self rememberType:globalValue in:setOfTypes. |
2145 |
^ setOfTypes. |
|
2146 |
]. |
|
3842 | 2147 |
]. |
2148 |
self breakPoint:#cg. |
|
2149 |
^ setOfTypes |
|
2150 |
]. |
|
2151 |
||
2152 |
self breakPoint:#cg. |
|
2153 |
^ setOfTypes |
|
2154 |
! |
|
2155 |
||
3798 | 2156 |
addTypesAssignedToInstvar:instVarName inClass:aClass method:aMethod to:setOfTypes |
2157 |
"look to asssignments to an instance variable, and pick up low hanging class information. |
|
3842 | 2158 |
This is far from being complete, but often gives a hint good enough for code completion |
3798 | 2159 |
and info in the browser." |
2160 |
||
2161 |
| code | |
|
2162 |
||
3902 | 2163 |
SourceCodeManagerError handle:[:ex | |
2164 |
] do:[ |
|
2165 |
code := aMethod source. |
|
2166 |
]. |
|
3798 | 2167 |
(code notNil) ifTrue:[ |
2168 |
self addTypesAssignedToInstvar:instVarName inClass:aClass source:code to:setOfTypes |
|
2169 |
] |
|
2170 |
||
2171 |
"Created: / 30-04-2016 / 15:07:33 / cg" |
|
2172 |
! |
|
2173 |
||
2174 |
addTypesAssignedToInstvar:instVarName inClass:aClass source:code to:setOfTypes |
|
2175 |
"look to asssignments to an instance variable, and pick up low hanging class information. |
|
3842 | 2176 |
This is far from being complete, but often gives a hint good enough for code completion |
3798 | 2177 |
and info in the browser." |
2178 |
||
3810 | 2179 |
|tree| |
3798 | 2180 |
|
2181 |
"/ quick check (avoids expensive parse) |
|
2182 |
(code includesString:instVarName) ifFalse:[ ^ self ]. |
|
2183 |
||
2184 |
tree := Parser parse:code class:aClass. |
|
2185 |
(tree isNil or:[tree == #Error]) ifTrue:[ ^ self ]. "/ unparsable |
|
2186 |
||
3810 | 2187 |
self addTypesAssignedToInstvar:instVarName inTree:tree to:setOfTypes. |
2188 |
||
2189 |
"Created: / 30-04-2016 / 15:09:18 / cg" |
|
2190 |
! |
|
2191 |
||
2192 |
addTypesAssignedToInstvar:instVarName inClass:aClass to:setOfTypes |
|
2193 |
"look to asssignments to an instance variable, and pick up low hanging class information. |
|
3842 | 2194 |
This is far from being complete, but often gives a hint good enough for code completion |
3810 | 2195 |
and info in the browser." |
2196 |
||
2197 |
aClass methodDictionary do:[:m | |
|
2198 |
self addTypesAssignedToInstvar:instVarName inClass:aClass method:m to:setOfTypes |
|
2199 |
]. |
|
2200 |
||
2201 |
"Created: / 30-04-2016 / 14:52:56 / cg" |
|
2202 |
! |
|
2203 |
||
2204 |
addTypesAssignedToInstvar:instVarName inTree:tree to:setOfTypes |
|
2205 |
"look to asssignments to an instance variable, and pick up low hanging class information. |
|
3842 | 2206 |
This is far from being complete, but often gives a hint good enough for code completion |
3810 | 2207 |
and info in the browser." |
2208 |
||
2209 |
|visitor| |
|
2210 |
||
3798 | 2211 |
visitor := PluggableParseNodeVisitor new. |
2212 |
visitor |
|
2213 |
actionForNodeClass:AssignmentNode |
|
2214 |
put:[:node | |
|
3853 | 2215 |
(node variable isInstanceVariableNamed:instVarName) ifTrue:[ |
2216 |
self addTypeOfExpressionNode:(node expression) forAssignmentTo:instVarName to:setOfTypes |
|
3798 | 2217 |
]. |
2218 |
true "/ yes - visit subnodes |
|
2219 |
]. |
|
2220 |
visitor visit:tree. |
|
2221 |
! |
|
2222 |
||
3810 | 2223 |
addTypesAssignedToLocal:localName inTree:tree to:setOfTypes |
3842 | 2224 |
"look to asssignments to a local variable, and pick up low hanging class information. |
2225 |
This is far from being complete, but often gives a hint good enough for code completion |
|
3798 | 2226 |
and info in the browser." |
2227 |
||
3810 | 2228 |
|visitor| |
2229 |
||
2230 |
"/ hack, allowing to deal with both types of AST (sigh) |
|
2231 |
(tree isKindOf:RBProgramNode) ifTrue:[ |
|
2232 |
visitor := RBPluggableProgramNodeVisitor new. |
|
2233 |
visitor |
|
2234 |
actionForNodeClass:RBAssignmentNode |
|
2235 |
put:[:node | |
|
3853 | 2236 |
|leftSide| |
3798 | 2237 |
|
3810 | 2238 |
leftSide := node variable. |
2239 |
(leftSide isTemporary and:[ leftSide name = localName ]) ifTrue:[ |
|
3853 | 2240 |
self addTypeOfExpressionNode:(node value) forAssignmentTo:localName to:setOfTypes |
3810 | 2241 |
]. |
2242 |
true "/ yes - visit subnodes |
|
2243 |
]. |
|
2244 |
visitor visitNode:tree. |
|
2245 |
] ifFalse:[ |
|
2246 |
visitor := PluggableParseNodeVisitor new. |
|
2247 |
visitor |
|
2248 |
actionForNodeClass:AssignmentNode |
|
2249 |
put:[:node | |
|
2250 |
|leftSide expr| |
|
2251 |
||
2252 |
leftSide := node variable. |
|
2253 |
(leftSide isLocalVariable and:[ leftSide name = localName ]) ifTrue:[ |
|
2254 |
expr := node expression. |
|
2255 |
self addTypeOfExpressionNode:expr forAssignmentTo:localName to:setOfTypes |
|
2256 |
]. |
|
2257 |
true "/ yes - visit subnodes |
|
2258 |
]. |
|
2259 |
visitor visit:tree. |
|
2260 |
]. |
|
3798 | 2261 |
! |
2262 |
||
2263 |
addTypesOfInstvar:instVarName inClass:aClass fromExistingInstancesTo:setOfTypes |
|
2264 |
"look for existing instances and see that type is there" |
|
2265 |
||
2266 |
|instIndex| |
|
2267 |
||
2268 |
instIndex := aClass instVarIndexFor:instVarName. |
|
2269 |
aClass allSubInstancesDo:[:i | |
|
3801 | 2270 |
|val varClass| |
3798 | 2271 |
|
3801 | 2272 |
val := (i instVarAt:instIndex). |
2273 |
val notNil ifTrue:[ |
|
2274 |
varClass := val class. |
|
2275 |
self rememberType:varClass in:setOfTypes. |
|
2276 |
]. |
|
3798 | 2277 |
]. |
2278 |
||
2279 |
"Created: / 30-04-2016 / 14:56:11 / cg" |
|
3801 | 2280 |
"Modified: / 30-04-2016 / 20:05:03 / cg" |
3798 | 2281 |
! |
2282 |
||
2283 |
compressSetOfTypes:setOfTypes |
|
2284 |
|toAdd toRemove| |
|
2285 |
||
2286 |
"/ reduce by eliminating common superclasses... |
|
2287 |
||
2288 |
toAdd := Set new. |
|
2289 |
toRemove := Set new. |
|
2290 |
setOfTypes do:[:type1 | |
|
2291 |
setOfTypes do:[:type2 | |
|
2292 |
|common| |
|
2293 |
||
2294 |
type1 superclass == type2 ifTrue:[ |
|
2295 |
toRemove add:type1. |
|
2296 |
] ifFalse:[ |
|
2297 |
type2 superclass == type1 ifTrue:[ |
|
2298 |
toRemove add:type2. |
|
2299 |
] ifFalse:[ |
|
2300 |
common := type1 commonSuperclass:type2. |
|
2301 |
common ~~ Object ifTrue:[ |
|
2302 |
toRemove add:type1. |
|
2303 |
toRemove add:type2. |
|
2304 |
toAdd add:common. |
|
2305 |
]. |
|
2306 |
]. |
|
2307 |
]. |
|
2308 |
] |
|
2309 |
]. |
|
2310 |
setOfTypes removeAll:toRemove. |
|
2311 |
setOfTypes addAll:toRemove. |
|
2312 |
||
2313 |
"/ hack |
|
2314 |
setOfTypes size == 2 ifTrue:[ |
|
2315 |
((setOfTypes includes:True) and:[setOfTypes includes:False]) ifTrue:[ |
|
2316 |
setOfTypes removeAll; add:Boolean. |
|
2317 |
^ self. |
|
2318 |
]. |
|
2319 |
((setOfTypes includes:SmallInteger) and:[setOfTypes includes:LargeInteger]) ifTrue:[ |
|
2320 |
setOfTypes removeAll; add:Integer. |
|
2321 |
^ self. |
|
2322 |
] |
|
2323 |
]. |
|
2324 |
||
2325 |
"Created: / 30-04-2016 / 15:37:38 / cg" |
|
2326 |
! |
|
2327 |
||
2328 |
rememberType:aClass in:setOfTypes |
|
2329 |
setOfTypes add:aClass |
|
2330 |
||
2331 |
"Created: / 30-04-2016 / 15:35:44 / cg" |
|
3801 | 2332 |
"Modified: / 30-04-2016 / 20:05:09 / cg" |
3798 | 2333 |
! ! |
2334 |
||
3838 | 2335 |
!Explainer class methodsFor:'utilities'! |
2336 |
||
3842 | 2337 |
actionToBrowseClass:class |
2338 |
^ self actionToBrowseClass:class selector:nil. |
|
2339 |
! |
|
2340 |
||
3838 | 2341 |
actionToBrowseClass:class selector:selectorOrNil |
3885 | 2342 |
^ self actionToBrowseClass:class selector:selectorOrNil info:nil |
3866 | 2343 |
! |
2344 |
||
2345 |
actionToBrowseClass:class selector:selectorOrNil info:info |
|
3885 | 2346 |
self assert:class isBehavior. |
3866 | 2347 |
^ ActionWithInfo |
2348 |
block: |
|
2349 |
[ |
|
2350 |
self thisOrNewBrowserInto:[:browser :openHow | |
|
2351 |
browser |
|
2352 |
spawnFullBrowserInClass:class selector:selectorOrNil in:openHow |
|
2353 |
] |
|
2354 |
] |
|
2355 |
info:info |
|
3838 | 2356 |
! |
2357 |
||
2358 |
actionToBrowseClasses:classes |
|
3868 | 2359 |
^ ActionWithInfo |
2360 |
block:[ |
|
2361 |
"/ Tools::NewSystemBrowser browseClasses:classes |
|
2362 |
self thisOrNewBrowserInto:[:browser :openHow | |
|
2363 |
browser spawnClassBrowserFor:classes in:openHow |
|
2364 |
] |
|
3855 | 2365 |
] |
3868 | 2366 |
info:nil |
3838 | 2367 |
! |
2368 |
||
3855 | 2369 |
actionToBrowseImplementorsOf:selector |
4032 | 2370 |
^ self actionToBrowseImplementorsOf:selector in:[Smalltalk allClasses] |
3866 | 2371 |
! |
2372 |
||
2373 |
actionToBrowseImplementorsOf:selector in:classes |
|
3868 | 2374 |
^ ActionWithInfo |
2375 |
block:[ |
|
2376 |
self thisOrNewBrowserInto:[:browser :openHow | |
|
2377 |
browser |
|
2378 |
spawnMethodImplementorsBrowserFor:{ selector } |
|
2379 |
match:false |
|
2380 |
in:openHow |
|
4032 | 2381 |
classes:classes value |
3868 | 2382 |
label:('Implementors of %1' bindWith:selector) |
2383 |
] |
|
3855 | 2384 |
] |
4032 | 2385 |
info:('Click to browse implementors') |
3842 | 2386 |
! |
2387 |
||
3853 | 2388 |
actionToBrowseInstvarRefsTo:instVarName inClass:class |
3868 | 2389 |
^ ActionWithInfo |
2390 |
block:[ |
|
2391 |
self thisOrNewBrowserInto:[:browser :openHow | |
|
2392 |
browser |
|
2393 |
browseVarRefsToAny:{ instVarName } |
|
2394 |
classes:{ class } |
|
2395 |
variables:#instVarNames access:#readOrWrite all:true |
|
2396 |
title:'references to ',instVarName |
|
2397 |
in:openHow |
|
2398 |
] |
|
2399 |
] |
|
2400 |
info:nil |
|
3853 | 2401 |
! |
2402 |
||
3842 | 2403 |
actionToBrowseMethod:mthd |
3885 | 2404 |
self assert:mthd isMethod. |
3842 | 2405 |
^ self actionToBrowseClass:(mthd mclass) selector:(mthd selector). |
2406 |
! |
|
2407 |
||
2408 |
actionToOpenMethodFinderFor:selector |
|
3838 | 2409 |
MethodFinderWindow notNil ifTrue:[ |
3868 | 2410 |
^ ActionWithInfo |
2411 |
block:[ |
|
2412 |
MethodFinderWindow openOnSelectorPattern:selector |
|
2413 |
] |
|
2414 |
info:nil. |
|
3838 | 2415 |
]. |
3842 | 2416 |
^ self actionToBrowseImplementorsOf:selector |
2417 |
! |
|
2418 |
||
2419 |
asClassLink:nameOfClass |
|
2420 |
"return text with a hyperlink to browse a class by that name" |
|
3838 | 2421 |
|
3842 | 2422 |
|cls| |
2423 |
||
2424 |
cls := Smalltalk classNamed:nameOfClass string asUppercaseFirst. |
|
2425 |
cls isNil ifTrue:[^ nameOfClass]. |
|
3859 | 2426 |
|
3866 | 2427 |
^ self |
2428 |
asLink:nameOfClass |
|
3878 | 2429 |
info:('Click to browse %1' bindWith:nameOfClass) |
3866 | 2430 |
to:(self actionToBrowseClass:cls) |
2431 |
! |
|
2432 |
||
2433 |
asLink:aString info:info to:actionOrNil |
|
2434 |
actionOrNil isNil ifTrue:[^ aString]. |
|
2435 |
^ aString asActionLinkTo:(ActionWithInfo block:actionOrNil info:info) |
|
2436 |
! |
|
2437 |
||
2438 |
asLink:aString to:actionOrNil |
|
2439 |
actionOrNil isNil ifTrue:[^ aString]. |
|
2440 |
^ aString asActionLinkTo:actionOrNil |
|
2441 |
! |
|
2442 |
||
3872 | 2443 |
infoStringForClasses:aCollectionOfClasses withPrefix:prefix |
2444 |
"get a nice user readable list for some classes. |
|
2445 |
Up to 4 are named, otherwise the count is presented. |
|
2446 |
The prefix can be sth like ' other', ' sub', ' super', |
|
2447 |
' implementing' etc. Or it can be an empty string. |
|
2448 |
To be shown in the info line at the bottom." |
|
2449 |
||
3885 | 2450 |
|nClassNames sortedByName classNames link1 link2 link3 link4| |
3872 | 2451 |
|
2452 |
aCollectionOfClasses isEmpty ifTrue:[ |
|
2453 |
^ 'No %1classes' bindWith:prefix. |
|
2454 |
]. |
|
2455 |
||
4032 | 2456 |
classNames := aCollectionOfClasses asIdentitySet asOrderedCollection |
2457 |
collect:[:each | each theNonMetaclass name]. |
|
3872 | 2458 |
|
2459 |
nClassNames := classNames size. |
|
2460 |
||
2461 |
nClassNames <= 4 ifTrue:[ |
|
3885 | 2462 |
sortedByName := classNames sort. |
2463 |
link1 := self asClassLink:sortedByName first. |
|
3872 | 2464 |
nClassNames == 1 ifTrue:[ |
3885 | 2465 |
^ '%2' "'1 %1class: %2'" bindWith:prefix with:link1. |
3872 | 2466 |
]. |
3885 | 2467 |
link2 := self asClassLink:classNames second. |
3872 | 2468 |
nClassNames == 2 ifTrue:[ |
2469 |
^ '%2 and %3' "'2 %1classes: %2 and %3'" bindWith:prefix |
|
3885 | 2470 |
with:link1 |
2471 |
with:link2. |
|
3872 | 2472 |
]. |
3885 | 2473 |
link3 := self asClassLink:classNames third. |
3872 | 2474 |
nClassNames == 3 ifTrue:[ |
2475 |
^ '%2, %3 and %4' "'3 %1classes: %2, %3 and %4'" bindWith:prefix |
|
3885 | 2476 |
with:link1 |
2477 |
with:link2 |
|
2478 |
with:link3. |
|
3872 | 2479 |
]. |
3885 | 2480 |
link4 := self asClassLink:classNames fourth. |
3872 | 2481 |
nClassNames == 4 ifTrue:[ |
2482 |
^ '%2, %3, %4 and %5' "'4 %1classes: %2, %3, %4 and %5'" bindWith:prefix |
|
3885 | 2483 |
with:link1 |
2484 |
with:link2 |
|
2485 |
with:link3 |
|
2486 |
with:link4. |
|
3872 | 2487 |
]. |
2488 |
]. |
|
3885 | 2489 |
^ self |
2490 |
asLink:('%1 %2classes' bindWith:nClassNames printString with:prefix) |
|
2491 |
info:'Browse classes' |
|
2492 |
to:(self actionToBrowseClasses:aCollectionOfClasses) |
|
3872 | 2493 |
|
2494 |
"Modified: / 27-07-2006 / 10:09:02 / cg" |
|
2495 |
! |
|
2496 |
||
3866 | 2497 |
infoStringForClasses:aCollectionOfClasses withPrefix:prefix actionGenerator:actionGeneratorOrNil |
2498 |
"get a nice user readable list for some classes. |
|
2499 |
Up to 4 are named, otherwise the count is presented. |
|
2500 |
The prefix can be sth like ' other', ' sub', ' super', |
|
2501 |
' implementing' etc. Or it can be an empty string. |
|
2502 |
To be shown in the info line at the bottom." |
|
2503 |
||
2504 |
|nClassNames classes sortedByName classNames |
|
2505 |
link1 link2 link3 link4 actionGenerator| |
|
2506 |
||
2507 |
aCollectionOfClasses isEmpty ifTrue:[ |
|
2508 |
^ 'No %1classes' bindWith:prefix. |
|
2509 |
]. |
|
2510 |
||
2511 |
actionGenerator := actionGeneratorOrNil isNil |
|
2512 |
ifTrue:[ [:classes | self actionToBrowseClasses:classes] ] |
|
2513 |
ifFalse:[ actionGeneratorOrNil ]. |
|
2514 |
||
2515 |
classes := aCollectionOfClasses asIdentitySet asOrderedCollection. |
|
2516 |
classNames := classes collect:[:each | each theNonMetaclass name]. |
|
2517 |
||
2518 |
nClassNames := classNames size. |
|
2519 |
||
2520 |
nClassNames <= 4 ifTrue:[ |
|
2521 |
sortedByName := classNames sortWith:classes. |
|
2522 |
||
2523 |
link1 := self asLink:classNames first "allBold" |
|
2524 |
info:('Browse %1' bindWith:classNames first) |
|
2525 |
to:(actionGenerator value:{classes first}). |
|
2526 |
nClassNames == 1 ifTrue:[ |
|
2527 |
^ '%2' "'1 %1class: %2'" |
|
2528 |
bindWith:prefix |
|
2529 |
with:link1. |
|
2530 |
]. |
|
2531 |
link2 := self asLink:classNames second "allBold" to:(actionGenerator value:{classes second}). |
|
2532 |
nClassNames == 2 ifTrue:[ |
|
2533 |
^ '%2 and %3' "'2 %1classes: %2 and %3'" |
|
2534 |
bindWith:prefix |
|
2535 |
with:link1 |
|
2536 |
with:link2. |
|
2537 |
]. |
|
2538 |
link3 := self asLink:classNames third "allBold" to:(actionGenerator value:{classes third}). |
|
2539 |
nClassNames == 3 ifTrue:[ |
|
2540 |
^ '%2, %3 and %4' "'3 %1classes: %2, %3 and %4'" |
|
2541 |
bindWith:prefix |
|
2542 |
with:link1 |
|
2543 |
with:link2 |
|
2544 |
with:link3. |
|
2545 |
]. |
|
2546 |
link4 := self asLink:classNames fourth "allBold" to:(actionGenerator value:{classes fourth}). |
|
2547 |
nClassNames == 4 ifTrue:[ |
|
2548 |
^ '%2, %3, %4 and %5' "'4 %1classes: %2, %3, %4 and %5'" |
|
2549 |
bindWith:prefix |
|
2550 |
with:link1 |
|
2551 |
with:link2 |
|
2552 |
with:link3 |
|
2553 |
with:link4. |
|
2554 |
]. |
|
2555 |
]. |
|
2556 |
^ self |
|
2557 |
asLink:('%1 %2classes' bindWith:(nClassNames printString "allBold") with:prefix) |
|
2558 |
to:(actionGenerator value:classes) |
|
2559 |
||
2560 |
"Modified: / 27-07-2006 / 10:09:02 / cg" |
|
3842 | 2561 |
! |
2562 |
||
2563 |
infoStringForMethods:aCollectionOfMethods withPrefix:prefix |
|
2564 |
"get a nice user readable list for some methods. |
|
2565 |
Up to 3 are named, otherwise the count is presented. |
|
2566 |
The prefix can be sth like ' other', ' sender', ' implementor', |
|
2567 |
Or it can be an empty string. |
|
2568 |
Result is meant to be shown in the info line at the bottom of a browser." |
|
2569 |
||
2570 |
|nMethodNames sortedByName methodNames| |
|
2571 |
||
2572 |
aCollectionOfMethods isEmpty ifTrue:[ |
|
2573 |
^ 'No %1' bindWith:prefix. |
|
2574 |
]. |
|
2575 |
||
2576 |
methodNames := aCollectionOfMethods asOrderedCollection |
|
2577 |
collect:[:each | each whoString]. |
|
2578 |
||
2579 |
nMethodNames := methodNames size. |
|
2580 |
||
2581 |
nMethodNames <= 3 ifTrue:[ |
|
2582 |
nMethodNames == 1 ifTrue:[ |
|
2583 |
^ '%2' "'1 %1class: %2'" bindWith:prefix with:(methodNames first allBold). |
|
2584 |
]. |
|
2585 |
sortedByName := methodNames sort. |
|
2586 |
nMethodNames == 2 ifTrue:[ |
|
2587 |
^ '%2 and %3' "'2 %1classes: %2 and %3'" bindWith:prefix |
|
2588 |
with:(sortedByName first allBold) |
|
2589 |
with:(sortedByName second allBold). |
|
2590 |
]. |
|
2591 |
nMethodNames == 3 ifTrue:[ |
|
2592 |
^ '%2, %3 and %4' "'3 %1classes: %2, %3 and %4'" bindWith:prefix |
|
2593 |
with:(sortedByName first allBold) |
|
2594 |
with:(sortedByName second allBold) |
|
2595 |
with:(sortedByName third allBold). |
|
2596 |
]. |
|
2597 |
nMethodNames == 4 ifTrue:[ |
|
2598 |
^ '%2, %3, %4 and %5' "'4 %1classes: %2, %3, %4 and %5'" bindWith:prefix |
|
2599 |
with:(sortedByName first allBold) |
|
2600 |
with:(sortedByName second allBold) |
|
2601 |
with:(sortedByName third allBold) |
|
2602 |
with:(sortedByName fourth allBold). |
|
2603 |
]. |
|
2604 |
]. |
|
2605 |
^ '%1 %2methods' bindWith:nMethodNames printString allBold with:prefix. |
|
2606 |
||
2607 |
" |
|
2608 |
Time millisecondsToRun:[ |
|
2609 |
self infoStringForMethods:(SystemBrowser allCallsOn:#'at:put:') withPrefix:'' |
|
2610 |
]. |
|
2611 |
Time millisecondsToRun:[ |
|
2612 |
self infoStringForMethods:(SystemBrowser allCallsOn:#'actionToBrowseClasses:') withPrefix:'' |
|
2613 |
]. |
|
2614 |
" |
|
2615 |
! |
|
2616 |
||
3872 | 2617 |
methodImplementorsInfoFor:aMethod inEnvironment:environment |
2618 |
"get something about the implementors of aMethod |
|
2619 |
to be shown in the info line at the bottom" |
|
2620 |
||
2621 |
|implementors msg senders msg2| |
|
2622 |
||
2623 |
implementors := SystemBrowser |
|
2624 |
findImplementorsOf:aMethod selector |
|
2625 |
in:(environment allClasses) |
|
2626 |
ignoreCase:false. |
|
2627 |
||
2628 |
implementors notEmpty ifTrue:[ |
|
2629 |
msg := 'Only implemented here.'. |
|
2630 |
implementors remove:aMethod ifAbsent:nil. |
|
2631 |
implementors notEmpty ifTrue:[ |
|
2632 |
implementors := implementors collect:[:mthd | mthd mclass ? mthd getMclass]. |
|
2633 |
implementors notEmpty ifTrue:[ |
|
4032 | 2634 |
msg := 'Also ',(self asLink:'implemented' to:(self actionToBrowseImplementorsOf:aMethod selector)),' in '. |
3872 | 2635 |
msg := msg , (self infoStringForClasses:implementors withPrefix:'other '). |
2636 |
msg := msg , '.'. |
|
2637 |
] |
|
2638 |
]. |
|
2639 |
]. |
|
2640 |
||
2641 |
false ifTrue:[ "/ too slow |
|
2642 |
senders := SystemBrowser |
|
2643 |
findSendersOf:aMethod selector |
|
2644 |
in:(environment allClasses) |
|
2645 |
ignoreCase:false. |
|
2646 |
senders notEmpty ifTrue:[ |
|
2647 |
msg2 := 'Sent from ' , senders size printString, ' methods.'. |
|
2648 |
] ifFalse:[ |
|
2649 |
msg2 := 'No senders.'. |
|
2650 |
]. |
|
2651 |
msg := msg , '/' , msg2 |
|
2652 |
]. |
|
2653 |
||
2654 |
^ msg |
|
2655 |
! |
|
2656 |
||
3871 | 2657 |
methodInheritanceInfoFor:aMethod |
2658 |
|methodsSuperclass inheritedClass msg methodsClass sel mthd selectorString| |
|
2659 |
||
2660 |
methodsClass := aMethod mclass. |
|
2661 |
methodsClass isNil ifTrue:[^ nil]. |
|
2662 |
||
2663 |
methodsSuperclass := methodsClass superclass. |
|
2664 |
methodsSuperclass isNil ifTrue:[^ nil]. |
|
2665 |
||
2666 |
sel := aMethod selector. |
|
2667 |
inheritedClass := methodsSuperclass whichClassIncludesSelector:sel. |
|
2668 |
inheritedClass notNil ifTrue:[ |
|
2669 |
mthd := inheritedClass compiledMethodAt:sel. |
|
4110 | 2670 |
(mthd sendsSelector:#'subclassResponsibility') ifTrue:[ |
3871 | 2671 |
msg := '%1 overrides subclassResponsibility in %2'. |
2672 |
] ifFalse:[ |
|
2673 |
msg := '%1 overrides implementation in %2'. |
|
2674 |
]. |
|
2675 |
selectorString := sel contractTo:30. |
|
2676 |
^ msg |
|
2677 |
bindWith:(self |
|
2678 |
asLink:selectorString "allBold" |
|
2679 |
info:('Browse all implementors of %1' bindWith:selectorString) |
|
2680 |
to:(self actionToOpenMethodFinderFor:sel)) |
|
2681 |
with:(self |
|
2682 |
asLink:inheritedClass name "allBold" |
|
4059 | 2683 |
info:('Browse %1 » %2' bindWith:inheritedClass name with:sel) |
3871 | 2684 |
to:(self actionToBrowseClass:inheritedClass selector:sel)). |
2685 |
]. |
|
2686 |
||
2687 |
^ msg |
|
4110 | 2688 |
|
2689 |
"Modified: / 05-02-2017 / 01:24:25 / cg" |
|
3871 | 2690 |
! |
2691 |
||
2692 |
methodRedefinitionInfoFor:aMethod |
|
2693 |
"return a user readable string telling in how many subclasses |
|
2694 |
a method is redefined. |
|
2695 |
To be shown in the info line of a browser" |
|
2696 |
||
4060 | 2697 |
|redefiningClasses msg methodsClass| |
2698 |
||
2699 |
methodsClass := aMethod mclass. |
|
2700 |
methodsClass isNil ifTrue:[^ nil]. |
|
2701 |
||
2702 |
redefiningClasses := methodsClass allSubclasses select:[:eachClass | eachClass includesSelector:aMethod selector]. |
|
2703 |
redefiningClasses size ~~ 0 ifTrue:[ |
|
3871 | 2704 |
msg := 'redefined in '. |
2705 |
msg := msg , (self |
|
2706 |
infoStringForClasses:redefiningClasses |
|
2707 |
withPrefix:'sub' |
|
2708 |
actionGenerator:[:classes | |
|
3878 | 2709 |
classes size == 1 ifTrue:[ |
2710 |
|cls| |
|
4060 | 2711 |
|
3878 | 2712 |
cls := classes first. |
2713 |
self |
|
2714 |
actionToBrowseClass:cls |
|
2715 |
selector:aMethod selector |
|
2716 |
info:('Browse implementation in %1' bindWith:cls name) |
|
2717 |
] ifFalse:[ |
|
2718 |
self |
|
2719 |
actionToBrowseImplementorsOf:aMethod selector |
|
2720 |
in:classes |
|
2721 |
]. |
|
2722 |
]). |
|
3871 | 2723 |
msg := msg , '.'. |
2724 |
]. |
|
2725 |
||
2726 |
^ msg |
|
2727 |
! |
|
2728 |
||
3842 | 2729 |
methodSendersInfoFor:selector inEnvironment:environment |
2730 |
"get something about the senders of a message. |
|
2731 |
to be shown in the info line at the bottom. |
|
2732 |
This may be slow; so think about doing it in background..." |
|
2733 |
||
2734 |
|senders| |
|
2735 |
||
2736 |
senders := SystemBrowser |
|
2737 |
findSendersOf:selector |
|
2738 |
in:(environment allClasses) |
|
2739 |
ignoreCase:false |
|
2740 |
match:false. |
|
2741 |
||
2742 |
senders notEmpty ifTrue:[ |
|
2743 |
^ 'Sent from ' , senders size printString, ' methods.'. |
|
2744 |
] ifFalse:[ |
|
2745 |
^ 'No senders.'. |
|
2746 |
]. |
|
3859 | 2747 |
! |
2748 |
||
3872 | 2749 |
methodSpecialInfoFor:aMethod |
2750 |
"handles special cases - such as documentation methods" |
|
2751 |
||
2752 |
|cls sel| |
|
2753 |
||
2754 |
(cls := aMethod mclass) isNil ifTrue:[^ nil]. |
|
2755 |
(sel := aMethod selector) isNil ifTrue:[^ nil]. |
|
2756 |
||
2757 |
cls isMeta ifTrue:[ |
|
2758 |
(AbstractSourceCodeManager isVersionMethodSelector:sel) ifTrue:[ |
|
2759 |
^ 'The version method is required for the source code repository - do not modify.'. |
|
2760 |
]. |
|
2761 |
sel == #documentation ifTrue:[ |
|
2762 |
^ 'ST/X stores documentation in this method (not in comment slots)'. |
|
2763 |
]. |
|
2764 |
]. |
|
2765 |
^ nil |
|
2766 |
! |
|
2767 |
||
3859 | 2768 |
thisOrNewBrowserInto:aTwoArgBlock |
2769 |
"if I am invoked by a browser, |
|
2770 |
invoke the twoArgBlock withit and an #newBuffer arg. |
|
2771 |
Otherwise, create a new (invisible) browser and pass it to the block |
|
2772 |
with a #newBrowser arg." |
|
2773 |
||
2774 |
|windowGroupClass browserClass wg app| |
|
2775 |
||
2776 |
"/ stupid: I am in libcomp; should be in libtool |
|
2777 |
windowGroupClass := Smalltalk at:#WindowGroup. |
|
2778 |
windowGroupClass isNil ifTrue:[^ self]. |
|
2779 |
browserClass := Smalltalk at:#'Tools::NewSystemBrowser'. |
|
2780 |
browserClass isNil ifTrue:[^ self]. |
|
2781 |
||
2782 |
((wg := windowGroupClass activeGroup) notNil |
|
2783 |
and:[ (app := wg application) isKindOf:browserClass ] |
|
2784 |
) ifTrue:[ |
|
2785 |
^ aTwoArgBlock value:app value:#newBuffer |
|
2786 |
]. |
|
2787 |
^ aTwoArgBlock value:(browserClass basicNew) value:#newBrowser |
|
3838 | 2788 |
! ! |
3826 | 2789 |
|
3866 | 2790 |
!Explainer::ActionWithInfo class methodsFor:'instance creation'! |
2791 |
||
2792 |
block:aBlock info:info |
|
2793 |
^ self new block:aBlock info:info |
|
2794 |
! ! |
|
2795 |
||
2796 |
!Explainer::ActionWithInfo methodsFor:'accessing'! |
|
2797 |
||
2798 |
block:blockArg info:infoArg |
|
2799 |
block := blockArg. |
|
2800 |
info := infoArg. |
|
2801 |
! |
|
2802 |
||
2803 |
info:something |
|
2804 |
info := something. |
|
2805 |
! ! |
|
2806 |
||
2807 |
!Explainer::ActionWithInfo methodsFor:'evaluation'! |
|
2808 |
||
2809 |
info |
|
2810 |
^ info |
|
2811 |
! |
|
2812 |
||
2813 |
value |
|
2814 |
^ block value |
|
2815 |
! |
|
2816 |
||
2817 |
value:arg |
|
2818 |
^ block value:arg |
|
2819 |
! ! |
|
2820 |
||
396 | 2821 |
!Explainer class methodsFor:'documentation'! |
146
3d407ab8ac44
suppress messages on transcript when explaining
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
2822 |
|
3d407ab8ac44
suppress messages on transcript when explaining
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
2823 |
version |
3697 | 2824 |
^ '$Header$' |
2214 | 2825 |
! |
2826 |
||
2827 |
version_CVS |
|
3697 | 2828 |
^ '$Header$' |
146
3d407ab8ac44
suppress messages on transcript when explaining
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
2829 |
! ! |
3209 | 2830 |