/
argument.go
218 lines (197 loc) · 4.5 KB
/
argument.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
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
package rules
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"math"
"sort"
"strings"
)
// Section is an interface offering general APIs of argument collections
type Section interface {
// CheckOrder checks whether the arguments in the collection is sorted
CheckOrder() bool
// ToString prints arguments in the collection in order
ToString() string
// GetRange returns the entire range of the argument collection
GetRange() *hcl.Range
}
func toString(sections ...Section) string {
var lines []string
for _, section := range sections {
line := section.ToString()
if line != "" {
lines = append(lines, line)
}
}
return strings.Join(lines, "\n")
}
func mergeRange(sections ...Section) *hcl.Range {
start := hcl.Pos{Line: math.MaxInt}
end := hcl.Pos{Line: -1}
filename := ""
isNil := true
for _, section := range sections {
r := section.GetRange()
if r == nil {
continue
}
isNil = false
if filename == "" {
filename = r.Filename
}
if r.Start.Line < start.Line {
start = r.Start
}
if r.End.Line > end.Line {
end = r.End
}
}
if isNil {
return nil
}
return &hcl.Range{
Filename: filename,
Start: start,
End: end,
}
}
// Arg is a wrapper of the attribute
type Arg struct {
Name string
Range hcl.Range
File *hcl.File
}
// ToString prints the arg content
func (a *Arg) ToString() string {
return string(hclwrite.Format(a.Range.SliceBytes(a.File.Bytes)))
}
// Args is the collection of args with the same type
type Args struct {
Args []*Arg
Range *hcl.Range
}
// CheckOrder checks whether this type of args are sorted
func (a *Args) CheckOrder() bool {
if a == nil {
return true
}
var name *string
for _, arg := range a.Args {
if name != nil && *name > arg.Name {
return false
}
name = &arg.Name
}
return true
}
// ToString prints this type of args in order
func (a *Args) ToString() string {
if a == nil {
return ""
}
sortedArgs := make([]*Arg, len(a.Args))
copy(sortedArgs, a.Args)
sort.Slice(sortedArgs, func(i, j int) bool {
return sortedArgs[i].Name < sortedArgs[j].Name
})
var lines []string
for _, arg := range sortedArgs {
lines = append(lines, arg.ToString())
}
return string(hclwrite.Format([]byte(strings.Join(lines, "\n"))))
}
// GetRange returns the entire range of this type of args
func (a *Args) GetRange() *hcl.Range {
if a == nil {
return nil
}
return a.Range
}
// HeadMetaArgs is the collection of head meta args
type HeadMetaArgs struct {
Args []*Arg
Range *hcl.Range
}
// CheckOrder checks whether the head meta args are sorted
func (a *HeadMetaArgs) CheckOrder() bool {
if a == nil {
return true
}
score := math.MaxInt
for _, arg := range a.Args {
if score < headMetaArgPriority[arg.Name] {
return false
}
score = headMetaArgPriority[arg.Name]
}
return true
}
// ToString prints the head meta args in order
func (a *HeadMetaArgs) ToString() string {
if a == nil {
return ""
}
sortedArgs := make([]*Arg, len(a.Args))
copy(sortedArgs, a.Args)
sort.Slice(sortedArgs, func(i, j int) bool {
return headMetaArgPriority[sortedArgs[i].Name] > headMetaArgPriority[sortedArgs[j].Name]
})
var lines []string
for _, arg := range sortedArgs {
lines = append(lines, arg.ToString())
}
return string(hclwrite.Format([]byte(strings.Join(lines, "\n"))))
}
// GetRange returns the entire range of head meta args
func (a *HeadMetaArgs) GetRange() *hcl.Range {
if a == nil {
return nil
}
return a.Range
}
func (a *Args) add(arg *Arg) {
a.Args = append(a.Args, arg)
a.updateRange(arg)
}
func (a *Args) updateRange(arg *Arg) {
if a.Range == nil {
a.Range = &hcl.Range{
Filename: arg.Range.Filename,
Start: hcl.Pos{Line: math.MaxInt},
End: hcl.Pos{Line: -1},
}
}
if a.Range.Start.Line > arg.Range.Start.Line {
a.Range.Start = arg.Range.Start
}
if a.Range.End.Line < arg.Range.End.Line {
a.Range.End = arg.Range.End
}
}
func (a *HeadMetaArgs) add(arg *Arg) {
a.Args = append(a.Args, arg)
a.updateRange(arg)
}
func (a *HeadMetaArgs) updateRange(arg *Arg) {
if a.Range == nil {
a.Range = &hcl.Range{
Filename: arg.Range.Filename,
Start: hcl.Pos{Line: math.MaxInt},
End: hcl.Pos{Line: -1},
}
}
if a.Range.Start.Line > arg.Range.Start.Line {
a.Range.Start = arg.Range.Start
}
if a.Range.End.Line < arg.Range.End.Line {
a.Range.End = arg.Range.End
}
}
func buildAttrArg(attr *hclsyntax.Attribute, file *hcl.File) *Arg {
return &Arg{
Name: attr.Name,
Range: attr.SrcRange,
File: file,
}
}