jenkins-bot has submitted this change and it was merged.

Change subject: diff: do not highlight entire line when only text is removed
......................................................................


diff: do not highlight entire line when only text is removed

When the last changed-to line in a hunk lacks formatting, reuse the format
indication of the changed-from line.

Bug: T130572
Change-Id: If91bc6de20d8634c5adf8b18aee4937a663fa925
---
M pywikibot/diff.py
1 file changed, 30 insertions(+), 9 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/diff.py b/pywikibot/diff.py
index f83033f..4acc645 100644
--- a/pywikibot/diff.py
+++ b/pywikibot/diff.py
@@ -118,20 +118,41 @@
         """Color diff lines."""
         diff = iter(self.diff)
 
-        l1, l2 = '', next(diff)
+        fmt, line1, line2 = '', '', next(diff)
         for line in diff:
-            l1, l2 = l2, line
+            fmt, line1, line2 = line1, line2, line
             # do not show lines starting with '?'.
-            if l1.startswith('?'):
+            if line1.startswith('?'):
                 continue
-            if l2.startswith('?'):
-                yield self.color_line(l1, l2)
-            else:
-                yield self.color_line(l1)
+            if line2.startswith('?'):
+                yield self.color_line(line1, line2)
+                continue
+            if line1.startswith('-'):
+                # Color whole line to be removed.
+                yield self.color_line(line1)
+            elif line1.startswith('+'):
+                # Reuse last available fmt as diff line, if possible,
+                # or color whole line to be added.
+                fmt = fmt if fmt.startswith('?') else ''
+                fmt = fmt[:min(len(fmt), len(line1))]
+                fmt = fmt if fmt else None
+                yield self.color_line(line1, fmt)
 
         # handle last line
-        if not l2.startswith('?'):
-            yield self.color_line(l2)
+        # If line line2 is removed, color the whole line.
+        # If line line2 is added, check if line1 is a '?-type' line, to prevent
+        # the entire line line2 to be colored (see T130572).
+        # The case where line2 start with '?' has been covered already.
+        if line2.startswith('-'):
+            # Color whole line to be removed.
+            yield self.color_line(line2)
+        elif line2.startswith('+'):
+            # Reuse last available line1 as diff line, if possible,
+            # or color whole line to be added.
+            fmt = line1 if line1.startswith('?') else ''
+            fmt = fmt[:min(len(fmt), len(line2))]
+            fmt = fmt if fmt else None
+            yield self.color_line(line2, fmt)
 
     def color_line(self, line, line_ref=None):
         """Color line characters.

-- 
To view, visit https://gerrit.wikimedia.org/r/278967
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If91bc6de20d8634c5adf8b18aee4937a663fa925
Gerrit-PatchSet: 7
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Mpaa <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to