From bbbff6689b0db153da496d9c2722023bd43ed589 Mon Sep 17 00:00:00 2001 From: Amy Boyd Date: Mon, 4 Jul 2016 17:25:40 +0100 Subject: [PATCH] Add line fixers: FixTabIndentationToSpaces, FixTabIndentationToSpaces, FixTabIndentationToSpaces. --- editorconfig/line_fixers.go | 55 ++++++++++++++++++++++++ editorconfig/line_fixers_test.go | 72 ++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 editorconfig/line_fixers.go create mode 100644 editorconfig/line_fixers_test.go diff --git a/editorconfig/line_fixers.go b/editorconfig/line_fixers.go new file mode 100644 index 0000000..9351e37 --- /dev/null +++ b/editorconfig/line_fixers.go @@ -0,0 +1,55 @@ +package editorconfig + +import ( + "strconv" + "strings" +) + +type LineFixer func(ruleValue string, line string) string + +func FixTabIndentationToSpaces(ruleValueNumberOfSpaces string, line string) string { + numberOfSpaces, _ := strconv.Atoi(ruleValueNumberOfSpaces) + + line = indentedWithTabsRegexp.ReplaceAllStringFunc(line, func(tabs string) string { + return strings.Repeat(" ", len(tabs)*numberOfSpaces) + }) + + return line +} + +func FixMixedIndentationToSpaces(ruleValueNumberOfSpaces string, line string) string { + numberOfSpaces, _ := strconv.Atoi(ruleValueNumberOfSpaces) + + for indentedWithMixedTabsAndSpacesRegexp.MatchString(line) { + line = indentedWithMixedTabsAndSpacesRegexp.ReplaceAllStringFunc(line, func(tabsAndSpaces string) string { + tabs := strings.Replace(tabsAndSpaces, " ", "", -1) + spaces := strings.Replace(tabsAndSpaces, "\t", "", -1) + + return strings.Repeat(" ", (len(tabs)*numberOfSpaces)+len(spaces)) + }) + } + + return line +} + +func FixUndividableIndentationToNearestSpacesAmount(ruleValueNumberOfSpaces string, line string) string { + numberOfSpaces, _ := strconv.Atoi(ruleValueNumberOfSpaces) + if numberOfSpaces < 1 { + ExitBecauseOfInternalError("Number of spaces must be integer greater than 0, is: " + ruleValueNumberOfSpaces) + } + + if GetNumberOfLeftSpaces(line) == 0 { + return line + } + + for true { + leftSpaces := GetNumberOfLeftSpaces(line) + if leftSpaces%numberOfSpaces != 0 { + line = " " + line + } else { + break + } + } + + return line +} diff --git a/editorconfig/line_fixers_test.go b/editorconfig/line_fixers_test.go new file mode 100644 index 0000000..ba7e569 --- /dev/null +++ b/editorconfig/line_fixers_test.go @@ -0,0 +1,72 @@ +package editorconfig + +import ( + "testing" +) + +func TestFixTabIndentationToSpaces(t *testing.T) { + var result string + + result = FixTabIndentationToSpaces("4", "\t\thello world") + if result != " hello world" { + t.Error("Unexpected result: " + result) + } + + result = FixTabIndentationToSpaces("3", "\thello world") + if result != " hello world" { + t.Error("Unexpected result: " + result) + } + + result = FixTabIndentationToSpaces("2", "\t\t\thello world") + if result != " hello world" { + t.Error("Unexpected result: " + result) + } +} + +func TestFixMixedIndentationToSpaces(t *testing.T) { + var result string + + result = FixMixedIndentationToSpaces("2", "\t \t hello worl d") + if result != " hello worl d" { + t.Error("Unexpected result: " + result) + } + + result = FixMixedIndentationToSpaces("3", " \thello world !") + if result != " hello world !" { + t.Error("Unexpected result: " + result) + } + + result = FixMixedIndentationToSpaces("2", " \t hello world !") + if result != " hello world !" { + t.Error("Unexpected result: " + result) + } +} + +func TestFixUndividableIndentationToNearestSpacesAmount(t *testing.T) { + var result string + + result = FixUndividableIndentationToNearestSpacesAmount("2", "hello") + if result != "hello" { + t.Error("String changed but it was already fine. Changed to: " + result) + } + + result = FixUndividableIndentationToNearestSpacesAmount("2", " hello") + if result != " hello" { + t.Error("String changed but it was already fine. Changed to: " + result) + } + + result = FixUndividableIndentationToNearestSpacesAmount("1", " hello") + if result != " hello" { + t.Error("String changed but it was already fine. Changed to: " + result) + } + + result = FixUndividableIndentationToNearestSpacesAmount("3", " hello") + if result != " hello" { + t.Error("Unexpected result: " + result) + } + + result = FixUndividableIndentationToNearestSpacesAmount("5", " hello") + if result != " hello" { + t.Error("Unexpected result: " + result) + } +}