-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathformat_composite_literals.go
82 lines (81 loc) · 1.58 KB
/
format_composite_literals.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
package valast
func formatCompositeLiterals(input []rune) []rune {
var (
inStringLiteral, inRawStringLiteral bool
depth int
breakFields bool
lineWidth int
result []rune
)
for i, r := range input {
switch {
case inStringLiteral || inRawStringLiteral:
// Reading a string literal.
switch {
case inStringLiteral:
if r == '"' && (i == 0 || input[i-1] != '\\') {
inStringLiteral = false
}
case inRawStringLiteral:
if r == '`' {
inRawStringLiteral = false
}
}
if r == '\n' {
depth = 0
lineWidth = 0
} else {
lineWidth++
}
result = append(result, r)
default:
if r == '"' {
inStringLiteral = true
result = append(result, r)
break
}
if r == '`' {
inRawStringLiteral = true
result = append(result, r)
break
}
if r == '\n' {
depth = 0
lineWidth = 0
} else {
lineWidth++
}
if lineWidth >= 50 {
breakFields = true
}
if r == ',' && breakFields {
result = append(result, r)
result = append(result, '\n')
break
}
if r == '{' {
depth++
if depth >= 2 {
depth = 0
breakFields = true
result = append(result, r)
result = append(result, '\n')
break
}
}
if r == '}' {
depth--
if depth >= 2 {
depth = 0
breakFields = false
result = append(result, r)
result = append(result, ',')
result = append(result, '\n')
break
}
}
result = append(result, r)
}
}
return result
}