-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvintageLines.py
97 lines (74 loc) · 3.3 KB
/
vintageLines.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
import os, shutil, math, sublime, sublime_plugin, types
class VintageLinesEventListener(sublime_plugin.EventListener):
def __init__(self):
self.in_check_settings = False
self.icon_count = 99
self.on_load = self.on_new = self.on_activated
# Set icon path depending on version
if int(sublime.version()) >= 3000:
self.icon_path = "Packages/VintageLines/icons/%s/%s.png"
else:
self.icon_path = "../VintageLines/icons/%s/%s"
def showRelativeNumbers(self):
view = self.view
view.settings().set('line_numbers', False)
cur_line = view.rowcol(view.sel()[0].begin())[0]
start_line = max(cur_line-self.icon_count, 0)
end_line = min(cur_line+self.icon_count, self.view.rowcol(self.view.size())[0])
lines = self.view.lines(sublime.Region(self.view.text_point(start_line, 0), self.view.text_point(end_line + 1, 0)))
# Append the last line's region manually (if necessary)
if (len(lines) < end_line - start_line + 1):
last_text_point = lines[-1].end() + 1
lines.append(sublime.Region(last_text_point, last_text_point))
for i in range(start_line, start_line + len(lines)):
name = 'linenum' + str(i-start_line)
icon = str(int(math.fabs(cur_line - i)))
view.add_regions(name, [lines[i-start_line]], 'linenums', self.icon_path % (sublime.platform(), icon), sublime.HIDDEN)
def removeRelativeNumbers(self):
self.view.settings().set('line_numbers', True)
# Remove all relative line number regions within viewport
for i in range(2*self.icon_count+1):
if self.view.get_regions('linenum' + str(i)):
self.view.erase_regions('linenum' + str(i))
def checkSettings(self):
cur_line = self.view.rowcol(self.view.sel()[0].begin())[0]
if cur_line == None:
settings.set("vintage_lines.force_mode", True);
if self.in_check_settings:
# As this function is called when a setting changes, and its children also
# changes settings, we don't want it to end up in an infinite loop.
return
self.in_check_settings = True
if self.view:
settings = self.view.settings()
if settings.has("vintage_lines.force_mode"):
show = settings.get("vintage_lines.force_mode")
elif type(settings.get('command_mode')) is bool:
show = settings.get('command_mode')
else:
show = False
mode = settings.get('vintage_lines.mode', False)
line = settings.get('vintage_lines.line', -1)
lines = settings.get('vintage_lines.lines', -1)
update = mode != show
update = update or line != self.view.rowcol(self.view.sel()[0].begin())[0]
update = update or lines != self.view.rowcol(self.view.size())[0]
if update:
if show:
self.removeRelativeNumbers()
self.showRelativeNumbers()
else:
self.removeRelativeNumbers()
self.view.settings().set('vintage_lines.line', self.view.rowcol(self.view.sel()[0].begin())[0])
self.view.settings().set('vintage_lines.mode', show)
self.view.settings().set('vintage_lines.lines', self.view.rowcol(self.view.size())[0])
self.in_check_settings = False
def on_activated(self, view):
self.view = view
if view:
view.settings().clear_on_change("VintageLines")
view.settings().set('vintage_lines.line', -1) # Just to force an update on activation
view.settings().add_on_change("VintageLines", self.checkSettings)
self.checkSettings()
def on_selection_modified(self, view):
sublime.set_timeout(self.checkSettings, 10)