Skip to content

Commit

Permalink
Merge b4e260c into 8d28899
Browse files Browse the repository at this point in the history
  • Loading branch information
kaste committed Sep 10, 2019
2 parents 8d28899 + b4e260c commit 1008052
Show file tree
Hide file tree
Showing 4 changed files with 375 additions and 9 deletions.
6 changes: 6 additions & 0 deletions GitSavvy.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@
"remove_background": "#A83732",
"remove_foreground_bold": "#F9F9F4",
"remove_background_bold": "#702820"
},
"log_graph": {
"commit_dot_foreground": "#9911",
"commit_dot_background": "#991",
"path_foreground": "#991",
"path_background": "#99991109"
}
},

Expand Down
24 changes: 22 additions & 2 deletions common/theme_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def apply_new_theme(self, name, target_view):

# Sublime expects `/`-delimited paths, even in Windows.
theme_path = os.path.join("Packages", path_in_packages).replace("\\", "/")
target_view.settings().set("color_scheme", theme_path)
try_apply_theme(target_view, theme_path)


class JSONThemeGenerator(ThemeGenerator):
Expand Down Expand Up @@ -149,4 +149,24 @@ def write_new_theme(self, name):

def apply_new_theme(self, name, target_view):
self.write_new_theme(name)
target_view.settings().set("color_scheme", self.get_theme_name(name))
theme_path = self.get_theme_name(name)
try_apply_theme(target_view, theme_path)


def try_apply_theme(view, theme_path, tries=0):
""" Safly apply new theme as color_scheme. """
try:
sublime.load_resource(theme_path)
except Exception:
if tries >= 8:
print(
'GitSavvy: The theme {} is not ready to load. Maybe restart to get colored '
'highlights.'.format(theme_path)
)
return

delay = (pow(2, tries) - 1) * 10
sublime.set_timeout_async(lambda: try_apply_theme(view, theme_path, tries + 1), delay)
return

view.settings().set("color_scheme", theme_path)
87 changes: 80 additions & 7 deletions core/commands/log_graph.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
from functools import lru_cache, partial
import re
import threading

import sublime
from sublime_plugin import WindowCommand, TextCommand, EventListener

from ..git_command import GitCommand
from . import log_graph_colorizer as colorizer
from .log import GsLogActionCommand, GsLogCommand
from .navigate import GsNavigate
from ...common import util
from ..git_command import GitCommand
from ..settings import GitSavvySettings
from ..ui_mixins.quick_panel import show_branch_panel
from ...common import util
from ...common.theme_generator import XMLThemeGenerator, JSONThemeGenerator


MYPY = False
if MYPY:
from typing import Iterator, Set, Tuple


COMMIT_NODE_CHAR = "●"
Expand All @@ -17,6 +26,8 @@
COMMIT_LINE = re.compile(
"^[{graph_chars}]*[{node_chars}][{graph_chars}]* (?P<commit_hash>[a-f0-9]{{5,40}})".format(
graph_chars=GRAPH_CHAR_OPTIONS, node_chars=COMMIT_NODE_CHAR_OPTIONS))
DOT_SCOPE = 'git_savvy.graph.dot'
PATH_SCOPE = 'git_savvy.graph.path_char'


class LogGraphMixin(object):
Expand All @@ -39,6 +50,7 @@ def run_async(self):
view.set_syntax_file("Packages/GitSavvy/syntax/graph.sublime-syntax")
view.run_command("gs_handle_vintageous")
view.run_command("gs_handle_arrow_keys")
threading.Thread(target=partial(augment_color_scheme, view)).run()

settings = view.settings()
settings.set("git_savvy.repo_path", repo_path)
Expand All @@ -52,6 +64,33 @@ def prepare_target_view(self, view):
pass


def augment_color_scheme(view):
# type: (sublime.View) -> None
settings = GitSavvySettings()
colors = settings.get('colors').get('log_graph')
if not colors:
return

color_scheme = view.settings().get('color_scheme')
if color_scheme.endswith(".tmTheme"):
themeGenerator = XMLThemeGenerator(color_scheme)
else:
themeGenerator = JSONThemeGenerator(color_scheme)
themeGenerator.add_scoped_style(
"GitSavvy Highlighted Commit Dot",
DOT_SCOPE,
background=colors['commit_dot_background'],
foreground=colors['commit_dot_foreground'],
)
themeGenerator.add_scoped_style(
"GitSavvy Highlighted Path Char",
PATH_SCOPE,
background=colors['path_background'],
foreground=colors['path_foreground'],
)
themeGenerator.apply_new_theme("log_graph_view", view)


class GsLogGraphRefreshCommand(TextCommand, GitCommand):

"""
Expand Down Expand Up @@ -230,16 +269,19 @@ def on_activated(self, view):
window.run_command("show_panel", {"panel": "output.show_commit_info"})

# `on_selection_modified` triggers twice per mouse click
# multiplied with the number of views into the same buffer.
# Well, ... sublime. Anyhow we're also not interested
# in vertical movement.
# We throttle in `draw_info_panel` below by line_text
# bc a log line is pretty unique if it contains the commit's sha.
# multiplied with the number of views into the same buffer,
# hence it is *important* to throttle these events.
# We do this seperately per side-effect. See the fn
# implementations.
def on_selection_modified_async(self, view):
if not self.is_applicable(view):
return

draw_info_panel(view, self.savvy_settings.get("graph_show_more_commit_info"))
# `colorize_dots` queries the view heavily. We want that to
# happen on the main thread (t.i. blocking) bc it is way, way
# faster.
sublime.set_timeout(lambda: colorize_dots(view))

def on_post_window_command(self, window, command_name, args):
# type: (sublime.Window, str, dict) -> None
Expand All @@ -266,6 +308,37 @@ def on_post_window_command(self, window, command_name, args):
draw_info_panel(view, show_panel)


def colorize_dots(view):
# type: (sublime.View) -> None
dots = tuple(find_dots(view))
_colorize_dots(view.id(), dots)


def find_dots(view):
# type: (sublime.View) -> Set[colorizer.Char]
return set(_find_dots(view))


def _find_dots(view):
# type: (sublime.View) -> Iterator[colorizer.Char]
for s in view.sel():
line_region = view.line(s.begin())
line_content = view.substr(line_region)
idx = line_content.find(COMMIT_NODE_CHAR)
if idx > -1:
yield colorizer.Char(view, line_region.begin() + idx)


@lru_cache(maxsize=1)
# ^- throttle side-effects
def _colorize_dots(vid, dots):
# type: (sublime.ViewId, Tuple[colorizer.Char]) -> None
view = sublime.View(vid)
view.add_regions('gs_log_graph_dot', [d.region() for d in dots], scope=DOT_SCOPE)
paths = [c.region() for d in dots for c in colorizer.follow_path(d)]
view.add_regions('gs_log_graph_follow_path', paths, scope=PATH_SCOPE)


def draw_info_panel(view, show_panel):
"""Extract line under the first cursor and draw info panel."""
try:
Expand Down
Loading

0 comments on commit 1008052

Please sign in to comment.