diff --git a/.golangci.yml b/.golangci.yml index 34b1315..fd140e5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,7 +5,7 @@ linters-settings: - default - prefix(github.com/editorconfig/editorconfig-core-go) cyclop: - max-complexity: 15 + max-complexity: 16 package-average: 10 linters: diff --git a/README.md b/README.md index df84898..7563bf7 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ type Definition struct { Charset string IndentStyle string IndentSize string + MaxLength int TabWidth int EndOfLine string TrimTrailingWhitespace *bool diff --git a/definition.go b/definition.go index 8da5f58..38c1eff 100644 --- a/definition.go +++ b/definition.go @@ -19,6 +19,7 @@ type Definition struct { 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"` + MaxLineLength int `ini:"-" json:"-"` TabWidth int `ini:"-" json:"-"` EndOfLine string `ini:"end_of_line" json:"end_of_line,omitempty"` TrimTrailingWhitespace *bool `ini:"-" json:"-"` @@ -58,6 +59,17 @@ func (d *Definition) normalize() error { d.InsertFinalNewline = &insert } + // max_line_length from Raw + maxLineLength, ok := d.Raw["max_line_length"] + if ok && (maxLineLength != UnsetValue && maxLineLength != OffValue) { + num, err := strconv.Atoi(maxLineLength) + if err != nil { + return fmt.Errorf("max_line_length=%s is not an acceptable value. %w", maxLineLength, err) + } + + d.MaxLineLength = num + } + // tab_width from Raw tabWidth, ok := d.Raw["tab_width"] if ok && tabWidth != UnsetValue { @@ -93,6 +105,10 @@ func (d *Definition) merge(md *Definition) { d.IndentSize = md.IndentSize } + if d.MaxLineLength <= 0 { + d.MaxLineLength = md.MaxLineLength + } + if d.TabWidth <= 0 { d.TabWidth = md.TabWidth } @@ -154,6 +170,13 @@ func (d *Definition) InsertToIniFile(iniFile *ini.File) { //nolint:funlen,gocogn v = d.EndOfLine case "indent_style": v = d.IndentStyle + case "max_line_length": + maxLineLength, ok := d.Raw["max_line_length"] + if ok && (maxLineLength == UnsetValue || maxLineLength == OffValue) { + v = maxLineLength + } else { + v = strconv.Itoa(d.MaxLineLength) + } case "tab_width": tabWidth, ok := d.Raw["tab_width"] if ok && tabWidth == UnsetValue { diff --git a/editorconfig.go b/editorconfig.go index dffdf0e..5fee37b 100644 --- a/editorconfig.go +++ b/editorconfig.go @@ -16,6 +16,8 @@ const ( ConfigNameDefault = ".editorconfig" // UnsetValue is the value that unsets a preexisting variable. UnsetValue = "unset" + // OffValue is the value that disables max_length (non-standard). + OffValue = "off" ) // IndentStyle possible values. diff --git a/editorconfig_test.go b/editorconfig_test.go index c933bc3..1958388 100644 --- a/editorconfig_test.go +++ b/editorconfig_test.go @@ -33,12 +33,14 @@ func testParse(t *testing.T, ec *Editorconfig) { assert.Equal(t, IndentStyleTab, def.IndentStyle) assert.Equal(t, "4", def.IndentSize) assert.Equal(t, 4, def.TabWidth) + assert.Equal(t, 120, def.MaxLineLength) def = ec.Definitions[2] assert.Equal(t, "*.{js,css,less,htm,html}", def.Selector) assert.Equal(t, IndentStyleSpaces, def.IndentStyle) assert.Equal(t, "2", def.IndentSize) assert.Equal(t, 2, def.TabWidth) + assert.Equal(t, 0, def.MaxLineLength) } func TestParseFile(t *testing.T) { diff --git a/testdata/.editorconfig b/testdata/.editorconfig index b2dd907..f94e094 100644 --- a/testdata/.editorconfig +++ b/testdata/.editorconfig @@ -12,6 +12,7 @@ indent_size = 8 [*.go] indent_style = tab indent_size = 4 +max_line_length = 120 [*.{js,css,less,htm,html}] indent_style = space