fix incorrect diff in comments and descriptions - part 2/2 fixes #719

Sun, 14 Sep 2025 15:58:27 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Sep 2025 15:58:27 +0200
changeset 387
a0c4d4038f21
parent 386
41ce87983ab5
child 388
698541f09380

fix incorrect diff in comments and descriptions - part 2/2 fixes #719

src/main/kotlin/de/uapcore/lightpit/servlet/FeedServlet.kt file | annotate | diff | comparison | revisions
--- a/src/main/kotlin/de/uapcore/lightpit/servlet/FeedServlet.kt	Sun Sep 14 15:42:23 2025 +0200
+++ b/src/main/kotlin/de/uapcore/lightpit/servlet/FeedServlet.kt	Sun Sep 14 15:58:27 2025 +0200
@@ -59,6 +59,20 @@
         .build()
     )
 
+    /**
+     * Fixes the incompetence of Java DiffUtils.
+     * Only the CHANGE and EQUAL cases are presented correctly out of the box.
+     * For DELETE and INSERT we need to add the tags ourselves.
+     */
+    fun presentDiff(row: DiffRow): String {
+        return when (row.tag) {
+            DiffRow.Tag.CHANGE -> row.oldLine
+            DiffRow.Tag.DELETE -> "<strike style=\"color:red\">${row.oldLine}</strike>"
+            DiffRow.Tag.INSERT -> "<i style=\"color: green\">${row.newLine}</i>"
+            DiffRow.Tag.EQUAL -> row.oldLine
+        }
+    }
+
     private fun fullContent(data: IssueCommentHistoryEntry) =
         CommentDiff(
             issueid = data.issueid,
@@ -75,9 +89,9 @@
             project = cur.project,
             currentSubject = cur.subject,
             comment = diffGenerator.generateDiffRows(
-                next.comment.replace("\r", "").split('\n'),
-                cur.comment.replace("\r", "").split('\n')
-            ).joinToString("\n", transform = DiffRow::getOldLine)
+                listOf(next.comment.replace("\r", "")),
+                listOf(cur.comment.replace("\r", ""))
+            ).joinToString("\n", transform = this::presentDiff)
         )
 
     private fun fullContent(http: HttpRequest, issue: IssueHistoryEntry) = IssueDiff(
@@ -112,18 +126,18 @@
         return IssueDiff(
             id = curContent.id,
             project = curContent.project,
-            subject = result[0].oldLine,
-            component = result[1].oldLine,
-            status = result[2].oldLine,
-            category = result[3].oldLine,
-            assignee = result[4].oldLine,
-            eta = result[5].oldLine,
-            affected = result[6].oldLine,
-            resolved = result[7].oldLine,
+            subject = presentDiff(result[0]),
+            component = presentDiff(result[1]),
+            status = presentDiff(result[2]),
+            category = presentDiff(result[3]),
+            assignee = presentDiff(result[4]),
+            eta = presentDiff(result[5]),
+            affected = presentDiff(result[6]),
+            resolved = presentDiff(result[7]),
             description = diffGenerator.generateDiffRows(
                 nextContent.description.split('\n'),
                 curContent.description.split('\n')
-            ).joinToString("\n", transform = DiffRow::getOldLine)
+            ).joinToString("\n", transform = this::presentDiff)
         )
     }
 

mercurial