Dont drop trailing escape codes when stripping trailing whitespace

This commit is contained in:
Kovid Goyal
2022-09-03 07:01:27 +05:30
parent 1ac7d9c10d
commit e433b90297
2 changed files with 9 additions and 3 deletions

View File

@@ -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)]
}

View File

@@ -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")
}