[meld] filediff: Refactor chunk-based actions to remove params from callback



commit dd7b8fd87cdf306a17f52c56561a7608d500b323
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sun Dec 14 08:00:56 2014 +1000

    filediff: Refactor chunk-based actions to remove params from callback

 data/ui/filediff.ui |   18 ++++++------
 meld/filediff.py    |   68 ++++++++++++++++++++++++++++++++++----------------
 2 files changed, 55 insertions(+), 31 deletions(-)
---
diff --git a/data/ui/filediff.ui b/data/ui/filediff.ui
index c0103db..1dbadd4 100644
--- a/data/ui/filediff.ui
+++ b/data/ui/filediff.ui
@@ -60,7 +60,7 @@
         <property name="label" translatable="yes">Push to Left</property>
         <property name="tooltip" translatable="yes">Push current change to the left</property>
         <property name="stock_id">gtk-go-back</property>
-        <signal name="activate" handler="push_change(-1)" swapped="no"/>
+        <signal name="activate" handler="action_push_change_left" swapped="no"/>
       </object>
       <accelerator key="Left" modifiers="GDK_MOD1_MASK"/>
     </child>
@@ -69,9 +69,9 @@
         <property name="label" translatable="yes">Push to Right</property>
         <property name="tooltip" translatable="yes">Push current change to the right</property>
         <property name="stock_id">gtk-go-forward</property>
-        <signal name="activate" handler="push_change(1)" swapped="no"/>
+        <signal name="activate" handler="action_push_change_right" swapped="no"/>
       </object>
-      <accelerator key="Right" modifiers=""/>
+      <accelerator key="Right" modifiers="GDK_MOD1_MASK"/>
     </child>
     <!-- FIXME: using LAST and FIRST is terrible and unreliable icon abuse -->
     <child>
@@ -79,7 +79,7 @@
         <property name="label" translatable="yes">Pull from Left</property>
         <property name="tooltip" translatable="yes">Pull change from the left</property>
         <property name="stock_id">gtk-goto-last</property>
-        <signal name="activate" handler="pull_change(-1)" swapped="no"/>
+        <signal name="activate" handler="action_pull_change_left" swapped="no"/>
       </object>
       <accelerator key="Right" modifiers="GDK_MOD1_MASK | GDK_SHIFT_MASK"/>
     </child>
@@ -88,7 +88,7 @@
         <property name="label" translatable="yes">Pull from Right</property>
         <property name="tooltip" translatable="yes">Pull change from the right</property>
         <property name="stock_id">gtk-goto-first</property>
-        <signal name="activate" handler="pull_change(1)" swapped="no"/>
+        <signal name="activate" handler="action_pull_change_right" swapped="no"/>
       </object>
       <accelerator key="Left" modifiers="GDK_MOD1_MASK | GDK_SHIFT_MASK"/>
     </child>
@@ -96,7 +96,7 @@
       <object class="GtkAction" id="CopyLeftUp">
         <property name="label" translatable="yes">Copy Above Left</property>
         <property name="tooltip" translatable="yes">Copy change above the left chunk</property>
-        <signal name="activate" handler="copy_change(-1, -1)" swapped="no"/>
+        <signal name="activate" handler="action_copy_change_left_up" swapped="no"/>
       </object>
       <accelerator key="bracketleft" modifiers="GDK_MOD1_MASK"/>
     </child>
@@ -104,7 +104,7 @@
       <object class="GtkAction" id="CopyLeftDown">
         <property name="label" translatable="yes">Copy Below Left</property>
         <property name="tooltip" translatable="yes">Copy change below the left chunk</property>
-        <signal name="activate" handler="self.copy_change(-1, 1)" swapped="no"/>
+        <signal name="activate" handler="action_copy_change_left_down" swapped="no"/>
       </object>
       <accelerator key="semicolon" modifiers="GDK_MOD1_MASK"/>
     </child>
@@ -112,7 +112,7 @@
       <object class="GtkAction" id="CopyRightUp">
         <property name="label" translatable="yes">Copy Above Right</property>
         <property name="tooltip" translatable="yes">Copy change above the right chunk</property>
-        <signal name="activate" handler="copy_change(1, -1)" swapped="no"/>
+        <signal name="activate" handler="action_copy_change_right_up" swapped="no"/>
       </object>
       <accelerator key="bracketright" modifiers="GDK_MOD1_MASK"/>
     </child>
@@ -120,7 +120,7 @@
       <object class="GtkAction" id="CopyRightDown">
         <property name="label" translatable="yes">Copy Below Right</property>
         <property name="tooltip" translatable="yes">Copy change below the right chunk</property>
-        <signal name="activate" handler="copy_change(1, 1)" swapped="no"/>
+        <signal name="activate" handler="action_copy_change_right_down" swapped="no"/>
       </object>
       <accelerator key="quoteright" modifiers="GDK_MOD1_MASK"/>
     </child>
diff --git a/meld/filediff.py b/meld/filediff.py
index d06a553..3e3da6b 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -556,33 +556,57 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
     def action_next_diff(self, *args):
         self.go_to_chunk(self.cursor.next)
 
-    def push_change(self, direction):
-        src = self._get_focused_pane()
-        dst = src + direction
-        chunk = self.linediffer.get_chunk(self.cursor.chunk, src, dst)
-        assert(src != -1 and self.cursor.chunk is not None)
-        assert(dst in (0, 1, 2))
-        assert(chunk is not None)
-        self.replace_chunk(src, dst, chunk)
+    def get_action_chunk(self, src, dst):
+        valid_panes = list(range(0, self.num_panes))
+        if (src not in valid_panes or dst not in valid_panes or
+                self.cursor.chunk is None):
+            raise ValueError("Action was taken on invalid panes")
 
-    def pull_change(self, direction):
-        dst = self._get_focused_pane()
-        src = dst + direction
         chunk = self.linediffer.get_chunk(self.cursor.chunk, src, dst)
-        assert(dst != -1 and self.cursor.chunk is not None)
-        assert(src in (0, 1, 2))
-        assert(chunk is not None)
-        self.replace_chunk(src, dst, chunk)
+        if chunk is None:
+            raise ValueError("Action was taken on a missing chunk")
+        return chunk
 
-    def copy_change(self, direction, copy_direction):
+    def get_action_panes(self, direction, reverse=False):
         src = self._get_focused_pane()
         dst = src + direction
-        chunk = self.linediffer.get_chunk(self.cursor.chunk, src, dst)
-        assert(src != -1 and self.cursor.chunk is not None)
-        assert(dst in (0, 1, 2))
-        assert(chunk is not None)
-        copy_up = True if copy_direction < 0 else False
-        self.copy_chunk(src, dst, chunk, copy_up)
+        return (dst, src) if reverse else (src, dst)
+
+    def action_push_change_left(self, *args):
+        src, dst = self.get_action_panes(-1)
+        self.replace_chunk(src, dst, self.get_action_chunk(src, dst))
+
+    def action_push_change_right(self, *args):
+        src, dst = self.get_action_panes(+1)
+        self.replace_chunk(src, dst, self.get_action_chunk(src, dst))
+
+    def action_pull_change_left(self, *args):
+        src, dst = self.get_action_panes(-1, reverse=True)
+        self.replace_chunk(src, dst, self.get_action_chunk(src, dst))
+
+    def action_pull_change_right(self, *args):
+        src, dst = self.get_action_panes(+1, reverse=True)
+        self.replace_chunk(src, dst, self.get_action_chunk(src, dst))
+
+    def action_copy_change_left_up(self, *args):
+        src, dst = self.get_action_panes(-1)
+        self.copy_chunk(
+            src, dst, self.get_action_chunk(src, dst), copy_up=True)
+
+    def action_copy_change_right_up(self, *args):
+        src, dst = self.get_action_panes(+1)
+        self.copy_chunk(
+            src, dst, self.get_action_chunk(src, dst), copy_up=True)
+
+    def action_copy_change_left_down(self, *args):
+        src, dst = self.get_action_panes(-1)
+        self.copy_chunk(
+            src, dst, self.get_action_chunk(src, dst), copy_up=False)
+
+    def action_copy_change_right_down(self, *args):
+        src, dst = self.get_action_panes(+1)
+        self.copy_chunk(
+            src, dst, self.get_action_chunk(src, dst), copy_up=False)
 
     def pull_all_non_conflicting_changes(self, direction):
         assert direction in (-1, 1)


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