Skip to content

Commit

Permalink
Merge 3ec4b43 into c2d42fb
Browse files Browse the repository at this point in the history
  • Loading branch information
randy3k committed Jul 26, 2017
2 parents c2d42fb + 3ec4b43 commit 71cc801
Show file tree
Hide file tree
Showing 17 changed files with 625 additions and 315 deletions.
2 changes: 1 addition & 1 deletion Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
},
{
"caption": "git: blame current file",
"command": "gs_blame"
"command": "gs_blame_current_file"
},
{
"caption": "GitSavvy: reload modules (debug)",
Expand Down
18 changes: 18 additions & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,24 @@
{ "key": "setting.git_savvy.blame_view", "operator": "equal", "operand": true }
]
},
{
"keys": ["w"],
"command": "gs_blame_toggle_setting",
"args": { "setting": "ignore_whitespace" },
"context": [
{ "key": "setting.command_mode", "operator": "equal", "operand": false },
{ "key": "setting.git_savvy.blame_view", "operator": "equal", "operand": true }
]
},
{
"keys": ["m"],
"command": "gs_blame_toggle_setting",
"args": { "setting": "detect_move_or_copy_within" },
"context": [
{ "key": "setting.command_mode", "operator": "equal", "operand": false },
{ "key": "setting.git_savvy.blame_view", "operator": "equal", "operand": true }
]
},
{
"keys": ["?"],
"command": "gs_interface_toggle_popup_help",
Expand Down
68 changes: 59 additions & 9 deletions common/theme_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
</dict>
"""

GLOBAL_STYLE_TEMPLATE = """
<dict>
<key>settings</key>
<dict>
{properties}
</dict>
</dict>
"""

PROPERTY_TEMPLATE = """
<key>{key}</key>
<string>{value}</string>
Expand All @@ -44,8 +53,33 @@ class ThemeGenerator():
def __init__(self, original_color_scheme):
color_scheme_xml = sublime.load_resource(original_color_scheme)
self.plist = ElementTree.XML(color_scheme_xml)
self.global_style = self.find_global_style()
self.styles = self.plist.find("./dict/array")

def find_global_style(self):
for style in self.plist.iterfind("./dict/array/dict"):
if "scope" not in [t.text for t in style.iterfind("./key")]:
return style
return None

def get_style_settings(self, style, key):
settings = style.find("./dict")
for i, entry in enumerate(settings):
if entry.text == key and settings[i+1].tag == "string":
return settings[i+1].text

def set_style_settings(self, style, key, value):
settings = style.find("./dict")
for i, entry in enumerate(settings):
if entry.text == key and settings[i+1].tag == "string":
settings[i+1].text = value

def get_global_settings(self, key):
return self.get_style_settings(self.global_style, key)

def set_global_settings(self, key, value):
self.set_style_settings(self.global_style, key, value)

def add_scoped_style(self, name, scope, **kwargs):
"""
Add scope-specific styles to the theme. A unique name should be provided
Expand All @@ -56,10 +90,26 @@ def add_scoped_style(self, name, scope, **kwargs):
new_style = STYLE_TEMPLATE.format(name=name, scope=scope, properties=properties)
self.styles.append(ElementTree.XML(new_style))

def set_global_style(self, **kwargs):
"""
Remvoe the global style (if found) and restruct it from the arguments.
"""
self.global_style = self.find_global_style()
if self.global_style:
self.styles.remove(self.global_style)
properties = "".join(PROPERTY_TEMPLATE.format(key=k, value=v) for k, v in kwargs.items())
new_style = GLOBAL_STYLE_TEMPLATE.format(properties=properties)
self.styles.insert(0, ElementTree.XML(new_style))
self.global_style = self.find_global_style()

def remove_all_styles(self):
styles = [s for s in self.styles]
for style in styles:
self.styles.remove(style)

def get_new_theme_path(self, name):
"""
Save the transformed theme to disk and return the path to that theme,
relative to the Sublime packages directory.
Return the path to the theme relative to the Sublime packages directory.
"""
if not os.path.exists(os.path.join(sublime.packages_path(), "User", "GitSavvy")):
os.makedirs(os.path.join(sublime.packages_path(), "User", "GitSavvy"))
Expand All @@ -68,20 +118,20 @@ def get_new_theme_path(self, name):
"GitSavvy",
"GitSavvy.{}.hidden-tmTheme".format(name))

full_path = os.path.join(sublime.packages_path(), path_in_packages)

with util.file.safe_open(full_path, "wb") as out_f:
out_f.write(STYLES_HEADER.encode("utf-8"))
out_f.write(ElementTree.tostring(self.plist, encoding="utf-8"))

return path_in_packages

def apply_new_theme(self, name, target_view):
"""
Apply the transformed theme to the specified target view.
Save the transformed theme to disk and apply the transformed theme to the specified target view.
"""
path_in_packages = self.get_new_theme_path(name)

full_path = os.path.join(sublime.packages_path(), path_in_packages)

with util.file.safe_open(full_path, "wb") as out_f:
out_f.write(STYLES_HEADER.encode("utf-8"))
out_f.write(ElementTree.tostring(self.plist, encoding="utf-8"))

# 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)
1 change: 1 addition & 0 deletions common/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
from . import debug
from . import diff_string
from . import reload
from . import color

super_key = "SUPER" if sys.platform == "darwin" else "CTRL"
9 changes: 9 additions & 0 deletions common/util/color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def next_color(color_text):
"""
Given a color string "#xxxxxy", returns its next color "#xxxxx{y+1}".
"""
hex_value = int(color_text[1:], 16)
if hex_value == 16777215: # #ffffff
return "#fffffe"
else:
return "#{}".format(hex(hex_value+1)[2:])
26 changes: 22 additions & 4 deletions common/util/view.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bisect
from contextlib import contextmanager

import sublime

Expand Down Expand Up @@ -68,10 +69,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 Expand Up @@ -149,3 +146,24 @@ def disable_other_plugins(view):
# https://github.com/guillermooo/Vintageous/wiki/Disabling
if sublime.load_settings("GitSavvy.sublime-settings").get("vintageous_friendly", False) is False:
view.settings().set("__vi_external_disable", False)


@contextmanager
def preserve_viewport(view):
was_empty = view.size() == 0
if len(view.sel()) > 0:
old_viewport = view.viewport_position()
cursor_layout = view.text_to_layout(view.sel()[0].begin())
yoffset = cursor_layout[1] - old_viewport[1]
else:
yoffset = 0

yield

if len(view.sel()) > 0:
if was_empty:
# if it was opened as a new file
view.show_at_center(view.line(view.sel()[0].begin()).begin())
else:
cursor_layout = view.text_to_layout(view.sel()[0].begin())
view.set_viewport_position((0, cursor_layout[1] - yoffset), animate=False)
Loading

0 comments on commit 71cc801

Please sign in to comment.