Skip to content

Commit

Permalink
Merge pull request #709 from divmain/randy3k/logpanel
Browse files Browse the repository at this point in the history
Refactor show_log_panel
  • Loading branch information
asfaltboy committed Aug 3, 2017
2 parents 66afdac + 15ee004 commit 99dce95
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 100 deletions.
4 changes: 0 additions & 4 deletions common/util/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ def refresh_gitsavvy(view, refresh_sidebar=False, refresh_status_bar=True):

if view.settings().get("git_savvy.interface") is not None:
view.run_command("gs_interface_refresh")
if view.settings().get("git_savvy.branch_commit_history_view") is not None:
view.run_command("gs_branches_diff_commit_history_refresh")
if view.settings().get("git_savvy.blame_view") is not None:
view.run_command("gs_blame_initialize_view")

if refresh_status_bar:
view.run_command("gs_update_status_bar")
Expand Down
71 changes: 27 additions & 44 deletions core/commands/blame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from ..commands import GsNavigate
from ..git_command import GitCommand
from ...common import util
from ...common.commands import GsHandleVintageousCommand
from ..ui_mixins.quick_panel import PanelActionMixin, LogPanel, show_log_panel
from ..ui_mixins.quick_panel import PanelActionMixin, show_log_panel


BlamedLine = namedtuple("BlamedLine", ("contents", "commit_hash", "orig_lineno", "final_lineno"))
Expand Down Expand Up @@ -39,8 +38,7 @@ def run(self, coords, file_path=None, repo_path=None, commit_hash=None):
def update_actions(self):
super().update_actions()
if self._commit_hash is None:
self.actions.insert(6,
["pick_commit", "Pick a commit"])
self.actions.insert(6, ["pick_commit", "Pick a commit"])

def blame(self, ignore_whitespace=True, option=None):
original_syntax = self.window.active_view().settings().get('syntax')
Expand All @@ -62,7 +60,7 @@ def blame(self, ignore_whitespace=True, option=None):
view.set_scratch(True)
view.set_read_only(True)

view.run_command("gs_blame_initialize_view")
view.run_command("gs_blame_refresh")
view.run_command("gs_handle_vintageous")

def pick_commit(self):
Expand All @@ -73,7 +71,7 @@ def picked_commit(self, commit_hash):
super().run()


class GsBlameInitializeViewCommand(TextCommand, GitCommand):
class GsBlameRefreshCommand(TextCommand, GitCommand):

def run(self, edit):
settings = self.view.settings()
Expand Down Expand Up @@ -113,7 +111,8 @@ def get_content(self, ignore_whitespace=False, detect_move_or_copy=None, commit_
filename_at_commit = self.file_path

blame_porcelain = self.git(
"blame", "-p", '-w' if ignore_whitespace else None, detect_move_or_copy, commit_hash, "--", filename_at_commit
"blame", "-p", '-w' if ignore_whitespace else None, detect_move_or_copy,
commit_hash, "--", filename_at_commit
)
blame_porcelain = unicodedata.normalize('NFC', blame_porcelain)
blamed_lines, commits = self.parse_blame(blame_porcelain.splitlines())
Expand Down Expand Up @@ -247,12 +246,15 @@ def scroll_to(self, lineno):
self.view.sel().add(sublime.Region(blame_view_pt, blame_view_pt))
sublime.set_timeout_async(lambda: self.view.show_at_center(blame_view_pt), 0)


class GsBlameNavigateChunkCommand(GsNavigate):

"""
Move cursor to the next (or previous) different commit
"""

offset = 0

def get_available_regions(self):
return [
branch_region
Expand Down Expand Up @@ -302,7 +304,7 @@ def find_lineno(self):
return 1
else:
line = self.view.substr(self.view.line(line_start))
_ , lineno = line.split("|")
_, lineno = line.split("|")
try:
return int(lineno.strip().split(" ")[0])
except Exception:
Expand All @@ -324,10 +326,8 @@ def commit_before(self, position, commit_hash):
log_commits = self.git("log", "--format=%H", "--follow", "--", self.file_path).strip()
log_commits = log_commits.split("\n")

commit_hash_len = len(commit_hash)

for idx, commit in enumerate(log_commits):
if commit.startswith(commit_hash) :
if commit.startswith(commit_hash):
if position == "older":
if idx < len(log_commits)-1:
return log_commits[idx+1]
Expand All @@ -348,9 +348,10 @@ def find_line_and_open(self):
if not commit_hash:
self.view.settings().set("git_savvy.commit_hash", self.newst_commit_for_file())
else:
self.view.settings().set("git_savvy.commit_hash", self.commit_before("older", commit_hash))
self.view.settings().set(
"git_savvy.commit_hash", self.commit_before("older", commit_hash))

self.view.run_command("gs_blame_initialize_view")
self.view.run_command("gs_blame_refresh")

def open(self, position):
settings = self.view.settings()
Expand All @@ -362,9 +363,10 @@ def open(self, position):
# cant be before first
pass
else:
previous_commit_hash = self.commit_before(position, settings.get("git_savvy.commit_hash"))
previous_commit_hash = self.commit_before(
position, settings.get("git_savvy.commit_hash"))
settings.set("git_savvy.commit_hash", previous_commit_hash)
self.view.run_command("gs_blame_initialize_view")
self.view.run_command("gs_blame_refresh")

def show_file_at_commit(self, from_line=False):
if from_line:
Expand All @@ -376,52 +378,33 @@ def show_file_at_commit(self, from_line=False):
"commit_hash": commit_hash,
"filepath": self.file_path,
"lineno": self.find_lineno(),
"lang" : self.view.settings().get('git_savvy.original_syntax', None)
"lang": self.view.settings().get('git_savvy.original_syntax', None)
})

def pick_new_commit(self):
self.view.run_command("gs_blame_pick_commit", {
"commit_hash": self.view.settings().get("git_savvy.commit_hash"),
})


class GsBlamePickCommitCommand(TextCommand, GitCommand):

def run(self, *args, commit_hash=None):
sublime.set_timeout_async(lambda: self.run_async(self.file_path), 0)

def run_async(self, file_path):
settings = self.view.settings()
settings.set("git_savvy.commit_hash_old", settings.get("git_savvy.commit_hash"))
lp = BlameCommitPanel(
self.commit_hash = self.view.settings().get("git_savvy.commit_hash")
show_log_panel(
self.log_generator(file_path=file_path, follow=True),
self.do_action,
selected_index=lambda entry: entry == self.commit_hash,
on_highlight=self.do_action
)
lp.selected_commit(settings.get("git_savvy.commit_hash"))
lp.show()

def do_action(self, commit_hash):
settings = self.view.settings()
# Canceled panel
if commit_hash is None:
# Canceled panel
settings.set("git_savvy.commit_hash", settings.get("git_savvy.commit_hash_old"))
settings.erase("git_savvy.commit_hash_old")
else:
settings.set("git_savvy.commit_hash", commit_hash)
self.view.run_command("gs_blame_initialize_view")


class BlameCommitPanel(LogPanel):
commit_hash = None
flags = sublime.MONOSPACE_FONT

def selected_commit(self, commit_hash):
self.commit_hash = commit_hash

def selected_index(self, entry):
return self.commit_hash == entry

def on_highlight(self, index):
sublime.set_timeout_async(lambda: self.on_done(self.ret_list[index]), 0)
commit_hash = self.commit_hash

def on_selection(self, index):
sublime.set_timeout_async(lambda: self.on_done(self.ret_list[index]), 10)
self.view.settings().set("git_savvy.commit_hash", commit_hash)
self.view.run_command("gs_blame_refresh")
14 changes: 9 additions & 5 deletions core/commands/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ class LogMixin(object):
but the subclass must also inherit fro GitCommand (for the `git()` method)
"""

def run(self, *args, file_path=None, branch=None):
sublime.set_timeout_async(lambda: self.run_async(file_path=file_path, branch=branch), 0)
def run(self, *args, file_path=None, **kwargs):
sublime.set_timeout_async(lambda: self.run_async(file_path=file_path, **kwargs), 0)

def run_async(self, file_path=None, branch=None):
def run_async(self, file_path=None, **kwargs):
show_log_panel(
self.log_generator(file_path=file_path, branch=branch),
lambda commit: self.do_action(commit, file_path=file_path)
self.log_generator(file_path=file_path, **kwargs),
lambda commit: self.on_done(commit, file_path=file_path, **kwargs)
)

def on_done(self, commit, **kwargs):
if commit:
self.do_action(commit, **kwargs)

def do_action(self, commit_hash, **kwargs):
if hasattr(self, 'window'):
window = self.window
Expand Down
6 changes: 5 additions & 1 deletion core/commands/reflog.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def run(self):

def run_async(self):
show_paginated_panel(
self.reflog_generator(limit=self._limit), self.do_action, limit=self._limit)
self.reflog_generator(limit=self._limit), self.on_done, limit=self._limit)

def on_done(self, commit):
if commit:
self.do_action(commit)

def do_action(self, commit_hash):
if hasattr(self, 'window'):
Expand Down
7 changes: 4 additions & 3 deletions core/commands/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ class ResetMixin(object):
def do_action(self, commit_hash, **kwargs):
if not commit_hash:
return

self._selected_hash = commit_hash

use_reset_mode = sublime.load_settings("GitSavvy.sublime-settings").get("use_reset_mode")
if use_reset_mode:
self.on_reset(use_reset_mode)
else:
self.window.show_quick_panel(
GIT_RESET_MODES, self.on_reset_mode_selection, flags=sublime.MONOSPACE_FONT
GIT_RESET_MODES,
self.on_reset_mode_selection,
flags=sublime.MONOSPACE_FONT
)

def on_reset_mode_selection(self, index):
if index == -1:
sublime.set_timeout_async(self.run_async, 1)
sublime.set_timeout_async(self.run_async, 100)
elif 0 <= index < len(GIT_RESET_MODES):
self.on_reset(GIT_RESET_MODES[index][0].strip())

Expand Down
Loading

0 comments on commit 99dce95

Please sign in to comment.