-
Notifications
You must be signed in to change notification settings - Fork 32
/
definition.go
203 lines (175 loc) · 5.46 KB
/
definition.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
package editorconfig
import (
"errors"
"fmt"
"strconv"
"strings"
"golang.org/x/mod/semver"
"gopkg.in/ini.v1"
)
// Definition represents a definition inside the .editorconfig file.
// E.g. a section of the file.
// The definition is composed of the selector ("*", "*.go", "*.{js.css}", etc),
// plus the properties of the selected files.
type Definition struct {
Selector string `ini:"-" json:"-"`
Charset string `ini:"charset" json:"charset,omitempty"`
IndentStyle string `ini:"indent_style" json:"indent_style,omitempty"`
IndentSize string `ini:"indent_size" json:"indent_size,omitempty"`
TabWidth int `ini:"-" json:"-"`
EndOfLine string `ini:"end_of_line" json:"end_of_line,omitempty"`
TrimTrailingWhitespace *bool `ini:"-" json:"-"`
InsertFinalNewline *bool `ini:"-" json:"-"`
Raw map[string]string `ini:"-" json:"-"`
version string
}
// NewDefinition builds a definition from a given config.
func NewDefinition(config Config) (*Definition, error) {
return config.Load(config.Path)
}
// normalize fixes some values to their lowercase value.
func (d *Definition) normalize() error {
var result error
d.Charset = strings.ToLower(d.Charset)
d.EndOfLine = strings.ToLower(d.Raw["end_of_line"])
d.IndentStyle = strings.ToLower(d.Raw["indent_style"])
trimTrailingWhitespace, ok := d.Raw["trim_trailing_whitespace"]
if ok && trimTrailingWhitespace != UnsetValue {
trim, err := strconv.ParseBool(trimTrailingWhitespace)
if err != nil {
result = errors.Join(
result,
fmt.Errorf("trim_trailing_whitespace=%s is not an acceptable value. %w", trimTrailingWhitespace, err),
)
} else {
d.TrimTrailingWhitespace = &trim
}
}
insertFinalNewline, ok := d.Raw["insert_final_newline"]
if ok && insertFinalNewline != UnsetValue {
insert, err := strconv.ParseBool(insertFinalNewline)
if err != nil {
result = errors.Join(
result,
fmt.Errorf("insert_final_newline=%s is not an acceptable value. %w", insertFinalNewline, err),
)
} else {
d.InsertFinalNewline = &insert
}
}
// tab_width from Raw
tabWidth, ok := d.Raw["tab_width"]
if ok && tabWidth != UnsetValue {
num, err := strconv.Atoi(tabWidth)
if err != nil {
result = errors.Join(result, fmt.Errorf("tab_width=%s is not an acceptable value. %w", tabWidth, err))
} else {
d.TabWidth = num
}
}
// tab_width defaults to indent_size:
// https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#tab_width
num, err := strconv.Atoi(d.IndentSize)
if err == nil && d.TabWidth <= 0 {
d.TabWidth = num
}
return result
}
// merge the parent definition into the child definition.
func (d *Definition) merge(md *Definition) {
if len(d.Charset) == 0 {
d.Charset = md.Charset
}
if len(d.IndentStyle) == 0 {
d.IndentStyle = md.IndentStyle
}
if len(d.IndentSize) == 0 {
d.IndentSize = md.IndentSize
}
if d.TabWidth <= 0 {
d.TabWidth = md.TabWidth
}
if len(d.EndOfLine) == 0 {
d.EndOfLine = md.EndOfLine
}
if trimTrailingWhitespace, ok := d.Raw["trim_trailing_whitespace"]; !ok || trimTrailingWhitespace != UnsetValue {
if d.TrimTrailingWhitespace == nil {
d.TrimTrailingWhitespace = md.TrimTrailingWhitespace
}
}
if insertFinalNewline, ok := d.Raw["insert_final_newline"]; !ok || insertFinalNewline != UnsetValue {
if d.InsertFinalNewline == nil {
d.InsertFinalNewline = md.InsertFinalNewline
}
}
for k, v := range md.Raw {
if _, ok := d.Raw[k]; !ok {
d.Raw[k] = v
}
}
}
// InsertToIniFile writes the definition into a ini file.
func (d *Definition) InsertToIniFile(iniFile *ini.File) { //nolint:funlen,gocognit,cyclop
iniSec := iniFile.Section(d.Selector)
for k, v := range d.Raw {
switch k {
case "insert_final_newline":
if d.InsertFinalNewline != nil {
v = strconv.FormatBool(*d.InsertFinalNewline)
} else {
insertFinalNewline, ok := d.Raw["insert_final_newline"]
if !ok {
break
}
v = strings.ToLower(insertFinalNewline)
}
case "trim_trailing_whitespace":
if d.TrimTrailingWhitespace != nil {
v = strconv.FormatBool(*d.TrimTrailingWhitespace)
} else {
trimTrailingWhitespace, ok := d.Raw["trim_trailing_whitespace"]
if !ok {
break
}
v = strings.ToLower(trimTrailingWhitespace)
}
case "charset":
v = d.Charset
case "end_of_line":
v = d.EndOfLine
case "indent_style":
v = d.IndentStyle
case "tab_width":
tabWidth, ok := d.Raw["tab_width"]
if ok && tabWidth == UnsetValue {
v = tabWidth
} else {
v = strconv.Itoa(d.TabWidth)
}
case "indent_size":
v = d.IndentSize
}
iniSec.NewKey(k, v) //nolint:errcheck
}
if _, ok := d.Raw["indent_size"]; !ok {
tabWidth, ok := d.Raw["tab_width"]
switch {
case ok && tabWidth == UnsetValue:
// do nothing
case d.TabWidth > 0:
iniSec.NewKey("indent_size", strconv.Itoa(d.TabWidth)) //nolint:errcheck
case d.IndentStyle == IndentStyleTab && (d.version == "" || semver.Compare(d.version, "v0.9.0") >= 0):
iniSec.NewKey("indent_size", IndentStyleTab) //nolint:errcheck
}
}
if _, ok := d.Raw["tab_width"]; !ok {
if d.IndentSize == UnsetValue {
iniSec.NewKey("tab_width", d.IndentSize) //nolint:errcheck
} else {
_, err := strconv.Atoi(d.IndentSize)
if err == nil {
iniSec.NewKey("tab_width", d.Raw["indent_size"]) //nolint:errcheck
}
}
}
}