-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathformat.go
More file actions
220 lines (196 loc) · 6.3 KB
/
Copy pathformat.go
File metadata and controls
220 lines (196 loc) · 6.3 KB
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
// This file was ported from https://github.com/Microsoft/vscode/blob/c0bc1ace7ca3ce2d6b1aeb2bde9d1bb0f4b4bae6/src/vs/base/common/jsonFormatter.ts,
// which is licensed as follows:
//
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.
package jsonx
import (
"strings"
)
// FormatOptions specifies formatting options.
//
// Source: https://github.com/Microsoft/vscode/blob/c0bc1ace7ca3ce2d6b1aeb2bde9d1bb0f4b4bae6/src/vs/base/common/jsonFormatter.ts#L9
type FormatOptions struct {
TabSize int // If indentation is based on spaces (InsertSpaces == true), then what is the number of spaces that make an indent?
InsertSpaces bool // Is indentation based on spaces?
EOL string // The default end of line line character
}
// Format returns edits that format the entire JSON document according to the format
// options. To apply the edits and obtain the formatted document content, use ApplyEdits.
func Format(text string, options FormatOptions) []Edit {
return FormatRange(text, 0, len([]rune(text)), options)
}
// FormatRange returns edits that format the JSON document (starting at the character
// offset and continuing for the character length) according to the format options.
// To apply the edits and obtain the formatted document content, use ApplyEdits.
//
// Source: https://github.com/Microsoft/vscode/blob/c0bc1ace7ca3ce2d6b1aeb2bde9d1bb0f4b4bae6/src/vs/base/common/jsonFormatter.ts#L41
func FormatRange(text string, offset, length int, options FormatOptions) []Edit {
chars := []rune(text)
rangeStart := offset
rangeEnd := rangeStart + length
for rangeStart > 0 && !isEOL(chars, rangeStart-1) {
rangeStart--
}
{
scanner := NewScanner(text, ScanOptions{Trivia: false})
scanner.SetPosition(rangeEnd)
scanner.Scan()
rangeEnd = scanner.Pos()
}
value := chars[rangeStart:rangeEnd]
initialIndentLevel := computeIndentLevel(value, 0, options)
eol := getEOL(options, chars)
lineBreak := false
indentValue := ""
if options.InsertSpaces {
indentValue = strings.Repeat(" ", options.TabSize)
} else {
indentValue = "\t"
}
scanner := NewScanner(string(value), ScanOptions{Trivia: true})
formatter := formatter{
input: chars,
scanner: scanner,
eol: eol,
indentLevel: 0,
indentValue: indentValue,
initialIndentLevel: initialIndentLevel,
lineBreak: lineBreak,
}
return formatter.format(rangeStart, rangeEnd)
}
type formatter struct {
input []rune
scanner *Scanner
eol string
indentLevel int
indentValue string
initialIndentLevel int
lineBreak bool
edits []Edit
}
func (f *formatter) format(rangeStart, rangeEnd int) (editOperations []Edit) {
firstToken := f.scanNext()
if firstToken != EOF {
firstTokenStart := f.scanner.TokenOffset() + rangeStart
initialIndent := strings.Repeat(f.indentValue, f.initialIndentLevel)
f.addEdit(initialIndent, rangeStart, firstTokenStart)
}
for firstToken != EOF {
firstTokenEnd := f.scanner.TokenOffset() + f.scanner.TokenLength() + rangeStart
secondToken := f.scanNext()
replaceContent := ""
for !f.lineBreak && (secondToken == LineCommentTrivia || secondToken == BlockCommentTrivia) {
// comments on the same line: keep them on the same line, but ignore them otherwise
commentTokenStart := f.scanner.TokenOffset() + rangeStart
f.addEdit(" ", firstTokenEnd, commentTokenStart)
firstTokenEnd = f.scanner.TokenOffset() + f.scanner.TokenLength() + rangeStart
if secondToken == LineCommentTrivia {
replaceContent = f.newLineAndIndent()
} else {
replaceContent = ""
}
secondToken = f.scanNext()
}
if secondToken == CloseBraceToken {
if firstToken != OpenBraceToken {
f.indentLevel--
replaceContent = f.newLineAndIndent()
}
} else if secondToken == CloseBracketToken {
if firstToken != OpenBracketToken {
f.indentLevel--
replaceContent = f.newLineAndIndent()
}
} else if secondToken != EOF {
switch firstToken {
case OpenBracketToken, OpenBraceToken:
f.indentLevel++
replaceContent = f.newLineAndIndent()
case CommaToken, LineCommentTrivia:
replaceContent = f.newLineAndIndent()
break
case BlockCommentTrivia:
if f.lineBreak {
replaceContent = f.newLineAndIndent()
} else {
// symbol following comment on the same line: keep on same line, separate with ' '
replaceContent = " "
}
case ColonToken:
replaceContent = " "
case NullKeyword, TrueKeyword, FalseKeyword, NumericLiteral:
if secondToken == NullKeyword || secondToken == FalseKeyword || secondToken == NumericLiteral {
replaceContent = " "
}
}
if f.lineBreak && (secondToken == LineCommentTrivia || secondToken == BlockCommentTrivia) {
replaceContent = f.newLineAndIndent()
}
}
secondTokenStart := f.scanner.TokenOffset() + rangeStart
f.addEdit(replaceContent, firstTokenEnd, secondTokenStart)
firstToken = secondToken
}
return f.edits
}
func (f *formatter) newLineAndIndent() string {
n := f.initialIndentLevel + f.indentLevel
if n < 0 {
n = 0
}
return f.eol + strings.Repeat(f.indentValue, n)
}
func (f *formatter) scanNext() SyntaxKind {
token := f.scanner.Scan()
f.lineBreak = false
for token == Trivia || token == LineBreakTrivia {
f.lineBreak = f.lineBreak || (token == LineBreakTrivia)
token = f.scanner.Scan()
}
return token
}
func (f *formatter) addEdit(text string, startOffset, endOffset int) {
if string(f.input[startOffset:endOffset]) != text {
f.edits = append(f.edits, Edit{Offset: startOffset, Length: endOffset - startOffset, Content: text})
}
}
func computeIndentLevel(chars []rune, offset int, options FormatOptions) int {
i := 0
nChars := 0
tabSize := options.TabSize
if tabSize == 0 {
tabSize = 4
}
for i < len(chars) {
ch := chars[i]
if ch == ' ' {
nChars++
} else if ch == '\t' {
nChars += tabSize
} else {
break
}
i++
}
return nChars / tabSize
}
func getEOL(options FormatOptions, chars []rune) string {
for i, ch := range chars {
if ch == '\r' {
if i+1 < len(chars) && chars[i+1] == '\n' {
return "\r\n"
}
return "\r"
} else if ch == '\n' {
return "\n"
}
}
if options.EOL != "" {
return options.EOL
}
return "\n"
}
func isEOL(chars []rune, offset int) bool {
return chars[offset] == '\r' || chars[offset] == '\n'
}