diff --git a/core/commands/diff.py b/core/commands/diff.py index 1e2031dd9..73a05164f 100644 --- a/core/commands/diff.py +++ b/core/commands/diff.py @@ -15,7 +15,7 @@ from . import intra_line_colorizer from . import stage_hunk from .navigate import GsNavigate -from ..fns import head, filter_, flatten, pairwise, unique +from ..fns import head, filter_, flatten, unique from ..parse_diff import SplittedDiff from ..git_command import GitCommand from ..runtime import ensure_on_ui, enqueue_on_worker @@ -1017,19 +1017,23 @@ class gs_diff_navigate(GsNavigate): offset = 0 log_position = True + shrink_to_cursor = False def get_available_regions(self): - return [ - sublime.Region(a.a, b.a - 1) - for a, b in pairwise( - chain( - self.view.find_by_selector( - "meta.diff.range.unified, meta.commit-info.header" - ), - [sublime.Region(self.view.size())] - ) - ) - ] + def _gen(): + # type: () -> Iterator[sublime.Region] + diff = SplittedDiff.from_view(self.view) + for hunk in diff.hunks: + yield sublime.Region(hunk.region().a) + chunks = list(chunkby(hunk.content().lines(), lambda line: not line.is_context())) + if len(chunks) > 1: + for chunk in chunks: + yield sublime.Region(chunk[0].region().a, chunk[-1].region().b) + + return sorted( + list(_gen()) + + self.view.find_by_selector("meta.commit-info.header") + ) class gs_diff_undo(TextCommand, GitCommand): diff --git a/core/commands/navigate.py b/core/commands/navigate.py index 96104522b..2b1b64b3e 100644 --- a/core/commands/navigate.py +++ b/core/commands/navigate.py @@ -20,6 +20,7 @@ class GsNavigate(TextCommand, GitCommand): show_at_center = False wrap = True wrap_with_force = False + shrink_to_cursor = True log_position = False # For the first entry, try to show the beginning of the buffer. # (Usually we have some info/help text there.) @@ -53,9 +54,11 @@ def run(self, edit, forward=True): self._just_jumped = 2 sel.clear() - # Position the cursor at the beginning of the section... - new_cursor_position = wanted_section.begin() + self.offset - sel.add(sublime.Region(new_cursor_position)) + if self.shrink_to_cursor or self.offset: + new_cursor_position = sublime.Region(wanted_section.begin() + self.offset) + else: + new_cursor_position = sublime.Region(wanted_section.b, wanted_section.a) + sel.add(new_cursor_position) if self.show_at_center: self.view.show_at_center(new_cursor_position)