Kernel-Classes.st 19 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 |
  299. self superclass printOn: stream.
  300. stream nextPutAll: ' subclass: '.
  301. self name asSymbolPrintOn: stream.
  302. stream lf; tab; nextPutAll: 'instanceVariableNames: '.
  303. (' ' join: self instanceVariableNames) printOn: stream.
  304. stream lf; tab; nextPutAll: 'package: '.
  305. self category printOn: stream ]
  306. !
  307. package
  308. ^ self basicAt: 'pkg'
  309. !
  310. package: aPackage
  311. | oldPackage |
  312. self package = aPackage ifTrue: [ ^ self ].
  313. oldPackage := self package.
  314. self basicAt: 'pkg' put: aPackage.
  315. oldPackage organization removeElement: self.
  316. aPackage organization addElement: self.
  317. SystemAnnouncer current announce: (ClassMoved new
  318. theClass: self;
  319. oldPackage: oldPackage;
  320. yourself)
  321. !
  322. rename: aString
  323. ClassBuilder new renameClass: self to: aString
  324. !
  325. subclasses
  326. <inlineJS: 'return self.subclasses._copy()'>
  327. !
  328. theMetaClass
  329. ^ self class
  330. !
  331. theNonMetaClass
  332. ^ self
  333. ! !
  334. !Class methodsFor: 'browsing'!
  335. browse
  336. Finder findClass: self
  337. ! !
  338. !Class methodsFor: 'class creation'!
  339. subclass: aString instanceVariableNames: anotherString
  340. "Kept for file-in compatibility."
  341. ^ self subclass: aString instanceVariableNames: anotherString package: nil
  342. !
  343. subclass: aString instanceVariableNames: aString2 category: aString3
  344. "Kept for file-in compatibility."
  345. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  346. !
  347. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  348. "Kept for file-in compatibility. ignores class variables and pools."
  349. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  350. !
  351. subclass: aString instanceVariableNames: aString2 package: aString3
  352. ^ ClassBuilder new
  353. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  354. ! !
  355. !Class methodsFor: 'converting'!
  356. asJavascript
  357. ^ '$globals.', self name
  358. ! !
  359. !Class methodsFor: 'testing'!
  360. isClass
  361. ^ true
  362. ! !
  363. Behavior subclass: #Metaclass
  364. instanceVariableNames: ''
  365. package: 'Kernel-Classes'!
  366. !Metaclass commentStamp!
  367. I am the root of the class hierarchy.
  368. 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.!
  369. !Metaclass methodsFor: 'accessing'!
  370. definition
  371. ^ String streamContents: [ :stream |
  372. self printOn: stream.
  373. stream nextPutAll: ' instanceVariableNames: '.
  374. (' ' join: self instanceVariableNames) printOn: stream ]
  375. !
  376. instanceClass
  377. <inlineJS: 'return self.instanceClass'>
  378. !
  379. instanceVariableNames: aCollection
  380. ClassBuilder new
  381. class: self instanceVariableNames: aCollection
  382. !
  383. name
  384. ^ self instanceClass name, ' class'
  385. !
  386. package
  387. ^ self instanceClass package
  388. !
  389. subclasses
  390. <inlineJS: 'return $core.metaSubclasses(self)'>
  391. !
  392. theMetaClass
  393. ^ self
  394. !
  395. theNonMetaClass
  396. ^ self instanceClass
  397. ! !
  398. !Metaclass methodsFor: 'converting'!
  399. asJavascript
  400. ^ '$globals.', self instanceClass name, '.klass'
  401. ! !
  402. !Metaclass methodsFor: 'testing'!
  403. isMetaclass
  404. ^ true
  405. ! !
  406. BehaviorBody subclass: #Trait
  407. instanceVariableNames: ''
  408. package: 'Kernel-Classes'!
  409. !Trait methodsFor: 'IDE compatibility'!
  410. allSubclassesDo: aBlock
  411. !
  412. superclass
  413. ^ nil
  414. ! !
  415. !Trait methodsFor: 'accessing'!
  416. category
  417. ^ self package ifNil: [ 'Unclassified' ] ifNotNil: [ self package name ]
  418. !
  419. classTag
  420. ^ 'trait'
  421. !
  422. definition
  423. ^ String streamContents: [ :stream |
  424. stream nextPutAll: 'Trait named: '.
  425. self name asSymbolPrintOn: stream.
  426. stream lf; tab; nextPutAll: 'package: '.
  427. self category printOn: stream ]
  428. !
  429. package
  430. ^ self basicAt: 'pkg'
  431. !
  432. theMetaClass
  433. ^ nil
  434. !
  435. theNonMetaClass
  436. ^ self
  437. ! !
  438. !Trait methodsFor: 'compiler compatibility'!
  439. allInstanceVariableNames
  440. ^ #()
  441. ! !
  442. !Trait class methodsFor: 'instance creation'!
  443. named: aString package: anotherString
  444. <return $core.addTrait(aString, anotherString)>
  445. ! !
  446. Object subclass: #ClassBuilder
  447. instanceVariableNames: ''
  448. package: 'Kernel-Classes'!
  449. !ClassBuilder commentStamp!
  450. I am responsible for compiling new classes or modifying existing classes in the system.
  451. Rather than using me directly to compile a class, use `Class >> subclass:instanceVariableNames:package:`.!
  452. !ClassBuilder methodsFor: 'accessing'!
  453. instanceVariableNamesFor: aString
  454. ^ (aString tokenize: ' ') reject: [ :each | each isEmpty ]
  455. ! !
  456. !ClassBuilder methodsFor: 'class definition'!
  457. addSubclassOf: aClass named: className instanceVariableNames: aCollection package: packageName
  458. | theClass thePackage |
  459. theClass := Smalltalk globals at: className.
  460. thePackage := Package named: packageName.
  461. theClass ifNotNil: [
  462. theClass package: thePackage.
  463. theClass superclass == aClass ifFalse: [
  464. ^ self
  465. migrateClassNamed: className
  466. superclass: aClass
  467. instanceVariableNames: aCollection
  468. package: packageName ] ].
  469. ^ self
  470. basicAddSubclassOf: aClass
  471. named: className
  472. instanceVariableNames: aCollection
  473. package: packageName
  474. !
  475. class: aClass instanceVariableNames: ivarNames
  476. self basicClass: aClass instanceVariableNames: ivarNames.
  477. SystemAnnouncer current
  478. announce: (ClassDefinitionChanged new
  479. theClass: aClass;
  480. yourself)
  481. !
  482. superclass: aClass subclass: className
  483. ^ self superclass: aClass subclass: className instanceVariableNames: '' package: nil
  484. !
  485. superclass: aClass subclass: className instanceVariableNames: ivarNames package: packageName
  486. | newClass |
  487. newClass := self addSubclassOf: aClass
  488. named: className instanceVariableNames: (self instanceVariableNamesFor: ivarNames)
  489. package: (packageName ifNil: [ 'unclassified' ]).
  490. SystemAnnouncer current
  491. announce: (ClassAdded new
  492. theClass: newClass;
  493. yourself).
  494. ^ newClass
  495. ! !
  496. !ClassBuilder methodsFor: 'class migration'!
  497. migrateClass: aClass superclass: anotherClass
  498. ^ self
  499. migrateClassNamed: aClass name
  500. superclass: anotherClass
  501. instanceVariableNames: aClass instanceVariableNames
  502. package: aClass package name
  503. !
  504. migrateClassNamed: className superclass: aClass instanceVariableNames: aCollection package: packageName
  505. | oldClass newClass tmp |
  506. tmp := 'new*', className.
  507. oldClass := Smalltalk globals at: className.
  508. newClass := self
  509. addSubclassOf: aClass
  510. named: tmp
  511. instanceVariableNames: aCollection
  512. package: packageName.
  513. self basicSwapClassNames: oldClass with: newClass.
  514. [ self copyClass: oldClass to: newClass ]
  515. on: Error
  516. do: [ :exception |
  517. self
  518. basicSwapClassNames: oldClass with: newClass;
  519. basicRemoveClass: newClass.
  520. exception resignal ].
  521. self
  522. rawRenameClass: oldClass to: tmp;
  523. rawRenameClass: newClass to: className.
  524. oldClass subclasses
  525. do: [ :each | self migrateClass: each superclass: newClass ].
  526. self basicRemoveClass: oldClass.
  527. SystemAnnouncer current announce: (ClassMigrated new
  528. theClass: newClass;
  529. oldClass: oldClass;
  530. yourself).
  531. ^ newClass
  532. !
  533. renameClass: aClass to: className
  534. self basicRenameClass: aClass to: className.
  535. "Recompile the class to fix potential issues with super sends"
  536. aClass recompile.
  537. SystemAnnouncer current
  538. announce: (ClassRenamed new
  539. theClass: aClass;
  540. yourself)
  541. ! !
  542. !ClassBuilder methodsFor: 'copying'!
  543. copyClass: aClass named: className
  544. | newClass |
  545. newClass := self
  546. addSubclassOf: aClass superclass
  547. named: className
  548. instanceVariableNames: aClass instanceVariableNames
  549. package: aClass package name.
  550. self copyClass: aClass to: newClass.
  551. SystemAnnouncer current
  552. announce: (ClassAdded new
  553. theClass: newClass;
  554. yourself).
  555. ^ newClass
  556. !
  557. copyClass: aClass to: anotherClass
  558. anotherClass comment: aClass comment.
  559. aClass methodDictionary valuesDo: [ :each |
  560. Compiler new install: each source forClass: anotherClass protocol: each protocol ].
  561. self basicClass: anotherClass class instanceVariables: aClass class instanceVariableNames.
  562. aClass class methodDictionary valuesDo: [ :each |
  563. Compiler new install: each source forClass: anotherClass class protocol: each protocol ]
  564. ! !
  565. !ClassBuilder methodsFor: 'method definition'!
  566. installMethod: aCompiledMethod forClass: aBehavior protocol: aString
  567. aCompiledMethod protocol: aString.
  568. aBehavior addCompiledMethod: aCompiledMethod.
  569. ^ aCompiledMethod
  570. ! !
  571. !ClassBuilder methodsFor: 'private'!
  572. basicAddSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  573. <inlineJS: '
  574. return $core.addClass(aString, aClass, aCollection, packageName);
  575. '>
  576. !
  577. basicClass: aClass instanceVariableNames: aString
  578. self basicClass: aClass instanceVariables: (self instanceVariableNamesFor: aString)
  579. !
  580. basicClass: aClass instanceVariables: aCollection
  581. aClass isMetaclass ifFalse: [ self error: aClass name, ' is not a metaclass' ].
  582. aClass basicAt: 'iVarNames' put: aCollection
  583. !
  584. basicRemoveClass: aClass
  585. <inlineJS: '$core.removeClass(aClass)'>
  586. !
  587. basicRenameClass: aClass to: aString
  588. <inlineJS: '
  589. $globals[aString] = aClass;
  590. delete $globals[aClass.className];
  591. aClass.className = aString;
  592. '>
  593. !
  594. basicSwapClassNames: aClass with: anotherClass
  595. <inlineJS: '
  596. var tmp = aClass.className;
  597. aClass.className = anotherClass.className;
  598. anotherClass.className = tmp;
  599. '>
  600. !
  601. rawRenameClass: aClass to: aString
  602. <inlineJS: '
  603. $globals[aString] = aClass;
  604. '>
  605. ! !
  606. !ClassBuilder methodsFor: 'public'!
  607. setupClass: aClass
  608. self deprecatedAPI: 'Classes are now auto-inited.'
  609. ! !
  610. Object subclass: #ClassSorterNode
  611. instanceVariableNames: 'theClass level nodes'
  612. package: 'Kernel-Classes'!
  613. !ClassSorterNode commentStamp!
  614. I provide an algorithm for sorting classes alphabetically.
  615. See [Issue #143](https://lolg.it/amber/amber/issues/143).!
  616. !ClassSorterNode methodsFor: 'accessing'!
  617. getNodesFrom: aCollection
  618. | children others |
  619. children := #().
  620. others := #().
  621. aCollection do: [ :each |
  622. (each superclass = self theClass)
  623. ifTrue: [ children add: each ]
  624. ifFalse: [ others add: each ]].
  625. nodes:= children collect: [ :each |
  626. ClassSorterNode on: each classes: others level: self level + 1 ]
  627. !
  628. level
  629. ^ level
  630. !
  631. level: anInteger
  632. level := anInteger
  633. !
  634. nodes
  635. ^ nodes
  636. !
  637. theClass
  638. ^ theClass
  639. !
  640. theClass: aClass
  641. theClass := aClass
  642. ! !
  643. !ClassSorterNode methodsFor: 'visiting'!
  644. traverseClassesWith: aCollection
  645. "sort classes alphabetically Issue #143"
  646. aCollection add: self theClass.
  647. (self nodes sorted: [ :a :b | a theClass name <= b theClass name ]) do: [ :aNode |
  648. aNode traverseClassesWith: aCollection ].
  649. ! !
  650. !ClassSorterNode class methodsFor: 'instance creation'!
  651. on: aClass classes: aCollection level: anInteger
  652. ^ self new
  653. theClass: aClass;
  654. level: anInteger;
  655. getNodesFrom: aCollection;
  656. yourself
  657. ! !