mirror of https://github.com/go-gitea/gitea.git
Fix: now highlights in diff view are getting the correct lines.
This commit is contained in:
parent
bf11ad19ea
commit
697b0e2aba
|
@ -77,28 +77,24 @@ func diffToHtml(diffRecord []diffmatchpatch.Diff, lineType DiffLineType) templat
|
||||||
return template.HTML(result)
|
return template.HTML(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (diffSection *DiffSection) GetLeftLine(idx int, sliceIdx int) *DiffLine {
|
// get an specific line by type (add or del) and file line number
|
||||||
for i, diffLine := range diffSection.Lines {
|
func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLine {
|
||||||
if diffLine.LeftIdx == idx && diffLine.RightIdx == 0 {
|
difference := 0
|
||||||
// ignore if the lines are too far from each other
|
|
||||||
if i > sliceIdx-5 && i < sliceIdx+5 {
|
for _, diffLine := range diffSection.Lines {
|
||||||
return diffLine
|
if diffLine.Type == DIFF_LINE_PLAIN {
|
||||||
} else {
|
// get the difference of line numbers between ADD and DEL versions
|
||||||
return nil
|
difference = diffLine.RightIdx - diffLine.LeftIdx
|
||||||
}
|
continue
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (diffSection *DiffSection) GetRightLine(idx int, sliceIdx int) *DiffLine {
|
if lineType == DIFF_LINE_DEL {
|
||||||
for i, diffLine := range diffSection.Lines {
|
if diffLine.RightIdx == 0 && diffLine.LeftIdx == idx - difference {
|
||||||
if diffLine.RightIdx == idx && diffLine.LeftIdx == 0 {
|
return diffLine
|
||||||
// ignore if the lines are too far from each other
|
}
|
||||||
if i > sliceIdx-5 && i < sliceIdx+5 {
|
} else if lineType == DIFF_LINE_ADD {
|
||||||
|
if diffLine.LeftIdx == 0 && diffLine.RightIdx == idx + difference {
|
||||||
return diffLine
|
return diffLine
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,7 +103,7 @@ func (diffSection *DiffSection) GetRightLine(idx int, sliceIdx int) *DiffLine {
|
||||||
|
|
||||||
// computes diff of each diff line and set the HTML on diffLine.ParsedContent
|
// computes diff of each diff line and set the HTML on diffLine.ParsedContent
|
||||||
func (diffSection *DiffSection) ComputeLinesDiff() {
|
func (diffSection *DiffSection) ComputeLinesDiff() {
|
||||||
for i, diffLine := range diffSection.Lines {
|
for _, diffLine := range diffSection.Lines {
|
||||||
var compareDiffLine *DiffLine
|
var compareDiffLine *DiffLine
|
||||||
var diff1, diff2 string
|
var diff1, diff2 string
|
||||||
|
|
||||||
|
@ -121,14 +117,14 @@ func (diffSection *DiffSection) ComputeLinesDiff() {
|
||||||
|
|
||||||
// try to find equivalent diff line. ignore, otherwise
|
// try to find equivalent diff line. ignore, otherwise
|
||||||
if diffLine.Type == DIFF_LINE_ADD {
|
if diffLine.Type == DIFF_LINE_ADD {
|
||||||
compareDiffLine = diffSection.GetLeftLine(diffLine.RightIdx, i)
|
compareDiffLine = diffSection.GetLine(DIFF_LINE_DEL, diffLine.RightIdx)
|
||||||
if compareDiffLine == nil {
|
if compareDiffLine == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
diff1 = compareDiffLine.Content
|
diff1 = compareDiffLine.Content
|
||||||
diff2 = diffLine.Content
|
diff2 = diffLine.Content
|
||||||
} else {
|
} else {
|
||||||
compareDiffLine = diffSection.GetRightLine(diffLine.LeftIdx, i)
|
compareDiffLine = diffSection.GetLine(DIFF_LINE_ADD, diffLine.LeftIdx)
|
||||||
if compareDiffLine == nil {
|
if compareDiffLine == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,12 @@ func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func assertLineEqual(t *testing.T, d1 *DiffLine, d2 *DiffLine) {
|
||||||
|
if d1 != d2 {
|
||||||
|
t.Errorf("%v should be equal %v", d1, d2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDiffToHtml(t *testing.T) {
|
func TestDiffToHtml(t *testing.T) {
|
||||||
assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHtml([]dmp.Diff{
|
assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHtml([]dmp.Diff{
|
||||||
dmp.Diff{dmp.DiffEqual, "foo "},
|
dmp.Diff{dmp.DiffEqual, "foo "},
|
||||||
|
@ -27,3 +33,38 @@ func TestDiffToHtml(t *testing.T) {
|
||||||
dmp.Diff{dmp.DiffEqual, " biz"},
|
dmp.Diff{dmp.DiffEqual, " biz"},
|
||||||
}, DIFF_LINE_DEL))
|
}, DIFF_LINE_DEL))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test if GetLine is return the correct lines
|
||||||
|
func TestGetLine(t *testing.T) {
|
||||||
|
ds := DiffSection{Lines: []*DiffLine{
|
||||||
|
&DiffLine{LeftIdx: 28, RightIdx: 28, Type: DIFF_LINE_PLAIN},
|
||||||
|
&DiffLine{LeftIdx: 29, RightIdx: 29, Type: DIFF_LINE_PLAIN},
|
||||||
|
&DiffLine{LeftIdx: 30, RightIdx: 30, Type: DIFF_LINE_PLAIN},
|
||||||
|
&DiffLine{LeftIdx: 31, RightIdx: 0, Type: DIFF_LINE_DEL},
|
||||||
|
&DiffLine{LeftIdx: 0, RightIdx: 31, Type: DIFF_LINE_ADD},
|
||||||
|
&DiffLine{LeftIdx: 0, RightIdx: 32, Type: DIFF_LINE_ADD},
|
||||||
|
&DiffLine{LeftIdx: 32, RightIdx: 33, Type: DIFF_LINE_PLAIN},
|
||||||
|
&DiffLine{LeftIdx: 33, RightIdx: 0, Type: DIFF_LINE_DEL},
|
||||||
|
&DiffLine{LeftIdx: 34, RightIdx: 0, Type: DIFF_LINE_DEL},
|
||||||
|
&DiffLine{LeftIdx: 35, RightIdx: 0, Type: DIFF_LINE_DEL},
|
||||||
|
&DiffLine{LeftIdx: 36, RightIdx: 0, Type: DIFF_LINE_DEL},
|
||||||
|
&DiffLine{LeftIdx: 0, RightIdx: 34, Type: DIFF_LINE_ADD},
|
||||||
|
&DiffLine{LeftIdx: 0, RightIdx: 35, Type: DIFF_LINE_ADD},
|
||||||
|
&DiffLine{LeftIdx: 0, RightIdx: 36, Type: DIFF_LINE_ADD},
|
||||||
|
&DiffLine{LeftIdx: 0, RightIdx: 37, Type: DIFF_LINE_ADD},
|
||||||
|
&DiffLine{LeftIdx: 37, RightIdx: 38, Type: DIFF_LINE_PLAIN},
|
||||||
|
&DiffLine{LeftIdx: 38, RightIdx: 39, Type: DIFF_LINE_PLAIN},
|
||||||
|
}}
|
||||||
|
|
||||||
|
assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 31), ds.Lines[4])
|
||||||
|
assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 31), ds.Lines[3])
|
||||||
|
|
||||||
|
assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 33), ds.Lines[11])
|
||||||
|
assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 34), ds.Lines[12])
|
||||||
|
assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 35), ds.Lines[13])
|
||||||
|
assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 36), ds.Lines[14])
|
||||||
|
assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 34), ds.Lines[7])
|
||||||
|
assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 35), ds.Lines[8])
|
||||||
|
assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 36), ds.Lines[9])
|
||||||
|
assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 37), ds.Lines[10])
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue