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

Add copy helper for commit messages #1823

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
49 changes: 45 additions & 4 deletions core/commands/show_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from webbrowser import open as open_in_browser

import sublime
from sublime_plugin import WindowCommand, TextCommand
from sublime_plugin import EventListener, TextCommand, WindowCommand

from . import diff
from . import intra_line_colorizer
Expand All @@ -13,8 +13,8 @@
from . import show_file_at_commit
from ..fns import filter_, flatten, unique
from ..git_command import GitCommand
from ..utils import flash, focus_view, Cache
from ..parse_diff import SplittedDiff
from ..utils import flash, flash_regions, focus_view, Cache
from ..parse_diff import SplittedDiff, TextRange
from ..runtime import ensure_on_ui, enqueue_on_worker
from ..view import replace_view_content, Position

Expand All @@ -38,11 +38,12 @@
"gs_show_commit_reword_commit",
"gs_line_history_reword_commit",
"gs_show_commit_edit_commit",
"GsShowCommitCopyCommitMessageHelper",
)

MYPY = False
if MYPY:
from typing import Dict, Optional, Tuple
from typing import Dict, Optional, Sequence, Tuple, Union
from ..types import LineNo, ColNo
from GitSavvy.core.base_commands import GsCommand, Args, Kont

Expand Down Expand Up @@ -453,3 +454,43 @@ def run(self, edit):
"all": True,
"follow": self.get_short_hash(commit_hash)
})


class GsShowCommitCopyCommitMessageHelper(EventListener):
def on_text_command(self, view, command_name, args):
# type: (sublime.View, str, Dict) -> Union[None, str]
if command_name != "copy":
return None

frozen_sel = [r for r in view.sel()]
if len(frozen_sel) != 1:
return None

sel = frozen_sel[0]
if sel.empty():
return None

if not view.match_selector(sel.begin(), "git-savvy.commit meta.commit_message"):
return None

selected_text = TextRange(view.substr(sel), *sel)
by_line = [
line[4:] if line.text.startswith(" ") else line
for line in selected_text.lines()
]
string_for_clipboard = "".join(line.text for line in by_line)
clip_content = sublime.get_clipboard(2048)

if string_for_clipboard == clip_content:
set_clipboard_and_flash(view, selected_text.text, [selected_text.region()])
return "noop"

regions = [line.region()[:-1] for line in by_line]
set_clipboard_and_flash(view, string_for_clipboard, regions)
return "noop"


def set_clipboard_and_flash(view, text, regions):
# type: (sublime.View, str, Sequence[sublime.Region]) -> None
sublime.set_clipboard(text)
flash_regions(view, regions)
14 changes: 13 additions & 1 deletion core/parse_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

MYPY = False
if MYPY:
from typing import Final, Iterator, List, NamedTuple, Optional, Tuple, Type
from typing import Final, Iterator, List, NamedTuple, Optional, Tuple, Type, Union
from typing_extensions import Self
from .types import LineNo


Expand Down Expand Up @@ -158,6 +159,10 @@ def __eq__(self, other):
return self._as_tuple() == other._as_tuple()
return False

def __getitem__(self, i):
# type: (Union[int, slice]) -> Self
return self.__class__(self.text[i], *self.region()[i])

def region(self):
# type: () -> Region
return Region(self.a, self.b)
Expand Down Expand Up @@ -324,6 +329,13 @@ def __sub__(self, other):
# type: (int) -> Region
return self.transpose(-other)

def __getitem__(self, i):
# type: (Union[int, slice]) -> Self
if isinstance(i, int):
i = slice(i, i + 1)
new_range = range(self.a, self.b)[i]
return self.__class__(new_range.start, new_range.stop)

def transpose(self, n):
# type: (int) -> Region
return Region(self.a + n, self.b + n)
Expand Down