Skip to content

Commit dd7b8fd

Browse files
committed
filediff: Refactor chunk-based actions to remove params from callback
1 parent d06b0b2 commit dd7b8fd

2 files changed

Lines changed: 55 additions & 31 deletions

File tree

data/ui/filediff.ui

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<property name="label" translatable="yes">Push to Left</property>
6161
<property name="tooltip" translatable="yes">Push current change to the left</property>
6262
<property name="stock_id">gtk-go-back</property>
63-
<signal name="activate" handler="push_change(-1)" swapped="no"/>
63+
<signal name="activate" handler="action_push_change_left" swapped="no"/>
6464
</object>
6565
<accelerator key="Left" modifiers="GDK_MOD1_MASK"/>
6666
</child>
@@ -69,17 +69,17 @@
6969
<property name="label" translatable="yes">Push to Right</property>
7070
<property name="tooltip" translatable="yes">Push current change to the right</property>
7171
<property name="stock_id">gtk-go-forward</property>
72-
<signal name="activate" handler="push_change(1)" swapped="no"/>
72+
<signal name="activate" handler="action_push_change_right" swapped="no"/>
7373
</object>
74-
<accelerator key="Right" modifiers=""/>
74+
<accelerator key="Right" modifiers="GDK_MOD1_MASK"/>
7575
</child>
7676
<!-- FIXME: using LAST and FIRST is terrible and unreliable icon abuse -->
7777
<child>
7878
<object class="GtkAction" id="PullLeft">
7979
<property name="label" translatable="yes">Pull from Left</property>
8080
<property name="tooltip" translatable="yes">Pull change from the left</property>
8181
<property name="stock_id">gtk-goto-last</property>
82-
<signal name="activate" handler="pull_change(-1)" swapped="no"/>
82+
<signal name="activate" handler="action_pull_change_left" swapped="no"/>
8383
</object>
8484
<accelerator key="Right" modifiers="GDK_MOD1_MASK | GDK_SHIFT_MASK"/>
8585
</child>
@@ -88,39 +88,39 @@
8888
<property name="label" translatable="yes">Pull from Right</property>
8989
<property name="tooltip" translatable="yes">Pull change from the right</property>
9090
<property name="stock_id">gtk-goto-first</property>
91-
<signal name="activate" handler="pull_change(1)" swapped="no"/>
91+
<signal name="activate" handler="action_pull_change_right" swapped="no"/>
9292
</object>
9393
<accelerator key="Left" modifiers="GDK_MOD1_MASK | GDK_SHIFT_MASK"/>
9494
</child>
9595
<child>
9696
<object class="GtkAction" id="CopyLeftUp">
9797
<property name="label" translatable="yes">Copy Above Left</property>
9898
<property name="tooltip" translatable="yes">Copy change above the left chunk</property>
99-
<signal name="activate" handler="copy_change(-1, -1)" swapped="no"/>
99+
<signal name="activate" handler="action_copy_change_left_up" swapped="no"/>
100100
</object>
101101
<accelerator key="bracketleft" modifiers="GDK_MOD1_MASK"/>
102102
</child>
103103
<child>
104104
<object class="GtkAction" id="CopyLeftDown">
105105
<property name="label" translatable="yes">Copy Below Left</property>
106106
<property name="tooltip" translatable="yes">Copy change below the left chunk</property>
107-
<signal name="activate" handler="self.copy_change(-1, 1)" swapped="no"/>
107+
<signal name="activate" handler="action_copy_change_left_down" swapped="no"/>
108108
</object>
109109
<accelerator key="semicolon" modifiers="GDK_MOD1_MASK"/>
110110
</child>
111111
<child>
112112
<object class="GtkAction" id="CopyRightUp">
113113
<property name="label" translatable="yes">Copy Above Right</property>
114114
<property name="tooltip" translatable="yes">Copy change above the right chunk</property>
115-
<signal name="activate" handler="copy_change(1, -1)" swapped="no"/>
115+
<signal name="activate" handler="action_copy_change_right_up" swapped="no"/>
116116
</object>
117117
<accelerator key="bracketright" modifiers="GDK_MOD1_MASK"/>
118118
</child>
119119
<child>
120120
<object class="GtkAction" id="CopyRightDown">
121121
<property name="label" translatable="yes">Copy Below Right</property>
122122
<property name="tooltip" translatable="yes">Copy change below the right chunk</property>
123-
<signal name="activate" handler="copy_change(1, 1)" swapped="no"/>
123+
<signal name="activate" handler="action_copy_change_right_down" swapped="no"/>
124124
</object>
125125
<accelerator key="quoteright" modifiers="GDK_MOD1_MASK"/>
126126
</child>

meld/filediff.py

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -556,33 +556,57 @@ def action_previous_diff(self, *args):
556556
def action_next_diff(self, *args):
557557
self.go_to_chunk(self.cursor.next)
558558

559-
def push_change(self, direction):
560-
src = self._get_focused_pane()
561-
dst = src + direction
562-
chunk = self.linediffer.get_chunk(self.cursor.chunk, src, dst)
563-
assert(src != -1 and self.cursor.chunk is not None)
564-
assert(dst in (0, 1, 2))
565-
assert(chunk is not None)
566-
self.replace_chunk(src, dst, chunk)
559+
def get_action_chunk(self, src, dst):
560+
valid_panes = list(range(0, self.num_panes))
561+
if (src not in valid_panes or dst not in valid_panes or
562+
self.cursor.chunk is None):
563+
raise ValueError("Action was taken on invalid panes")
567564

568-
def pull_change(self, direction):
569-
dst = self._get_focused_pane()
570-
src = dst + direction
571565
chunk = self.linediffer.get_chunk(self.cursor.chunk, src, dst)
572-
assert(dst != -1 and self.cursor.chunk is not None)
573-
assert(src in (0, 1, 2))
574-
assert(chunk is not None)
575-
self.replace_chunk(src, dst, chunk)
566+
if chunk is None:
567+
raise ValueError("Action was taken on a missing chunk")
568+
return chunk
576569

577-
def copy_change(self, direction, copy_direction):
570+
def get_action_panes(self, direction, reverse=False):
578571
src = self._get_focused_pane()
579572
dst = src + direction
580-
chunk = self.linediffer.get_chunk(self.cursor.chunk, src, dst)
581-
assert(src != -1 and self.cursor.chunk is not None)
582-
assert(dst in (0, 1, 2))
583-
assert(chunk is not None)
584-
copy_up = True if copy_direction < 0 else False
585-
self.copy_chunk(src, dst, chunk, copy_up)
573+
return (dst, src) if reverse else (src, dst)
574+
575+
def action_push_change_left(self, *args):
576+
src, dst = self.get_action_panes(-1)
577+
self.replace_chunk(src, dst, self.get_action_chunk(src, dst))
578+
579+
def action_push_change_right(self, *args):
580+
src, dst = self.get_action_panes(+1)
581+
self.replace_chunk(src, dst, self.get_action_chunk(src, dst))
582+
583+
def action_pull_change_left(self, *args):
584+
src, dst = self.get_action_panes(-1, reverse=True)
585+
self.replace_chunk(src, dst, self.get_action_chunk(src, dst))
586+
587+
def action_pull_change_right(self, *args):
588+
src, dst = self.get_action_panes(+1, reverse=True)
589+
self.replace_chunk(src, dst, self.get_action_chunk(src, dst))
590+
591+
def action_copy_change_left_up(self, *args):
592+
src, dst = self.get_action_panes(-1)
593+
self.copy_chunk(
594+
src, dst, self.get_action_chunk(src, dst), copy_up=True)
595+
596+
def action_copy_change_right_up(self, *args):
597+
src, dst = self.get_action_panes(+1)
598+
self.copy_chunk(
599+
src, dst, self.get_action_chunk(src, dst), copy_up=True)
600+
601+
def action_copy_change_left_down(self, *args):
602+
src, dst = self.get_action_panes(-1)
603+
self.copy_chunk(
604+
src, dst, self.get_action_chunk(src, dst), copy_up=False)
605+
606+
def action_copy_change_right_down(self, *args):
607+
src, dst = self.get_action_panes(+1)
608+
self.copy_chunk(
609+
src, dst, self.get_action_chunk(src, dst), copy_up=False)
586610

587611
def pull_all_non_conflicting_changes(self, direction):
588612
assert direction in (-1, 1)

0 commit comments

Comments
 (0)