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