-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yoan Blanc <[email protected]>
- Loading branch information
Showing
6 changed files
with
203 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package eclint | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/editorconfig/editorconfig-core-go/v2" | ||
) | ||
|
||
// definition contains the fields that aren't native to EditorConfig.Definition | ||
type definition struct { | ||
editorconfig.Definition | ||
BlockCommentStart []byte | ||
BlockComment []byte | ||
BlockCommentEnd []byte | ||
MaxLength int | ||
TabWidth int | ||
IndentSize int | ||
LastLine []byte | ||
LastIndex int | ||
InsideBlockComment bool | ||
} | ||
|
||
func newDefinition(d *editorconfig.Definition) (*definition, error) { | ||
def := &definition{ | ||
Definition: *d, | ||
TabWidth: d.TabWidth, | ||
} | ||
|
||
if d.IndentSize != "" && d.IndentSize != UnsetValue { | ||
is, err := strconv.Atoi(d.IndentSize) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
def.IndentSize = is | ||
} | ||
|
||
if def.IndentStyle != "" && def.IndentStyle != UnsetValue { | ||
bs, ok := def.Raw["block_comment_start"] | ||
if ok && bs != "" && bs != UnsetValue { | ||
def.BlockCommentStart = []byte(bs) | ||
bc, ok := def.Raw["block_comment"] | ||
|
||
if ok && bc != "" && bs != UnsetValue { | ||
def.BlockComment = []byte(bc) | ||
} | ||
|
||
be, ok := def.Raw["block_comment_end"] | ||
if !ok || be == "" || be == UnsetValue { | ||
return nil, fmt.Errorf(".editorconfig: block_comment_end was expected, none were found") | ||
} | ||
|
||
def.BlockCommentEnd = []byte(be) | ||
} | ||
} | ||
|
||
if mll, ok := def.Raw["max_line_length"]; ok && mll != "off" && mll != UnsetValue { | ||
ml, er := strconv.Atoi(mll) | ||
if er != nil || ml < 0 { | ||
return nil, fmt.Errorf(".editorconfig: max_line_length expected a non-negative number, got %q", mll) | ||
} | ||
|
||
def.MaxLength = ml | ||
|
||
if def.TabWidth <= 0 { | ||
def.TabWidth = DefaultTabWidth | ||
} | ||
} | ||
|
||
return def, nil | ||
} | ||
|
||
// OverrideDefinitionUsingPrefix is an helper that takes the prefixed values. | ||
// | ||
// It replaces thoses values into the nominal ones. That way a tool could a | ||
// different set of definition than the real editor would. | ||
func OverrideDefinitionUsingPrefix(def *editorconfig.Definition, prefix string) error { | ||
for k, v := range def.Raw { | ||
if strings.HasPrefix(k, prefix) { | ||
nk := k[len(prefix):] | ||
def.Raw[nk] = v | ||
|
||
switch nk { | ||
case "indent_style": | ||
def.IndentStyle = v | ||
case "indent_size": | ||
def.IndentSize = v | ||
case "charset": | ||
def.Charset = v | ||
case "end_of_line": | ||
def.EndOfLine = v | ||
case "tab_width": | ||
i, err := strconv.Atoi(v) | ||
if err != nil { | ||
return fmt.Errorf("tab_width cannot be set. %w", err) | ||
} | ||
|
||
def.TabWidth = i | ||
case "trim_trailing_whitespace": | ||
return fmt.Errorf("%v cannot be overridden yet, pr welcome", nk) | ||
case "insert_final_newline": | ||
return fmt.Errorf("%v cannot be overridden yet, pr welcome", nk) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package eclint_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/editorconfig/editorconfig-core-go/v2" | ||
"gitlab.com/greut/eclint" | ||
) | ||
|
||
func TestOverridingUsingPrefix(t *testing.T) { | ||
def := &editorconfig.Definition{ | ||
Charset: "utf-8 bom", | ||
IndentStyle: "tab", | ||
IndentSize: "3", | ||
TabWidth: 3, | ||
} | ||
|
||
raw := make(map[string]string) | ||
raw["@_charset"] = "unset" | ||
raw["@_indent_style"] = "space" | ||
raw["@_indent_size"] = "4" | ||
raw["@_tab_width"] = "4" | ||
def.Raw = raw | ||
|
||
if err := eclint.OverrideDefinitionUsingPrefix(def, "@_"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if def.Charset != "unset" { | ||
t.Errorf("charset not changed, got %q", def.Charset) | ||
} | ||
|
||
if def.IndentStyle != "space" { | ||
t.Errorf("indent_style not changed, got %q", def.IndentStyle) | ||
} | ||
|
||
if def.IndentSize != "4" { | ||
t.Errorf("indent_size not changed, got %q", def.IndentSize) | ||
} | ||
|
||
if def.TabWidth != 4 { | ||
t.Errorf("tab_width not changed, got %d", def.TabWidth) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.