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