feat: Improve diff being generated

Add `DiffCleanupSemantic` into the mix when generated diffs (PR review,
commit view and issue/comment history). This avoids trying to produce a
optimal diff and tries to reduce the amount of edits, by combing them
into larger edits, which is nicer and easier to 'look at'. There's no
need for a perfect minimal diff, as the output isn't being parsed by a
computer, it's parsed by people.

Ref: https://codeberg.org/forgejo/forgejo/issues/4996
This commit is contained in:
Gusted 2024-08-18 23:26:41 +02:00 committed by Otto Richter
parent d97e36f6d7
commit 58ee9fdc4a
2 changed files with 2 additions and 0 deletions

View file

@ -154,6 +154,7 @@ func GetContentHistoryDetail(ctx *context.Context) {
dmp := diffmatchpatch.New()
// `checklines=false` makes better diff result
diff := dmp.DiffMain(prevHistoryContentText, history.ContentText, false)
diff = dmp.DiffCleanupSemantic(diff)
diff = dmp.DiffCleanupEfficiency(diff)
// use chroma to render the diff html

View file

@ -97,6 +97,7 @@ func (hcd *HighlightCodeDiff) diffWithHighlight(filename, language, codeA, codeB
convertedCodeB := hcd.ConvertToPlaceholders(string(highlightCodeB))
diffs := diffMatchPatch.DiffMain(convertedCodeA, convertedCodeB, true)
diffs = diffMatchPatch.DiffCleanupSemantic(diffs)
diffs = diffMatchPatch.DiffCleanupEfficiency(diffs)
for i := range diffs {