| 1 | // Copyright (c) 2019, Daniel Martà <[email protected]> |
| 2 | // See LICENSE for licensing information |
| 3 | |
| 4 | // Package editorconfig allows parsing and using EditorConfig files, as defined |
| 5 | // in https://editorconfig.org/. |
| 6 | package editorconfigscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/ |
| 7 | |
| 8 | import ( |
| 9 | "bufioscip-go gomod github.com/golang/go/src go1.22 bufio/" |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.22 fmt/" |
| 11 | "ioscip-go gomod github.com/golang/go/src go1.22 io/" |
| 12 | "osscip-go gomod github.com/golang/go/src go1.22 os/" |
| 13 | "path/filepathscip-go gomod github.com/golang/go/src go1.22 `path/filepath`/" |
| 14 | "regexpscip-go gomod github.com/golang/go/src go1.22 regexp/" |
| 15 | "strconvscip-go gomod github.com/golang/go/src go1.22 strconv/" |
| 16 | "stringsscip-go gomod github.com/golang/go/src go1.22 strings/" |
| 17 | ) |
| 18 | |
| 19 | const DefaultNamescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/DefaultName. = ".editorconfig" |
| 20 | |
| 21 | // File is an EditorConfig file with a number of sections. |
| 22 | type Filescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File# struct { |
| 23 | Rootscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Root. bool |
| 24 | Sectionsscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Sections. []Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section# |
| 25 | } |
| 26 | |
| 27 | // Section is a single EditorConfig section, which applies a number of |
| 28 | // properties to the filenames matching it. |
| 29 | type Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section# struct { |
| 30 | // Name is the section's name. Usually, this will be a valid pattern |
| 31 | // matching string, such as "[*.go]" without the square brackets. |
| 32 | // |
| 33 | // It may also describe a language such as "[[shell]]", |
| 34 | // although this is an out-of-spec feature that may be changed at any time. |
| 35 | Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name. string |
| 36 | |
| 37 | // Properties is the list of name-value properties contained by a |
| 38 | // section. It is kept in increasing order, to allow binary searches. |
| 39 | Propertiesscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Properties. []Propertyscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property# |
| 40 | |
| 41 | // TODO: properties are not actually kept in increasing order |
| 42 | } |
| 43 | |
| 44 | // Property is a single property with a name and a value, which can be |
| 45 | // represented as a single line like "indent_size=8". |
| 46 | type Propertyscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property# struct { |
| 47 | // Name is always lowercase and allows identifying a property. |
| 48 | Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Name. string |
| 49 | // Value holds data for a property. |
| 50 | Valuescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Value. string |
| 51 | } |
| 52 | |
| 53 | // String turns a property into its INI format. |
| 54 | func (p Propertyscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#) Stringscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#String().() string { return fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.22 fmt/Sprintf().("%s=%s", p.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Name., p.Valuescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Value.) } |
| 55 | |
| 56 | // String turns a file into its INI format. |
| 57 | func (f *Filescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#) Stringscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#String().() string { |
| 58 | var b stringsscip-go gomod github.com/golang/go/src go1.22 strings/.Builderscip-go gomod github.com/golang/go/src go1.22 strings/Builder# |
| 59 | if f.Rootscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Root. { |
| 60 | fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.22 fmt/Fprintf().(&b, "root=true\n\n") |
| 61 | } |
| 62 | for i, section := range f.Sectionsscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Sections. { |
| 63 | if i > 0 { |
| 64 | fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.22 fmt/Fprintln().(&b) |
| 65 | } |
| 66 | fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.22 fmt/Fprintf().(&b, "[%s]\n", section.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name.) |
| 67 | for _, prop := range section.Propertiesscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Properties. { |
| 68 | fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.22 fmt/Fprintf().(&b, "%s=%s\n", prop.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Name., prop.Valuescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Value.) |
| 69 | } |
| 70 | } |
| 71 | return b.Stringscip-go gomod github.com/golang/go/src go1.22 strings/Builder#String().() |
| 72 | } |
| 73 | |
| 74 | // Lookup finds a property by its name within a section and returns a pointer to |
| 75 | // it, or nil if no such property exists. |
| 76 | // |
| 77 | // Note that most of the time, Get should be used instead. |
| 78 | func (s Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#) Lookupscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Lookup().(name string) *Propertyscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property# { |
| 79 | // TODO: binary search |
| 80 | for i, prop := range s.Propertiesscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Properties. { |
| 81 | if prop.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Name. == name { |
| 82 | return &s.Propertiesscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Properties.[i] |
| 83 | } |
| 84 | } |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | // Get returns the value of a property found by its name. If no such property |
| 89 | // exists, an empty string is returned. |
| 90 | func (s Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#) Getscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Get().(name string) string { |
| 91 | if prop := s.Lookupscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Lookup().(name); prop != nil { |
| 92 | return prop.Valuescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Value. |
| 93 | } |
| 94 | return "" |
| 95 | } |
| 96 | |
| 97 | // IndentSize is a shortcut for Get("indent_size") as an int. |
| 98 | func (s Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#) IndentSizescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#IndentSize().() int { |
| 99 | n, _ := strconvscip-go gomod github.com/golang/go/src go1.22 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.22 strconv/Atoi().(s.Getscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Get().("indent_size")) |
| 100 | return n |
| 101 | } |
| 102 | |
| 103 | // IndentSize is a shortcut for Get("trim_trailing_whitespace") as a bool. |
| 104 | func (s Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#) TrimTrailingWhitespacescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#TrimTrailingWhitespace().() bool { |
| 105 | return s.Getscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Get().("trim_trailing_whitespace") == "true" |
| 106 | } |
| 107 | |
| 108 | // IndentSize is a shortcut for Get("insert_final_newline") as a bool. |
| 109 | func (s Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#) InsertFinalNewlinescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#InsertFinalNewline().() bool { |
| 110 | return s.Getscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Get().("insert_final_newline") == "true" |
| 111 | } |
| 112 | |
| 113 | // IndentSize is similar to Get("indent_size"), but it handles the "tab" default |
| 114 | // and returns an int. When unset, it returns 0. |
| 115 | func (s Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#) TabWidthscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#TabWidth().() int { |
| 116 | value := s.Getscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Get().("indent_size") |
| 117 | if value == "tab" { |
| 118 | value = s.Getscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Get().("tab_width") |
| 119 | } |
| 120 | n, _ := strconvscip-go gomod github.com/golang/go/src go1.22 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.22 strconv/Atoi().(value) |
| 121 | return n |
| 122 | } |
| 123 | |
| 124 | // Add introduces a number of properties to the section. Properties that were |
| 125 | // already part of the section are ignored. |
| 126 | func (s *Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#) Addscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Add().(properties ...Propertyscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#) { |
| 127 | for _, prop := range properties { |
| 128 | if s.Lookupscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Lookup().(prop.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Name.) == nil { |
| 129 | s.Propertiesscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Properties. = append(s.Propertiesscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Properties., prop) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // String turns a section into its INI format. |
| 135 | func (s Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#) Stringscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#String().() string { |
| 136 | var b stringsscip-go gomod github.com/golang/go/src go1.22 strings/.Builderscip-go gomod github.com/golang/go/src go1.22 strings/Builder# |
| 137 | if s.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name. != "" { |
| 138 | fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.22 fmt/Fprintf().(&b, "[%s]\n", s.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name.) |
| 139 | } |
| 140 | for _, prop := range s.Propertiesscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Properties. { |
| 141 | fmtscip-go gomod github.com/golang/go/src go1.22 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.22 fmt/Fprintf().(&b, "%s=%s\n", prop.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Name., prop.Valuescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Value.) |
| 142 | } |
| 143 | return b.Stringscip-go gomod github.com/golang/go/src go1.22 strings/Builder#String().() |
| 144 | } |
| 145 | |
| 146 | // Filter returns the set of properties in f which apply to a file |
| 147 | // given its name and optional languages. |
| 148 | // Properties from later sections take precedence. The name should be a path |
| 149 | // relative to the directory holding the EditorConfig. |
| 150 | // |
| 151 | // If cache is non-nil, the map will be used to reuse patterns translated and |
| 152 | // compiled to regular expressions. |
| 153 | // |
| 154 | // Note that this function doesn't apply defaults; for that, see Find. |
| 155 | // |
| 156 | // Note that, since the EditorConfig spec doesn't allow backslashes as path |
| 157 | // separators, backslashes in name are converted to forward slashes. |
| 158 | func (f *Filescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#) Filterscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Filter().(name string, languages []string, cache map[string]*regexpscip-go gomod github.com/golang/go/src go1.22 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.22 regexp/Regexp#) Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section# { |
| 159 | name = filepathscip-go gomod github.com/golang/go/src go1.22 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.22 `path/filepath`/ToSlash().(name) |
| 160 | result := Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#{} |
| 161 | for i := len(f.Sectionsscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Sections.) - 1; i >= 0; i-- { |
| 162 | section := f.Sectionsscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Sections.[i] |
| 163 | |
| 164 | if len(section.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name.) > 2 && section.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name.[0] == '[' && section.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name.[len(section.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name.)-1] == ']' { |
| 165 | sectionLang := section.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name.[1 : len(section.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name.)-1] |
| 166 | for _, language := range languages { |
| 167 | if language == sectionLang { |
| 168 | result.Addscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Add().(section.Propertiesscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Properties....) |
| 169 | break |
| 170 | } |
| 171 | } |
| 172 | continue |
| 173 | } |
| 174 | |
| 175 | rx := cache[section.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name.] |
| 176 | if rx == nil { |
| 177 | rx = toRegexpscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/toRegexp().(section.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name.) |
| 178 | if cache != nil { |
| 179 | cache[section.Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name.] = rx |
| 180 | } |
| 181 | } |
| 182 | if rx.MatchStringscip-go gomod github.com/golang/go/src go1.22 regexp/Regexp#MatchString().(name) { |
| 183 | result.Addscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Add().(section.Propertiesscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Properties....) |
| 184 | } |
| 185 | } |
| 186 | return result |
| 187 | } |
| 188 | |
| 189 | // Find figures out the properties that apply to a file name on disk, and |
| 190 | // returns them as a section. The name doesn't need to be an absolute path. |
| 191 | // |
| 192 | // It is equivalent to Query{}.Find; please note that no caching at all takes |
| 193 | // place in this mode. |
| 194 | func Findscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Find().(name string, languages []string) (Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#, error) { |
| 195 | return Queryscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#{}.Findscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#Find().(name, languages) |
| 196 | } |
| 197 | |
| 198 | // Query allows fine-grained control of how EditorConfig files are found and |
| 199 | // used. It also attempts to cache and reuse work, which makes its Find method |
| 200 | // significantly faster when used on many files. |
| 201 | type Queryscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query# struct { |
| 202 | // ConfigName specifies what EditorConfig file name to use when |
| 203 | // searching for files on disk. If empty, it defaults to DefaultName. |
| 204 | ConfigNamescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#ConfigName. string |
| 205 | |
| 206 | // FileCache keeps track of which directories are known to contain an |
| 207 | // EditorConfig. Existing entries which are nil mean that the directory |
| 208 | // is known to not contain an EditorConfig. |
| 209 | // |
| 210 | // If nil, no caching takes place. |
| 211 | FileCachescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#FileCache. map[string]*Filescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File# |
| 212 | |
| 213 | // RegexpCache keeps track of patterns which have already been |
| 214 | // translated to a regular expression and compiled, to save repeating |
| 215 | // the work. |
| 216 | // |
| 217 | // If nil, no caching takes place. |
| 218 | RegexpCachescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#RegexpCache. map[string]*regexpscip-go gomod github.com/golang/go/src go1.22 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.22 regexp/Regexp# |
| 219 | |
| 220 | // Version specifies an EditorConfig version to use when applying its |
| 221 | // spec. When empty, it defaults to the latest version. This field |
| 222 | // should generally be left untouched. |
| 223 | Versionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#Version. string |
| 224 | } |
| 225 | |
| 226 | // Find figures out the properties that apply to a file on disk |
| 227 | // given its name and languages, returns them as a section. |
| 228 | // The name doesn't need to be an absolute path. |
| 229 | // |
| 230 | // Any relevant EditorConfig files are parsed and used as necessary. Parsing the |
| 231 | // files can be cached in Query. |
| 232 | // |
| 233 | // The defaults for supported properties are applied before returning. |
| 234 | func (q Queryscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#) Findscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#Find().(name string, languages []string) (Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#, error) { |
| 235 | name, err := filepathscip-go gomod github.com/golang/go/src go1.22 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.22 `path/filepath`/Abs().(name) |
| 236 | if err != nil { |
| 237 | return Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#{}, err |
| 238 | } |
| 239 | configName := q.ConfigNamescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#ConfigName. |
| 240 | if configName == "" { |
| 241 | configName = DefaultNamescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/DefaultName. |
| 242 | } |
| 243 | |
| 244 | result := Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#{} |
| 245 | dir := name |
| 246 | for { |
| 247 | if d := filepathscip-go gomod github.com/golang/go/src go1.22 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.22 `path/filepath`/Dir().(dir); d != dir { |
| 248 | dir = d |
| 249 | } else { |
| 250 | break |
| 251 | } |
| 252 | file, e := q.FileCachescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#FileCache.[dir] |
| 253 | if !e { |
| 254 | // TODO: replace with io/fs |
| 255 | f, err := osscip-go gomod github.com/golang/go/src go1.22 os/.Openscip-go gomod github.com/golang/go/src go1.22 os/Open().(filepathscip-go gomod github.com/golang/go/src go1.22 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.22 `path/filepath`/Join().(dir, configName)) |
| 256 | if osscip-go gomod github.com/golang/go/src go1.22 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.22 os/IsNotExist().(err) { |
| 257 | // continue below, caching the nil file |
| 258 | } else if err != nil { |
| 259 | return Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#{}, err |
| 260 | } else { |
| 261 | var err error |
| 262 | file, err = Parsescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Parse().(f) |
| 263 | f.Closescip-go gomod github.com/golang/go/src go1.22 os/File#Close().() |
| 264 | if err != nil { |
| 265 | return Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#{}, err |
| 266 | } |
| 267 | } |
| 268 | if q.FileCachescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#FileCache. != nil { |
| 269 | q.FileCachescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#FileCache.[dir] = file |
| 270 | } |
| 271 | } |
| 272 | if file == nil { |
| 273 | continue |
| 274 | } |
| 275 | relative := name[len(dir)+1:] |
| 276 | result.Addscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Add().(file.Filterscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Filter().(relative, languages, q.RegexpCachescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#RegexpCache.).Propertiesscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Properties....) |
| 277 | if file.Rootscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Root. { |
| 278 | break |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | if result.Getscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Get().("indent_style") == "tab" { |
| 283 | if value := result.Getscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Get().("tab_width"); value != "" { |
| 284 | // When indent_style is "tab" and tab_width is set, |
| 285 | // indent_size should default to tab_width. |
| 286 | result.Addscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Add().(Propertyscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#{Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Name.: "indent_size", Valuescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Value.: value}) |
| 287 | } |
| 288 | if q.Versionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#Version. != "" && q.Versionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Query#Version. < "0.9.0" { // TODO: semver comparison? |
| 289 | } else if result.Getscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Get().("indent_size") == "" { |
| 290 | // When indent_style is "tab", indent_size defaults to |
| 291 | // "tab". Only on 0.9.0 and later. |
| 292 | result.Addscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Add().(Propertyscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#{Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Name.: "indent_size", Valuescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Value.: "tab"}) |
| 293 | } |
| 294 | } else if result.Getscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Get().("tab_width") == "" { |
| 295 | if value := result.Getscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Get().("indent_size"); value != "" && value != "tab" { |
| 296 | // tab_width defaults to the value of indent_size. |
| 297 | result.Addscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Add().(Propertyscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#{Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Name.: "tab_width", Valuescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Value.: value}) |
| 298 | } |
| 299 | } |
| 300 | return result, nil |
| 301 | } |
| 302 | |
| 303 | // Bundle mvdan.cc/sh/v3/pattern into pattern_bundle.go, |
| 304 | // since mvdan.cc/sh/v3/cmd/shfmt depends on this module |
| 305 | // and we don't want to end up with circular module dependencies. |
| 306 | // This should be fine, as the package is small, and the toolchain can omit what is unused. |
| 307 | // Note that we can't use @version on the sh/v3 module, so we automatically pull @latest via go.mod. |
| 308 | |
| 309 | func toRegexpscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/toRegexp().(pat string) *regexpscip-go gomod github.com/golang/go/src go1.22 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.22 regexp/Regexp# { |
| 310 | if i := stringsscip-go gomod github.com/golang/go/src go1.22 strings/.IndexBytescip-go gomod github.com/golang/go/src go1.22 strings/IndexByte().(pat, '/'); i == 0 { |
| 311 | pat = pat[1:] |
| 312 | } else if i < 0 { |
| 313 | pat = "**/" + pat |
| 314 | } |
| 315 | rxStr, err := patternRegexpscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/patternRegexp().(pat, patternFilenamesscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/patternFilenames.|patternBracesscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/patternBraces.|patternEntireStringscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/patternEntireString.) |
| 316 | if err != nil { |
| 317 | panic(err) |
| 318 | } |
| 319 | return regexpscip-go gomod github.com/golang/go/src go1.22 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.22 regexp/MustCompile().(rxStr) |
| 320 | } |
| 321 | |
| 322 | func Parsescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Parse().(r ioscip-go gomod github.com/golang/go/src go1.22 io/.Readerscip-go gomod github.com/golang/go/src go1.22 io/Reader#) (*Filescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#, error) { |
| 323 | f := &Filescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#{} |
| 324 | scanner := bufioscip-go gomod github.com/golang/go/src go1.22 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.22 bufio/NewScanner().(r) |
| 325 | var section *Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section# |
| 326 | for scanner.Scanscip-go gomod github.com/golang/go/src go1.22 bufio/Scanner#Scan().() { |
| 327 | line := scanner.Textscip-go gomod github.com/golang/go/src go1.22 bufio/Scanner#Text().() |
| 328 | if i := stringsscip-go gomod github.com/golang/go/src go1.22 strings/.Indexscip-go gomod github.com/golang/go/src go1.22 strings/Index().(line, " #"); i >= 0 { |
| 329 | line = line[:i] |
| 330 | } else if i := stringsscip-go gomod github.com/golang/go/src go1.22 strings/.Indexscip-go gomod github.com/golang/go/src go1.22 strings/Index().(line, " ;"); i >= 0 { |
| 331 | line = line[:i] |
| 332 | } |
| 333 | line = stringsscip-go gomod github.com/golang/go/src go1.22 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.22 strings/TrimSpace().(line) |
| 334 | |
| 335 | if len(line) > 2 && line[0] == '[' && line[len(line)-1] == ']' { |
| 336 | name := line[1 : len(line)-1] |
| 337 | if len(name) > 4096 { |
| 338 | section = &Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#{} // ignore |
| 339 | continue |
| 340 | } |
| 341 | f.Sectionsscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Sections. = append(f.Sectionsscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Sections., Sectionscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#{Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Name.: name}) |
| 342 | section = &f.Sectionsscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Sections.[len(f.Sectionsscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Sections.)-1] |
| 343 | continue |
| 344 | } |
| 345 | i := stringsscip-go gomod github.com/golang/go/src go1.22 strings/.IndexAnyscip-go gomod github.com/golang/go/src go1.22 strings/IndexAny().(line, "=:") |
| 346 | if i < 0 { |
| 347 | continue |
| 348 | } |
| 349 | key := stringsscip-go gomod github.com/golang/go/src go1.22 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.22 strings/ToLower().(stringsscip-go gomod github.com/golang/go/src go1.22 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.22 strings/TrimSpace().(line[:i])) |
| 350 | value := stringsscip-go gomod github.com/golang/go/src go1.22 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.22 strings/TrimSpace().(line[i+1:]) |
| 351 | switch key { |
| 352 | case "root", "indent_style", "indent_size", "tab_width", "end_of_line", |
| 353 | "charset", "trim_trailing_whitespace", "insert_final_newline": |
| 354 | value = stringsscip-go gomod github.com/golang/go/src go1.22 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.22 strings/ToLower().(value) |
| 355 | } |
| 356 | // The spec tests require supporting at least these lengths. |
| 357 | // Larger lengths rarely make sense, |
| 358 | // and they could mean holding onto lots of memory, |
| 359 | // so use them as limits. |
| 360 | if len(key) > 1024 || len(value) > 4096 { |
| 361 | continue |
| 362 | } |
| 363 | if section != nil { |
| 364 | section.Addscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Section#Add().(Propertyscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#{Namescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Name.: key, Valuescip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/Property#Value.: value}) |
| 365 | } else if key == "root" { |
| 366 | f.Rootscip-go gomod mvdan.cc/editorconfig . `mvdan.cc/editorconfig`/File#Root. = value == "true" |
| 367 | } |
| 368 | } |
| 369 | return f, nil |
| 370 | } |