Kernel-Classes.st 19 KB

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