Skip to content

Commit d06b0b2

Browse files
committed
filediff: Refactor next_diff to be usable for all diff traversals
1 parent 894fd3c commit d06b0b2

2 files changed

Lines changed: 39 additions & 39 deletions

File tree

data/ui/filediff.ui

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@
4343
<object class="GtkAction" id="PrevConflict">
4444
<property name="label" translatable="yes">Previous Conflict</property>
4545
<property name="tooltip" translatable="yes">Go to the previous conflict</property>
46-
<signal name="activate" handler="on_next_conflict(Gdk.ScrollDirection.UP)" swapped="no"/>
46+
<signal name="activate" handler="action_previous_conflict" swapped="no"/>
4747
</object>
4848
<accelerator key="I" modifiers="GDK_CONTROL_MASK"/>
4949
</child>
5050
<child>
5151
<object class="GtkAction" id="NextConflict">
5252
<property name="label" translatable="yes">Next Conflict</property>
5353
<property name="tooltip" translatable="yes">Go to the next conflict</property>
54-
<signal name="activate" handler="on_next_conflict(Gdk.ScrollDirection.DOWN)" swapped="no"/>
54+
<signal name="activate" handler="action_next_conflict" swapped="no"/>
5555
</object>
5656
<accelerator key="K" modifiers="GDK_CONTROL_MASK"/>
5757
</child>

meld/filediff.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -517,20 +517,44 @@ def on_next_conflict_changed(self, doc, have_prev, have_next):
517517
self.actiongroup.get_action("PrevConflict").set_sensitive(have_prev)
518518
self.actiongroup.get_action("NextConflict").set_sensitive(have_next)
519519

520-
def on_next_conflict(self, direction):
521-
if direction == Gdk.ScrollDirection.DOWN:
522-
target = self.cursor.next_conflict
523-
else: # direction == Gdk.ScrollDirection.UP
524-
target = self.cursor.prev_conflict
525-
520+
def go_to_chunk(self, target, pane=None, centered=False):
526521
if target is None:
527522
return
528523

529-
buf = self.textbuffer[self.cursor.pane]
530-
chunk = self.linediffer.get_chunk(target, self.cursor.pane)
531-
buf.place_cursor(buf.get_iter_at_line(chunk[1]))
532-
self.textview[self.cursor.pane].scroll_to_mark(
533-
buf.get_insert(), 0.1, True, 0.5, 0.5)
524+
if pane is None:
525+
pane = self._get_focused_pane()
526+
if pane == -1:
527+
pane = 1 if len(self.textview) > 1 else 0
528+
529+
chunk = self.linediffer.get_chunk(target, pane)
530+
if not chunk:
531+
return
532+
533+
# Warp the cursor to the first line of the chunk
534+
buf = self.textbuffer[pane]
535+
if self.cursor.line != chunk[1]:
536+
buf.place_cursor(buf.get_iter_at_line(chunk[1]))
537+
538+
tolerance = 0.0 if centered else 0.2
539+
self.textview[pane].scroll_to_mark(
540+
buf.get_insert(), tolerance, True, 0.5, 0.5)
541+
542+
def next_diff(self, direction, centered=False):
543+
target = (self.cursor.next if direction == Gdk.ScrollDirection.DOWN
544+
else self.cursor.prev)
545+
go_to_chunk(target, centered=centered)
546+
547+
def action_previous_conflict(self, *args):
548+
self.go_to_chunk(self.cursor.prev_conflict, self.cursor.pane)
549+
550+
def action_next_conflict(self, *args):
551+
self.go_to_chunk(self.cursor.next_conflict, self.cursor.pane)
552+
553+
def action_previous_diff(self, *args):
554+
self.go_to_chunk(self.cursor.prev)
555+
556+
def action_next_diff(self, *args):
557+
self.go_to_chunk(self.cursor.next)
534558

535559
def push_change(self, direction):
536560
src = self._get_focused_pane()
@@ -1138,7 +1162,8 @@ def _diff_files(self, refresh=False):
11381162

11391163
if self.cursor.next is not None:
11401164
self.scheduler.add_task(
1141-
lambda: self.next_diff(Gdk.ScrollDirection.DOWN, True), True)
1165+
lambda: self.go_to_chunk(self.cursor.next, centered=True),
1166+
True)
11421167
else:
11431168
buf = self.textbuffer[1 if self.num_panes > 1 else 0]
11441169
self.on_cursor_position_changed(buf, None, True)
@@ -1848,31 +1873,6 @@ def coords_by_chunk():
18481873
self.queue_draw()
18491874
self.recompute_label()
18501875

1851-
def next_diff(self, direction, centered=False):
1852-
target = (self.cursor.next if direction == Gdk.ScrollDirection.DOWN
1853-
else self.cursor.prev)
1854-
if target is None:
1855-
return
1856-
1857-
pane = self._get_focused_pane()
1858-
if pane == -1:
1859-
if len(self.textview) > 1:
1860-
pane = 1
1861-
else:
1862-
pane = 0
1863-
1864-
chunk = self.linediffer.get_chunk(target, pane)
1865-
if not chunk:
1866-
return
1867-
1868-
# Warp the cursor to the first line of next chunk
1869-
buf = self.textbuffer[pane]
1870-
if self.cursor.line != chunk[1]:
1871-
buf.place_cursor(buf.get_iter_at_line(chunk[1]))
1872-
tolerance = 0.0 if centered else 0.2
1873-
self.textview[pane].scroll_to_mark(
1874-
buf.get_insert(), tolerance, True, 0.5, 0.5)
1875-
18761876
def copy_chunk(self, src, dst, chunk, copy_up):
18771877
b0, b1 = self.textbuffer[src], self.textbuffer[dst]
18781878
start = b0.get_iter_at_line_or_eof(chunk[1])

0 commit comments

Comments
 (0)