forked from jisaacks/GitGutter
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathvcs_gutter.py
92 lines (79 loc) · 2.95 KB
/
vcs_gutter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import sublime
import sublime_plugin
try:
from .view_collection import ViewCollection
except ValueError:
from view_collection import ViewCollection
ST3 = int(sublime.version()) >= 3000
_show_in_minimap = False
def plugin_loaded():
"""
Ugly hack for icons in ST3
kudos:
github.com/facelessuser/BracketHighlighter/blob/BH2ST3/bh_core.py#L1380
"""
from os import makedirs
from os.path import exists, join
icon_path = join(sublime.packages_path(), "Theme - Default")
if not exists(icon_path):
makedirs(icon_path)
settings = sublime.load_settings('VcsGutter.sublime-settings')
global _show_in_minimap
_show_in_minimap = settings.get('show_in_minimap', False)
class VcsGutterCommand(sublime_plugin.WindowCommand):
region_names = ['deleted_top', 'deleted_bottom',
'deleted_dual', 'inserted', 'changed']
def run(self):
self.view = self.window.active_view()
if not self.view:
# View is not ready yet, try again later.
sublime.set_timeout(self.run, 1)
return
self.clear_all()
inserted, modified, deleted = ViewCollection.diff(self.view)
self.lines_removed(deleted)
self.bind_icons('inserted', inserted)
self.bind_icons('changed', modified)
def clear_all(self):
for region_name in self.region_names:
self.view.erase_regions('vcs_gutter_%s' % region_name)
def lines_to_regions(self, lines):
regions = []
for line in lines:
position = self.view.text_point(line - 1, 0)
region = sublime.Region(position, position+1)
regions.append(region)
return regions
def lines_removed(self, lines):
top_lines = lines
bottom_lines = [line - 1 for line in lines if line > 1]
dual_lines = []
for line in top_lines:
if line in bottom_lines:
dual_lines.append(line)
for line in dual_lines:
bottom_lines.remove(line)
top_lines.remove(line)
self.bind_icons('deleted_top', top_lines)
self.bind_icons('deleted_bottom', bottom_lines)
self.bind_icons('deleted_dual', dual_lines)
def icon_path(self, icon_name):
if int(sublime.version()) < 3014:
path = '..'
extn = ''
else:
path = 'Packages'
extn = '.png'
return path + '/VCS Gutter/icons/' + icon_name + extn
def bind_icons(self, event, lines):
regions = self.lines_to_regions(lines)
event_scope = event
if event.startswith('deleted'):
event_scope = 'deleted'
scope = 'markup.%s.vcs_gutter' % event_scope
icon = self.icon_path(event)
if ST3 and _show_in_minimap:
flags = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE
else:
flags = sublime.HIDDEN
self.view.add_regions('vcs_gutter_%s' % event, regions, scope, icon, flags)