Kernel-Classes.st 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. Smalltalk createPackage: 'Kernel-Classes'!
  2. Object subclass: #BehaviorBody
  3. instanceVariableNames: ''
  4. package: 'Kernel-Classes'!
  5. !BehaviorBody commentStamp!
  6. I am the superclass of all behaviors.
  7. My instances hold the method dictionary.
  8. I also provides methods for compiling methods and examining the method dictionary.!
  9. !BehaviorBody methodsFor: 'accessing'!
  10. >> aString
  11. ^ self methodAt: aString
  12. !
  13. comment
  14. ^ (self basicAt: 'comment') ifNil: [ '' ]
  15. !
  16. comment: aString
  17. self basicAt: 'comment' put: aString.
  18. SystemAnnouncer current
  19. announce: (ClassCommentChanged new
  20. theClass: self;
  21. yourself)
  22. !
  23. definition
  24. ^ ''
  25. !
  26. methodAt: aString
  27. ^ self methodDictionary at: aString
  28. !
  29. methodDictionary
  30. <inlineJS: 'var dict = $globals.HashedCollection._new();
  31. var methods = self.methods;
  32. Object.keys(methods).forEach(function(i) {
  33. if(methods[i].selector) {
  34. dict._at_put_(methods[i].selector, methods[i]);
  35. }
  36. });
  37. return dict'>
  38. !
  39. methodTemplate
  40. ^ String streamContents: [ :stream |
  41. stream
  42. nextPutAll: 'messageSelectorAndArgumentNames';
  43. nextPutAll: String lf, String tab;
  44. nextPutAll: '"comment stating purpose of message"';
  45. nextPutAll: String lf, String lf, String tab;
  46. nextPutAll: '| temporary variable names |';
  47. nextPutAll: String lf, String tab;
  48. nextPutAll: 'statements' ]
  49. !
  50. methods
  51. ^ self methodDictionary values
  52. !
  53. methodsInProtocol: aString
  54. ^ self methods select: [ :each | each protocol = aString ]
  55. !
  56. name
  57. <inlineJS: 'return self.className || nil'>
  58. !
  59. organization
  60. ^ self basicAt: 'organization'
  61. !
  62. ownMethods
  63. "Answer the methods of the receiver that are not package extensions"
  64. ^ (self ownProtocols
  65. inject: OrderedCollection new
  66. into: [ :acc :each | acc, (self methodsInProtocol: each) ])
  67. sorted: [ :a :b | a selector <= b selector ]
  68. !
  69. ownProtocols
  70. "Answer the protocols of the receiver that are not package extensions"
  71. ^ self protocols reject: [ :each |
  72. each match: '^\*' ]
  73. !
  74. packageOfProtocol: aString
  75. "Answer the package the method of receiver belongs to:
  76. - if it is an extension method, answer the corresponding package
  77. - else answer the receiver's package"
  78. (aString beginsWith: '*') ifFalse: [
  79. ^ self package ].
  80. ^ Package
  81. named: aString allButFirst
  82. ifAbsent: [ nil ]
  83. !
  84. protocols
  85. ^ self organization elements sorted
  86. !
  87. removeProtocolIfEmpty: aString
  88. self methods
  89. detect: [ :each | each protocol = aString ]
  90. ifNone: [ self organization removeElement: aString ]
  91. !
  92. selectors
  93. ^ self methodDictionary keys
  94. !
  95. theMetaClass
  96. self subclassResponsibility
  97. !
  98. theNonMetaClass
  99. self subclassResponsibility
  100. ! !
  101. !BehaviorBody methodsFor: 'compiling'!
  102. addCompiledMethod: aMethod
  103. | oldMethod announcement |
  104. oldMethod := self methodDictionary
  105. at: aMethod selector
  106. ifAbsent: [ nil ].
  107. (self protocols includes: aMethod protocol)
  108. ifFalse: [ self organization addElement: aMethod protocol ].
  109. self basicAddCompiledMethod: aMethod.
  110. oldMethod ifNotNil: [
  111. self removeProtocolIfEmpty: oldMethod protocol ].
  112. announcement := oldMethod
  113. ifNil: [
  114. MethodAdded new
  115. method: aMethod;
  116. yourself ]
  117. ifNotNil: [
  118. MethodModified new
  119. oldMethod: oldMethod;
  120. method: aMethod;
  121. yourself ].
  122. SystemAnnouncer current
  123. announce: announcement
  124. !
  125. compile: aString protocol: anotherString
  126. ^ Compiler new
  127. install: aString
  128. forClass: self
  129. protocol: anotherString
  130. !
  131. recompile
  132. ^ Compiler new recompile: self
  133. !
  134. removeCompiledMethod: aMethod
  135. self basicRemoveCompiledMethod: aMethod.
  136. self removeProtocolIfEmpty: aMethod protocol.
  137. SystemAnnouncer current
  138. announce: (MethodRemoved new
  139. method: aMethod;
  140. yourself)
  141. ! !
  142. !BehaviorBody methodsFor: 'enumerating'!
  143. protocolsDo: aBlock
  144. "Execute aBlock for each method protocol with
  145. its collection of methods in the sort order of protocol name."
  146. | methodsByProtocol |
  147. methodsByProtocol := HashedCollection new.
  148. self methodDictionary valuesDo: [ :m |
  149. (methodsByProtocol at: m protocol ifAbsentPut: [ Array new ])
  150. add: m ].
  151. self protocols do: [ :protocol |
  152. aBlock value: protocol value: (methodsByProtocol at: protocol) ]
  153. ! !
  154. !BehaviorBody methodsFor: 'printing'!
  155. printOn: aStream
  156. self name
  157. ifNil: [ super printOn: aStream ]
  158. ifNotNil: [ :name | aStream nextPutAll: name ]
  159. ! !
  160. !BehaviorBody methodsFor: 'private'!
  161. basicAddCompiledMethod: aMethod
  162. <inlineJS: '$core.addMethod(aMethod, self)'>
  163. !
  164. basicRemoveCompiledMethod: aMethod
  165. <inlineJS: '$core.removeMethod(aMethod,self)'>
  166. ! !
  167. !BehaviorBody methodsFor: 'testing'!
  168. includesSelector: aString
  169. ^ self methodDictionary includesKey: aString
  170. ! !
  171. BehaviorBody subclass: #Behavior
  172. instanceVariableNames: ''
  173. package: 'Kernel-Classes'!
  174. !Behavior commentStamp!
  175. I am the superclass of all class objects.
  176. In addition to BehaviorBody, I define superclass/subclass relationships and instantiation.
  177. I define the protocol for creating instances of a class with `#basicNew` and `#new` (see `boot.js` for class constructors details).
  178. My instances know about the subclass/superclass relationships between classes and contain the description that instances are created from.
  179. I also provide iterating over the class hierarchy.!
  180. !Behavior methodsFor: 'accessing'!
  181. allInstanceVariableNames
  182. | result |
  183. result := self instanceVariableNames copy.
  184. self superclass ifNotNil: [
  185. result addAll: self superclass allInstanceVariableNames ].
  186. ^ result
  187. !
  188. allSelectors
  189. ^ self allSuperclasses
  190. inject: self selectors
  191. into: [ :acc :each | acc addAll: each selectors; yourself ]
  192. !
  193. allSubclasses
  194. "Answer an collection of the receiver's and the receiver's descendent's subclasses. "
  195. ^ Array streamContents: [ :str | self allSubclassesDo: [ :each | str nextPut: each ] ]
  196. !
  197. allSuperclasses
  198. self superclass ifNil: [ ^ #() ].
  199. ^ (OrderedCollection with: self superclass)
  200. addAll: self superclass allSuperclasses;
  201. yourself
  202. !
  203. definition
  204. ^ ''
  205. !
  206. instanceVariableNames
  207. <inlineJS: 'return self.iVarNames'>
  208. !
  209. javascriptConstructor
  210. "Answer the JS constructor used to instantiate. See boot.js"
  211. <inlineJS: 'return self.fn'>
  212. !
  213. javascriptConstructor: aJavaScriptFunction
  214. "Set the JS constructor used to instantiate.
  215. See the JS counter-part in boot.js `$core.setClassConstructor'"
  216. <inlineJS: '$core.setClassConstructor(self, aJavaScriptFunction);'>
  217. !
  218. lookupSelector: selector
  219. "Look up the given selector in my methodDictionary.
  220. Return the corresponding method if found.
  221. Otherwise chase the superclass chain and try again.
  222. Return nil if no method is found."
  223. | lookupClass |
  224. lookupClass := self.
  225. [ lookupClass = nil ] whileFalse: [
  226. (lookupClass includesSelector: selector)
  227. ifTrue: [ ^ lookupClass methodAt: selector ].
  228. lookupClass := lookupClass superclass ].
  229. ^ nil
  230. !
  231. prototype
  232. <inlineJS: 'return self.fn.prototype'>
  233. !
  234. subclasses
  235. self subclassResponsibility
  236. !
  237. superclass
  238. <inlineJS: 'return self.superclass'>
  239. !
  240. theMetaClass
  241. self subclassResponsibility
  242. !
  243. theNonMetaClass
  244. self subclassResponsibility
  245. !
  246. withAllSubclasses
  247. ^ (Array with: self) addAll: self allSubclasses; yourself
  248. ! !
  249. !Behavior methodsFor: 'enumerating'!
  250. allSubclassesDo: aBlock
  251. "Evaluate the argument, aBlock, for each of the receiver's subclasses."
  252. <inlineJS: '$core.traverseClassTree(self, function(subclass) {
  253. if (subclass !!== self) aBlock._value_(subclass);
  254. })'>
  255. ! !
  256. !Behavior methodsFor: 'instance creation'!
  257. basicNew
  258. <inlineJS: 'return new self.fn()'>
  259. !
  260. new
  261. ^ self basicNew initialize
  262. ! !
  263. !Behavior methodsFor: 'testing'!
  264. canUnderstand: aSelector
  265. ^ (self includesSelector: aSelector asString) or: [
  266. self superclass notNil and: [ self superclass canUnderstand: aSelector ]]
  267. !
  268. includesBehavior: aClass
  269. ^ self == aClass or: [
  270. self inheritsFrom: aClass ]
  271. !
  272. inheritsFrom: aClass
  273. self superclass ifNil: [ ^ false ].
  274. ^ aClass == self superclass or: [
  275. self superclass inheritsFrom: aClass ]
  276. !
  277. isBehavior
  278. ^ true
  279. ! !
  280. Behavior subclass: #Class
  281. instanceVariableNames: ''
  282. package: 'Kernel-Classes'!
  283. !Class commentStamp!
  284. I am __the__ class object.
  285. My instances are the classes of the system.
  286. Class creation is done throught a `ClassBuilder` instance.!
  287. !Class methodsFor: 'accessing'!
  288. category
  289. ^ self package ifNil: [ 'Unclassified' ] ifNotNil: [ self package name ]
  290. !
  291. classTag
  292. "Returns a tag or general category for this class.
  293. Typically used to help tools do some reflection.
  294. Helios, for example, uses this to decide what icon the class should display."
  295. ^ 'class'
  296. !
  297. definition
  298. ^ String streamContents: [ :stream | stream
  299. print: self superclass; write: ' subclass: '; printSymbol: self name; lf;
  300. tab; write: 'instanceVariableNames: '; print: (' ' join: self instanceVariableNames); lf;
  301. tab; write: 'package: '; print: self category ]
  302. !
  303. package
  304. ^ self basicAt: 'pkg'
  305. !
  306. package: aPackage
  307. | oldPackage |
  308. self package = aPackage ifTrue: [ ^ self ].
  309. oldPackage := self package.
  310. self basicAt: 'pkg' put: aPackage.
  311. oldPackage organization removeElement: self.
  312. aPackage organization addElement: self.
  313. SystemAnnouncer current announce: (ClassMoved new
  314. theClass: self;
  315. oldPackage: oldPackage;
  316. yourself)
  317. !
  318. rename: aString
  319. ClassBuilder new renameClass: self to: aString
  320. !
  321. subclasses
  322. <inlineJS: 'return self.subclasses._copy()'>
  323. !
  324. theMetaClass
  325. ^ self class
  326. !
  327. theNonMetaClass
  328. ^ self
  329. ! !
  330. !Class methodsFor: 'browsing'!
  331. browse
  332. Finder findClass: self
  333. ! !
  334. !Class methodsFor: 'class creation'!
  335. subclass: aString instanceVariableNames: anotherString
  336. "Kept for file-in compatibility."
  337. ^ self subclass: aString instanceVariableNames: anotherString package: nil
  338. !
  339. subclass: aString instanceVariableNames: aString2 category: aString3
  340. "Kept for file-in compatibility."
  341. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  342. !
  343. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  344. "Kept for file-in compatibility. ignores class variables and pools."
  345. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  346. !
  347. subclass: aString instanceVariableNames: aString2 package: aString3
  348. ^ ClassBuilder new
  349. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  350. ! !
  351. !Class methodsFor: 'converting'!
  352. asJavascript
  353. ^ '$globals.', self name
  354. ! !
  355. !Class methodsFor: 'testing'!
  356. isClass
  357. ^ true
  358. ! !
  359. Behavior subclass: #Metaclass
  360. instanceVariableNames: ''
  361. package: 'Kernel-Classes'!
  362. !Metaclass commentStamp!
  363. I am the root of the class hierarchy.
  364. My instances are metaclasses, one for each real class, and have a single instance, which they hold onto: the class that they are the metaclass of.!
  365. !Metaclass methodsFor: 'accessing'!
  366. definition
  367. ^ String streamContents: [ :stream | stream
  368. print: self;
  369. write: ' instanceVariableNames: ';
  370. print: (' ' join: self instanceVariableNames) ]
  371. !
  372. instanceClass
  373. <inlineJS: 'return self.instanceClass'>
  374. !
  375. instanceVariableNames: aCollection
  376. ClassBuilder new
  377. class: self instanceVariableNames: aCollection
  378. !
  379. name
  380. ^ self instanceClass name, ' class'
  381. !
  382. package
  383. ^ self instanceClass package
  384. !
  385. subclasses
  386. <inlineJS: 'return $core.metaSubclasses(self)'>
  387. !
  388. theMetaClass
  389. ^ self
  390. !
  391. theNonMetaClass
  392. ^ self instanceClass
  393. ! !
  394. !Metaclass methodsFor: 'converting'!
  395. asJavascript
  396. ^ '$globals.', self instanceClass name, '.klass'
  397. ! !
  398. !Metaclass methodsFor: 'testing'!
  399. isMetaclass
  400. ^ true
  401. ! !
  402. BehaviorBody subclass: #Trait
  403. instanceVariableNames: ''
  404. package: 'Kernel-Classes'!
  405. !Trait methodsFor: 'IDE compatibility'!
  406. allSubclassesDo: aBlock
  407. !
  408. superclass
  409. ^ nil
  410. ! !
  411. !Trait methodsFor: 'accessing'!
  412. category
  413. ^ self package ifNil: [ 'Unclassified' ] ifNotNil: [ self package name ]
  414. !
  415. classTag
  416. ^ 'trait'
  417. !
  418. definition
  419. ^ String streamContents: [ :stream | stream
  420. write: 'Trait named: '; printSymbol: self name; lf;
  421. tab; write: 'package: '; print: self category ]
  422. !
  423. package
  424. ^ self basicAt: 'pkg'
  425. !
  426. theMetaClass
  427. ^ nil
  428. !
  429. theNonMetaClass
  430. ^ self
  431. ! !
  432. !Trait methodsFor: 'compiler compatibility'!
  433. allInstanceVariableNames
  434. ^ #()
  435. ! !
  436. !Trait methodsFor: 'converting'!
  437. asJavascript
  438. ^ '$globals.', self name
  439. ! !
  440. !Trait class methodsFor: 'instance creation'!
  441. named: aString package: anotherString
  442. <return $core.addTrait(aString, anotherString)>
  443. ! !
  444. Object subclass: #ClassBuilder
  445. instanceVariableNames: ''
  446. package: 'Kernel-Classes'!
  447. !ClassBuilder commentStamp!
  448. I am responsible for compiling new classes or modifying existing classes in the system.
  449. Rather than using me directly to compile a class, use `Class >> subclass:instanceVariableNames:package:`.!
  450. !ClassBuilder methodsFor: 'accessing'!
  451. instanceVariableNamesFor: aString
  452. ^ (aString tokenize: ' ') reject: [ :each | each isEmpty ]
  453. ! !
  454. !ClassBuilder methodsFor: 'class definition'!
  455. addSubclassOf: aClass named: className instanceVariableNames: aCollection package: packageName
  456. | theClass thePackage |
  457. theClass := Smalltalk globals at: className.
  458. thePackage := Package named: packageName.
  459. theClass ifNotNil: [
  460. theClass package: thePackage.
  461. theClass superclass == aClass ifFalse: [
  462. ^ self
  463. migrateClassNamed: className
  464. superclass: aClass
  465. instanceVariableNames: aCollection
  466. package: packageName ] ].
  467. ^ self
  468. basicAddSubclassOf: aClass
  469. named: className
  470. instanceVariableNames: aCollection
  471. package: packageName
  472. !
  473. class: aClass instanceVariableNames: ivarNames
  474. self basicClass: aClass instanceVariableNames: ivarNames.
  475. SystemAnnouncer current
  476. announce: (ClassDefinitionChanged new
  477. theClass: aClass;
  478. yourself)
  479. !
  480. superclass: aClass subclass: className
  481. ^ self superclass: aClass subclass: className instanceVariableNames: '' package: nil
  482. !
  483. superclass: aClass subclass: className instanceVariableNames: ivarNames package: packageName
  484. | newClass |
  485. newClass := self addSubclassOf: aClass
  486. named: className instanceVariableNames: (self instanceVariableNamesFor: ivarNames)
  487. package: (packageName ifNil: [ 'unclassified' ]).
  488. SystemAnnouncer current
  489. announce: (ClassAdded new
  490. theClass: newClass;
  491. yourself).
  492. ^ newClass
  493. ! !
  494. !ClassBuilder methodsFor: 'class migration'!
  495. migrateClass: aClass superclass: anotherClass
  496. ^ self
  497. migrateClassNamed: aClass name
  498. superclass: anotherClass
  499. instanceVariableNames: aClass instanceVariableNames
  500. package: aClass package name
  501. !
  502. migrateClassNamed: className superclass: aClass instanceVariableNames: aCollection package: packageName
  503. | oldClass newClass tmp |
  504. tmp := 'new*', className.
  505. oldClass := Smalltalk globals at: className.
  506. newClass := self
  507. addSubclassOf: aClass
  508. named: tmp
  509. instanceVariableNames: aCollection
  510. package: packageName.
  511. self basicSwapClassNames: oldClass with: newClass.
  512. [ self copyClass: oldClass to: newClass ]
  513. on: Error
  514. do: [ :exception |
  515. self
  516. basicSwapClassNames: oldClass with: newClass;
  517. basicRemoveClass: newClass.
  518. exception resignal ].
  519. self
  520. rawRenameClass: oldClass to: tmp;
  521. rawRenameClass: newClass to: className.
  522. oldClass subclasses
  523. do: [ :each | self migrateClass: each superclass: newClass ].
  524. self basicRemoveClass: oldClass.
  525. SystemAnnouncer current announce: (ClassMigrated new
  526. theClass: newClass;
  527. oldClass: oldClass;
  528. yourself).
  529. ^ newClass
  530. !
  531. renameClass: aClass to: className
  532. self basicRenameClass: aClass to: className.
  533. "Recompile the class to fix potential issues with super sends"
  534. aClass recompile.
  535. SystemAnnouncer current
  536. announce: (ClassRenamed new
  537. theClass: aClass;
  538. yourself)
  539. ! !
  540. !ClassBuilder methodsFor: 'copying'!
  541. copyClass: aClass named: className
  542. | newClass |
  543. newClass := self
  544. addSubclassOf: aClass superclass
  545. named: className
  546. instanceVariableNames: aClass instanceVariableNames
  547. package: aClass package name.
  548. self copyClass: aClass to: newClass.
  549. SystemAnnouncer current
  550. announce: (ClassAdded new
  551. theClass: newClass;
  552. yourself).
  553. ^ newClass
  554. !
  555. copyClass: aClass to: anotherClass
  556. anotherClass comment: aClass comment.
  557. aClass methodDictionary valuesDo: [ :each |
  558. Compiler new install: each source forClass: anotherClass protocol: each protocol ].
  559. self basicClass: anotherClass class instanceVariables: aClass class instanceVariableNames.
  560. aClass class methodDictionary valuesDo: [ :each |
  561. Compiler new install: each source forClass: anotherClass class protocol: each protocol ]
  562. ! !
  563. !ClassBuilder methodsFor: 'method definition'!
  564. installMethod: aCompiledMethod forClass: aBehavior protocol: aString
  565. aCompiledMethod protocol: aString.
  566. aBehavior addCompiledMethod: aCompiledMethod.
  567. ^ aCompiledMethod
  568. ! !
  569. !ClassBuilder methodsFor: 'private'!
  570. basicAddSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  571. <inlineJS: '
  572. return $core.addClass(aString, aClass, aCollection, packageName);
  573. '>
  574. !
  575. basicClass: aClass instanceVariableNames: aString
  576. self basicClass: aClass instanceVariables: (self instanceVariableNamesFor: aString)
  577. !
  578. basicClass: aClass instanceVariables: aCollection
  579. aClass isMetaclass ifFalse: [ self error: aClass name, ' is not a metaclass' ].
  580. aClass basicAt: 'iVarNames' put: aCollection
  581. !
  582. basicRemoveClass: aClass
  583. <inlineJS: '$core.removeClass(aClass)'>
  584. !
  585. basicRenameClass: aClass to: aString
  586. <inlineJS: '
  587. $globals[aString] = aClass;
  588. delete $globals[aClass.className];
  589. aClass.className = aString;
  590. '>
  591. !
  592. basicSwapClassNames: aClass with: anotherClass
  593. <inlineJS: '
  594. var tmp = aClass.className;
  595. aClass.className = anotherClass.className;
  596. anotherClass.className = tmp;
  597. '>
  598. !
  599. rawRenameClass: aClass to: aString
  600. <inlineJS: '
  601. $globals[aString] = aClass;
  602. '>
  603. ! !
  604. !ClassBuilder methodsFor: 'public'!
  605. setupClass: aClass
  606. self deprecatedAPI: 'Classes are now auto-inited.'
  607. ! !
  608. Object subclass: #ClassSorterNode
  609. instanceVariableNames: 'theClass level nodes'
  610. package: 'Kernel-Classes'!
  611. !ClassSorterNode commentStamp!
  612. I provide an algorithm for sorting classes alphabetically.
  613. See [Issue #143](https://lolg.it/amber/amber/issues/143).!
  614. !ClassSorterNode methodsFor: 'accessing'!
  615. getNodesFrom: aCollection
  616. | children others |
  617. children := #().
  618. others := #().
  619. aCollection do: [ :each |
  620. (each superclass = self theClass)
  621. ifTrue: [ children add: each ]
  622. ifFalse: [ others add: each ]].
  623. nodes:= children collect: [ :each |
  624. ClassSorterNode on: each classes: others level: self level + 1 ]
  625. !
  626. level
  627. ^ level
  628. !
  629. level: anInteger
  630. level := anInteger
  631. !
  632. nodes
  633. ^ nodes
  634. !
  635. theClass
  636. ^ theClass
  637. !
  638. theClass: aClass
  639. theClass := aClass
  640. ! !
  641. !ClassSorterNode methodsFor: 'visiting'!
  642. traverseClassesWith: aCollection
  643. "sort classes alphabetically Issue #143"
  644. aCollection add: self theClass.
  645. (self nodes sorted: [ :a :b | a theClass name <= b theClass name ]) do: [ :aNode |
  646. aNode traverseClassesWith: aCollection ].
  647. ! !
  648. !ClassSorterNode class methodsFor: 'instance creation'!
  649. on: aClass classes: aCollection level: anInteger
  650. ^ self new
  651. theClass: aClass;
  652. level: anInteger;
  653. getNodesFrom: aCollection;
  654. yourself
  655. ! !