blob: fc574a5eecd942dca76665abd8ff18641fa59106 (
plain) (
blame)
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
|
package engine
import "strings"
type CommentMode int
const (
CommentsFollow CommentMode = iota
CommentsPrecede
CommentsStandalone
)
type Engine struct {
CommentMode CommentMode
GroupSingleLineScopes bool
}
func (e *Engine) format(events []LineEvent, resultBuilder *strings.Builder) {
hasWrittenContent := false
previousWasOpenBrace := false
previousStatementType := ""
previousWasComment := false
previousWasTopLevel := false
previousWasScoped := false
previousWasSingleLineScope := false
for eventIndex, event := range events {
if event.InRawString {
if hasWrittenContent {
resultBuilder.WriteByte('\n')
}
resultBuilder.WriteString(event.Content)
hasWrittenContent = true
continue
}
if event.IsBlank {
continue
}
currentStatementType := event.StatementType
if event.IsPackageDecl {
currentStatementType = "package"
}
needsBlankLine := false
currentIsTopLevel := event.HasASTInfo && event.IsTopLevel
currentIsScoped := event.HasASTInfo && event.IsScoped
currentIsSingleLineScope := currentIsScoped && !event.IsOpeningBrace && !event.IsClosingBrace
if hasWrittenContent && !previousWasOpenBrace && !event.IsClosingBrace && !event.IsCaseLabel && !event.IsContinuation {
if currentIsTopLevel && previousWasTopLevel && currentStatementType != previousStatementType {
if e.CommentMode != CommentsFollow || !previousWasComment {
needsBlankLine = true
}
} else if event.HasASTInfo && (currentIsScoped || previousWasScoped) {
if e.GroupSingleLineScopes && currentIsSingleLineScope && previousWasSingleLineScope && currentStatementType == previousStatementType {
needsBlankLine = false
} else if e.CommentMode != CommentsFollow || !previousWasComment {
needsBlankLine = true
}
} else if currentStatementType != "" && previousStatementType != "" && currentStatementType != previousStatementType {
if e.CommentMode != CommentsFollow || !previousWasComment {
needsBlankLine = true
}
}
if e.CommentMode == CommentsFollow && event.IsCommentOnly && !previousWasComment {
nextIndex := e.findNextNonComment(events, eventIndex+1)
if nextIndex >= 0 {
nextNonCommentEvent := events[nextIndex]
if nextNonCommentEvent.HasASTInfo {
nextIsTopLevel := nextNonCommentEvent.IsTopLevel
nextIsScoped := nextNonCommentEvent.IsScoped
if nextIsTopLevel && previousWasTopLevel && nextNonCommentEvent.StatementType != previousStatementType {
needsBlankLine = true
} else if nextIsScoped || previousWasScoped {
needsBlankLine = true
} else if nextNonCommentEvent.StatementType != "" && previousStatementType != "" && nextNonCommentEvent.StatementType != previousStatementType {
needsBlankLine = true
}
}
}
}
}
if needsBlankLine {
resultBuilder.WriteByte('\n')
}
if hasWrittenContent {
resultBuilder.WriteByte('\n')
}
resultBuilder.WriteString(event.Content)
hasWrittenContent = true
previousWasOpenBrace = event.IsOpeningBrace || event.IsCaseLabel
previousWasComment = event.IsCommentOnly
if event.HasASTInfo {
previousStatementType = event.StatementType
previousWasTopLevel = event.IsTopLevel
previousWasScoped = event.IsScoped
previousWasSingleLineScope = currentIsSingleLineScope
} else if currentStatementType != "" {
previousStatementType = currentStatementType
previousWasTopLevel = false
previousWasScoped = false
previousWasSingleLineScope = false
}
}
resultBuilder.WriteByte('\n')
}
func (e *Engine) FormatToString(events []LineEvent) string {
var resultBuilder strings.Builder
resultBuilder.Grow(len(events) * 40)
e.format(events, &resultBuilder)
return resultBuilder.String()
}
func (e *Engine) FormatToBytes(events []LineEvent) []byte {
var resultBuilder strings.Builder
resultBuilder.Grow(len(events) * 40)
e.format(events, &resultBuilder)
return []byte(resultBuilder.String())
}
func (e *Engine) findNextNonComment(events []LineEvent, startIndex int) int {
for eventIndex := startIndex; eventIndex < len(events); eventIndex++ {
if events[eventIndex].IsBlank {
continue
}
if events[eventIndex].IsCommentOnly {
continue
}
return eventIndex
}
return -1
}
|