[meld] filediff: Use cursor position for EOF chunk deletion (bgo#778856 take 2)



commit 18b6e4bda5578e711d33efa7fed7583dc956c9aa
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Mon Feb 20 06:14:43 2017 +1000

    filediff: Use cursor position for EOF chunk deletion (bgo#778856 take 2)
    
    A problem with the previous approach, demonstrated by the test cases
    added, is that GtkTextView inexplicably doesn't bother to add newlines
    that match the existing ones while editing a file.
    
    The new approach is to avoid doing this calculation manually ourselves,
    and instead rely on cursor position manipulation, since that
    will/should always result in us ending up on the last non-linebreak
    character of the previous line.

 meld/filediff.py           |    5 +----
 test/test_chunk_actions.py |   10 +++++++---
 2 files changed, 8 insertions(+), 7 deletions(-)
---
diff --git a/meld/filediff.py b/meld/filediff.py
index 8b05609..81a96ce 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -1818,10 +1818,7 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
         if chunk[2] >= b0.get_line_count():
             # If this is the end of the buffer, we need to remove the
             # previous newline, because the current line has none.
-            it.backward_char()
-            newline_type = b0.data.sourcefile.get_newline_type()
-            if newline_type == GtkSource.NewlineType.CR_LF:
-                it.backward_char()
+            it.backward_cursor_position()
         b0.delete(it, b0.get_iter_at_line_or_eof(chunk[2]))
         mark0 = b0.create_mark(None, it, True)
         mark1 = b0.create_mark(None, it, True)
diff --git a/test/test_chunk_actions.py b/test/test_chunk_actions.py
index b233303..b2c2c10 100644
--- a/test/test_chunk_actions.py
+++ b/test/test_chunk_actions.py
@@ -21,9 +21,13 @@ from meld.matchers.myers import DiffChunk
     ("ree\reee", GtkSource.NewlineType.LF, 'ree'),
     ("ree\reee\r", GtkSource.NewlineType.LF, 'ree\reee'),
 
-    # This case is intentionally incorrect; the newline information is
-    # wrong for the text, and the result is also incorrect.
-    ("ree\r\neee", GtkSource.NewlineType.CR, 'ree\r'),
+    # Mismatched newline and text
+    ("ree\r\neee", GtkSource.NewlineType.CR, 'ree'),
+
+    # Mismatched newline types within text
+    ("ree\r\neee\n", GtkSource.NewlineType.CR_LF, 'ree\r\neee'),
+    ("ree\r\neee\nqqq", GtkSource.NewlineType.CR_LF, 'ree\r\neee'),
+    ("ree\r\neee\nqqq\r\n", GtkSource.NewlineType.CR_LF, 'ree\r\neee\nqqq'),
 ])
 def test_delete_last_line_crlf(text, newline, expected_text):
     filediff = mock.Mock(FileDiff)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]