-
Notifications
You must be signed in to change notification settings - Fork 67
/
format.go
318 lines (292 loc) · 8.09 KB
/
format.go
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
package core
import (
"fmt"
"io"
"regexp"
"sort"
"unicode/utf8"
)
func seqFirst(seq Seq, w io.Writer, indent int) (Seq, int) {
if !seq.IsEmpty() {
indent = formatObject(seq.First(), indent, w)
seq = seq.Rest()
}
return seq, indent
}
// TODO: maybe merge it with seqFirstAfterBreak
// or extract common part into a separate function
func seqFirstAfterSpace(seq Seq, w io.Writer, indent int, insideDefRecord bool) (Seq, Object, int) {
var obj Object
if !seq.IsEmpty() {
fmt.Fprint(w, " ")
obj = seq.First()
// Seq handling here is needed to properly format methods
// inside defrecord
if s, ok := obj.(Seq); ok && !obj.Equals(NIL) {
if info := obj.GetInfo(); info != nil {
fmt.Fprint(w, info.prefix)
indent += utf8.RuneCountInString(info.prefix)
}
indent = formatSeqEx(s, w, indent+1, insideDefRecord)
} else {
indent = formatObject(obj, indent+1, w)
}
seq = seq.Rest()
}
return seq, obj, indent
}
func writeNewLines(w io.Writer, prevObj Object, obj Object) int {
cnt := newLineCount(prevObj, obj)
for i := 0; i < cnt; i++ {
fmt.Fprint(w, "\n")
}
return cnt
}
func seqFirstAfterBreak(prevObj Object, seq Seq, w io.Writer, indent int, insideDefRecord bool) (Seq, Object, int) {
var obj Object
if !seq.IsEmpty() {
obj = seq.First()
writeNewLines(w, prevObj, obj)
writeIndent(w, indent)
// Seq handling here is needed to properly format methods
// inside defrecord
if s, ok := obj.(Seq); ok && !obj.Equals(NIL) {
if info := obj.GetInfo(); info != nil {
fmt.Fprint(w, info.prefix)
indent += utf8.RuneCountInString(info.prefix)
}
indent = formatSeqEx(s, w, indent, insideDefRecord)
} else {
indent = formatObject(obj, indent, w)
}
seq = seq.Rest()
}
return seq, obj, indent
}
func seqFirstAfterForcedBreak(seq Seq, w io.Writer, indent int) (Seq, Object, int) {
var obj Object
if !seq.IsEmpty() {
obj = seq.First()
fmt.Fprint(w, "\n")
writeIndent(w, indent)
indent = formatObject(obj, indent, w)
seq = seq.Rest()
}
return seq, obj, indent
}
func formatBindings(v *Vector, w io.Writer, indent int) int {
return v.Format(w, indent)
}
func formatVectorVertically(v *Vector, w io.Writer, indent int) int {
fmt.Fprint(w, "[")
newIndent := indent + 1
for i := 0; i < v.count; i++ {
newIndent = formatObject(v.at(i), indent+1, w)
if i+1 < v.count {
fmt.Fprint(w, "\n")
writeIndent(w, indent+1)
}
}
if v.count > 0 {
if isComment(v.at(v.count - 1)) {
fmt.Fprint(w, "\n")
writeIndent(w, indent+1)
newIndent = indent + 1
}
}
fmt.Fprint(w, "]")
return newIndent + 1
}
var defRegex *regexp.Regexp = regexp.MustCompile("^def.*$")
var ifRegex *regexp.Regexp = regexp.MustCompile("^if(-.+)?$")
var whenRegex *regexp.Regexp = regexp.MustCompile("^when(-.+)?$")
var doIndentRegex *regexp.Regexp = regexp.MustCompile("^(do|try|finally|go|alt!|alt!!)$")
var bodyIndentRegexes []*regexp.Regexp = []*regexp.Regexp{
regexp.MustCompile("^(bound-fn|if|if-not|case|cond|cond->|cond->>|as->|condp|when|while|when-not|when-first|do|future|thread)$"),
regexp.MustCompile("^(comment|doto|locking|proxy|with-[^\\s]*|reify|fdef)$"),
regexp.MustCompile("^(defprotocol|extend|extend-protocol|extend-type|catch|let|letfn|binding|loop|for|go-loop)$"),
regexp.MustCompile("^(doseq|dotimes|when-let|if-let|defstruct|struct-map|defmethod|testing|are|deftest|context|use-fixtures)$"),
regexp.MustCompile("^(POST|GET|PUT|DELETE)"),
regexp.MustCompile("^(handler-case|handle|dotrace|deftrace|match)$"),
}
func isOneAndBodyExpr(obj Object) bool {
switch s := obj.(type) {
case Symbol:
return defRegex.MatchString(*s.name) ||
ifRegex.MatchString(*s.name) ||
whenRegex.MatchString(*s.name)
default:
return false
}
}
func isDoIndent(obj Object) bool {
switch s := obj.(type) {
case Symbol:
return doIndentRegex.MatchString(*s.name)
default:
return false
}
}
func isBodyIndent(obj Object) bool {
switch s := obj.(type) {
case Symbol:
for _, re := range bodyIndentRegexes {
if re.MatchString(*s.name) {
return true
}
}
return false
default:
return false
}
}
func isNewLine(obj, nextObj Object) bool {
info, nextInfo := obj.GetInfo(), nextObj.GetInfo()
return !(info == nil || nextInfo == nil || info.endLine == nextInfo.startLine)
}
func newLineCount(obj, nextObj Object) int {
info, nextInfo := obj.GetInfo(), nextObj.GetInfo()
if info == nil || nextInfo == nil {
return 0
}
return nextInfo.startLine - info.endLine
}
func formatSeq(seq Seq, w io.Writer, indent int) int {
return formatSeqEx(seq, w, indent, false)
}
func formatSeqSimple(seq Seq, w io.Writer, indent int) int {
ind := indent + 1
fmt.Fprint(w, "(")
var prevObj Object
for !seq.IsEmpty() {
obj := seq.First()
if prevObj != nil {
ind = maybeNewLine(w, prevObj, obj, indent+1, ind)
}
ind = formatObject(obj, ind, w)
prevObj = obj
seq = seq.Rest()
}
if prevObj != nil {
if isComment(prevObj) {
fmt.Fprint(w, "\n")
writeIndent(w, indent+1)
ind = indent + 1
}
}
fmt.Fprint(w, ")")
return ind + 1
}
type RequireSort []Object
func (rs RequireSort) Len() int { return len(rs) }
func (rs RequireSort) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
func (rs RequireSort) Less(i, j int) bool {
a := rs[i]
if s, ok := a.(Seqable); ok {
a = s.Seq().First()
}
b := rs[j]
if s, ok := b.(Seqable); ok {
b = s.Seq().First()
}
return a.ToString(false) < b.ToString(false)
}
func sortRequire(seq Seq) Seq {
s := RequireSort(ToSlice(seq))
sort.Sort(s)
return &ArraySeq{arr: s}
}
func formatSeqEx(seq Seq, w io.Writer, indent int, formatAsDef bool) int {
if info := seq.GetInfo(); info != nil {
if info.prefix == "#?" || info.prefix == "#?@" {
return formatSeqSimple(seq, w, indent)
}
}
i := indent + 1
restIndent := indent + 2
fmt.Fprint(w, "(")
obj := seq.First()
prevObj := obj
seq, i = seqFirst(seq, w, i)
isDefRecord := false
if obj.Equals(SYMBOLS.defrecord) ||
obj.Equals(SYMBOLS.defprotocol) ||
obj.Equals(SYMBOLS.extendProtocol) ||
obj.Equals(SYMBOLS.reify) ||
obj.Equals(SYMBOLS.deftype) ||
obj.Equals(SYMBOLS.proxy) ||
obj.Equals(SYMBOLS.extendType) {
isDefRecord = true
}
if obj.Equals(SYMBOLS.ns) || isOneAndBodyExpr(obj) {
seq, prevObj, i = seqFirstAfterSpace(seq, w, i, isDefRecord)
} else if obj.Equals(KEYWORDS.require) || obj.Equals(KEYWORDS._import) {
seq = sortRequire(seq)
seq, obj, _ = seqFirstAfterSpace(seq, w, i, isDefRecord)
for !seq.IsEmpty() {
seq, obj, _ = seqFirstAfterForcedBreak(seq, w, i+1)
}
} else if obj.Equals(SYMBOLS.catch) {
if !seq.IsEmpty() {
seq, prevObj, i = seqFirstAfterSpace(seq, w, i, isDefRecord)
seq, prevObj, i = seqFirstAfterSpace(seq, w, i, isDefRecord)
}
} else if obj.Equals(SYMBOLS.fn) {
if !seq.IsEmpty() {
switch seq.First().(type) {
case *Vector:
seq, prevObj, i = seqFirstAfterSpace(seq, w, i, isDefRecord)
case Symbol:
seq, prevObj, i = seqFirstAfterSpace(seq, w, i, isDefRecord)
seq, prevObj, i = seqFirstAfterSpace(seq, w, i, isDefRecord)
default:
if !isNewLine(obj, seq.First()) {
restIndent = i + 1
}
}
}
} else if obj.Equals(SYMBOLS.let) || obj.Equals(SYMBOLS.loop) {
if v, ok := seq.First().(*Vector); ok {
fmt.Fprint(w, " ")
i = formatBindings(v, w, i+1)
prevObj = seq.First()
seq = seq.Rest()
}
} else if obj.Equals(SYMBOLS.letfn) {
if v, ok := seq.First().(*Vector); ok {
fmt.Fprint(w, " ")
i = formatVectorVertically(v, w, i+1)
prevObj = seq.First()
seq = seq.Rest()
}
} else if isDoIndent(obj) {
if !seq.IsEmpty() && !isNewLine(obj, seq.First()) {
restIndent = i + 1
}
} else if formatAsDef {
} else if isBodyIndent(obj) {
restIndent = indent + 2
} else {
// Indent function call arguments.
restIndent = indent + 1
if !seq.IsEmpty() && !isNewLine(obj, seq.First()) {
restIndent = i + 1
}
}
for !seq.IsEmpty() {
nextObj := seq.First()
if isNewLine(obj, nextObj) {
seq, prevObj, i = seqFirstAfterBreak(prevObj, seq, w, restIndent, isDefRecord)
} else {
seq, prevObj, i = seqFirstAfterSpace(seq, w, i, isDefRecord)
}
obj = nextObj
}
if isComment(obj) {
fmt.Fprint(w, "\n")
writeIndent(w, restIndent)
i = restIndent
}
fmt.Fprint(w, ")")
return i + 1
}