web: keep an added line's leading dash in the diff !131
2 files changed, +27 −1
Layout: unified · split
internal/httpd/diff.go +4 −1
| @@ -192,11 +192,14 @@ func highlightFile(f *diffFile) { | |||
| 192 | } | 192 | } |
| 193 | 193 | ||
| 194 | // sideText joins one side of a hunk: context plus the given change class. | 194 | // sideText joins one side of a hunk: context plus the given change class. |
| 195 | // Content, not Text: the marker is already off it. Taking it off Text here | ||
| 196 | // meant stripping "+" and then "-", which ate the dash of an added line that | ||
| 197 | // begins with one. | ||
| 195 | func sideText(hunk []diffLine, class string) string { | 198 | func sideText(hunk []diffLine, class string) string { |
| 196 | var b strings.Builder | 199 | var b strings.Builder |
| 197 | for _, l := range hunk { | 200 | for _, l := range hunk { |
| 198 | if l.Class == "ctx" || l.Class == class { | 201 | if l.Class == "ctx" || l.Class == class { |
| 199 | b.WriteString(strings.TrimPrefix(strings.TrimPrefix(l.Text, "+"), "-")) | 202 | b.WriteString(l.Content) |
| 200 | b.WriteByte('\n') | 203 | b.WriteByte('\n') |
| 201 | } | 204 | } |
| 202 | } | 205 | } |
internal/httpd/diff_test.go +23
| @@ -183,3 +183,26 @@ func stripTags(s string) string { | |||
| 183 | } | 183 | } |
| 184 | return strings.ReplaceAll(b.String(), """, `"`) | 184 | return strings.ReplaceAll(b.String(), """, `"`) |
| 185 | } | 185 | } |
| 186 | |||
| 187 | // An added line whose own text begins with a dash — a Markdown bullet, a | ||
| 188 | // docstring list — keeps it. Deriving the highlighting source from the raw | ||
| 189 | // line means stripping the marker, and stripping "+" then "-" takes the | ||
| 190 | // content's dash along with it. | ||
| 191 | func TestParseDiffKeepsDashAfterMarker(t *testing.T) { | ||
| 192 | files := parseDiff(`diff --git a/README.md b/README.md | ||
| 193 | --- a/README.md | ||
| 194 | +++ b/README.md | ||
| 195 | @@ -1,2 +1,3 @@ | ||
| 196 | - one | ||
| 197 | +- two | ||
| 198 | - three | ||
| 199 | `) | ||
| 200 | for _, l := range files[0].Lines { | ||
| 201 | if l.Class != "add" { | ||
| 202 | continue | ||
| 203 | } | ||
| 204 | if text := stripTags(string(l.Code)); text != l.Content { | ||
| 205 | t.Errorf("added line %q highlighted as %q", l.Content, text) | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||