-
Notifications
You must be signed in to change notification settings - Fork 0
/
indent_guides.py
138 lines (110 loc) · 3.58 KB
/
indent_guides.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import sublime
import sublime_plugin
import indentation
import datetime
import re
class IndentGuidesListener(sublime_plugin.EventListener):
def __init__(self):
self.cache = {}
self.guides = []
self.current_tab = None
self.region = None
self.current_region = None
sublime.set_timeout(self.on_timer, 50)
# Counts the real number of spaces on a line and uses a cache.
def get_indent(self, region, view, tab_size):
pos = 0
tabs = []
line = view.rowcol(region.a)[0]
if line not in self.cache:
text = view.substr(region)
for c in range(0, len(text)):
if text[c].isspace():
pos += 1
elif text[c] == '\t':
pos += tab_size - (pos % tab_size)
else:
break
if pos % tab_size == 0:
tabs.append(c + 1)
if len(tabs) > 0:
tabs.pop()
self.cache[line] = tabs
return self.cache[line]
# Redraw all indent guides on the page
def refresh(self, view, clear_cache):
if clear_cache:
self.cache = {}
tab = self.calculate_active_guide(view)
if clear_cache or tab != self.current_tab:
self.guides = []
self.region_start = 0
self.region_end = 0
self.current_tab = tab
visible = view.visible_region()
self.region = visible
expanded_region = sublime.Region(max(visible.a - 250, 0), min(visible.b + 5, view.size()))
visible_regions = view.split_by_newlines(visible)
tab_size = indentation.get_tab_size(view)
for region in visible_regions:
line = view.rowcol(region.a)[0]
tabs = self.get_indent(region, view, tab_size)
if self.current_tab < len(tabs):
pos = region.a + tabs[tab]
self.guides.append(sublime.Region(pos, pos))
view.add_regions('IndentGuidesListener', self.guides, 'active_guide', sublime.DRAW_EMPTY)
def check_indent(self, view, start, forward, tab_size):
start_indent = self.get_indent(start, view, tab_size)
if len(start_indent) <= 0:
return 0
while True:
if start.a <= 0:
return 0
if start.b >= view.size():
return 0
if forward:
start = view.full_line(start.b + 1)
else:
start = view.full_line(start.a - 1)
if abs(start.b - start.a) <= 1:
continue
tabs = self.get_indent(start, view, tab_size)
return len(tabs) - len(start_indent)
def calculate_active_guide(self, view):
tab_size = indentation.get_tab_size(view)
cursor = view.sel()[-1].b
region = view.line(cursor)
cursorCol = view.rowcol(cursor)[1]
pos = 0
for pt in xrange(region.begin(), region.end()):
ch = view.substr(pt)
if ch == '\t':
pos += tab_size - (pos % tab_size)
elif ch.isspace():
pos += 1
else:
break
tab = int(pos / tab_size)
if cursorCol < pos:
tab = int(cursorCol / tab_size)
else:
before = self.check_indent(view, region, False, tab_size)
after = self.check_indent(view, region, True, tab_size)
if after <= 0:
tab -= 1
return max(tab - 1, 0)
def on_load(self, view):
self.refresh(view, True)
def on_activated(self, view):
self.refresh(view, True)
def on_selection_modified(self, view):
self.refresh(view, False)
def on_modified(self, view):
self.refresh(view, True)
def on_timer(self):
sublime.set_timeout(self.on_timer, 50)
view = sublime.active_window().active_view()
region = view.visible_region()
if self.region == None or self.region.a != region.a or self.region.b != region.b:
#print datetime.datetime.now()
self.refresh(view, False)