author | Stefan Vogel <sv@exept.de> |
Fri, 02 Dec 2016 13:31:06 +0100 | |
changeset 4059 | 35a0032c3d95 |
parent 4040 | 59634354b0c5 |
child 4060 | 25c54b8b50c6 |
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:[ |
603 |
firstIndent := line2 indexOfNonSeparator - 1 max:0. |
|
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" |
|
3798 | 639 |
"Modified (comment): / 30-04-2016 / 16:17:18 / 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 |
3798 | 779 |
spc sym sel stringText cls clsName val valString| |
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:[ |
835 |
clsName := c name. |
|
836 |
shortText ifTrue:[ |
|
3866 | 837 |
clsName := self asClassLink:clsName. |
3233 | 838 |
stringText := stringText , ': a classVar in ' , clsName |
839 |
] ifFalse:[ |
|
840 |
stringText := stringText , ': a class variable in ' , clsName |
|
841 |
]. |
|
2315
45f09cea2063
changed: #explain:in:forClass:short:
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
842 |
|
3233 | 843 |
val := c theNonMetaclass classVarAt:string. "/ Smalltalk at:(clsName , ':' , string) asSymbol. |
844 |
valString := self valueStringFor:val. |
|
845 |
^ stringText , ' (' , valString , ').' |
|
846 |
]. |
|
1693 | 847 |
|
3233 | 848 |
"private classes" |
849 |
c := aClass theNonMetaclass. |
|
850 |
c privateClasses do:[:pClass | |
|
851 |
(pClass name = string |
|
852 |
or:[pClass nameWithoutPrefix = string]) ifTrue:[ |
|
3866 | 853 |
stringText := self asClassLink:pClass name. |
3233 | 854 |
stringText := stringText , ': a private class in ''' , c name , '''.'. |
855 |
shortText ifFalse:[ |
|
856 |
stringText := (stringText , '\\It is only visible locally.') withCRs |
|
857 |
]. |
|
858 |
^ stringText withCRs |
|
859 |
]. |
|
860 |
]. |
|
422 | 861 |
|
3233 | 862 |
aClass theNonMetaclass sharedPoolNames do:[:eachPoolName | |
3810 | 863 |
|sharedPool sharedPoolSym poolName| |
2037
003d9b5a2788
explain shared pool variables
Claus Gittinger <cg@exept.de>
parents:
2031
diff
changeset
|
864 |
|
3407 | 865 |
sharedPoolSym := string asSymbolIfInterned. |
866 |
sharedPoolSym notNil ifTrue:[ |
|
867 |
sharedPool := Smalltalk classNamed:eachPoolName. |
|
868 |
sharedPool notNil ifTrue:[ |
|
869 |
sharedPool isSharedPool ifFalse:[ |
|
870 |
^ 'oops - not a shared pool: ',eachPoolName |
|
871 |
]. |
|
872 |
(sharedPool includesKey:sharedPoolSym) ifTrue:[ |
|
3810 | 873 |
poolName := sharedPool name. |
3866 | 874 |
poolName := self asClassLink:eachPoolName. |
3810 | 875 |
stringText := stringText , ': a pool variable in ',poolName. |
3407 | 876 |
val := sharedPool at:sharedPoolSym. |
877 |
valString := self valueStringFor:val. |
|
878 |
^ stringText , ' (' , valString , ').' |
|
879 |
]. |
|
3233 | 880 |
]. |
881 |
]. |
|
882 |
]. |
|
2037
003d9b5a2788
explain shared pool variables
Claus Gittinger <cg@exept.de>
parents:
2031
diff
changeset
|
883 |
|
3233 | 884 |
"namespace & global variables" |
885 |
(spc := aClass nameSpace) notNil ifTrue:[ |
|
886 |
sym := (spc name , '::' , string) asSymbolIfInterned. |
|
887 |
sym notNil ifTrue:[ |
|
888 |
(cls := Smalltalk at:sym) isBehavior ifTrue:[ |
|
3866 | 889 |
stringText := self asClassLink:sym. |
3233 | 890 |
string := stringText , ': '. |
891 |
cls name = sym ifFalse:[ |
|
892 |
string := string , 'refers to ',cls name,', ' |
|
893 |
]. |
|
894 |
cls isSharedPool ifTrue:[ |
|
895 |
string := string , 'a sharedPool'. |
|
896 |
] ifFalse:[ |
|
897 |
string := string , 'a class'. |
|
898 |
]. |
|
3049 | 899 |
|
3233 | 900 |
string := string , ' in the ''' , spc name , ''' nameSpace'. |
901 |
string := string , ' {', cls category ,'}'. |
|
902 |
shortText ifFalse:[ |
|
903 |
string := (string |
|
904 |
, '\\It is only visible within this nameSpace.' |
|
905 |
, '\Access from the outside is possible' |
|
906 |
, '\by the special name ''' , spc name , '::' , string , '''.') withCRs |
|
907 |
]. |
|
908 |
^ string withCRs |
|
909 |
]. |
|
910 |
]. |
|
911 |
]. |
|
422 | 912 |
|
3866 | 913 |
"/ string knownAsSymbol ifTrue:[ |
3233 | 914 |
"globals & symbols" |
839 | 915 |
|
3319 | 916 |
explanation := self explainKnownSymbol:string inClass:aClass short:shortText. |
917 |
explanation notNil ifTrue:[ |
|
3233 | 918 |
string isBinarySelector ifTrue:[ |
919 |
"/ some are both known as syntax AND as selector (for example: #| ) |
|
920 |
tmp1 := self explainSyntax:string short:shortText. |
|
921 |
tmp1 notNil ifTrue:[ |
|
3319 | 922 |
^ tmp1 , '\\also:\\' withCRs , explanation |
3233 | 923 |
]. |
924 |
]. |
|
3319 | 925 |
^ explanation |
3233 | 926 |
]. |
927 |
||
928 |
"/ try with added colon ... |
|
929 |
sel := string , ':'. |
|
930 |
Symbol allInstancesDo:[:sym | |
|
931 |
(sym startsWith:sel) ifTrue:[ |
|
3319 | 932 |
explanation := self explainKnownSymbol:sym inClass:aClass short:shortText. |
933 |
explanation notNil ifTrue:[ ^ explanation]. |
|
3233 | 934 |
] |
935 |
]. |
|
3866 | 936 |
"/ ]. |
2 | 937 |
|
3233 | 938 |
"try for some obvious things" |
3319 | 939 |
explanation := self explainPseudoVariable:string in:aClass short:true. |
940 |
explanation notNil ifTrue:[ ^ explanation]. |
|
2315
45f09cea2063
changed: #explain:in:forClass:short:
Claus Gittinger <cg@exept.de>
parents:
2314
diff
changeset
|
941 |
]. |
2 | 942 |
|
943 |
"try syntax ..." |
|
944 |
||
3319 | 945 |
explanation := self explainSyntax:string short:shortText. |
946 |
explanation notNil ifTrue:[ ^ explanation]. |
|
20 | 947 |
|
1183 | 948 |
shortText ifTrue:[ |
3883 | 949 |
|selector| |
950 |
||
951 |
(selector := SystemBrowser extractSelectorFrom:string) notNil ifTrue:[ |
|
952 |
selector ~= string string ifTrue:[ |
|
953 |
^ self explain:selector in:source forClass:aClass short:shortText |
|
954 |
]. |
|
955 |
]. |
|
3233 | 956 |
^ 'no explanation' |
1183 | 957 |
]. |
958 |
||
3319 | 959 |
explainer isNil ifTrue:[ |
3233 | 960 |
^ 'parse error - no explanation' |
2 | 961 |
]. |
3319 | 962 |
^ 'Sorry, I cannot explain this (could not figure out what this is). |
963 |
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
|
964 |
|
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
965 |
"Created: / 03-12-1995 / 12:47:37 / cg" |
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
966 |
"Modified: / 16-04-1997 / 12:46:11 / stefan" |
3798 | 967 |
"Modified: / 30-04-2016 / 15:00:28 / cg" |
2 | 968 |
! |
969 |
||
1188 | 970 |
explainGlobal:string inClass:aClass short:shortText |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
971 |
"return an explanation or nil" |
1183 | 972 |
|
2427 | 973 |
^ self explainGlobalOrPoolVariable:string inClass:aClass short:shortText |
974 |
||
975 |
"Modified: / 14-10-2010 / 11:33:17 / cg" |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
976 |
"Modified (comment): / 28-02-2012 / 10:45:58 / cg" |
2427 | 977 |
! |
978 |
||
3878 | 979 |
explainGlobalOrPoolVariable:varName inClass:aClass short:shortText |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
980 |
"return an explanation or nil" |
2427 | 981 |
|
3879 | 982 |
|template bindings |
3972 | 983 |
sym explanation val pool valText doc| |
141 | 984 |
|
3878 | 985 |
"if not even known as key, it's definitely not a global" |
986 |
sym := varName asSymbolIfInterned. |
|
796 | 987 |
sym isNil ifTrue:[^ nil]. |
3879 | 988 |
|
989 |
template := nil. |
|
990 |
bindings := Dictionary new. |
|
991 |
||
2427 | 992 |
"a pool variable?" |
993 |
aClass notNil ifTrue:[ |
|
3319 | 994 |
aClass theNonMetaclass sharedPoolNames do:[:eachPoolName | |
995 |
pool := Smalltalk at:eachPoolName. |
|
996 |
pool isNil ifTrue:[ |
|
3878 | 997 |
"/ oops - on the fly we encountered a non existing pool... |
3319 | 998 |
] ifFalse:[ |
3878 | 999 |
(pool classVarNames includes:varName) ifTrue:[ |
3879 | 1000 |
template := '%(varName): pool variable in "%(poolName)"'. |
1001 |
bindings at:'poolName' put:eachPoolName. |
|
3319 | 1002 |
val := pool classVarAt:sym. |
1003 |
]. |
|
1004 |
]. |
|
1005 |
]. |
|
2427 | 1006 |
]. |
1007 |
||
3879 | 1008 |
bindings at:'varName' put:varName allBold. |
3810 | 1009 |
|
3879 | 1010 |
template isNil ifTrue:[ |
3319 | 1011 |
"try globals" |
3879 | 1012 |
(Smalltalk includesKey:sym) ifFalse:[ |
1013 |
^ '%(varName): an undefined variable.' bindWithArguments:bindings. |
|
1014 |
]. |
|
1015 |
template := '%(varName): global'. |
|
1016 |
val := Smalltalk at:sym. |
|
2427 | 1017 |
]. |
141 | 1018 |
|
1245 | 1019 |
val isBehavior ifTrue:[ |
3319 | 1020 |
"/ a class |
1021 |
val name = sym ifFalse:[ |
|
3879 | 1022 |
"/ an alias (such as OperatingSystem, Screen etc.) |
4040 | 1023 |
template := template,'. Refers to %(realName)'. |
3879 | 1024 |
bindings at:'realName' put:val name. |
3319 | 1025 |
]. |
1979 | 1026 |
|
3319 | 1027 |
(val isRealNameSpace) ifTrue:[ |
3879 | 1028 |
template := template,', a namespace.'. |
1029 |
^ template bindWithArguments:bindings. |
|
3319 | 1030 |
]. |
3879 | 1031 |
|
1032 |
"/ a real class |
|
1033 |
bindings at:'category' put:(val category ? 'uncategorized'). |
|
3319 | 1034 |
shortText ifTrue:[ |
3883 | 1035 |
template := template , ',' , (val isLoaded ifTrue:[' a'] ifFalse:[' an autoloaded']). |
3879 | 1036 |
template := template , (val isSharedPool ifTrue:[' pool'] ifFalse:[' class']). |
1037 |
template := template , ' in %(package) {%(category)}.'. |
|
1038 |
bindings at:'package' put:val package. |
|
4040 | 1039 |
bindings at:'varName' put:(self asClassLink:varName "val name"). |
3879 | 1040 |
^ template bindWithArguments:bindings |
3319 | 1041 |
]. |
1330 | 1042 |
|
3878 | 1043 |
val name = varName ifTrue:[ |
3322 | 1044 |
false ifTrue:[ |
1045 |
"/ TODO: generate a short documentation string (comment plus interface) |
|
1046 |
"/ and return { #html->htmlText . #text->regularText} |
|
3972 | 1047 |
"/ so caller can extract what he wants to see... |
3322 | 1048 |
^ (HTMLDocGenerator htmlDocOf:val). |
1049 |
]. |
|
3972 | 1050 |
explanation := varName , ' is '. |
3319 | 1051 |
explanation := explanation , (val isLoaded ifTrue:['a'] ifFalse:['an autoloaded']). |
1052 |
explanation := explanation , (val isSharedPool ifTrue:[' sharedPool'] ifFalse:[' class']). |
|
3972 | 1053 |
explanation := explanation , ' categorized as "' , val category , '"'. |
3319 | 1054 |
explanation := explanation , '\' withCRs , 'in the "' , val package , '" package.'. |
1055 |
(doc := val commentOrDocumentationString) notEmptyOrNil ifTrue:[ |
|
1056 |
doc := doc asStringCollection. |
|
1057 |
doc size > 25 ifTrue:[ |
|
1058 |
doc := doc copyTo:25. |
|
1059 |
doc add:''; add:' ... <more documentation cut off>'. |
|
1060 |
]. |
|
3864 | 1061 |
doc := doc asString withColor:(UserPreferences current commentColor). |
3319 | 1062 |
explanation := explanation,'\\' withCRs,val name,'''s documentation:\'withCRs, doc |
1063 |
]. |
|
1064 |
^ explanation. |
|
1065 |
]. |
|
1245 | 1066 |
|
3319 | 1067 |
explanation := explanation , ' |
1183 | 1068 |
|
3878 | 1069 |
' , varName , ' is bound to the class ' , val name , |
3972 | 1070 |
' in the "' , val category , '" category.'. |
3319 | 1071 |
^ explanation. |
141 | 1072 |
]. |
1073 |
||
2427 | 1074 |
valText := self valueStringFor:val. |
1075 |
||
1245 | 1076 |
shortText ifTrue:[ |
3881 | 1077 |
^ '%1: a global (%2)' |
1078 |
bindWith:varName |
|
1079 |
with:(self asLink:valText to:(self actionToBrowseClass:val class selector:nil)) |
|
1245 | 1080 |
]. |
3881 | 1081 |
|
3319 | 1082 |
explanation := explanation , ' |
1245 | 1083 |
|
2466
75b29e4a34c0
changed: #explainGlobalOrPoolVariable:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2427
diff
changeset
|
1084 |
Its current value is "%1".' bindWith:valText. |
3319 | 1085 |
^ explanation. |
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
1086 |
|
2427 | 1087 |
"Created: / 14-10-2010 / 11:33:04 / cg" |
4040 | 1088 |
"Modified: / 16-11-2016 / 13:08:07 / cg" |
1188 | 1089 |
! |
1090 |
||
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1091 |
explainHereIn:aClass short:shortText |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1092 |
"return an explanation" |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1093 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1094 |
|className| |
1188 | 1095 |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1096 |
className := aClass name. |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1097 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1098 |
shortText ifTrue:[ |
3810 | 1099 |
^ '"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
|
1100 |
]. |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1101 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1102 |
^ '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
|
1103 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1104 |
However, when sending a message to "here", the search for methods |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1105 |
implementing this message will start in the defining class (' , className , '), |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1106 |
instead of the receiver''s class (' , className , ' or subclass). |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1107 |
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
|
1108 |
that the local method is called.' |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1109 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1110 |
"Created: / 28-02-2012 / 10:44:55 / cg" |
1188 | 1111 |
! |
1112 |
||
3798 | 1113 |
explainInstanceVariable:instVarName inClass:aClass short:shortText |
3853 | 1114 |
|varNameInText classNameInText template stringText setOfTypes typesDescription| |
3798 | 1115 |
|
3853 | 1116 |
varNameInText := instVarName allBold. |
1117 |
classNameInText := aClass name. |
|
1118 |
||
3798 | 1119 |
shortText ifTrue:[ |
3883 | 1120 |
template := '%1: instVar in %2'. |
3878 | 1121 |
varNameInText := self |
1122 |
asLink:varNameInText |
|
1123 |
info:('Click to browse references to %1' bindWith:instVarName) |
|
1124 |
to:(self actionToBrowseInstvarRefsTo:instVarName inClass:aClass). |
|
3866 | 1125 |
classNameInText := self asClassLink:classNameInText. |
3798 | 1126 |
] ifFalse:[ |
1127 |
template := '%1: an instance variable in %2' |
|
1128 |
]. |
|
3853 | 1129 |
stringText := template bindWith:varNameInText with:classNameInText. |
3798 | 1130 |
|
1131 |
"/ look for instances |
|
1132 |
setOfTypes := IdentitySet new. |
|
1133 |
self addTypesOfInstvar:instVarName inClass:aClass fromExistingInstancesTo:setOfTypes. |
|
1134 |
"/ look for assignments |
|
3878 | 1135 |
aClass withAllSubclassesDo:[:cls | |
1136 |
self addTypesAssignedToInstvar:instVarName inClass:cls to:setOfTypes |
|
1137 |
]. |
|
1138 |
||
1139 |
"/ generate a list of up-to 3 links |
|
3810 | 1140 |
typesDescription := self typeDescriptionFor:setOfTypes andSelector:nil. |
1141 |
typesDescription notNil ifTrue:[ |
|
1142 |
stringText := stringText,' (',typesDescription,')' |
|
1143 |
]. |
|
3798 | 1144 |
^ stringText |
1145 |
! |
|
1146 |
||
1188 | 1147 |
explainKnownSymbol:string inClass:aClass |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1148 |
"return an explanation or nil" |
1188 | 1149 |
|
1150 |
^ self explainKnownSymbol:string inClass:aClass short:false |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1151 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1152 |
"Modified (comment): / 28-02-2012 / 10:45:40 / cg" |
1188 | 1153 |
! |
1154 |
||
1155 |
explainKnownSymbol:string inClass:aClass short:shortText |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1156 |
"return an explanation or nil" |
1188 | 1157 |
|
2471 | 1158 |
|sym expl| |
1188 | 1159 |
|
1160 |
sym := string asSymbolIfInterned. |
|
1161 |
sym isNil ifTrue:[^ nil]. |
|
1162 |
||
2427 | 1163 |
"try globals and pools" |
1188 | 1164 |
(Smalltalk includesKey:sym) ifTrue:[ |
3810 | 1165 |
expl := self explainGlobalOrPoolVariable:string inClass:aClass short:shortText. |
1166 |
expl notNil ifTrue:[^ expl]. |
|
1188 | 1167 |
]. |
1168 |
||
2471 | 1169 |
expl := self explainSelector:string inClass:aClass short:shortText. |
1170 |
expl notNil ifTrue:[^ expl]. |
|
1171 |
^ self explainGlobalOrPoolVariable:string inClass:aClass short:shortText. |
|
2427 | 1172 |
|
2471 | 1173 |
"Modified: / 18-01-2011 / 19:13:07 / cg" |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1174 |
"Modified (comment): / 28-02-2012 / 10:46:06 / cg" |
1188 | 1175 |
! |
1176 |
||
1177 |
explainPseudoVariable:string in:aClass |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1178 |
"return an explanation for the pseudoVariables self, super etc." |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1179 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1180 |
<resource: #obsolete> |
1188 | 1181 |
|
1182 |
^ self explainPseudoVariable:string in:aClass short:false |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1183 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1184 |
"Modified (format): / 28-02-2012 / 10:47:17 / cg" |
1188 | 1185 |
! |
1186 |
||
1187 |
explainPseudoVariable:string in:aClass short:shortText |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1188 |
"return an explanation for the pseudoVariables self, super etc." |
1188 | 1189 |
|
1190 |
(string = 'self') ifTrue:[ |
|
3810 | 1191 |
^ self explainSelfIn:aClass short:shortText |
1188 | 1192 |
]. |
1193 |
||
1194 |
(string = 'super') ifTrue:[ |
|
3810 | 1195 |
^ self explainSuperIn:aClass short:shortText |
1188 | 1196 |
]. |
1197 |
||
1198 |
(string = 'here') ifTrue:[ |
|
3810 | 1199 |
^ self explainHereIn:aClass short:shortText |
1188 | 1200 |
]. |
1201 |
||
1202 |
(string = 'thisContext') ifTrue:[ |
|
3810 | 1203 |
shortText ifTrue:[ |
1204 |
^ '''thisContext'' - the current stack frame as an object.' |
|
1205 |
]. |
|
1206 |
^ 'thisContext is a pseudo variable (i.e. it is built in). |
|
1188 | 1207 |
|
1208 |
ThisContext always refers to the context object for the currently executed method or |
|
1209 |
block (an instance of Context or BlockContext respectively). The calling chain and calling |
|
1210 |
receivers/selectors can be accessed via thisContext.' |
|
1211 |
]. |
|
1212 |
||
1213 |
(string = 'true') ifTrue:[ |
|
3810 | 1214 |
shortText ifTrue:[ |
1215 |
^ '''true'' - the truth and nothing but the truth.' |
|
1216 |
]. |
|
1217 |
^ 'true is a pseudo variable (i.e. it is built in). |
|
1188 | 1218 |
|
1219 |
True represents logical truth. It is the one and only instance of class True.' |
|
1220 |
]. |
|
1221 |
||
1222 |
(string = 'false') ifTrue:[ |
|
3810 | 1223 |
shortText ifTrue:[ |
1224 |
^ '''false'' - obvisously not true.' |
|
1225 |
]. |
|
1226 |
^ 'false is a pseudo variable (i.e. it is built in). |
|
1188 | 1227 |
|
1228 |
False represents logical falseness. It is the one and only instance of class False.' |
|
1229 |
]. |
|
1230 |
||
1231 |
(string = 'nil') ifTrue:[ |
|
3810 | 1232 |
shortText ifTrue:[ |
1233 |
^ '''nil'' - undefined, unknown, void or dont care.' |
|
1234 |
]. |
|
1235 |
^ 'nil is a pseudo variable (i.e. it is built in). |
|
1188 | 1236 |
|
1237 |
Nil is used for unitialized variables (among other uses). |
|
1238 |
Nil is the one and only instance of class UndefinedObject.' |
|
1239 |
]. |
|
1240 |
^ nil |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1241 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1242 |
"Modified (comment): / 28-02-2012 / 10:46:18 / cg" |
1188 | 1243 |
! |
1244 |
||
1245 |
explainSelector:string inClass:aClass short:shortText |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1246 |
"return an explanation or nil" |
1188 | 1247 |
|
3810 | 1248 |
|selector listOfImplementingClasses listOfImplementingClassNames listOfSimilarSelectors |
3203 | 1249 |
firstImplementingClassOfSimilar count tmp commonSuperClass s s2 |
3798 | 1250 |
firstImplementingClass |
1251 |
firstImplementingClassName secondImplementingClassName thirdImplementingClassName |
|
1252 |
classProvidingComment |
|
1253 |
cm msg t check| |
|
1188 | 1254 |
|
3810 | 1255 |
selector := string asSymbolIfInterned. |
1256 |
selector isNil ifTrue:[^ nil]. |
|
1188 | 1257 |
|
3810 | 1258 |
"/ look who implements it |
3798 | 1259 |
listOfImplementingClassNames := Set new. |
3174 | 1260 |
listOfImplementingClasses := Set new. |
1261 |
listOfSimilarSelectors := Set new. |
|
2288
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1262 |
|
3203 | 1263 |
check := |
3233 | 1264 |
[:sel :mthd :cls | |
3810 | 1265 |
sel == selector ifTrue:[ |
3798 | 1266 |
listOfImplementingClasses add:cls. |
1267 |
listOfImplementingClassNames add:(cls name). |
|
3233 | 1268 |
firstImplementingClass isNil ifTrue:[ |
3798 | 1269 |
firstImplementingClass := cls. |
1270 |
firstImplementingClassName := cls name. |
|
3233 | 1271 |
] |
1272 |
] ifFalse:[ |
|
3810 | 1273 |
(sel startsWith:selector) ifTrue:[ |
3233 | 1274 |
listOfSimilarSelectors add:sel. |
1275 |
firstImplementingClassOfSimilar isNil ifTrue:[ |
|
1276 |
firstImplementingClassOfSimilar := cls |
|
1277 |
] |
|
1278 |
] |
|
1279 |
] |
|
1280 |
]. |
|
2288
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1281 |
|
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1282 |
Smalltalk allClassesDo:[:c| |
3233 | 1283 |
c methodDictionary keysAndValuesDo:[:sel :mthd | |
1284 |
check value:sel value:mthd value:c |
|
1285 |
]. |
|
1286 |
c class methodDictionary keysAndValuesDo:[:sel :mthd | |
|
1287 |
check value:sel value:mthd value:c class |
|
1288 |
]. |
|
141 | 1289 |
]. |
1290 |
||
3810 | 1291 |
(aClass canUnderstand:selector) ifTrue:[ |
1292 |
s2 := ('Instances of ''' , aClass name , ''' respond to #') , selector "allBold" , '.'. |
|
3233 | 1293 |
shortText ifFalse:[ |
1294 |
s2 := '\\' , s2 |
|
1295 |
, '\- inherited from ' withCRs |
|
3810 | 1296 |
, (aClass whichClassIncludesSelector:selector) name "allBold". |
3233 | 1297 |
]. |
3810 | 1298 |
firstImplementingClass := (aClass whichClassIncludesSelector:selector) |
835 | 1299 |
] ifFalse:[ |
3233 | 1300 |
s2 := ''. |
835 | 1301 |
]. |
1302 |
||
3798 | 1303 |
count := listOfImplementingClassNames size. |
141 | 1304 |
(count ~~ 0) ifTrue:[ |
3233 | 1305 |
" |
1306 |
for up-to 4 implementing classes, |
|
1307 |
list them |
|
1308 |
" |
|
3798 | 1309 |
listOfImplementingClassNames := listOfImplementingClassNames asOrderedCollection sort. |
3233 | 1310 |
shortText ifTrue:[ |
1311 |
tmp := ' is implemented in '. |
|
1312 |
] ifFalse:[ |
|
1313 |
tmp := ' is a selector implemented in '. |
|
1314 |
]. |
|
3881 | 1315 |
"/ s := string allBold. |
3855 | 1316 |
count > 1 ifTrue:[ |
3881 | 1317 |
s := self asLink:selector to:(self actionToOpenMethodFinderFor:selector). |
3842 | 1318 |
] ifFalse:[ |
3885 | 1319 |
s := self asLink:selector to:(self actionToBrowseClass:listOfImplementingClasses first selector:selector). |
3842 | 1320 |
]. |
3810 | 1321 |
|
1322 |
shortText ifTrue:[ |
|
1323 |
|typesDescription| |
|
1324 |
||
1325 |
msg := s , tmp. |
|
3842 | 1326 |
typesDescription := self typeDescriptionFor:listOfImplementingClasses andSelector:selector wordBetween:'and'. |
3810 | 1327 |
typesDescription notNil ifTrue:[ |
3842 | 1328 |
msg := msg,typesDescription |
3233 | 1329 |
]. |
1330 |
] ifFalse:[ |
|
3810 | 1331 |
(count == 1) ifTrue:[ |
1332 |
t := firstImplementingClassName. |
|
1333 |
firstImplementingClass isMeta ifTrue:[ |
|
1334 |
t := 'the ' , t |
|
1335 |
]. |
|
1336 |
msg := s , tmp , t , '.'. |
|
3233 | 1337 |
shortText ifFalse:[ |
1338 |
msg := msg , s2. |
|
3810 | 1339 |
] |
3233 | 1340 |
] ifFalse:[ |
3810 | 1341 |
firstImplementingClassName := listOfImplementingClassNames at:1. |
1342 |
secondImplementingClassName := listOfImplementingClassNames at:2. |
|
1343 |
(count == 2) ifTrue:[ |
|
1344 |
msg := s,tmp,firstImplementingClassName,' and ',secondImplementingClassName,'.'. |
|
3233 | 1345 |
shortText ifFalse:[ |
1346 |
msg := msg , s2. |
|
1347 |
]. |
|
1348 |
] ifFalse:[ |
|
3810 | 1349 |
thirdImplementingClassName := listOfImplementingClassNames at:3. |
1350 |
(count == 3) ifTrue:[ |
|
1351 |
msg := s,tmp,firstImplementingClassName,',',secondImplementingClassName,' and ',thirdImplementingClassName,'.'. |
|
1352 |
shortText ifFalse:[ |
|
1353 |
msg := msg , s2. |
|
1354 |
]. |
|
1355 |
] ifFalse:[ |
|
1356 |
false "shortText" ifTrue:[ |
|
1357 |
msg := s , tmp , count printString , ' classes'. |
|
1358 |
commonSuperClass := Class commonSuperclassOf:listOfImplementingClassNames. |
|
1359 |
commonSuperClass notNil ifTrue:[ |
|
1360 |
(commonSuperClass == Object |
|
1361 |
and:[commonSuperClass includesSelector:selector]) ifTrue:[ |
|
1362 |
msg := msg , ' (including ' , 'Object' "allBold", ')' |
|
1363 |
] ifFalse:[ |
|
1364 |
(commonSuperClass ~= Object) ifTrue:[ |
|
1365 |
msg := msg , ' (under ' , commonSuperClass name, ')' |
|
1366 |
] |
|
1367 |
]. |
|
3492 | 1368 |
]. |
3810 | 1369 |
msg := msg , '.'. |
1370 |
^ msg |
|
3233 | 1371 |
]. |
1183 | 1372 |
|
3810 | 1373 |
" |
1374 |
if there are more, look for a common |
|
1375 |
superclass and show it ... |
|
1376 |
" |
|
1377 |
commonSuperClass := (Behavior commonSuperclassOf:listOfImplementingClasses) ? Object. |
|
1378 |
(commonSuperClass ~~ Object |
|
1379 |
and:[commonSuperClass ~~ Behavior |
|
1380 |
and:[commonSuperClass ~~ Class |
|
1381 |
and:[commonSuperClass ~~ ClassDescription]]]) ifTrue:[ |
|
1382 |
(listOfImplementingClasses includes:commonSuperClass) ifTrue:[ |
|
1383 |
msg := s . tmp , count printString , commonSuperClass name |
|
1384 |
, ' and redefined in ' , (count - 1) printString |
|
1385 |
, ' subclasses' |
|
1386 |
, s2. |
|
1387 |
firstImplementingClass := commonSuperClass |
|
1388 |
] ifFalse:[ |
|
1389 |
msg := s , tmp, count printString , ' subclasses of ' , commonSuperClass name , s2 |
|
1390 |
] |
|
3233 | 1391 |
] ifFalse:[ |
3810 | 1392 |
(commonSuperClass == Object |
1393 |
and:[commonSuperClass includesSelector:selector]) ifTrue:[ |
|
1394 |
shortText ifTrue:[ |
|
1395 |
msg := s , tmp , count printString , ' classes (incl. Object)'. |
|
1396 |
] ifFalse:[ |
|
1397 |
msg := s , tmp , count printString , ' classes. |
|
3804 | 1398 |
|
3203 | 1399 |
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
|
1400 |
since there is an implementation in Object.' , s2. |
3810 | 1401 |
]. |
1402 |
firstImplementingClass := Object |
|
1403 |
] ifFalse:[ |
|
1404 |
((commonSuperClass == Behavior |
|
1405 |
or:[commonSuperClass == Class |
|
1406 |
or:[commonSuperClass == ClassDescription]]) |
|
1407 |
and:[commonSuperClass includesSelector:selector]) ifTrue:[ |
|
1408 |
shortText ifTrue:[ |
|
1409 |
msg := s , tmp , count printString , ' classes (incl. all classes)'. |
|
1410 |
] ifFalse:[ |
|
1411 |
msg := s , tmp , count printString , ' classes. |
|
913 | 1412 |
|
3203 | 1413 |
All classes seem to respond to that message, |
913 | 1414 |
since there is an implementation in ' , commonSuperClass name , '.' , s2. |
3810 | 1415 |
]. |
1416 |
firstImplementingClass := commonSuperClass |
|
1417 |
] ifFalse:[ |
|
1418 |
" |
|
1419 |
otherwise just give the number. |
|
1420 |
" |
|
1421 |
msg := s , tmp , count printString , ' classes.' , s2 |
|
1422 |
] |
|
3233 | 1423 |
] |
1424 |
] |
|
1425 |
] |
|
3810 | 1426 |
]. |
3233 | 1427 |
]. |
1428 |
]. |
|
3798 | 1429 |
"/ look for a comment... |
1430 |
count == 1 ifTrue:[ |
|
1431 |
classProvidingComment := firstImplementingClass. |
|
3233 | 1432 |
] ifFalse:[ |
3798 | 1433 |
commonSuperClass isNil ifTrue:[ |
1434 |
commonSuperClass := Behavior commonSuperclassOf:listOfImplementingClasses. |
|
1435 |
] . |
|
1436 |
(listOfImplementingClasses includes:commonSuperClass) ifTrue:[ |
|
3810 | 1437 |
(commonSuperClass implements:selector) ifTrue:[ |
1438 |
classProvidingComment := commonSuperClass |
|
1439 |
] ifFalse:[ |
|
1440 |
classProvidingComment := firstImplementingClass. |
|
1441 |
]. |
|
3798 | 1442 |
]. |
1443 |
]. |
|
1444 |
classProvidingComment notNil ifTrue:[ |
|
3885 | 1445 |
cm := self fetchDescriptionOfMethod:"fetchCommentOfMethod:"(classProvidingComment compiledMethodAt:selector). |
3798 | 1446 |
]. |
1447 |
cm isNil ifTrue:[ |
|
1448 |
"/ should: find the class with most subclasses from the list |
|
1449 |
(listOfImplementingClasses includes:Collection) ifTrue:[ |
|
1450 |
classProvidingComment := Collection |
|
1451 |
] ifFalse:[ |
|
1452 |
(listOfImplementingClasses includes:Stream) ifTrue:[ |
|
1453 |
classProvidingComment := Stream |
|
1454 |
] ifFalse:[ |
|
3883 | 1455 |
classProvidingComment := listOfImplementingClasses |
1456 |
detect:[:cls | |
|
1457 |
|mthd| |
|
1458 |
(mthd := cls compiledMethodAt:selector) notNil |
|
3885 | 1459 |
and:[ (self fetchCommentOfMethod:mthd) notNil]] |
3883 | 1460 |
ifNone:nil. |
3798 | 1461 |
] |
1462 |
]. |
|
3883 | 1463 |
cm isNil ifTrue:[ |
1464 |
classProvidingComment notNil ifTrue:[ |
|
3885 | 1465 |
cm := self fetchDescriptionOfMethod:(classProvidingComment compiledMethodAt:selector). |
3883 | 1466 |
] |
1467 |
]. |
|
1468 |
cm notNil ifTrue:[ |
|
3885 | 1469 |
cm := (' %1 says:\' withCRs bindWith:(self asClassLink:classProvidingComment name)),cm |
3798 | 1470 |
]. |
1471 |
]. |
|
1472 |
cm notNil ifTrue:[ |
|
3883 | 1473 |
"/ msg := msg,(msg last isSeparator ifTrue:[''] ifFalse:[' ']),cm |
1474 |
msg := msg,(Character cr),cm |
|
3233 | 1475 |
]. |
1476 |
^ msg |
|
141 | 1477 |
]. |
1478 |
||
3881 | 1479 |
"/ none implements it (typo?); |
3174 | 1480 |
count := listOfSimilarSelectors size. |
2288
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1481 |
(count ~~ 0) ifTrue:[ |
3233 | 1482 |
listOfSimilarSelectors := listOfSimilarSelectors asOrderedCollection sort. |
2288
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1483 |
|
3233 | 1484 |
(count == 1) ifTrue:[ |
3881 | 1485 |
|sel selLink clsLink implementors| |
1486 |
||
1487 |
sel := listOfSimilarSelectors first. |
|
1488 |
selLink := self asLink:sel to:(self actionToOpenMethodFinderFor:sel). |
|
1489 |
"/ clsLink := self asLink:firstImplementingClassOfSimilar name to:(self actionToBrowseClass:firstImplementingClassOfSimilar selector:sel). |
|
1490 |
implementors := Smalltalk allImplementorsOf:sel. |
|
1491 |
clsLink := self typeDescriptionFor:implementors andSelector:sel. |
|
1492 |
msg := selLink , ' is implemented in ' , clsLink , '.'. |
|
3233 | 1493 |
shortText ifFalse:[ |
1494 |
msg := msg , s2. |
|
1495 |
] |
|
1496 |
] ifFalse:[ |
|
3881 | 1497 |
|sel1 sel2| |
1498 |
sel1 := listOfSimilarSelectors first. |
|
1499 |
sel2 := listOfSimilarSelectors second. |
|
1500 |
msg := 'similar selectors: %1, %2' |
|
1501 |
bindWith:(self asLink:sel1 to:(self actionToOpenMethodFinderFor:sel1)) |
|
1502 |
with:(self asLink:sel2 to:(self actionToOpenMethodFinderFor:sel1)). |
|
3233 | 1503 |
count > 2 ifTrue:[ |
1504 |
msg := msg,'...'. |
|
1505 |
]. |
|
1506 |
]. |
|
1507 |
^ msg |
|
2288
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1508 |
]. |
92c7e16d6916
changed: #explainSelector:inClass:short:
Claus Gittinger <cg@exept.de>
parents:
2287
diff
changeset
|
1509 |
|
141 | 1510 |
^ nil |
290
d1650b17b9e9
Change the name of a classes Metaclass from e.g. "SmallIntegerclass" to
Stefan Vogel <sv@exept.de>
parents:
263
diff
changeset
|
1511 |
|
1909 | 1512 |
"Modified: / 17-06-1996 / 17:09:30 / stefan" |
1513 |
"Created: / 23-03-1999 / 13:29:33 / cg" |
|
3804 | 1514 |
"Modified: / 01-05-2016 / 15:42:24 / cg" |
141 | 1515 |
! |
1516 |
||
1183 | 1517 |
explainSelfIn:aClass short:shortText |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1518 |
"return an explanation" |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1519 |
|
3883 | 1520 |
|subClasses subNames selfString className nSubClasses |
1521 |
classLink classLink2 subclassesLink| |
|
1198 | 1522 |
|
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
1523 |
selfString := '''' , 'self' "allBold" , ''''. |
6 | 1524 |
|
3883 | 1525 |
subClasses := aClass allSubclasses. |
1526 |
nSubClasses := subClasses size. |
|
1244 | 1527 |
|
1528 |
aClass isMeta ifTrue:[ |
|
3810 | 1529 |
className := aClass theNonMetaclass name. |
3883 | 1530 |
subNames := subClasses collect:[:c | c theNonMetaclass name]. |
1531 |
shortText ifTrue:[ |
|
1532 |
classLink := (self asClassLink:className). |
|
1533 |
nSubClasses == 0 ifTrue:[ |
|
1534 |
^ selfString , (' - the %1 class.' bindWith:classLink) |
|
1535 |
]. |
|
1536 |
nSubClasses == 1 ifTrue:[ |
|
1537 |
classLink2 := self asClassLink:subNames first. |
|
1538 |
^ selfString , (' - the %1- or %2 class.' bindWith:classLink with:classLink2) |
|
1539 |
]. |
|
1540 |
subclassesLink := self |
|
1541 |
asLink:('%1 subclasses' bindWith:nSubClasses) |
|
1542 |
info:('Click to browse subclasses') |
|
1543 |
to:(self actionToBrowseClasses:subClasses). |
|
1544 |
||
1545 |
^ selfString , (' - the %1 class or one of its %2.' bindWith:classLink with:subclassesLink) |
|
1546 |
]. |
|
1547 |
||
3810 | 1548 |
nSubClasses == 0 ifTrue:[ |
1549 |
^ selfString , 'refers to the object which received the message. |
|
1244 | 1550 |
|
1551 |
In this case, it will be the ' , className , '-class itself.' |
|
3810 | 1552 |
]. |
1244 | 1553 |
|
3810 | 1554 |
nSubClasses <= 5 ifTrue:[ |
1555 |
^ selfString , ' refers to the object which received the message. |
|
1244 | 1556 |
|
1557 |
In this case, it will be the ' , className , '-class |
|
1558 |
or one of its subclasses: |
|
1559 |
||
1560 |
' , subNames asStringCollection asString |
|
3810 | 1561 |
]. |
1244 | 1562 |
|
3810 | 1563 |
^ selfString , ' refers to the object which received the message. |
1244 | 1564 |
|
1565 |
In this case, it will be the ' , className , '-class |
|
1566 |
or one of its ' , nSubClasses printString , ' subclasses.' |
|
1567 |
]. |
|
1568 |
||
3883 | 1569 |
subClasses := aClass allSubclasses. |
1570 |
subNames := subClasses collect:[:c | c theNonMetaclass name]. |
|
1244 | 1571 |
className := aClass name. |
3883 | 1572 |
shortText ifTrue:[ |
1573 |
classLink := self asClassLink:className. |
|
1574 |
nSubClasses == 0 ifTrue:[ |
|
1575 |
^ selfString , (' - an instance of %1.' bindWith:classLink) |
|
1576 |
]. |
|
1577 |
nSubClasses == 1 ifTrue:[ |
|
1578 |
classLink2 := self asClassLink:subNames first. |
|
1579 |
^ selfString , (' - an instance of %1 or %2.' bindWith:classLink with:classLink2) |
|
1580 |
]. |
|
1581 |
subclassesLink := self |
|
1582 |
asLink:('%1 subclasses' bindWith:nSubClasses) |
|
1583 |
info:('Click to browse subclasses') |
|
1584 |
to:(self actionToBrowseClasses:subClasses). |
|
1585 |
^ selfString , (' - an instance of %1 or one of its %2.' bindWith:classLink with:subclassesLink) |
|
1586 |
]. |
|
1587 |
||
1244 | 1588 |
nSubClasses == 0 ifTrue:[ |
3810 | 1589 |
^ selfString , 'refers to the object which received the message. |
6 | 1590 |
|
1244 | 1591 |
In this case, it will be an instance of ' , className , '.' |
6 | 1592 |
]. |
1593 |
||
1244 | 1594 |
nSubClasses <= 5 ifTrue:[ |
3810 | 1595 |
^ selfString , ' refers to the object which received the message. |
6 | 1596 |
|
1244 | 1597 |
In this case, it will be an instance of ' , className , ' |
6 | 1598 |
or one of its subclasses: |
1599 |
||
1256 | 1600 |
' , subNames asStringCollection asString |
6 | 1601 |
]. |
1602 |
||
1198 | 1603 |
^ selfString , ' refers to the object which received the message. |
6 | 1604 |
|
1244 | 1605 |
In this case, it will be an instance of ' , className , ' |
1606 |
or one of its ' , nSubClasses printString , ' subclasses.' |
|
1926
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
1607 |
|
935dbaeca76f
show class package in explanation
Claus Gittinger <cg@exept.de>
parents:
1910
diff
changeset
|
1608 |
"Modified: / 09-10-2006 / 12:11:44 / cg" |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1609 |
"Modified (comment): / 28-02-2012 / 10:47:06 / cg" |
6 | 1610 |
! |
1611 |
||
1188 | 1612 |
explainSuperIn:aClass short:shortText |
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1613 |
"return an explanation" |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1614 |
|
1352 | 1615 |
|superName| |
1244 | 1616 |
|
1617 |
superName := aClass superclass name. |
|
1188 | 1618 |
|
1619 |
shortText ifTrue:[ |
|
3842 | 1620 |
^ '"super" - message lookup starts in superclass "%1" (call redefined method).' bindWith:(self asClassLink:superName) |
1188 | 1621 |
]. |
1622 |
||
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1623 |
^ 'like "self", "','super'allBold,'" refers to the object which received the message. |
6 | 1624 |
|
2828
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1625 |
However, when sending a message to "super", the search for methods |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1626 |
implementing this message will start in the superclass (' , superName , '), |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1627 |
instead of the receiver''s class (' , aClass name , ' or subclass). |
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1628 |
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
|
1629 |
|
aa93cebd33e4
added: #explainHereIn:short:
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
1630 |
"Modified (comment): / 28-02-2012 / 10:47:03 / cg" |
6 | 1631 |
! |
2 | 1632 |
|
49 | 1633 |
explainSyntax:string |
1634 |
"try syntax ...; return explanation or nil" |
|
1635 |
||
1183 | 1636 |
^ self explainSyntax:string short:false |
1637 |
! |
|
1638 |
||
1639 |
explainSyntax:string short:shortText |
|
3842 | 1640 |
"try syntax ...; return explanation or nil. |
1641 |
This is meant for beginners..." |
|
1183 | 1642 |
|
3883 | 1643 |
|fullMsg stringWithoutSeparators| |
3881 | 1644 |
|
49 | 1645 |
((string = ':=') or:[string = '_']) ifTrue:[ |
3881 | 1646 |
fullMsg := '<variable> := <expression> |
49 | 1647 |
|
3233 | 1648 |
":=" and "_" (which is left-arrow in some fonts) mean assignment. |
3464 | 1649 |
The variable is bound to (i.e. points to) the value of <expression>. |
3881 | 1650 |
The "_" form is historic and should not be used with new code.'. |
1651 |
||
1652 |
shortText ifTrue:[ |
|
1653 |
string = '_' ifTrue:[ |
|
1654 |
^ (self |
|
1655 |
asLink:'"_"' |
|
1656 |
info:fullMsg |
|
1657 |
to:nil),' - old style for assignment. Consider changing to ":=".' |
|
1658 |
]. |
|
1659 |
^ (self |
|
1660 |
asLink:'":="' |
|
1661 |
info:fullMsg |
|
1662 |
to:nil),' - assign to variable on the left (syntax).' |
|
1663 |
]. |
|
1664 |
^ fullMsg |
|
49 | 1665 |
]. |
1666 |
||
1667 |
(string = '^') ifTrue:[ |
|
3233 | 1668 |
shortText ifTrue:[ |
3607 | 1669 |
^ '"^" - return value from method (syntax)'. |
3233 | 1670 |
]. |
1671 |
^ '^ <expression> |
|
49 | 1672 |
|
3319 | 1673 |
A return statement evaluates <expression> and returns its value from the method. |
1674 |
A return inside a block exits the method where the block is defined (not only the block).' |
|
49 | 1675 |
]. |
1676 |
||
1677 |
(string = ';') ifTrue:[ |
|
3233 | 1678 |
shortText ifTrue:[ |
3607 | 1679 |
^ '";" - a cascade expression (syntax)'. |
3233 | 1680 |
]. |
1681 |
^ '<expression> ; selector1 ; .... ; selectorN |
|
49 | 1682 |
|
3203 | 1683 |
a cascade expression; evaluate expression, and send messages |
1684 |
<selector1> ... <selectorN> to the first expression''s receiver. |
|
49 | 1685 |
Returns the value of the last send. The cascade sends may also have arguments.' |
1686 |
]. |
|
1687 |
||
1688 |
(string = '|') ifTrue:[ |
|
3233 | 1689 |
shortText ifTrue:[ |
3607 | 1690 |
^ '"|" - local variables (syntax)'. |
3233 | 1691 |
]. |
1692 |
^ '| locals | or: [:arg | statements] |
|
49 | 1693 |
|
3233 | 1694 |
"|" is used to mark a local variable declaration or separates arguments |
49 | 1695 |
from the statements in a block. Notice, that in a block-argument declaration |
3319 | 1696 |
these arg nams must be prefixed by a colon character. |
3233 | 1697 |
"|" is also a selector understood by Booleans.' |
49 | 1698 |
]. |
1699 |
||
3319 | 1700 |
(string startsWith:'$' ) ifTrue:[ |
1701 |
shortText ifTrue:[ |
|
3842 | 1702 |
^ '"$x" - ',(self asClassLink:'Character'),' literal (syntax)'. |
3319 | 1703 |
]. |
1704 |
^ 'is a Character literal constant. |
|
1705 |
||
1706 |
Character objects represent indivdual text cheracters in Unicode encoding. |
|
1707 |
For example, $a is the character "a" with an encoding of 97 "hex: 16r61". |
|
1708 |
Smalltalk/X supports unicode and uses this encoding internally for all characters |
|
1709 |
and strings. Notice, that not all Smalltalk implementations allow non-ascii (i.e. codes > 255).' |
|
1710 |
]. |
|
1711 |
||
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1712 |
(string startsWith:'#' ) ifTrue:[ |
3233 | 1713 |
(string startsWith:'#(' ) ifTrue:[ |
1714 |
shortText ifTrue:[ |
|
3842 | 1715 |
^ '"#(..)" - ',(self asClassLink:'Array'),' literal (syntax)'. |
3233 | 1716 |
]. |
3607 | 1717 |
^ 'is a constant Array (literal). |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1718 |
|
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1719 |
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
|
1720 |
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
|
1721 |
The elements of a constant Array must be numbers, strings, symbols, nil, true or false, |
3194 | 1722 |
other array constants or byte-arrays. |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1723 |
(notice, that not all Smalltalk implementations allow true, false and nil as |
3194 | 1724 |
element in an Array-constant).' |
3233 | 1725 |
]. |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1726 |
|
3233 | 1727 |
(string startsWith:'#[') ifTrue:[ |
1728 |
shortText ifTrue:[ |
|
3842 | 1729 |
^ '"#[..]" - ',(self asClassLink:'ByteArray'),' literal (syntax)'. |
3233 | 1730 |
]. |
3607 | 1731 |
^ 'is a constant ByteArray (literal). |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1732 |
|
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1733 |
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
|
1734 |
0 .. 255. |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1735 |
(notice, that not all Smalltalk implementations support constant ByteArrays).' |
3233 | 1736 |
]. |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1737 |
|
3233 | 1738 |
(string startsWith:'#''') ifTrue:[ |
1739 |
shortText ifTrue:[ |
|
3842 | 1740 |
^ '"#''..''" - ',(self asClassLink:'Symbol'),' literal (syntax)'. |
3233 | 1741 |
]. |
1742 |
^ 'is a constant symbol containing non-alphanumeric characters. |
|
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1743 |
|
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1744 |
Symbols are unique strings, meaning that there exists |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1745 |
exactly one instance of a given symbol. Therefore symbols can |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1746 |
be compared using == (identity compare) in addition to = (contents compare). |
3194 | 1747 |
Beside this, Symbols behave mostly like Strings but are immutable.' |
3233 | 1748 |
]. |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1749 |
|
3233 | 1750 |
shortText ifTrue:[ |
3842 | 1751 |
^ '"#.." - ',(self asClassLink:'Symbol'),' literal (syntax)'. |
3233 | 1752 |
]. |
3319 | 1753 |
^ 'is a constant symbol. |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1754 |
|
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1755 |
Symbols are unique strings, meaning that there exists |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1756 |
exactly one instance of a given symbol. Therefore symbols can |
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1757 |
be compared using == (identity compare) in addition to = (contents compare). |
3194 | 1758 |
Beside this, Symbols behave mostly like Strings but are immutable.' |
2287
61f5dfccf2c0
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2286
diff
changeset
|
1759 |
]. |
49 | 1760 |
((string startsWith:'(') or:[string endsWith:')']) ifTrue:[ |
3233 | 1761 |
shortText ifTrue:[ |
1762 |
^ ''. |
|
1763 |
]. |
|
1764 |
^ '( <expression> ) |
|
49 | 1765 |
|
3194 | 1766 |
expression grouping. Without them, expressions are evaluated left to right, |
1767 |
with unary messages preceeding binary messages, preceeding keyword mesages.' |
|
49 | 1768 |
]. |
1769 |
||
1770 |
((string startsWith:'[') or:[string endsWith:']']) ifTrue:[ |
|
3233 | 1771 |
shortText ifTrue:[ |
3842 | 1772 |
^ '"[..]" - a ',(self asClassLink:'Block'),' (aka lambda/closure for experts)'. |
3233 | 1773 |
]. |
1774 |
^ '[:arg1 .. :argN | statements] |
|
49 | 1775 |
|
3203 | 1776 |
defines a block. |
3194 | 1777 |
Blocks represent pieces of executable code. The definition of a block does |
3203 | 1778 |
not evaluate it, instead a reference to the blocks computation is created. |
3194 | 1779 |
The block can be evaluated later, by sending it a value/value: message |
1780 |
(it can be even evaluated multiple times). |
|
1781 |
Blocks are often passed as arguments to Booleans (i.e. "bool ifTrue:[...]"), |
|
1782 |
other blocks (i.e. "[...] whileTrue: [...]"]) or collections (i.e. "coll do:[...]"). |
|
1783 |
Blocks are also often used as callbacks from UI components or as exception handlers.' |
|
49 | 1784 |
]. |
1785 |
||
2286
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1786 |
((string startsWith:'{') or:[string endsWith:'}']) ifTrue:[ |
3233 | 1787 |
shortText ifTrue:[ |
3842 | 1788 |
^ '"{..}" ',(self asClassLink:'Array'),' instantiation (syntax)'. |
3233 | 1789 |
]. |
1790 |
^ '{ <expr1>. .. <exprN> } |
|
2286
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1791 |
|
3319 | 1792 |
This is syntactic sugar for "Array with:<expr1> .. with:<exprN>". |
1793 |
||
2286
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1794 |
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
|
1795 |
The expressions are separated by a period statement separator. |
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1796 |
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
|
1797 |
when executed (as opposed to an array literal, which is computed only once at |
3233 | 1798 |
compilation time) and which is shared. |
3319 | 1799 |
Notice that this construct is not supported by all Smalltalk dialects, so its use |
1800 |
makes your program somewhat dialect specific.' |
|
2286
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1801 |
]. |
cabf7db33a38
changed: #explainSyntax:short:
Claus Gittinger <cg@exept.de>
parents:
2226
diff
changeset
|
1802 |
|
49 | 1803 |
(string = ':') ifTrue:[ |
3233 | 1804 |
shortText ifTrue:[ |
1805 |
^ ''. |
|
1806 |
]. |
|
1807 |
^ 'colons have different meaning depending on context: |
|
49 | 1808 |
|
1809 |
1) they separate keyword-parts in symbols and keyword-messages as in: |
|
1810 |
||
1811 |
#at:put: a constant keyword symbol |
|
1812 |
||
1813 |
rec at:index put:value sends the #at:put: message to rec, |
|
3233 | 1814 |
passing index and value as arguments. |
49 | 1815 |
|
1816 |
2) within block-argument declarations as in: |
|
1817 |
||
1818 |
[:arg1 :arg2 | statements] |
|
834 | 1819 |
|
1820 |
3) within an identifier, they separate the nameSpace part from |
|
1821 |
the name part; as in: |
|
1822 |
||
1823 |
Smalltalk::Array - the Array class in the Smalltalk nameSpace. |
|
1824 |
Foo::Array - the Array class in the Foo nameSpace. |
|
49 | 1825 |
' |
1826 |
]. |
|
1827 |
||
1828 |
(string = '.') ifTrue:[ |
|
3233 | 1829 |
^ 'statement. "<- period here" |
49 | 1830 |
statement |
1831 |
||
1832 |
within a method or block, individual statements are separated by periods. |
|
1833 |
' |
|
1834 |
]. |
|
1835 |
||
3883 | 1836 |
stringWithoutSeparators := string withoutSeparators. |
1837 |
||
1838 |
(stringWithoutSeparators startsWith:'"') ifTrue:[ |
|
1839 |
(stringWithoutSeparators startsWith:'"/') ifTrue:[ |
|
3464 | 1840 |
shortText ifTrue:[ |
3607 | 1841 |
^ 'an end-of-line comment (syntax)'. |
3464 | 1842 |
]. |
1843 |
^ '"/ comment |
|
3193 | 1844 |
EOL (end-of-line) comment |
1845 |
||
1846 |
anything up to the end of line is a comment and ignored (but not inside a string). |
|
3233 | 1847 |
Notice that EOL-comments are only supported by Smalltalk/X (i.e. non-portable). |
3193 | 1848 |
' |
3464 | 1849 |
]. |
3883 | 1850 |
(stringWithoutSeparators startsWith:'"<<') ifTrue:[ |
3464 | 1851 |
shortText ifTrue:[ |
3607 | 1852 |
^ 'a token delimited comment (syntax)'. |
3464 | 1853 |
]. |
1854 |
^ '"<<TOKEN |
|
1855 |
... |
|
1856 |
TOKEN |
|
1857 |
||
1858 |
Token delimited comment |
|
1859 |
||
1860 |
anything up to a line which contains the token alone is a comment and ignored (but not inside a string). |
|
1861 |
Notice that Token-comments are only supported by Smalltalk/X (i.e. non-portable). |
|
1862 |
' |
|
1863 |
]. |
|
1864 |
||
3233 | 1865 |
shortText ifTrue:[ |
3607 | 1866 |
^ 'a comment (syntax)'. |
3233 | 1867 |
]. |
1868 |
^ '" comment ... " |
|
3193 | 1869 |
comment |
1870 |
||
1871 |
anything between double quotes is a comment and ignored (but not inside a string). |
|
1872 |
' |
|
1873 |
]. |
|
1874 |
||
49 | 1875 |
"/ is it a symbol without hash-character ? |
1876 |
"/ |
|
796 | 1877 |
"/ string knownAsSymbol ifTrue:[ |
1878 |
"/ ^ 'is nothing, but #' , string , ' is known as a symbol. |
|
1879 |
"/ |
|
1880 |
"/Symbols are unique strings, meaning that there exists |
|
1881 |
"/exactly one instance of a given symbol. Therefore symbols can |
|
1882 |
"/be compared using == (identity compare) in addition to = (contents compare). |
|
1883 |
"/Beside this, Symbols behave mostly like Strings.' |
|
1884 |
"/ ]. |
|
49 | 1885 |
|
3883 | 1886 |
((stringWithoutSeparators startsWith:'<') and:[stringWithoutSeparators endsWith:'>']) ifTrue:[ |
1887 |
shortText ifTrue:[ |
|
1888 |
(stringWithoutSeparators includesString:'resource:') ifTrue:[ |
|
1889 |
^ 'a ',(self |
|
1890 |
asLink:'resource annotation' |
|
1891 |
info:fullMsg |
|
1892 |
to:(self actionToBrowseClass:Annotation)),' (syntax)' |
|
1893 |
]. |
|
1894 |
^ 'an ',(self |
|
1895 |
asLink:'annotation / pragma' |
|
1896 |
info:fullMsg |
|
1897 |
to:(self actionToBrowseClass:Annotation)),' (syntax)' |
|
1898 |
]. |
|
1899 |
]. |
|
1900 |
||
49 | 1901 |
^ nil |
796 | 1902 |
|
3233 | 1903 |
"Modified: / 27-07-2013 / 10:08:57 / cg" |
2214 | 1904 |
! |
1905 |
||
3810 | 1906 |
typeDescriptionFor:setOfTypes andSelector:selectorOrNil |
1907 |
"up to 3 types are shown by name; more are simply counted" |
|
1908 |
||
3842 | 1909 |
^ self typeDescriptionFor:setOfTypes andSelector:selectorOrNil wordBetween:'or' |
1910 |
! |
|
1911 |
||
1912 |
typeDescriptionFor:setOfTypes andSelector:selectorOrNil wordBetween:wordbetween |
|
1913 |
"up to 3 types are shown by name; more are simply counted" |
|
1914 |
||
3810 | 1915 |
|types numTypes |
1916 |
type1 type2 type3 |
|
3855 | 1917 |
nm1 nm2 nm3 link| |
3810 | 1918 |
|
1919 |
"/ reduce... |
|
1920 |
self compressSetOfTypes:setOfTypes. |
|
1921 |
setOfTypes isEmpty ifTrue:[^ nil]. |
|
1922 |
||
1923 |
types := setOfTypes asOrderedCollection. |
|
1924 |
types sortBySelector:#name. |
|
1925 |
||
1926 |
"/ now make this a nice string |
|
1927 |
numTypes := types size. |
|
1928 |
type1 := types first. |
|
3859 | 1929 |
nm1 := self asLink:type1 name to:(self actionToBrowseClass:type1 selector:selectorOrNil). |
3810 | 1930 |
numTypes == 1 ifTrue:[ |
1931 |
^ nm1 |
|
1932 |
]. |
|
3842 | 1933 |
|
3810 | 1934 |
type2 := types second. |
3859 | 1935 |
nm2 := self asLink:type2 name to:(self actionToBrowseClass:type2 selector:selectorOrNil). |
3810 | 1936 |
numTypes == 2 ifTrue:[ |
3842 | 1937 |
^ nm1,' ',wordbetween,' ',nm2 |
3810 | 1938 |
]. |
1939 |
type3 := types third. |
|
3859 | 1940 |
nm3 := self asLink:type3 name to:(self actionToBrowseClass:type3 selector:selectorOrNil). |
3810 | 1941 |
numTypes == 3 ifTrue:[ |
3842 | 1942 |
^ nm1,', ',nm2,' ',wordbetween,' ',nm3 |
3810 | 1943 |
]. |
3855 | 1944 |
link := self actionToBrowseClasses:types. |
1945 |
"/ selectorOrNil notNil ifTrue:[ |
|
1946 |
"/ link := self actionToOpenMethodFinderFor:selectorOrNil. |
|
1947 |
"/ ]. |
|
3859 | 1948 |
^ self asLink:('%1 classes' bindWith:numTypes) to:link. |
3810 | 1949 |
! |
1950 |
||
2214 | 1951 |
valueStringFor:aValue |
1952 |
|valString| |
|
1953 |
||
1954 |
"/ only show the value, if it is really short... |
|
3203 | 1955 |
(aValue isBoolean |
3708 | 1956 |
or:[aValue isNil |
1957 |
or:[aValue isString |
|
1958 |
or:[aValue isNumber |
|
1959 |
or:[aValue isPoint |
|
1960 |
or:[aValue isCharacter |
|
1961 |
]]]]]) ifTrue:[ |
|
1962 |
^ aValue storeString. |
|
2226 | 1963 |
]. |
2427 | 1964 |
|
2226 | 1965 |
(aValue isColor) ifTrue:[ |
3708 | 1966 |
^ 'Color: ',(' ' emphasizeAllWith:(#backgroundColor->aValue)). |
2226 | 1967 |
]. |
1968 |
||
3203 | 1969 |
((aValue isKindOf:GUID) |
1970 |
or:[aValue isUUID]) ifTrue:[ |
|
3708 | 1971 |
valString := aValue printString. |
2427 | 1972 |
]. |
1973 |
||
3203 | 1974 |
(valString isEmptyOrNil |
2899
8b8701a65441
Avoid #isKindOf: where possible
Stefan Vogel <sv@exept.de>
parents:
2828
diff
changeset
|
1975 |
or:[ valString size > 50 |
8b8701a65441
Avoid #isKindOf: where possible
Stefan Vogel <sv@exept.de>
parents:
2828
diff
changeset
|
1976 |
or:[ valString includes:Character cr ]]) ifTrue:[ |
3708 | 1977 |
valString := aValue classNameWithArticle. |
2214 | 1978 |
]. |
1979 |
||
1980 |
^ valString |
|
2427 | 1981 |
|
1982 |
"Modified: / 14-10-2010 / 11:57:52 / cg" |
|
141 | 1983 |
! ! |
49 | 1984 |
|
3798 | 1985 |
!Explainer class methodsFor:'naive type inferer'! |
1986 |
||
1987 |
addTypeOfExpressionNode:expr forAssignmentTo:varName to:setOfTypes |
|
3842 | 1988 |
"pick up low hanging type information. |
1989 |
This is far from being complete, but often gives a hint good enough for code completion |
|
1990 |
and info in the browser." |
|
1991 |
||
1992 |
|val valClass| |
|
3798 | 1993 |
|
1994 |
"/ only look for wellknown types on the right side. |
|
3810 | 1995 |
expr isLiteral ifTrue:[ |
1996 |
val := expr value. |
|
3801 | 1997 |
valClass := val class. |
3798 | 1998 |
val isImmutable ifTrue:[ |
3801 | 1999 |
valClass := [ valClass mutableClass ] on:Error do:[ valClass ]. |
3798 | 2000 |
]. |
2001 |
self rememberType:valClass in:setOfTypes. |
|
3826 | 2002 |
^ setOfTypes. |
3798 | 2003 |
]. |
2004 |
||
2005 |
expr isMessage ifTrue:[ |
|
3842 | 2006 |
self addTypeOfMessageNode:expr forAssignmentTo:varName to:setOfTypes. |
2007 |
^ setOfTypes. |
|
3798 | 2008 |
]. |
3855 | 2009 |
|
3826 | 2010 |
^ setOfTypes |
2011 |
||
3798 | 2012 |
"Created: / 30-04-2016 / 15:28:59 / cg" |
3801 | 2013 |
"Modified: / 30-04-2016 / 20:17:35 / cg" |
3798 | 2014 |
! |
2015 |
||
3842 | 2016 |
addTypeOfMessageNode:expr forAssignmentTo:varName to:setOfTypes |
2017 |
"pick up low hanging type information. |
|
2018 |
This is far from being complete, but often gives a hint good enough for code completion |
|
2019 |
and info in the browser." |
|
2020 |
||
2021 |
|valClass msgSelector msgReceiver| |
|
2022 |
||
2023 |
msgSelector := expr selector. |
|
2024 |
msgReceiver := expr receiver. |
|
2025 |
||
2026 |
msgSelector == #? ifTrue:[ |
|
2027 |
self addTypeOfExpressionNode:(expr arg1) forAssignmentTo:varName to:setOfTypes. |
|
2028 |
^ setOfTypes |
|
2029 |
]. |
|
2030 |
||
2031 |
"/ really really only very low hanging fruit... |
|
2032 |
"/ ignore #/ here, because of filename |
|
2033 |
( #(+ - *) includes:msgSelector ) ifTrue:[ |
|
3885 | 2034 |
true "(msgReceiver isLiteralNumber or:[expr arg1 isLiteralNumber])" ifTrue:[ |
2035 |
self rememberType:Number in:setOfTypes. |
|
2036 |
^ setOfTypes. |
|
2037 |
]. |
|
2038 |
"/ "/ ignore foo := foo OP expr |
|
2039 |
"/ "/ ignore foo := expr OP foo |
|
2040 |
"/ (msgReceiver isVariable and:[msgReceiver name = varName]) ifFalse:[ |
|
2041 |
"/ (expr arg1 isVariable and:[expr arg1 name = varName]) ifFalse:[ |
|
2042 |
"/ self rememberType:Number in:setOfTypes. |
|
2043 |
"/ ] |
|
2044 |
"/ ]. |
|
3842 | 2045 |
^ setOfTypes. |
2046 |
]. |
|
2047 |
||
2048 |
( #(// size hash identityHash) includes:msgSelector ) ifTrue:[ |
|
2049 |
self rememberType:Integer in:setOfTypes. |
|
2050 |
^ setOfTypes. |
|
2051 |
]. |
|
2052 |
||
2053 |
( #(next next:) includes:msgSelector ) ifTrue:[ |
|
2054 |
|rcvrTypes| |
|
2055 |
||
2056 |
rcvrTypes := self addTypeOfExpressionNode:msgReceiver forAssignmentTo:nil to:Set new. |
|
2057 |
rcvrTypes notEmpty ifTrue:[ |
|
3885 | 2058 |
self breakPoint:#cg. |
3842 | 2059 |
self rememberType:Character in:setOfTypes. |
2060 |
]. |
|
2061 |
^ setOfTypes. |
|
2062 |
]. |
|
2063 |
||
2064 |
( msgSelector startsWith:'as') ifTrue:[ |
|
2065 |
valClass := Smalltalk classNamed:(msgSelector copyFrom:3). |
|
2066 |
valClass notNil ifTrue:[ |
|
2067 |
self rememberType:valClass in:setOfTypes. |
|
2068 |
^ setOfTypes. |
|
2069 |
]. |
|
2070 |
]. |
|
2071 |
||
2072 |
( #(copy shallowCopy) includes:msgSelector ) ifTrue:[ |
|
2073 |
msgReceiver isLiteral ifTrue:[ |
|
2074 |
self addTypeOfExpressionNode:msgReceiver forAssignmentTo:varName to:setOfTypes. |
|
2075 |
]. |
|
2076 |
^ setOfTypes |
|
2077 |
]. |
|
2078 |
||
2079 |
msgReceiver isGlobal ifTrue:[ |
|
3853 | 2080 |
|instCreatorMessages globalValue implMethod| |
3842 | 2081 |
|
3853 | 2082 |
instCreatorMessages := #(new new: basicNew basicNew:). |
2083 |
||
3842 | 2084 |
globalValue := msgReceiver value. |
2085 |
globalValue isBehavior ifTrue:[ |
|
3853 | 2086 |
( instCreatorMessages includes:msgSelector ) ifTrue:[ |
3842 | 2087 |
self rememberType:globalValue in:setOfTypes. |
2088 |
^ setOfTypes. |
|
2089 |
]. |
|
3853 | 2090 |
implMethod := globalValue class lookupMethodFor:msgSelector. |
2091 |
"/ mhmh - fuzzy; if the implementing message sends any of the above to itself... |
|
2092 |
"/ assume it is returning it. |
|
2093 |
implMethod isNil ifTrue:[ |
|
2094 |
"/ will not be understood |
|
3857 | 2095 |
self breakPoint:#cg. |
3853 | 2096 |
^ setOfTypes. |
2097 |
]. |
|
2098 |
(implMethod messagesSentToSelf includesAny:instCreatorMessages) ifTrue:[ |
|
3857 | 2099 |
self breakPoint:#cg. |
3853 | 2100 |
self rememberType:globalValue in:setOfTypes. |
2101 |
^ setOfTypes. |
|
2102 |
]. |
|
2103 |
"/ very fuzzy - if the implementing method is in the "instance creation" category... |
|
2104 |
((implMethod category ? '') startsWith:'instance creation') ifTrue:[ |
|
3857 | 2105 |
self breakPoint:#cg. |
3853 | 2106 |
self rememberType:globalValue in:setOfTypes. |
2107 |
^ setOfTypes. |
|
2108 |
]. |
|
3842 | 2109 |
]. |
2110 |
self breakPoint:#cg. |
|
2111 |
^ setOfTypes |
|
2112 |
]. |
|
2113 |
||
2114 |
self breakPoint:#cg. |
|
2115 |
^ setOfTypes |
|
2116 |
! |
|
2117 |
||
3798 | 2118 |
addTypesAssignedToInstvar:instVarName inClass:aClass method:aMethod to:setOfTypes |
2119 |
"look to asssignments to an instance variable, and pick up low hanging class information. |
|
3842 | 2120 |
This is far from being complete, but often gives a hint good enough for code completion |
3798 | 2121 |
and info in the browser." |
2122 |
||
2123 |
| code | |
|
2124 |
||
3902 | 2125 |
SourceCodeManagerError handle:[:ex | |
2126 |
] do:[ |
|
2127 |
code := aMethod source. |
|
2128 |
]. |
|
3798 | 2129 |
(code notNil) ifTrue:[ |
2130 |
self addTypesAssignedToInstvar:instVarName inClass:aClass source:code to:setOfTypes |
|
2131 |
] |
|
2132 |
||
2133 |
"Created: / 30-04-2016 / 15:07:33 / cg" |
|
2134 |
! |
|
2135 |
||
2136 |
addTypesAssignedToInstvar:instVarName inClass:aClass source:code to:setOfTypes |
|
2137 |
"look to asssignments to an instance variable, and pick up low hanging class information. |
|
3842 | 2138 |
This is far from being complete, but often gives a hint good enough for code completion |
3798 | 2139 |
and info in the browser." |
2140 |
||
3810 | 2141 |
|tree| |
3798 | 2142 |
|
2143 |
"/ quick check (avoids expensive parse) |
|
2144 |
(code includesString:instVarName) ifFalse:[ ^ self ]. |
|
2145 |
||
2146 |
tree := Parser parse:code class:aClass. |
|
2147 |
(tree isNil or:[tree == #Error]) ifTrue:[ ^ self ]. "/ unparsable |
|
2148 |
||
3810 | 2149 |
self addTypesAssignedToInstvar:instVarName inTree:tree to:setOfTypes. |
2150 |
||
2151 |
"Created: / 30-04-2016 / 15:09:18 / cg" |
|
2152 |
! |
|
2153 |
||
2154 |
addTypesAssignedToInstvar:instVarName inClass:aClass to:setOfTypes |
|
2155 |
"look to asssignments to an instance variable, and pick up low hanging class information. |
|
3842 | 2156 |
This is far from being complete, but often gives a hint good enough for code completion |
3810 | 2157 |
and info in the browser." |
2158 |
||
2159 |
aClass methodDictionary do:[:m | |
|
2160 |
self addTypesAssignedToInstvar:instVarName inClass:aClass method:m to:setOfTypes |
|
2161 |
]. |
|
2162 |
||
2163 |
"Created: / 30-04-2016 / 14:52:56 / cg" |
|
2164 |
! |
|
2165 |
||
2166 |
addTypesAssignedToInstvar:instVarName inTree:tree to:setOfTypes |
|
2167 |
"look to asssignments to an instance variable, and pick up low hanging class information. |
|
3842 | 2168 |
This is far from being complete, but often gives a hint good enough for code completion |
3810 | 2169 |
and info in the browser." |
2170 |
||
2171 |
|visitor| |
|
2172 |
||
3798 | 2173 |
visitor := PluggableParseNodeVisitor new. |
2174 |
visitor |
|
2175 |
actionForNodeClass:AssignmentNode |
|
2176 |
put:[:node | |
|
3853 | 2177 |
(node variable isInstanceVariableNamed:instVarName) ifTrue:[ |
2178 |
self addTypeOfExpressionNode:(node expression) forAssignmentTo:instVarName to:setOfTypes |
|
3798 | 2179 |
]. |
2180 |
true "/ yes - visit subnodes |
|
2181 |
]. |
|
2182 |
visitor visit:tree. |
|
2183 |
! |
|
2184 |
||
3810 | 2185 |
addTypesAssignedToLocal:localName inTree:tree to:setOfTypes |
3842 | 2186 |
"look to asssignments to a local variable, and pick up low hanging class information. |
2187 |
This is far from being complete, but often gives a hint good enough for code completion |
|
3798 | 2188 |
and info in the browser." |
2189 |
||
3810 | 2190 |
|visitor| |
2191 |
||
2192 |
"/ hack, allowing to deal with both types of AST (sigh) |
|
2193 |
(tree isKindOf:RBProgramNode) ifTrue:[ |
|
2194 |
visitor := RBPluggableProgramNodeVisitor new. |
|
2195 |
visitor |
|
2196 |
actionForNodeClass:RBAssignmentNode |
|
2197 |
put:[:node | |
|
3853 | 2198 |
|leftSide| |
3798 | 2199 |
|
3810 | 2200 |
leftSide := node variable. |
2201 |
(leftSide isTemporary and:[ leftSide name = localName ]) ifTrue:[ |
|
3853 | 2202 |
self addTypeOfExpressionNode:(node value) forAssignmentTo:localName to:setOfTypes |
3810 | 2203 |
]. |
2204 |
true "/ yes - visit subnodes |
|
2205 |
]. |
|
2206 |
visitor visitNode:tree. |
|
2207 |
] ifFalse:[ |
|
2208 |
visitor := PluggableParseNodeVisitor new. |
|
2209 |
visitor |
|
2210 |
actionForNodeClass:AssignmentNode |
|
2211 |
put:[:node | |
|
2212 |
|leftSide expr| |
|
2213 |
||
2214 |
leftSide := node variable. |
|
2215 |
(leftSide isLocalVariable and:[ leftSide name = localName ]) ifTrue:[ |
|
2216 |
expr := node expression. |
|
2217 |
self addTypeOfExpressionNode:expr forAssignmentTo:localName to:setOfTypes |
|
2218 |
]. |
|
2219 |
true "/ yes - visit subnodes |
|
2220 |
]. |
|
2221 |
visitor visit:tree. |
|
2222 |
]. |
|
3798 | 2223 |
! |
2224 |
||
2225 |
addTypesOfInstvar:instVarName inClass:aClass fromExistingInstancesTo:setOfTypes |
|
2226 |
"look for existing instances and see that type is there" |
|
2227 |
||
2228 |
|instIndex| |
|
2229 |
||
2230 |
instIndex := aClass instVarIndexFor:instVarName. |
|
2231 |
aClass allSubInstancesDo:[:i | |
|
3801 | 2232 |
|val varClass| |
3798 | 2233 |
|
3801 | 2234 |
val := (i instVarAt:instIndex). |
2235 |
val notNil ifTrue:[ |
|
2236 |
varClass := val class. |
|
2237 |
self rememberType:varClass in:setOfTypes. |
|
2238 |
]. |
|
3798 | 2239 |
]. |
2240 |
||
2241 |
"Created: / 30-04-2016 / 14:56:11 / cg" |
|
3801 | 2242 |
"Modified: / 30-04-2016 / 20:05:03 / cg" |
3798 | 2243 |
! |
2244 |
||
2245 |
compressSetOfTypes:setOfTypes |
|
2246 |
|toAdd toRemove| |
|
2247 |
||
2248 |
"/ reduce by eliminating common superclasses... |
|
2249 |
||
2250 |
toAdd := Set new. |
|
2251 |
toRemove := Set new. |
|
2252 |
setOfTypes do:[:type1 | |
|
2253 |
setOfTypes do:[:type2 | |
|
2254 |
|common| |
|
2255 |
||
2256 |
type1 superclass == type2 ifTrue:[ |
|
2257 |
toRemove add:type1. |
|
2258 |
] ifFalse:[ |
|
2259 |
type2 superclass == type1 ifTrue:[ |
|
2260 |
toRemove add:type2. |
|
2261 |
] ifFalse:[ |
|
2262 |
common := type1 commonSuperclass:type2. |
|
2263 |
common ~~ Object ifTrue:[ |
|
2264 |
toRemove add:type1. |
|
2265 |
toRemove add:type2. |
|
2266 |
toAdd add:common. |
|
2267 |
]. |
|
2268 |
]. |
|
2269 |
]. |
|
2270 |
] |
|
2271 |
]. |
|
2272 |
setOfTypes removeAll:toRemove. |
|
2273 |
setOfTypes addAll:toRemove. |
|
2274 |
||
2275 |
"/ hack |
|
2276 |
setOfTypes size == 2 ifTrue:[ |
|
2277 |
((setOfTypes includes:True) and:[setOfTypes includes:False]) ifTrue:[ |
|
2278 |
setOfTypes removeAll; add:Boolean. |
|
2279 |
^ self. |
|
2280 |
]. |
|
2281 |
((setOfTypes includes:SmallInteger) and:[setOfTypes includes:LargeInteger]) ifTrue:[ |
|
2282 |
setOfTypes removeAll; add:Integer. |
|
2283 |
^ self. |
|
2284 |
] |
|
2285 |
]. |
|
2286 |
||
2287 |
"Created: / 30-04-2016 / 15:37:38 / cg" |
|
2288 |
! |
|
2289 |
||
2290 |
rememberType:aClass in:setOfTypes |
|
2291 |
setOfTypes add:aClass |
|
2292 |
||
2293 |
"Created: / 30-04-2016 / 15:35:44 / cg" |
|
3801 | 2294 |
"Modified: / 30-04-2016 / 20:05:09 / cg" |
3798 | 2295 |
! ! |
2296 |
||
3838 | 2297 |
!Explainer class methodsFor:'utilities'! |
2298 |
||
3842 | 2299 |
actionToBrowseClass:class |
2300 |
^ self actionToBrowseClass:class selector:nil. |
|
2301 |
! |
|
2302 |
||
3838 | 2303 |
actionToBrowseClass:class selector:selectorOrNil |
3885 | 2304 |
^ self actionToBrowseClass:class selector:selectorOrNil info:nil |
3866 | 2305 |
! |
2306 |
||
2307 |
actionToBrowseClass:class selector:selectorOrNil info:info |
|
3885 | 2308 |
self assert:class isBehavior. |
3866 | 2309 |
^ ActionWithInfo |
2310 |
block: |
|
2311 |
[ |
|
2312 |
self thisOrNewBrowserInto:[:browser :openHow | |
|
2313 |
browser |
|
2314 |
spawnFullBrowserInClass:class selector:selectorOrNil in:openHow |
|
2315 |
] |
|
2316 |
] |
|
2317 |
info:info |
|
3838 | 2318 |
! |
2319 |
||
2320 |
actionToBrowseClasses:classes |
|
3868 | 2321 |
^ ActionWithInfo |
2322 |
block:[ |
|
2323 |
"/ Tools::NewSystemBrowser browseClasses:classes |
|
2324 |
self thisOrNewBrowserInto:[:browser :openHow | |
|
2325 |
browser spawnClassBrowserFor:classes in:openHow |
|
2326 |
] |
|
3855 | 2327 |
] |
3868 | 2328 |
info:nil |
3838 | 2329 |
! |
2330 |
||
3855 | 2331 |
actionToBrowseImplementorsOf:selector |
4032 | 2332 |
^ self actionToBrowseImplementorsOf:selector in:[Smalltalk allClasses] |
3866 | 2333 |
! |
2334 |
||
2335 |
actionToBrowseImplementorsOf:selector in:classes |
|
3868 | 2336 |
^ ActionWithInfo |
2337 |
block:[ |
|
2338 |
self thisOrNewBrowserInto:[:browser :openHow | |
|
2339 |
browser |
|
2340 |
spawnMethodImplementorsBrowserFor:{ selector } |
|
2341 |
match:false |
|
2342 |
in:openHow |
|
4032 | 2343 |
classes:classes value |
3868 | 2344 |
label:('Implementors of %1' bindWith:selector) |
2345 |
] |
|
3855 | 2346 |
] |
4032 | 2347 |
info:('Click to browse implementors') |
3842 | 2348 |
! |
2349 |
||
3853 | 2350 |
actionToBrowseInstvarRefsTo:instVarName inClass:class |
3868 | 2351 |
^ ActionWithInfo |
2352 |
block:[ |
|
2353 |
self thisOrNewBrowserInto:[:browser :openHow | |
|
2354 |
browser |
|
2355 |
browseVarRefsToAny:{ instVarName } |
|
2356 |
classes:{ class } |
|
2357 |
variables:#instVarNames access:#readOrWrite all:true |
|
2358 |
title:'references to ',instVarName |
|
2359 |
in:openHow |
|
2360 |
] |
|
2361 |
] |
|
2362 |
info:nil |
|
3853 | 2363 |
! |
2364 |
||
3842 | 2365 |
actionToBrowseMethod:mthd |
3885 | 2366 |
self assert:mthd isMethod. |
3842 | 2367 |
^ self actionToBrowseClass:(mthd mclass) selector:(mthd selector). |
2368 |
! |
|
2369 |
||
2370 |
actionToOpenMethodFinderFor:selector |
|
3838 | 2371 |
MethodFinderWindow notNil ifTrue:[ |
3868 | 2372 |
^ ActionWithInfo |
2373 |
block:[ |
|
2374 |
MethodFinderWindow openOnSelectorPattern:selector |
|
2375 |
] |
|
2376 |
info:nil. |
|
3838 | 2377 |
]. |
3842 | 2378 |
^ self actionToBrowseImplementorsOf:selector |
2379 |
! |
|
2380 |
||
2381 |
asClassLink:nameOfClass |
|
2382 |
"return text with a hyperlink to browse a class by that name" |
|
3838 | 2383 |
|
3842 | 2384 |
|cls| |
2385 |
||
2386 |
cls := Smalltalk classNamed:nameOfClass string asUppercaseFirst. |
|
2387 |
cls isNil ifTrue:[^ nameOfClass]. |
|
3859 | 2388 |
|
3866 | 2389 |
^ self |
2390 |
asLink:nameOfClass |
|
3878 | 2391 |
info:('Click to browse %1' bindWith:nameOfClass) |
3866 | 2392 |
to:(self actionToBrowseClass:cls) |
2393 |
! |
|
2394 |
||
2395 |
asLink:aString info:info to:actionOrNil |
|
2396 |
actionOrNil isNil ifTrue:[^ aString]. |
|
2397 |
^ aString asActionLinkTo:(ActionWithInfo block:actionOrNil info:info) |
|
2398 |
! |
|
2399 |
||
2400 |
asLink:aString to:actionOrNil |
|
2401 |
actionOrNil isNil ifTrue:[^ aString]. |
|
2402 |
^ aString asActionLinkTo:actionOrNil |
|
2403 |
! |
|
2404 |
||
3872 | 2405 |
infoStringForClasses:aCollectionOfClasses withPrefix:prefix |
2406 |
"get a nice user readable list for some classes. |
|
2407 |
Up to 4 are named, otherwise the count is presented. |
|
2408 |
The prefix can be sth like ' other', ' sub', ' super', |
|
2409 |
' implementing' etc. Or it can be an empty string. |
|
2410 |
To be shown in the info line at the bottom." |
|
2411 |
||
3885 | 2412 |
|nClassNames sortedByName classNames link1 link2 link3 link4| |
3872 | 2413 |
|
2414 |
aCollectionOfClasses isEmpty ifTrue:[ |
|
2415 |
^ 'No %1classes' bindWith:prefix. |
|
2416 |
]. |
|
2417 |
||
4032 | 2418 |
classNames := aCollectionOfClasses asIdentitySet asOrderedCollection |
2419 |
collect:[:each | each theNonMetaclass name]. |
|
3872 | 2420 |
|
2421 |
nClassNames := classNames size. |
|
2422 |
||
2423 |
nClassNames <= 4 ifTrue:[ |
|
3885 | 2424 |
sortedByName := classNames sort. |
2425 |
link1 := self asClassLink:sortedByName first. |
|
3872 | 2426 |
nClassNames == 1 ifTrue:[ |
3885 | 2427 |
^ '%2' "'1 %1class: %2'" bindWith:prefix with:link1. |
3872 | 2428 |
]. |
3885 | 2429 |
link2 := self asClassLink:classNames second. |
3872 | 2430 |
nClassNames == 2 ifTrue:[ |
2431 |
^ '%2 and %3' "'2 %1classes: %2 and %3'" bindWith:prefix |
|
3885 | 2432 |
with:link1 |
2433 |
with:link2. |
|
3872 | 2434 |
]. |
3885 | 2435 |
link3 := self asClassLink:classNames third. |
3872 | 2436 |
nClassNames == 3 ifTrue:[ |
2437 |
^ '%2, %3 and %4' "'3 %1classes: %2, %3 and %4'" bindWith:prefix |
|
3885 | 2438 |
with:link1 |
2439 |
with:link2 |
|
2440 |
with:link3. |
|
3872 | 2441 |
]. |
3885 | 2442 |
link4 := self asClassLink:classNames fourth. |
3872 | 2443 |
nClassNames == 4 ifTrue:[ |
2444 |
^ '%2, %3, %4 and %5' "'4 %1classes: %2, %3, %4 and %5'" bindWith:prefix |
|
3885 | 2445 |
with:link1 |
2446 |
with:link2 |
|
2447 |
with:link3 |
|
2448 |
with:link4. |
|
3872 | 2449 |
]. |
2450 |
]. |
|
3885 | 2451 |
^ self |
2452 |
asLink:('%1 %2classes' bindWith:nClassNames printString with:prefix) |
|
2453 |
info:'Browse classes' |
|
2454 |
to:(self actionToBrowseClasses:aCollectionOfClasses) |
|
3872 | 2455 |
|
2456 |
"Modified: / 27-07-2006 / 10:09:02 / cg" |
|
2457 |
! |
|
2458 |
||
3866 | 2459 |
infoStringForClasses:aCollectionOfClasses withPrefix:prefix actionGenerator:actionGeneratorOrNil |
2460 |
"get a nice user readable list for some classes. |
|
2461 |
Up to 4 are named, otherwise the count is presented. |
|
2462 |
The prefix can be sth like ' other', ' sub', ' super', |
|
2463 |
' implementing' etc. Or it can be an empty string. |
|
2464 |
To be shown in the info line at the bottom." |
|
2465 |
||
2466 |
|nClassNames classes sortedByName classNames |
|
2467 |
link1 link2 link3 link4 actionGenerator| |
|
2468 |
||
2469 |
aCollectionOfClasses isEmpty ifTrue:[ |
|
2470 |
^ 'No %1classes' bindWith:prefix. |
|
2471 |
]. |
|
2472 |
||
2473 |
actionGenerator := actionGeneratorOrNil isNil |
|
2474 |
ifTrue:[ [:classes | self actionToBrowseClasses:classes] ] |
|
2475 |
ifFalse:[ actionGeneratorOrNil ]. |
|
2476 |
||
2477 |
classes := aCollectionOfClasses asIdentitySet asOrderedCollection. |
|
2478 |
classNames := classes collect:[:each | each theNonMetaclass name]. |
|
2479 |
||
2480 |
nClassNames := classNames size. |
|
2481 |
||
2482 |
nClassNames <= 4 ifTrue:[ |
|
2483 |
sortedByName := classNames sortWith:classes. |
|
2484 |
||
2485 |
link1 := self asLink:classNames first "allBold" |
|
2486 |
info:('Browse %1' bindWith:classNames first) |
|
2487 |
to:(actionGenerator value:{classes first}). |
|
2488 |
nClassNames == 1 ifTrue:[ |
|
2489 |
^ '%2' "'1 %1class: %2'" |
|
2490 |
bindWith:prefix |
|
2491 |
with:link1. |
|
2492 |
]. |
|
2493 |
link2 := self asLink:classNames second "allBold" to:(actionGenerator value:{classes second}). |
|
2494 |
nClassNames == 2 ifTrue:[ |
|
2495 |
^ '%2 and %3' "'2 %1classes: %2 and %3'" |
|
2496 |
bindWith:prefix |
|
2497 |
with:link1 |
|
2498 |
with:link2. |
|
2499 |
]. |
|
2500 |
link3 := self asLink:classNames third "allBold" to:(actionGenerator value:{classes third}). |
|
2501 |
nClassNames == 3 ifTrue:[ |
|
2502 |
^ '%2, %3 and %4' "'3 %1classes: %2, %3 and %4'" |
|
2503 |
bindWith:prefix |
|
2504 |
with:link1 |
|
2505 |
with:link2 |
|
2506 |
with:link3. |
|
2507 |
]. |
|
2508 |
link4 := self asLink:classNames fourth "allBold" to:(actionGenerator value:{classes fourth}). |
|
2509 |
nClassNames == 4 ifTrue:[ |
|
2510 |
^ '%2, %3, %4 and %5' "'4 %1classes: %2, %3, %4 and %5'" |
|
2511 |
bindWith:prefix |
|
2512 |
with:link1 |
|
2513 |
with:link2 |
|
2514 |
with:link3 |
|
2515 |
with:link4. |
|
2516 |
]. |
|
2517 |
]. |
|
2518 |
^ self |
|
2519 |
asLink:('%1 %2classes' bindWith:(nClassNames printString "allBold") with:prefix) |
|
2520 |
to:(actionGenerator value:classes) |
|
2521 |
||
2522 |
"Modified: / 27-07-2006 / 10:09:02 / cg" |
|
3842 | 2523 |
! |
2524 |
||
2525 |
infoStringForMethods:aCollectionOfMethods withPrefix:prefix |
|
2526 |
"get a nice user readable list for some methods. |
|
2527 |
Up to 3 are named, otherwise the count is presented. |
|
2528 |
The prefix can be sth like ' other', ' sender', ' implementor', |
|
2529 |
Or it can be an empty string. |
|
2530 |
Result is meant to be shown in the info line at the bottom of a browser." |
|
2531 |
||
2532 |
|nMethodNames sortedByName methodNames| |
|
2533 |
||
2534 |
aCollectionOfMethods isEmpty ifTrue:[ |
|
2535 |
^ 'No %1' bindWith:prefix. |
|
2536 |
]. |
|
2537 |
||
2538 |
methodNames := aCollectionOfMethods asOrderedCollection |
|
2539 |
collect:[:each | each whoString]. |
|
2540 |
||
2541 |
nMethodNames := methodNames size. |
|
2542 |
||
2543 |
nMethodNames <= 3 ifTrue:[ |
|
2544 |
nMethodNames == 1 ifTrue:[ |
|
2545 |
^ '%2' "'1 %1class: %2'" bindWith:prefix with:(methodNames first allBold). |
|
2546 |
]. |
|
2547 |
sortedByName := methodNames sort. |
|
2548 |
nMethodNames == 2 ifTrue:[ |
|
2549 |
^ '%2 and %3' "'2 %1classes: %2 and %3'" bindWith:prefix |
|
2550 |
with:(sortedByName first allBold) |
|
2551 |
with:(sortedByName second allBold). |
|
2552 |
]. |
|
2553 |
nMethodNames == 3 ifTrue:[ |
|
2554 |
^ '%2, %3 and %4' "'3 %1classes: %2, %3 and %4'" bindWith:prefix |
|
2555 |
with:(sortedByName first allBold) |
|
2556 |
with:(sortedByName second allBold) |
|
2557 |
with:(sortedByName third allBold). |
|
2558 |
]. |
|
2559 |
nMethodNames == 4 ifTrue:[ |
|
2560 |
^ '%2, %3, %4 and %5' "'4 %1classes: %2, %3, %4 and %5'" bindWith:prefix |
|
2561 |
with:(sortedByName first allBold) |
|
2562 |
with:(sortedByName second allBold) |
|
2563 |
with:(sortedByName third allBold) |
|
2564 |
with:(sortedByName fourth allBold). |
|
2565 |
]. |
|
2566 |
]. |
|
2567 |
^ '%1 %2methods' bindWith:nMethodNames printString allBold with:prefix. |
|
2568 |
||
2569 |
" |
|
2570 |
Time millisecondsToRun:[ |
|
2571 |
self infoStringForMethods:(SystemBrowser allCallsOn:#'at:put:') withPrefix:'' |
|
2572 |
]. |
|
2573 |
Time millisecondsToRun:[ |
|
2574 |
self infoStringForMethods:(SystemBrowser allCallsOn:#'actionToBrowseClasses:') withPrefix:'' |
|
2575 |
]. |
|
2576 |
" |
|
2577 |
! |
|
2578 |
||
3872 | 2579 |
methodImplementorsInfoFor:aMethod inEnvironment:environment |
2580 |
"get something about the implementors of aMethod |
|
2581 |
to be shown in the info line at the bottom" |
|
2582 |
||
2583 |
|implementors msg senders msg2| |
|
2584 |
||
2585 |
implementors := SystemBrowser |
|
2586 |
findImplementorsOf:aMethod selector |
|
2587 |
in:(environment allClasses) |
|
2588 |
ignoreCase:false. |
|
2589 |
||
2590 |
implementors notEmpty ifTrue:[ |
|
2591 |
msg := 'Only implemented here.'. |
|
2592 |
implementors remove:aMethod ifAbsent:nil. |
|
2593 |
implementors notEmpty ifTrue:[ |
|
2594 |
implementors := implementors collect:[:mthd | mthd mclass ? mthd getMclass]. |
|
2595 |
implementors notEmpty ifTrue:[ |
|
4032 | 2596 |
msg := 'Also ',(self asLink:'implemented' to:(self actionToBrowseImplementorsOf:aMethod selector)),' in '. |
3872 | 2597 |
msg := msg , (self infoStringForClasses:implementors withPrefix:'other '). |
2598 |
msg := msg , '.'. |
|
2599 |
] |
|
2600 |
]. |
|
2601 |
]. |
|
2602 |
||
2603 |
false ifTrue:[ "/ too slow |
|
2604 |
senders := SystemBrowser |
|
2605 |
findSendersOf:aMethod selector |
|
2606 |
in:(environment allClasses) |
|
2607 |
ignoreCase:false. |
|
2608 |
senders notEmpty ifTrue:[ |
|
2609 |
msg2 := 'Sent from ' , senders size printString, ' methods.'. |
|
2610 |
] ifFalse:[ |
|
2611 |
msg2 := 'No senders.'. |
|
2612 |
]. |
|
2613 |
msg := msg , '/' , msg2 |
|
2614 |
]. |
|
2615 |
||
2616 |
^ msg |
|
2617 |
! |
|
2618 |
||
3871 | 2619 |
methodInheritanceInfoFor:aMethod |
2620 |
|methodsSuperclass inheritedClass msg methodsClass sel mthd selectorString| |
|
2621 |
||
2622 |
methodsClass := aMethod mclass. |
|
2623 |
methodsClass isNil ifTrue:[^ nil]. |
|
2624 |
||
2625 |
methodsSuperclass := methodsClass superclass. |
|
2626 |
methodsSuperclass isNil ifTrue:[^ nil]. |
|
2627 |
||
2628 |
sel := aMethod selector. |
|
2629 |
inheritedClass := methodsSuperclass whichClassIncludesSelector:sel. |
|
2630 |
inheritedClass notNil ifTrue:[ |
|
2631 |
mthd := inheritedClass compiledMethodAt:sel. |
|
2632 |
(mthd sends:#'subclassResponsibility') ifTrue:[ |
|
2633 |
msg := '%1 overrides subclassResponsibility in %2'. |
|
2634 |
] ifFalse:[ |
|
2635 |
msg := '%1 overrides implementation in %2'. |
|
2636 |
]. |
|
2637 |
selectorString := sel contractTo:30. |
|
2638 |
^ msg |
|
2639 |
bindWith:(self |
|
2640 |
asLink:selectorString "allBold" |
|
2641 |
info:('Browse all implementors of %1' bindWith:selectorString) |
|
2642 |
to:(self actionToOpenMethodFinderFor:sel)) |
|
2643 |
with:(self |
|
2644 |
asLink:inheritedClass name "allBold" |
|
4059 | 2645 |
info:('Browse %1 » %2' bindWith:inheritedClass name with:sel) |
3871 | 2646 |
to:(self actionToBrowseClass:inheritedClass selector:sel)). |
2647 |
]. |
|
2648 |
||
2649 |
^ msg |
|
2650 |
! |
|
2651 |
||
2652 |
methodRedefinitionInfoFor:aMethod |
|
2653 |
"return a user readable string telling in how many subclasses |
|
2654 |
a method is redefined. |
|
2655 |
To be shown in the info line of a browser" |
|
2656 |
||
3878 | 2657 |
|redefiningClasses msg cls| |
3871 | 2658 |
|
2659 |
cls := aMethod mclass. |
|
2660 |
cls isNil ifTrue:[^ nil]. |
|
2661 |
||
2662 |
redefiningClasses := cls allSubclasses select:[:cls | cls includesSelector:aMethod selector. ]. |
|
2663 |
redefiningClasses size > 0 ifTrue:[ |
|
2664 |
msg := 'redefined in '. |
|
2665 |
msg := msg , (self |
|
2666 |
infoStringForClasses:redefiningClasses |
|
2667 |
withPrefix:'sub' |
|
2668 |
actionGenerator:[:classes | |
|
3878 | 2669 |
classes size == 1 ifTrue:[ |
2670 |
|cls| |
|
2671 |
cls := classes first. |
|
2672 |
self |
|
2673 |
actionToBrowseClass:cls |
|
2674 |
selector:aMethod selector |
|
2675 |
info:('Browse implementation in %1' bindWith:cls name) |
|
2676 |
] ifFalse:[ |
|
2677 |
self |
|
2678 |
actionToBrowseImplementorsOf:aMethod selector |
|
2679 |
in:classes |
|
2680 |
]. |
|
2681 |
]). |
|
3871 | 2682 |
msg := msg , '.'. |
2683 |
]. |
|
2684 |
||
2685 |
^ msg |
|
2686 |
! |
|
2687 |
||
3842 | 2688 |
methodSendersInfoFor:selector inEnvironment:environment |
2689 |
"get something about the senders of a message. |
|
2690 |
to be shown in the info line at the bottom. |
|
2691 |
This may be slow; so think about doing it in background..." |
|
2692 |
||
2693 |
|senders| |
|
2694 |
||
2695 |
senders := SystemBrowser |
|
2696 |
findSendersOf:selector |
|
2697 |
in:(environment allClasses) |
|
2698 |
ignoreCase:false |
|
2699 |
match:false. |
|
2700 |
||
2701 |
senders notEmpty ifTrue:[ |
|
2702 |
^ 'Sent from ' , senders size printString, ' methods.'. |
|
2703 |
] ifFalse:[ |
|
2704 |
^ 'No senders.'. |
|
2705 |
]. |
|
3859 | 2706 |
! |
2707 |
||
3872 | 2708 |
methodSpecialInfoFor:aMethod |
2709 |
"handles special cases - such as documentation methods" |
|
2710 |
||
2711 |
|cls sel| |
|
2712 |
||
2713 |
(cls := aMethod mclass) isNil ifTrue:[^ nil]. |
|
2714 |
(sel := aMethod selector) isNil ifTrue:[^ nil]. |
|
2715 |
||
2716 |
cls isMeta ifTrue:[ |
|
2717 |
(AbstractSourceCodeManager isVersionMethodSelector:sel) ifTrue:[ |
|
2718 |
^ 'The version method is required for the source code repository - do not modify.'. |
|
2719 |
]. |
|
2720 |
sel == #documentation ifTrue:[ |
|
2721 |
^ 'ST/X stores documentation in this method (not in comment slots)'. |
|
2722 |
]. |
|
2723 |
]. |
|
2724 |
^ nil |
|
2725 |
! |
|
2726 |
||
3859 | 2727 |
thisOrNewBrowserInto:aTwoArgBlock |
2728 |
"if I am invoked by a browser, |
|
2729 |
invoke the twoArgBlock withit and an #newBuffer arg. |
|
2730 |
Otherwise, create a new (invisible) browser and pass it to the block |
|
2731 |
with a #newBrowser arg." |
|
2732 |
||
2733 |
|windowGroupClass browserClass wg app| |
|
2734 |
||
2735 |
"/ stupid: I am in libcomp; should be in libtool |
|
2736 |
windowGroupClass := Smalltalk at:#WindowGroup. |
|
2737 |
windowGroupClass isNil ifTrue:[^ self]. |
|
2738 |
browserClass := Smalltalk at:#'Tools::NewSystemBrowser'. |
|
2739 |
browserClass isNil ifTrue:[^ self]. |
|
2740 |
||
2741 |
((wg := windowGroupClass activeGroup) notNil |
|
2742 |
and:[ (app := wg application) isKindOf:browserClass ] |
|
2743 |
) ifTrue:[ |
|
2744 |
^ aTwoArgBlock value:app value:#newBuffer |
|
2745 |
]. |
|
2746 |
^ aTwoArgBlock value:(browserClass basicNew) value:#newBrowser |
|
3838 | 2747 |
! ! |
3826 | 2748 |
|
3866 | 2749 |
!Explainer::ActionWithInfo class methodsFor:'instance creation'! |
2750 |
||
2751 |
block:aBlock info:info |
|
2752 |
^ self new block:aBlock info:info |
|
2753 |
! ! |
|
2754 |
||
2755 |
!Explainer::ActionWithInfo methodsFor:'accessing'! |
|
2756 |
||
2757 |
block:blockArg info:infoArg |
|
2758 |
block := blockArg. |
|
2759 |
info := infoArg. |
|
2760 |
! |
|
2761 |
||
2762 |
info:something |
|
2763 |
info := something. |
|
2764 |
! ! |
|
2765 |
||
2766 |
!Explainer::ActionWithInfo methodsFor:'evaluation'! |
|
2767 |
||
2768 |
info |
|
2769 |
^ info |
|
2770 |
! |
|
2771 |
||
2772 |
value |
|
2773 |
^ block value |
|
2774 |
! |
|
2775 |
||
2776 |
value:arg |
|
2777 |
^ block value:arg |
|
2778 |
! ! |
|
2779 |
||
396 | 2780 |
!Explainer class methodsFor:'documentation'! |
146
3d407ab8ac44
suppress messages on transcript when explaining
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
2781 |
|
3d407ab8ac44
suppress messages on transcript when explaining
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
2782 |
version |
3697 | 2783 |
^ '$Header$' |
2214 | 2784 |
! |
2785 |
||
2786 |
version_CVS |
|
3697 | 2787 |
^ '$Header$' |
146
3d407ab8ac44
suppress messages on transcript when explaining
Claus Gittinger <cg@exept.de>
parents:
141
diff
changeset
|
2788 |
! ! |
3209 | 2789 |