Skip to content

Commit 3187d7c

Browse files
authored
Fix minor mis-accounting of column position in report.wordWrap (#449)
This PR also adds a test to exercise the corner case.
1 parent 797d668 commit 3187d7c

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

experimental/report/testdata/no-snippets.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ diagnostics:
3636
level: LEVEL_REMARK
3737
in_file: foo.proto
3838
notes:
39-
- "this footer is very very very very very very very very very very very very very very very very very very long"
39+
- "this footer is a very very very very very very very very very very very very very very very very very very very very very very long footer"
4040
- "this one is also long, and it's also supercalifragilistcexpialidocious, leading to a very early break"
4141
help:
4242
- "this help is very long (and triggers the same word-wrapping code path)"

experimental/report/testdata/no-snippets.yaml.color.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
⟨b.cyn⟩remark: very long footers
1616
⟨blu⟩ --> foo.proto
17-
⟨blu⟩ = ⟨b.cyn⟩note: ⟨reset⟩this footer is very very very very very very very very very very very
18-
very very very very very very very long
17+
⟨blu⟩ = ⟨b.cyn⟩note: ⟨reset⟩this footer is a very very very very very very very very very very
18+
very very very very very very very very very very very very long
19+
footer
1920
⟨blu⟩ = ⟨b.cyn⟩note: ⟨reset⟩this one is also long, and it's also
2021
supercalifragilistcexpialidocious, leading to a very early break
2122
⟨blu⟩ = ⟨b.cyn⟩help: ⟨reset⟩this help is very long (and triggers the same word-wrapping code

experimental/report/testdata/no-snippets.yaml.fancy.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ warning: file consists only of the byte `0xaa`
1414

1515
remark: very long footers
1616
--> foo.proto
17-
= note: this footer is very very very very very very very very very very very
18-
very very very very very very very long
17+
= note: this footer is a very very very very very very very very very very
18+
very very very very very very very very very very very very long
19+
footer
1920
= note: this one is also long, and it's also
2021
supercalifragilistcexpialidocious, leading to a very early break
2122
= help: this help is very long (and triggers the same word-wrapping code

experimental/report/width.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,34 @@ func wordWrap(text string, width int) iter.Seq[string] {
4848
// Split along lines first, since those are hard breaks we don't plan
4949
// to change.
5050
stringsx.Lines(text)(func(line string) bool {
51-
line = strings.TrimSpace(line)
5251
var nextIsSpace bool
5352
var column, cursor int
5453

5554
stringsx.PartitionKey(line, unicode.IsSpace)(func(start int, chunk string) bool {
5655
isSpace := nextIsSpace
5756
nextIsSpace = !nextIsSpace
5857

59-
column = stringWidth(column, chunk, true, nil)
60-
if column <= width {
58+
if isSpace && column == 0 {
6159
return true
6260
}
6361

64-
line := line[cursor:start]
65-
if !yield(strings.TrimSpace(line)) {
62+
w := stringWidth(column, chunk, true, nil) - column
63+
if column+w <= width {
64+
column += w
65+
return true
66+
}
67+
68+
if !yield(strings.TrimSpace(line[cursor:start])) {
6669
return false
6770
}
68-
cursor = start
71+
6972
if isSpace {
70-
cursor += len(chunk)
73+
cursor = start + len(chunk)
74+
column = 0
75+
} else {
76+
cursor = start
77+
column = w
7178
}
72-
column = 0
7379
return true
7480
})
7581

0 commit comments

Comments
 (0)