Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

definition: max_line_length support #66

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Definition struct {
Charset string
IndentStyle string
IndentSize string
MaxLength int
TabWidth int
EndOfLine string
TrimTrailingWhitespace *bool
Expand Down
23 changes: 23 additions & 0 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions editorconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we use unset here too? What's the difference between toggling this off and unsetting it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unset means restore default value, this is more like a false.. but yeah...

https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#max_line_length

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that off is a bad naming... go-gitea/gitea#20694

)

// IndentStyle possible values.
Expand Down
2 changes: 2 additions & 0 deletions editorconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions testdata/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down