Skip to content

Commit f4a4f33

Browse files
committed
fix diff GetLine
1 parent 0e12b7c commit f4a4f33

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

services/gitdiff/gitdiff.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const (
7878
type DiffLine struct {
7979
LeftIdx int // line number, 1-based
8080
RightIdx int // line number, 1-based
81-
Match int // line number, 1-based
81+
Match int // the diff matched index. -1: no match. 0: plain and no need to match. >0: for add/del, "Lines" slice index of the other side
8282
Type DiffLineType
8383
Content string
8484
Comments issues_model.CommentList // related PR code comments
@@ -206,8 +206,17 @@ type DiffSection struct {
206206
Lines []*DiffLine
207207
}
208208

209+
func (diffSection *DiffSection) GetLine(idx int) *DiffLine {
210+
if idx <= 0 {
211+
return nil
212+
}
213+
return diffSection.Lines[idx]
214+
}
215+
209216
// GetLine gets a specific line by type (add or del) and file line number
210-
func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLine {
217+
// This algorithm is not quite right.
218+
// Actually now we have "Match" field, it is always right, so use it instead in new GetLine
219+
func (diffSection *DiffSection) getLineLegacy(lineType DiffLineType, idx int) *DiffLine { //nolint:unused
211220
var (
212221
difference = 0
213222
addCount = 0
@@ -327,10 +336,10 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine, loc
327336
case DiffLineSection:
328337
return getLineContent(diffLine.Content[1:], locale)
329338
case DiffLineAdd:
330-
compareDiffLine := diffSection.GetLine(DiffLineDel, diffLine.RightIdx)
339+
compareDiffLine := diffSection.GetLine(diffLine.Match)
331340
return diffSection.getDiffLineForRender(DiffLineAdd, compareDiffLine, diffLine, locale)
332341
case DiffLineDel:
333-
compareDiffLine := diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx)
342+
compareDiffLine := diffSection.GetLine(diffLine.Match)
334343
return diffSection.getDiffLineForRender(DiffLineDel, diffLine, compareDiffLine, locale)
335344
default: // Plain
336345
// TODO: there was an "if" check: `if diffLine.Content >strings.IndexByte(" +-", diffLine.Content[0]) > -1 { ... } else { ... }`

services/gitdiff/highlightdiff_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ func TestDiffWithHighlight(t *testing.T) {
2323
assert.Equal(t, `x <span class="k"><span class="added-code">bar</span></span> y`, string(outAdd))
2424
})
2525

26+
t.Run("CleanUp", func(t *testing.T) {
27+
hcd := newHighlightCodeDiff()
28+
codeA := template.HTML(`<span class="cm>this is a comment</span>`)
29+
codeB := template.HTML(`<span class="cm>this is updated comment</span>`)
30+
outDel := hcd.diffLineWithHighlight(DiffLineDel, codeA, codeB)
31+
assert.Equal(t, `<span class="cm>this is <span class="removed-code">a</span> comment</span>`, string(outDel))
32+
outAdd := hcd.diffLineWithHighlight(DiffLineAdd, codeA, codeB)
33+
assert.Equal(t, `<span class="cm>this is <span class="added-code">updated</span> comment</span>`, string(outAdd))
34+
})
35+
2636
t.Run("OpenCloseTags", func(t *testing.T) {
2737
hcd := newHighlightCodeDiff()
2838
hcd.placeholderTokenMap['O'], hcd.placeholderTokenMap['C'] = "<span>", "</span>"

0 commit comments

Comments
 (0)