17
17
/// Preserves user personal style to some degree
18
18
// TODO ^
19
19
class Prettify {
20
+ var tabs = ''
21
+
22
+ @inline fun pushTab() {
23
+ tabs += '\t'
24
+ }
25
+
26
+ @inline fun popTab() {
27
+ tabs = tabs.substring(0, tabs.length - 1)
28
+ }
29
+
20
30
new () {
21
31
// Initializes state for nested blocks indentation
22
32
}
@@ -47,7 +57,11 @@ class Prettify {
47
57
// TODO remove `:` here!
48
58
case Block(el):
49
59
// TODO depth!
50
- return `{\n\t` + el.map(e => this.stringify(e)).join('\n\t') + `\n}`
60
+ pushTab()
61
+ let body = `{\n` + tabs + el.map(e => this.stringify(e)).join('\n' + tabs)
62
+ popTab()
63
+
64
+ return body + `\n` + tabs + `}`
51
65
52
66
/// `if condition[0], ...condition[n] { then } [else { otherwise }]`
53
67
case If(condition, then, otherwise, ternary):
@@ -119,7 +133,13 @@ class Prettify {
119
133
/// `external class t extends extend implements implement { fields }`
120
134
// TODO IDE: hover over `(className)` in the pattern itself must show its type
121
135
case Class(className, extend, implement, fields, external, kind):
122
- return 'class ' + NodeType.stringify(className) + ' ' + (extend ? 'extends ' + extend : '') + (implement.length > 0 ? 'implements ' + implement : '') + ' {\n' + fields.map(f => this.stringify(f)).join('\n') + '\n}'
136
+ let head = 'class ' + NodeType.stringify(className) + ' ' + (extend ? 'extends ' + extend : '') + (implement.length > 0 ? 'implements ' + implement : '')
137
+
138
+ pushTab()
139
+ let body = '{\n' + tabs + fields.map(f => this.stringify(f)).join('\n' + tabs) + '\n}'
140
+ popTab()
141
+
142
+ return head + body
123
143
124
144
/// var name T { get { return x } set (v) {} }
125
145
// TODO .Var, .Function, .Function
@@ -143,8 +163,14 @@ class Prettify {
143
163
/// case cases[n]: conditions[n]
144
164
/// }
145
165
case Switch(inputs , expressions , guards , patterns ):
146
- return 'switch ' + [for i in inputs.length this.stringify(inputs[i])].join(', ') + ' {\n' +
147
- [for i in patterns.length 'case ' + this.stringify(expressions[i]) + ': ' + this.stringify(patterns[i])].join('\n') + '\n}'
166
+ let head = 'switch ' + inputs.map(i => this.stringify(i)).join(', ') + ' {\n'
167
+
168
+ pushTab()
169
+ let body =
170
+ [for i in patterns.length tabs + 'case ' + this.stringify(expressions[i]) + ': ' + this.stringify(patterns[i])].join('\n')
171
+ popTab()
172
+
173
+ return head + body + '\n' + tabs + '}'
148
174
149
175
/// module path[0].path[1].r..path[n] { el }
150
176
case Module(path , el ):
0 commit comments