diff --git a/tools/utils/style/indent-and-wrap.go b/tools/utils/style/indent-and-wrap.go index f69163ccc..e73ed4996 100644 --- a/tools/utils/style/indent-and-wrap.go +++ b/tools/utils/style/indent-and-wrap.go @@ -254,7 +254,14 @@ type line_builder struct { func (self *line_builder) reset() string { ans := self.buf.String() if len(ans) > self.last_text_pos { - ans = ans[:self.last_text_pos] + prefix := ans[:self.last_text_pos] + suffix := ans[self.last_text_pos:] + prefix = strings.TrimRightFunc(prefix, unicode.IsSpace) + if len(prefix) != self.last_text_pos { + ans = prefix + suffix + } + } else { + ans = strings.TrimRightFunc(ans, unicode.IsSpace) } sz := self.buf.Len() self.buf.Reset() @@ -316,7 +323,6 @@ func (self *wrapper) newline_prefix() { func (self *wrapper) end_current_line() { line := self.current_line.reset() - line = strings.TrimRightFunc(line, unicode.IsSpace) if strings.HasSuffix(line, self.indent) && wcswidth.Stringwidth(line) == self.indent_width { line = line[:len(line)-len(self.indent)] } diff --git a/tools/utils/style/indent-and-wrap_test.go b/tools/utils/style/indent-and-wrap_test.go index 620db74a7..91e871fe6 100644 --- a/tools/utils/style/indent-and-wrap_test.go +++ b/tools/utils/style/indent-and-wrap_test.go @@ -21,6 +21,6 @@ func TestFormatWithIndent(t *testing.T) { tx("testing\n\ntwo", "testing\n\n__two") tx("testing\n \ntwo", "testing\n\n__two") - tx("123456 \x1b[31m789a", "123456\n\x1b[39m__\x1b[31m789a") + tx("123456 \x1b[31m789a", "123456\x1b[31m\n\x1b[39m__\x1b[31m789a") tx("12 \x1b[31m789 abcd", "12 \x1b[31m789\n\x1b[39m__\x1b[31mabcd") }