-
Notifications
You must be signed in to change notification settings - Fork 8
/
multiline.go
148 lines (137 loc) · 3.81 KB
/
multiline.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
package parser
import (
"strings"
)
// multilineItem is used to represent multiline array and map literals
// as used in formatting. It does not explicitly store array or map
// values, just placeholders - "el" for next element in arrays, keys for
// maps.
//
// A multilineItem can be:
// 1. Empty `""` representing a newline.
// 2. Starting with `"//"` represents a full comment.
// 3a. For arrays the multiline item `"el"` represents the next array
// literal element.
// 3b. For maps all the multiline items that don't represent
// newline (`""`) or comments (`// …`) are the key of the next map pair.
//
// The follow example shows a multiline array literal and its
// multilineItem slice representation:
//
// arr := [ 1 // commment1
// 2
//
// // comment2
// 3
// ]
//
// has the representation:
//
// [ "el", "// comment1", "el", "", "", "// comment2", "el", ""]
//
// The next example shows a multiline map literal and its
// multilineItem slice representation:
//
// map := {
// a: 1 // commment1
// b: 2
//
// // comment2
// c: 3
// }
//
// has the representation:
//
// [ "", "a","// comment1", "b", "", "", "// comment2", "c", ""]
type multilineItem string
func (m multilineItem) isComment() bool {
return strings.HasPrefix(string(m), "//")
}
func (m multilineItem) isNL() bool {
return m == multilineNL
}
func (m multilineItem) isKey() bool {
return !m.isNL() && !m.isComment()
}
const (
multilineEl = multilineItem("el")
multilineNL = multilineItem("\n")
)
func multilineComment(s string) multilineItem {
s = strings.TrimSpace(s)
return multilineItem(s + "\n")
}
func formatMultiline(multilineItems []multilineItem) []multilineItem {
formatted := make([]multilineItem, 0, len(multilineItems))
nlCount := 0
for _, item := range multilineItems {
switch {
case item.isNL():
nlCount++
case item.isComment():
nlCount = 1
default: // array element or map key
nlCount = 0
}
if nlCount <= 2 {
formatted = append(formatted, item)
}
}
return formatted
}
// accumulation classifies statements into "empty", "comment", "stmt"
// and "func". It is used for newline insertion before function and
// eventHandler declarations taking leading comments into account.
type accumulation struct {
stmtType string // "empty", "comment", "stmt, "func"
idx int
}
// nlAfter returns a map (set) of statements that need to be
// followed by a newline.
func nlAfter(stmts []Node, comments map[Node]string) map[int]bool {
accums := newAccumulations(stmts, comments)
indices := map[int]bool{}
length := len(accums)
if length == 0 {
return indices
}
for i, accum := range accums[:length-1] {
switch {
case accum.stmtType == "empty" || accum.stmtType == "comment":
// do nothing for empty lines and comments
case accum.stmtType == "func" && accums[i+1].stmtType == "stmt":
// add NL after func decl directly followed by stmt
indices[accum.idx] = true
case accums[i+1].stmtType == "func":
// add NL before func decl (before stmt or other func decl)
beforeFuncIdx := accums[i+1].idx - 1
indices[beforeFuncIdx] = true
case i+2 < length && accums[i+1].stmtType == "comment" && accums[i+2].stmtType == "func":
// add NL before comments of func decl (after stmt or other func decl)
indices[accum.idx] = true
}
}
return indices
}
func newAccumulations(stmts []Node, comments map[Node]string) []accumulation {
lastStmtType := ""
var accums []accumulation
for i, stmt := range stmts {
stmtType := "stmt"
switch s := stmt.(type) {
case *EmptyStmt:
stmtType = "empty"
if comments[s] != "" {
stmtType = "comment"
}
case *FuncDefStmt, *EventHandlerStmt:
stmtType = "func"
}
if stmtType != lastStmtType || stmtType == "func" {
accum := accumulation{stmtType: stmtType, idx: i}
accums = append(accums, accum)
lastStmtType = stmtType
}
}
return accums
}