diff: Add keyboard shortcuts to got to next/previous change

This commit is contained in:
Kovid Goyal 2018-05-09 08:12:40 +05:30
parent 3a1f85cb69
commit a65c807a4a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 24 additions and 7 deletions

View File

@ -97,6 +97,18 @@ def current_position(self, ref):
def num_lines(self):
return self.screen_size.rows - 1
def scroll_to_next_change(self, backwards=False):
if backwards:
r = range(self.scroll_pos - 1, -1, -1)
else:
r = range(self.scroll_pos + 1, len(self.diff_lines))
for i in r:
line = self.diff_lines[i]
if line.is_change_start:
self.scroll_lines(i - self.scroll_pos)
return
self.cmd.bell()
def set_scrolling_region(self):
self.cmd.set_scrolling_region(self.screen_size, 0, self.num_lines - 2)
@ -191,6 +203,9 @@ def on_text(self, text, in_bracketed_paste=False):
else:
new_ctx += (-1 if text == '-' else 1) * 5
self.change_context_count(new_ctx)
if text in 'np':
self.scroll_to_next_change(backwards=text == 'p')
return
def on_key(self, key_event):
if key_event.type is RELEASE:

View File

@ -45,16 +45,18 @@ def __init__(self, path, extra=None):
class Line:
__slots__ = ('text', 'ref')
__slots__ = ('text', 'ref', 'is_change_start')
def __init__(self, text, ref):
def __init__(self, text, ref, change_start=False):
self.text = text
self.ref = ref
self.is_change_start = change_start
def yield_lines_from(iterator, reference):
def yield_lines_from(iterator, reference, is_change_start=True):
for text in iterator:
yield Line(text, reference)
yield Line(text, reference, is_change_start)
is_change_start = False
def human_readable(size, sep=' '):
@ -305,7 +307,7 @@ def lines_for_chunk(data, hunk_num, chunk, chunk_num):
x.extend(repeat(data.filler_line, count))
for wli, (left_line, right_line) in enumerate(zip(ll, rl)):
ref = Reference(ref_path, LineRef(ref_ln, wli))
yield Line(left_line + right_line, ref)
yield Line(left_line + right_line, ref, i == 0 and wli == 0)
def lines_for_diff(left_path, right_path, hunks, args, columns, margin_size):
@ -340,7 +342,7 @@ def highlights(num):
'', _('This file was added') if is_add else _('This file was removed'),
'filler', margin_size, available_cols)
text = (empty + hl) if is_add else (hl + empty)
yield Line(text, ref)
yield Line(text, ref, line_number == 0 and i == 0)
def rename_lines(path, other_path, args, columns, margin_size):
@ -363,7 +365,7 @@ def render_diff(collection, diff_map, args, columns):
for path, item_type, other_path in collection:
item_ref = Reference(path)
is_binary = isinstance(data_for_path(path), bytes)
yield from yield_lines_from(title_lines(path, other_path, args, columns, margin_size), item_ref)
yield from yield_lines_from(title_lines(path, other_path, args, columns, margin_size), item_ref, False)
if item_type == 'diff':
if is_binary:
ans = yield_lines_from(binary_lines(path, other_path, columns, margin_size), item_ref)