@@ -54,10 +54,12 @@ CTKeyedTree >> atPath: anArray [
5454
5555{ #category : #accessing }
5656CTKeyedTree >> atPath: anArray ifAbsent: aBlock [
57- " Answer the element referenced by the given key path. Answer the value of aBlock if not found."
5857 | element |
5958 element := self .
60- anArray do: [ :key | element := element at: key ifAbsent: [ ^ aBlock value ] ].
59+ anArray do: [ :key |
60+ (element isKindOf: self class )
61+ ifTrue: [ element := element at: key ifAbsent: [ ^ aBlock value ] ]
62+ ifFalse: [ ^ aBlock value ] ].
6163 ^ element
6264]
6365
@@ -117,15 +119,25 @@ CTKeyedTree >> postCopy [
117119
118120{ #category : #printing }
119121CTKeyedTree >> putFormattedTextOn: aStream level: indentLevel indentString: aString [
120- " Place a description of the receiver on the given stream with the given indentation level."
121- (self keys asSortedCollection: self sortBlock) do: [ :k | | v |
122- indentLevel timesRepeat: [ aStream nextPutAll: aString ].
123- aStream nextPutAll: k printString.
124- v := self at: k.
125- (v isKindOf: self class )
126- ifTrue: [ aStream cr.
122+ " Write a textual representation of the receiver to aStream, indenting to indentLevel using aString as the indent string."
123+
124+ (self keys asSortedCollection: self sortBlock)
125+ do: [ :k |
126+ | v |
127+ indentLevel = 0
128+ ifFalse: [
129+ indentLevel = 1
130+ ifTrue: [ aStream nextPutAll: aString ]
131+ ifFalse: [ indentLevel - 1 timesRepeat: [ aStream nextPutAll: aString ]. aStream nextPutAll: ' ' ] ].
132+ aStream nextPutAll: k printString.
133+ v := self at: k.
134+ (v isKindOf: self class )
135+ ifTrue: [ aStream cr.
127136 v putFormattedTextOn: aStream level: indentLevel + 1 indentString: aString ]
128- ifFalse: [ aStream nextPutAll: ' : ' ; nextPutAll: v printString. aStream cr ] ]
137+ ifFalse: [ aStream
138+ nextPutAll: ' : ' ;
139+ nextPutAll: v printString.
140+ aStream cr ] ]
129141]
130142
131143{ #category : #removing }
@@ -146,12 +158,32 @@ CTKeyedTree >> removePath: anArray ifAbsent: aBlock [
146158
147159{ #category : #accessing }
148160CTKeyedTree >> sortBlock [
149- " Answer the block to sort tree keys with."
150- ^ [ :a :b | [ a <= b ] on: Error do: [ a class name <= b class name ] ]
161+ " Answer a sort block that can be used to sort the keys of the receiver."
162+
163+ ^ [ :a :b |
164+ a class = b class
165+ ifTrue: [ a <= b ]
166+ ifFalse: [
167+ (a isSymbol and : [ b isNumber ])
168+ ifTrue: [ true ]
169+ ifFalse: [
170+ (b isSymbol and : [ a isNumber ])
171+ ifTrue: [ false ]
172+ ifFalse: [ a class name <= b class name ] ] ] ]
151173]
152174
153175{ #category : #accessing }
154176CTKeyedTree >> subtrees [
155177 " Answer the subtrees of the receiver."
156178 ^ (self select: [ :v | v isKindOf: CTKeyedTree ]) values
179+ ]
180+
181+ { #category : #tests }
182+ CTKeyedTreeTest >> testFormattedText [
183+ " Test the formatted text representation of the tree."
184+
185+ self assert: self t13 formattedText equals:
186+ ' 1 : ' ' 1-3-1' '
187+ 2 : ' ' 1-3-2' '
188+ '
157189]
0 commit comments