blob: 7a5399c1b382763fcae7db1a0c7e37e016925d2b (
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
|
package engine
import "strings"
type CommentMode int
const (
CommentsFollow CommentMode = iota
CommentsPrecede
CommentsStandalone
)
type Engine struct {
CommentMode CommentMode
}
func (e *Engine) Format(events []LineEvent) []string {
resultLines := make([]string, 0, len(events))
previousWasOpenBrace := false
previousStatementType := ""
previousWasComment := false
previousWasTopLevel := false
previousWasScoped := false
for eventIndex, event := range events {
if event.InRawString {
resultLines = append(resultLines, event.Content)
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
if len(resultLines) > 0 && !previousWasOpenBrace && !event.IsClosingBrace && !event.IsCaseLabel {
if currentIsTopLevel && previousWasTopLevel && currentStatementType != previousStatementType {
if !(e.CommentMode == CommentsFollow && previousWasComment) {
needsBlankLine = true
}
} else if event.HasASTInfo && (currentIsScoped || previousWasScoped) {
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 {
next := events[nextIndex]
if next.HasASTInfo {
nextIsTopLevel := next.IsTopLevel
nextIsScoped := next.IsScoped
if nextIsTopLevel && previousWasTopLevel && next.StatementType != previousStatementType {
needsBlankLine = true
} else if nextIsScoped || previousWasScoped {
needsBlankLine = true
} else if next.StatementType != "" && previousStatementType != "" && next.StatementType != previousStatementType {
needsBlankLine = true
}
}
}
}
}
if needsBlankLine {
resultLines = append(resultLines, "")
}
resultLines = append(resultLines, event.Content)
previousWasOpenBrace = event.IsOpeningBrace || event.IsCaseLabel
previousWasComment = event.IsCommentOnly
if event.HasASTInfo {
previousStatementType = event.StatementType
previousWasTopLevel = event.IsTopLevel
previousWasScoped = event.IsScoped
} else if currentStatementType != "" {
previousStatementType = currentStatementType
previousWasTopLevel = false
previousWasScoped = false
}
}
return resultLines
}
func (e *Engine) FormatToString(events []LineEvent) string {
lines := e.Format(events)
output := strings.Join(lines, "\n")
if !strings.HasSuffix(output, "\n") {
output += "\n"
}
return output
}
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
}
|