Skip to content

Commit 4026741

Browse files
committed
Do not break out quoted text in block formatting
1 parent b3d85a0 commit 4026741

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

block.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,28 @@ func splitsBlockLines(msg string, width int) ([]string, int) {
6262
for _, line := range strings.Split(msg, "\n") {
6363
line = strings.ReplaceAll(line, "\t", " ")
6464
lastLinePos := 0
65+
lastOpeningQuotePos := 0
6566
inAnOpeningTag := false
6667
inAClosingTag := false
6768
inATagBody := false
69+
inQuotes := false
6870
length := 0
6971
var lastChar rune
7072
for pos, char := range line {
71-
if char == '<' && lastChar != '\\' {
72-
if len(line) > pos+1 && line[pos+1] == '/' {
73-
inAClosingTag = true
74-
inATagBody = false
75-
} else {
76-
inAnOpeningTag = true
73+
if lastChar != '\\' {
74+
switch char {
75+
case '<':
76+
if len(line) > pos+1 && line[pos+1] == '/' {
77+
inAClosingTag = true
78+
inATagBody = false
79+
} else {
80+
inAnOpeningTag = true
81+
}
82+
case '"':
83+
inQuotes = !inQuotes
84+
if inQuotes {
85+
lastOpeningQuotePos = pos
86+
}
7787
}
7888
}
7989

@@ -92,10 +102,18 @@ func splitsBlockLines(msg string, width int) ([]string, int) {
92102
}
93103

94104
if length >= width && !inAClosingTag && !inAnOpeningTag && !inATagBody {
105+
// if we cross the line boundary but are currently within a
106+
// quoted text, we jump back just before the opening quote
107+
// because we don't want to cut the text inside
108+
if inQuotes && lastOpeningQuotePos > lastLinePos {
109+
length = pos - lastOpeningQuotePos
110+
pos = lastOpeningQuotePos - 1
111+
} else {
112+
maxLen = width
113+
length = 0
114+
}
95115
lines = append(lines, line[lastLinePos:pos+1])
96-
maxLen = width
97116
lastLinePos = pos + 1
98-
length = 0
99117
}
100118

101119
lastChar = char

block_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ func (ts *OutputBlockSuite) TestSplitsBlockLines(c *C) {
4545
c.Assert(maxLen, Equals, 3)
4646
}
4747

48+
func (ts *OutputBlockSuite) TestEnclosedWithQuotes(c *C) {
49+
lines, maxLen := splitsBlockLines(`The "foo bar" file has moved to "a very long path". Please consider it.`, 40)
50+
c.Assert(lines, DeepEquals, []string{`The "foo bar" file has moved to `, `"a very long path". Please consider it.`})
51+
c.Assert(maxLen, Equals, 38)
52+
53+
lines, maxLen = splitsBlockLines(`The "foo bar" file has moved to "a very very very very very very very very very unterminated long path.`, 40)
54+
c.Assert(lines, DeepEquals, []string{`The "foo bar" file has moved to `, `"a very very very very very very very ver`, `y very unterminated long path.`})
55+
c.Assert(maxLen, Equals, 40)
56+
57+
lines, maxLen = splitsBlockLines(`This is an example of a message about a "/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/long/path" that needs to be moved to "/another/very/very/very/very/very/very/very/long/path"`, 107)
58+
c.Assert(lines, DeepEquals, []string{`This is an example of a message about a `, `"/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/long/path" that needs`, ` to be moved to "/another/very/very/very/very/very/very/very/long/path"`})
59+
c.Assert(maxLen, Equals, 107)
60+
}
61+
4862
func (ts *OutputBlockSuite) TestSplitsBlockLinesDonotPanic(c *C) {
4963
lines, maxLen := splitsBlockLines("Foo Baz<", 4)
5064
c.Assert(lines, DeepEquals, []string{"Foo ", "Baz<"})

0 commit comments

Comments
 (0)