Refactoring of inline diff computing to prevent empty diff box. Fix #2489

This commit is contained in:
Andrey Nering 2016-01-27 18:54:08 -02:00
parent 93f40995b3
commit 5deb726f3f
4 changed files with 37 additions and 50 deletions

View file

@ -51,7 +51,6 @@ type DiffLine struct {
RightIdx int RightIdx int
Type DiffLineType Type DiffLineType
Content string Content string
ParsedContent template.HTML
} }
func (d *DiffLine) GetType() int { func (d *DiffLine) GetType() int {
@ -112,42 +111,42 @@ func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLin
return nil return nil
} }
// computes diff of each diff line and set the HTML on diffLine.ParsedContent // computes inline diff for the given line
func (diffSection *DiffSection) ComputeLinesDiff() { func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) template.HTML {
for _, diffLine := range diffSection.Lines { var compareDiffLine *DiffLine
var compareDiffLine *DiffLine var diff1, diff2 string
var diff1, diff2 string
diffLine.ParsedContent = template.HTML(html.EscapeString(diffLine.Content[1:])) getDefaultReturn := func() template.HTML {
return template.HTML(html.EscapeString(diffLine.Content[1:]))
// just compute diff for adds and removes
if diffLine.Type != DIFF_LINE_ADD && diffLine.Type != DIFF_LINE_DEL {
continue
}
// try to find equivalent diff line. ignore, otherwise
if diffLine.Type == DIFF_LINE_ADD {
compareDiffLine = diffSection.GetLine(DIFF_LINE_DEL, diffLine.RightIdx)
if compareDiffLine == nil {
continue
}
diff1 = compareDiffLine.Content
diff2 = diffLine.Content
} else {
compareDiffLine = diffSection.GetLine(DIFF_LINE_ADD, diffLine.LeftIdx)
if compareDiffLine == nil {
continue
}
diff1 = diffLine.Content
diff2 = compareDiffLine.Content
}
dmp := diffmatchpatch.New()
diffRecord := dmp.DiffMain(diff1[1:], diff2[1:], true)
diffRecord = dmp.DiffCleanupSemantic(diffRecord)
diffLine.ParsedContent = diffToHTML(diffRecord, diffLine.Type)
} }
// just compute diff for adds and removes
if diffLine.Type != DIFF_LINE_ADD && diffLine.Type != DIFF_LINE_DEL {
return getDefaultReturn()
}
// try to find equivalent diff line. ignore, otherwise
if diffLine.Type == DIFF_LINE_ADD {
compareDiffLine = diffSection.GetLine(DIFF_LINE_DEL, diffLine.RightIdx)
if compareDiffLine == nil {
return getDefaultReturn()
}
diff1 = compareDiffLine.Content
diff2 = diffLine.Content
} else {
compareDiffLine = diffSection.GetLine(DIFF_LINE_ADD, diffLine.LeftIdx)
if compareDiffLine == nil {
return getDefaultReturn()
}
diff1 = diffLine.Content
diff2 = compareDiffLine.Content
}
dmp := diffmatchpatch.New()
diffRecord := dmp.DiffMain(diff1[1:], diff2[1:], true)
diffRecord = dmp.DiffCleanupSemantic(diffRecord)
return diffToHTML(diffRecord, diffLine.Type)
} }
type DiffFile struct { type DiffFile struct {

View file

@ -168,12 +168,6 @@ func Diff(ctx *middleware.Context) {
} }
} }
for _, diffFile := range diff.Files {
for _, diffSection := range diffFile.Sections {
diffSection.ComputeLinesDiff()
}
}
ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split" ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
ctx.Data["Username"] = userName ctx.Data["Username"] = userName
ctx.Data["Reponame"] = repoName ctx.Data["Reponame"] = repoName

View file

@ -349,12 +349,6 @@ func ViewPullFiles(ctx *middleware.Context) {
ctx.Data["Diff"] = diff ctx.Data["Diff"] = diff
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0 ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
for _, diffFile := range diff.Files {
for _, diffSection := range diffFile.Sections {
diffSection.ComputeLinesDiff()
}
}
commit, err := gitRepo.GetCommit(endCommitID) commit, err := gitRepo.GetCommit(endCommitID)
if err != nil { if err != nil {
ctx.Handle(500, "GetCommit", err) ctx.Handle(500, "GetCommit", err)

View file

@ -76,13 +76,13 @@
<span rel="{{if $line.LeftIdx}}diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}{{end}}">{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}</span> <span rel="{{if $line.LeftIdx}}diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}{{end}}">{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}</span>
</td> </td>
<td class="lines-code halfwidth"> <td class="lines-code halfwidth">
<pre class="wrap">{{if $line.LeftIdx}}{{$line.ParsedContent}}{{end}}</pre> <pre class="wrap">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</pre>
</td> </td>
<td class="lines-num lines-num-new"> <td class="lines-num lines-num-new">
<span rel="{{if $line.RightIdx}}diff-{{Sha1 $file.Name}}R{{$line.RightIdx}}{{end}}">{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}</span> <span rel="{{if $line.RightIdx}}diff-{{Sha1 $file.Name}}R{{$line.RightIdx}}{{end}}">{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}</span>
</td> </td>
<td class="lines-code halfwidth"> <td class="lines-code halfwidth">
<pre class="wrap">{{if $line.RightIdx}}{{$line.ParsedContent}}{{end}}</pre> <pre class="wrap">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</pre>
</td> </td>
</tr> </tr>
{{end}} {{end}}
@ -104,7 +104,7 @@
</td> </td>
{{end}} {{end}}
<td class="lines-code"> <td class="lines-code">
<pre>{{$line.ParsedContent}}</pre> <pre>{{$section.GetComputedInlineDiffFor $line}}</pre>
</td> </td>
</tr> </tr>
{{end}} {{end}}