forked from jisaacks/GitGutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_gutter.py
75 lines (65 loc) · 3.13 KB
/
git_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
import sublime
import sublime_plugin
from view_collection import ViewCollection
class GitGutterCommand(sublime_plugin.WindowCommand):
def run(self):
self.view = self.window.active_view()
if not self.view:
# Sometimes GitGutter tries to run when there is no active window
# and it throws an error because self.view is None.
# I have only been able to reproduce this in the following scenario:
# you clicked on FileA in the sidebar (FileA is not previously open)
# not to open it but to preview it. While previewing it you press
# ctrl+` to open a console. With the console selected and the
# unopened FileA preview showing in the window you click on another
# unopened file, FileB to preview that file. There will be no active
# window at this time and GitGutter will throw an error. So we can
# just skip running this time because immediately after selecting
# FileB, focus will shift from the console to its preview. This will
# cause GitGutter to run again on the FileB preview.
# Wow that was a really long explanation.
return
self.clear_all()
inserted, modified, deleted = ViewCollection.diff(self.view)
self.lines_removed(deleted)
self.lines_added(inserted)
self.lines_modified(modified)
def clear_all(self):
self.view.erase_regions('git_gutter_deleted_top')
self.view.erase_regions('git_gutter_deleted_bottom')
self.view.erase_regions('git_gutter_inserted')
self.view.erase_regions('git_gutter_changed')
def lines_to_regions(self, lines):
regions = []
for line in lines:
position = self.view.text_point(line - 1, 0)
region = sublime.Region(position, position)
regions.append(region)
return regions
def lines_removed(self, lines):
bottom_lines = []
for line in lines:
if line != 1:
bottom_lines.append(line - 1)
self.lines_removed_top(lines)
self.lines_removed_bottom(bottom_lines)
def lines_removed_top(self, lines):
regions = self.lines_to_regions(lines)
scope = 'markup.deleted.git_gutter'
icon = '../GitGutter/icons/deleted_top'
self.view.add_regions('git_gutter_deleted_top', regions, scope, icon)
def lines_removed_bottom(self, lines):
regions = self.lines_to_regions(lines)
scope = 'markup.deleted.git_gutter'
icon = '../GitGutter/icons/deleted_bottom'
self.view.add_regions('git_gutter_deleted_bottom', regions, scope, icon)
def lines_added(self, lines):
regions = self.lines_to_regions(lines)
scope = 'markup.inserted.git_gutter'
icon = '../GitGutter/icons/inserted'
self.view.add_regions('git_gutter_inserted', regions, scope, icon)
def lines_modified(self, lines):
regions = self.lines_to_regions(lines)
scope = 'markup.changed.git_gutter'
icon = '../GitGutter/icons/changed'
self.view.add_regions('git_gutter_changed', regions, scope, icon)