diff --git a/indent/indent.go b/indent/indent.go index fef2b762..d95ff4f1 100644 --- a/indent/indent.go +++ b/indent/indent.go @@ -56,13 +56,16 @@ func MaxCommonIndentation(lines []string) int { // // redundantSpaces — the number of leading spaces to remove from each line. // +// If a line is shorter than redundantSpaces, the whole line is removed. +// // Returns processed lines. func CutIndent(lines []string, redundantSpaces int) []string { linesChanged := make([]string, len(lines)) copy(linesChanged, lines) for i, line := range linesChanged { if len(line) > 0 { - linesChanged[i] = line[redundantSpaces:] + cutLength := min(redundantSpaces, len(line)) + linesChanged[i] = line[cutLength:] } } diff --git a/indent/indent_test.go b/indent/indent_test.go index f1374fa9..a9bffd7e 100644 --- a/indent/indent_test.go +++ b/indent/indent_test.go @@ -60,4 +60,20 @@ var _ = Describe("Indent", func() { Expect(changedLines).ShouldNot(Equal(testLines)) }) + It("should cut lines shorter than the indentation", func() { + testLines := []string{ + " System.out.println(\"Hi\");", + " ", + " return;", + } + + changedLines := indent.CutIndent(testLines, 8) + + Expect(changedLines).Should(Equal([]string{ + "System.out.println(\"Hi\");", + "", + "return;", + })) + }) + })