Skip to content

Commit

Permalink
Merge pull request #1830 from timbrel/toggle-sides
Browse files Browse the repository at this point in the history
  • Loading branch information
kaste authored Dec 8, 2023
2 parents 32d8a08 + 47f5f58 commit c242315
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
18 changes: 18 additions & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@
{ "key": "setting.git_savvy.inline_diff_view", "operator": "equal", "operand": true }
]
},
{
"keys": ["a"],
"command": "gs_inline_diff_toggle_side",
"args": {"side": "a"},
"context": [
{ "key": "setting.command_mode", "operator": "equal", "operand": false },
{ "key": "setting.git_savvy.inline_diff_view", "operator": "equal", "operand": true }
]
},
{
"keys": ["b"],
"command": "gs_inline_diff_toggle_side",
"args": {"side": "b"},
"context": [
{ "key": "setting.command_mode", "operator": "equal", "operand": false },
{ "key": "setting.git_savvy.inline_diff_view", "operator": "equal", "operand": true }
]
},
{
"keys": ["tab"],
"command": "gs_inline_diff_toggle_cached_mode",
Expand Down
1 change: 1 addition & 0 deletions core/commands/_help_popups.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class gs_inline_diff_help_tooltip(GsAbstractHelpPopup):
key_bindings = dedent("""\
### Actions ###
[tab] switch between staged/unstaged area
[a]/[b] show/hide the a and b (red or green) sides of the diff
[l] stage line, unstage in cached mode
[h] stage hunk, unstage in cached mode
[L] reset line
Expand Down
73 changes: 63 additions & 10 deletions core/commands/inline_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"gs_inline_diff",
"gs_inline_diff_open",
"gs_inline_diff_refresh",
"gs_inline_diff_toggle_side",
"gs_inline_diff_toggle_cached_mode",
"gs_inline_diff_stage_or_reset_line",
"gs_inline_diff_stage_or_reset_hunk",
Expand All @@ -37,7 +38,7 @@

MYPY = False
if MYPY:
from typing import Dict, Iterable, List, NamedTuple, Optional, Tuple
from typing import Dict, Iterable, List, Literal, NamedTuple, Optional, Tuple
from ..types import LineNo, ColNo, Row
from GitSavvy.common.util.parse_diff import Hunk as InlineDiff_Hunk

Expand Down Expand Up @@ -471,17 +472,18 @@ def draw(self, view, title, match_position, inline_diff_contents, hunks):
and self.savvy_settings.get("inline_diff_auto_scroll", True)
)

replace_view_content(view, inline_diff_contents)
self.view.set_name(title)
with reapply_possible_fold(view):
replace_view_content(view, inline_diff_contents)
view.set_name(title)

if match_position:
row, col, row_offset = match_position
new_row = translate_row_to_inline_diff(view, row)
apply_position(view, new_row, col, row_offset)
elif navigate_to_first_hunk:
view.run_command("gs_inline_diff_navigate_hunk")
if match_position:
row, col, row_offset = match_position
new_row = translate_row_to_inline_diff(view, row)
apply_position(view, new_row, col, row_offset)
elif navigate_to_first_hunk:
view.run_command("gs_inline_diff_navigate_hunk")

self.highlight_regions(hunks)
self.highlight_regions(hunks)

def get_inline_diff_contents(self, original_contents, diff):
# type: (str, List[InlineDiff_Hunk]) -> Tuple[str, List[HunkReference]]
Expand Down Expand Up @@ -608,6 +610,57 @@ def highlight_regions(self, replaced_lines):
)


@contextmanager
def reapply_possible_fold(view):
current_fold_mode = fold_mode(view)
if current_fold_mode == "ab":
yield
else:
view.run_command("unfold_all")
yield
view.run_command("gs_inline_diff_toggle_side", {"side": current_fold_mode})


def fold_mode(view):
# type: (sublime.View) -> Literal["a", "b", "ab"]
currently_folded = view.folded_regions()
if not currently_folded:
return "ab"
if currently_folded == regions(view, "b"):
return "a"
if currently_folded == regions(view, "a"):
return "b"
return "ab"


def regions(view, side):
# type: (sublime.View, Literal["a", "b"]) -> List[sublime.Region]
selector = "git-savvy-removed-lines" if side == "a" else "git-savvy-added-lines"
return [sublime.Region(r.a, r.b - 1) for r in view.get_regions(selector)]


class gs_inline_diff_toggle_side(TextCommand, GitCommand):
def run(self, edit, side):
# type: (sublime.Edit, Literal["a", "b"]) -> None
view = self.view
currently_folded = view.folded_regions()

if side == "a":
b_regions = regions(view, "b")
if currently_folded:
view.run_command("unfold_all")
if currently_folded == b_regions:
return
view.fold(b_regions)
else:
a_regions = regions(view, "a")
if currently_folded:
view.run_command("unfold_all")
if currently_folded == a_regions:
return
view.fold(a_regions)


class gs_inline_diff_toggle_cached_mode(TextCommand, GitCommand):

"""
Expand Down

0 comments on commit c242315

Please sign in to comment.