Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigate chunkwise in the diff view #1829

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions core/commands/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 6 additions & 3 deletions core/commands/navigate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down Expand Up @@ -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)
Expand Down