Debian Codegraph @

definition.go

raw symbols SCIP
1 package editorconfig
2
3 import (
4 "errors"
5 "fmt"
6 "strconv"
7 "strings"
8
9 "golang.org/x/mod/semver"
10 "gopkg.in/ini.v1"
11 )
12
13 // Definition represents a definition inside the .editorconfig file.
14 // E.g. a section of the file.
15 // The definition is composed of the selector ("*", "*.go", "*.{js.css}", etc),
16 // plus the properties of the selected files.
17 type Definition struct {
18 Selector string `ini:"-" json:"-"`
19
20 Charset string `ini:"charset" json:"charset,omitempty"`
21 IndentStyle string `ini:"indent_style" json:"indent_style,omitempty"`
22 IndentSize string `ini:"indent_size" json:"indent_size,omitempty"`
23 TabWidth int `ini:"-" json:"-"`
24 EndOfLine string `ini:"end_of_line" json:"end_of_line,omitempty"`
25 TrimTrailingWhitespace *bool `ini:"-" json:"-"`
26 InsertFinalNewline *bool `ini:"-" json:"-"`
27 Raw map[string]string `ini:"-" json:"-"`
28 version string
29 }
30
31 // NewDefinition builds a definition from a given config.
32 func NewDefinition(config Config) (*Definition, error) {
33 return config.Load(config.Path)
34 }
35
36 // InsertToIniFile writes the definition into a ini file.
37 func (d *Definition) InsertToIniFile(iniFile *ini.File) { //nolint:funlen,gocognit,cyclop
38 iniSec := iniFile.Section(d.Selector)
39
40 for k, v := range d.Raw {
41 switch k {
42 case "insert_final_newline":
43 if d.InsertFinalNewline != nil {
44 v = strconv.FormatBool(*d.InsertFinalNewline)
45 } else {
46 insertFinalNewline, ok := d.Raw["insert_final_newline"]
47 if !ok {
48 break
49 }
50
51 v = strings.ToLower(insertFinalNewline)
52 }
53 case "trim_trailing_whitespace":
54 if d.TrimTrailingWhitespace != nil {
55 v = strconv.FormatBool(*d.TrimTrailingWhitespace)
56 } else {
57 trimTrailingWhitespace, ok := d.Raw["trim_trailing_whitespace"]
58 if !ok {
59 break
60 }
61
62 v = strings.ToLower(trimTrailingWhitespace)
63 }
64 case "charset":
65 v = d.Charset
66 case "end_of_line":
67 v = d.EndOfLine
68 case "indent_style":
69 v = d.IndentStyle
70 case "tab_width":
71 tabWidth, ok := d.Raw["tab_width"]
72 if ok && tabWidth == UnsetValue {
73 v = tabWidth
74 } else {
75 v = strconv.Itoa(d.TabWidth)
76 }
77 case "indent_size":
78 v = d.IndentSize
79 }
80
81 iniSec.NewKey(k, v) //nolint:errcheck
82 }
83
84 if _, ok := d.Raw["indent_size"]; !ok {
85 tabWidth, ok := d.Raw["tab_width"]
86
87 switch {
88 case ok && tabWidth == UnsetValue:
89 // do nothing
90 case d.TabWidth > 0:
91 iniSec.NewKey("indent_size", strconv.Itoa(d.TabWidth)) //nolint:errcheck
92 case d.IndentStyle == IndentStyleTab && (d.version == "" || semver.Compare(d.version, "v0.9.0") >= 0):
93 iniSec.NewKey("indent_size", IndentStyleTab) //nolint:errcheck
94 }
95 }
96
97 if _, ok := d.Raw["tab_width"]; !ok {
98 if d.IndentSize == UnsetValue {
99 iniSec.NewKey("tab_width", d.IndentSize) //nolint:errcheck
100 } else {
101 _, err := strconv.Atoi(d.IndentSize)
102 if err == nil {
103 iniSec.NewKey("tab_width", d.Raw["indent_size"]) //nolint:errcheck
104 }
105 }
106 }
107 }
108
109 // normalize fixes some values to their lowercase value.
110 func (d *Definition) normalize() error {
111 var result error
112
113 d.Charset = strings.ToLower(d.Charset)
114 d.EndOfLine = strings.ToLower(d.Raw["end_of_line"])
115 d.IndentStyle = strings.ToLower(d.Raw["indent_style"])
116
117 trimTrailingWhitespace, ok := d.Raw["trim_trailing_whitespace"]
118 if ok && trimTrailingWhitespace != UnsetValue {
119 trim, err := strconv.ParseBool(trimTrailingWhitespace)
120 if err != nil {
121 result = errors.Join(
122 result,
123 fmt.Errorf("trim_trailing_whitespace=%s is not an acceptable value. %w", trimTrailingWhitespace, err),
124 )
125 } else {
126 d.TrimTrailingWhitespace = &trim
127 }
128 }
129
130 insertFinalNewline, ok := d.Raw["insert_final_newline"]
131 if ok && insertFinalNewline != UnsetValue {
132 insert, err := strconv.ParseBool(insertFinalNewline)
133 if err != nil {
134 result = errors.Join(
135 result,
136 fmt.Errorf("insert_final_newline=%s is not an acceptable value. %w", insertFinalNewline, err),
137 )
138 } else {
139 d.InsertFinalNewline = &insert
140 }
141 }
142
143 // tab_width from Raw
144 tabWidth, ok := d.Raw["tab_width"]
145 if ok && tabWidth != UnsetValue {
146 num, err := strconv.Atoi(tabWidth)
147 if err != nil {
148 result = errors.Join(result, fmt.Errorf("tab_width=%s is not an acceptable value. %w", tabWidth, err))
149 } else {
150 d.TabWidth = num
151 }
152 }
153
154 // tab_width defaults to indent_size:
155 // https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#tab_width
156 num, err := strconv.Atoi(d.IndentSize)
157 if err == nil && d.TabWidth <= 0 {
158 d.TabWidth = num
159 }
160
161 return result
162 }
163
164 // merge the parent definition into the child definition.
165 func (d *Definition) merge(md *Definition) {
166 if len(d.Charset) == 0 {
167 d.Charset = md.Charset
168 }
169
170 if len(d.IndentStyle) == 0 {
171 d.IndentStyle = md.IndentStyle
172 }
173
174 if len(d.IndentSize) == 0 {
175 d.IndentSize = md.IndentSize
176 }
177
178 if d.TabWidth <= 0 {
179 d.TabWidth = md.TabWidth
180 }
181
182 if len(d.EndOfLine) == 0 {
183 d.EndOfLine = md.EndOfLine
184 }
185
186 if trimTrailingWhitespace, ok := d.Raw["trim_trailing_whitespace"]; !ok || trimTrailingWhitespace != UnsetValue {
187 if d.TrimTrailingWhitespace == nil {
188 d.TrimTrailingWhitespace = md.TrimTrailingWhitespace
189 }
190 }
191
192 if insertFinalNewline, ok := d.Raw["insert_final_newline"]; !ok || insertFinalNewline != UnsetValue {
193 if d.InsertFinalNewline == nil {
194 d.InsertFinalNewline = md.InsertFinalNewline
195 }
196 }
197
198 for k, v := range md.Raw {
199 if _, ok := d.Raw[k]; !ok {
200 d.Raw[k] = v
201 }
202 }
203 }