-
Notifications
You must be signed in to change notification settings - Fork 2
/
pretty.st
352 lines (303 loc) · 10.4 KB
/
pretty.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
Namespace current: Shampoo [
Object subclass: MethodSource [
<category: 'Shampoo-Pretty printing'>
| selectorWithArguments body |
MethodSource class >> from: aString [
<category: 'instance creation'>
^(self new)
init: aString;
yourself
]
MethodSource >> init: aString [
<category: 'private'>
| r |
r := ReadStream on: aString.
selectorWithArguments := (r upTo: $[) trimSeparators.
body := ((r upToEnd) copyUpToLast: $]) lines.
]
selectorWithArguments [
<category: 'accessors'>
^selectorWithArguments
]
body [
<category: 'accessors'>
^body
]
methodIndent [
<category: 'accessors'>
| notEmpty spaceLens |
"At first, drop all empty strings"
notEmpty := body select:
[:each | each trimSeparators isEmpty not].
spaceLens := notEmpty collect:
[:each | (each takeWhile: [:c | c isSeparator]) size].
^spaceLens isEmpty
ifTrue: [0]
ifFalse: [spaceLens fold: [:a :b | a min: b]]
]
compact [
<category: 'pretty printing'>
| r fst |
"Remove empty lines from the beginning, if any"
r := ReadStream on: body.
r forwardWhile: [:line | line trimSeparators isEmpty].
body := body drop: r position.
body isEmpty ifTrue: [^self].
"Remove newline characters from the beginning of the
first line"
fst := body first breakIf: [:c | c isSeparator not].
fst := (fst first copyWithout: Character nl), fst second.
body at: 1 put: fst.
"Remove empty lines from the end, if any"
r := ReadStream on: body reverse.
r forwardWhile: [:line | line trimSeparators isEmpty].
body := body take: body size - r position.
]
stripIndent [
<category: 'pretty printing'>
| indent |
indent := self methodIndent.
body := body collect: [:each | each drop: indent].
]
indentWith: aString [
<category: 'pretty printing'>
body := body collect: [:each | aString, each].
]
printOn: aStream [
aStream
nextPutAll: selectorWithArguments;
space;
nextPut: $[;
nl.
body do: [:line | aStream nextPutAll: line; nl].
aStream nextPut: $]; nl.
]
sourceString [
<category: 'pretty printing'>
| w |
w := WriteStream on: String new.
self printOn: w.
^w contents
]
]
Object subclass: ClassSource [
<category: 'Shampoo-Pretty printing'>
| printedClass namespaces |
ClassSource class >> of: aClass [
<category: 'instance creation'>
^self new
printedClass: aClass;
yourself
]
printedClass: aClass [
<category: 'private'>
printedClass := aClass.
namespaces := printedClass namespaceChain.
]
enclosingNamespaces: aStream do: aBlock [
<category: 'pretty printing'>
namespaces do:
[:ns | aStream
nextPutAll: 'Namespace current:';
space;
nextPutAll: ns name asString;
space;
nextPut: $[;
space].
aStream nl.
aBlock value.
namespaces size timesRepeat: [aStream nextPut: $]].
aStream nl
]
enclosingClassDefinition: aStream do: aBlock [
<category: 'pretty printing'>
| superName |
superName := printedClass superclass
ifNil: ['nil']
ifNotNil: [printedClass superclass name asString].
aStream
nextPutAll: superName;
space;
nextPutAll: 'subclass:';
space;
nextPutAll: printedClass name asString;
space;
nextPut: $[;
nl.
aStream increasingIndent: [aBlock value].
aStream nextPut: $]; nl
]
writePragma: aPragmaName value: aString on: aStream [
<category: 'pretty printing'>
aStream
nextPut: $<;
nextPutAll: aPragmaName;
nextPut: $:;
space;
nextPutAll: aString printString;
nextPut: $>
]
writeCommentsOn: aStream [
<category: 'pretty printing'>
printedClass category isNil ifFalse:
[self writePragma: 'category'
value: printedClass category
on: aStream.
aStream nl].
printedClass comment isNil ifFalse:
[self writePragma: 'comment'
value: printedClass comment
on: aStream.
aStream nl]
]
writeInstVarsOn: aStream [
<category: 'pretty printing'>
| instvars |
instvars := printedClass instVarNames asStringArray.
instvars isEmpty ifFalse:
[aStream
nl;
nextPut: $|;
space;
nextPutAll: instvars elementsString;
space;
nextPut: $|;
nl]
]
writeClassVarsOn: aStream [
<category: 'pretty printing'>
printedClass classVarNames asStringArray do:
[:each | aStream
nextPutAll: each;
space;
nextPutAll: ':= nil.';
nl]
]
writeMethod: aMethod on: aStream [
<category: 'pretty printing'>
| ms |
ms := MethodSource from: aMethod methodSourceString.
ms compact; stripIndent.
aStream nl.
ms printOn: aStream.
]
writeClassMethod: aMethod on: aStream [
<category: 'pretty printing'>
| ms start |
ms := MethodSource from: aMethod methodSourceString.
ms compact; stripIndent.
aStream nl.
start := aStream position.
aStream
nextPutAll: printedClass name asString;
space;
nextPutAll: 'class >> '.
aStream position + ms selectorWithArguments size - start
>= 67 ifTrue: [aStream nl].
ms printOn: aStream.
]
writeMethodsOn: aStream [
<category: 'pretty printing'>
printedClass methodDictionary ifNotNil:
[printedClass methodDictionary values do:
[:each | self writeMethod: each on: aStream]]
]
writeClassMethodsOn: aStream [
<category: 'pretty printing'>
printedClass class methodDictionary ifNotNil:
[printedClass class methodDictionary values do:
[:each | self writeClassMethod: each on: aStream]]
]
sourceString [
<category: 'pretty printing'>
| w is ns |
w := WriteStream on: String new.
is := IndentedStreamDecorator on: w.
self enclosingNamespaces: is do:
[self enclosingClassDefinition: is do:
[self
writeCommentsOn: is;
writeInstVarsOn: is;
writeClassVarsOn: is;
writeClassMethodsOn: is;
writeMethodsOn: is
]].
^is contents.
]
]
Object subclass: PrettyPrinter [
<category: 'Shampoo-Pretty printing'>
PrettyPrinter class >> prettifyMethod: aSourceString [
<category: 'pretty printing'>
^(MethodSource from: aSourceString)
compact;
stripIndent;
indentWith: ' ';
sourceString
]
PrettyPrinter class >> prettifyClass: aClass [
<category: 'pretty printing'>
^(ClassSource of: aClass)
sourceString
]
PrettyPrinter class >> prettifyClasses: aCollectionOfClasses [
<category: 'pretty printing'>
| prettified |
prettified := aCollectionOfClasses collect:
[:each | self prettifyClass: each].
^String join: prettified
]
]
Decorator subclass: IndentedStreamDecorator [
<category: 'Shampoo-Pretty printing'>
| state indentLevel levelSpaces |
prepareIndent [
<category: 'indentation fsm'>
state := #preparingToIndent
]
tryIndent [
<category: 'indentation fsm'>
self state = #preparingToIndent ifTrue: [self indent]
]
increasingIndent: aBlock [
<category: 'indentation'>
indentLevel := self indentLevel + 1.
[aBlock value] ensure:
[indentLevel := self indentLevel - 1]
]
indent [
<category: 'indentation fsm'>
self indentLevel * self levelSpaces timesRepeat:
[self underlyingObject space].
state := #indented
]
nl [
<category: 'stream decorator'>
self prepareIndent.
self underlyingObject nl
]
nextPut: anObject [
<category: 'stream decorator'>
anObject = Character nl
ifTrue: [self prepareIndent]
ifFalse: [self tryIndent].
self underlyingObject nextPut: anObject.
]
nextPutAll: aCollection [
<category: 'stream decorator'>
aCollection do: [:each | self nextPut: each]
]
state [
<category: 'accessors'>
^state ifNil: [state := #preparingToIndent]
]
indentLevel [
<category: 'accessors'>
^indentLevel ifNil: [indentLevel := 0]
]
levelSpaces [
<category: 'accessors'>
^levelSpaces ifNil: [levelSpaces := 4]
]
]
]