forked from bollu/sublimeBookmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmark.py
188 lines (128 loc) · 4.44 KB
/
bookmark.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from .common import *
class Bookmark:
def __init__(self, uid, name, window, activeView):
self.uid = int(uid)
self.name = str(name)
self.filePath = str(activeView.file_name())
self.projectPath = getCurrentProjectPath(window)
region = getCurrentLineRegion(activeView)
self.regionA = int(region.a)
self.regionB = int(region.b)
self.group = getCurrentActiveGroup(window)
self.lineStr = activeView.substr(region)
cursorPos = activeView.sel()[0].begin()
self.lineNumber = activeView.rowcol(cursorPos)[0]
self.bufferID = activeView.buffer_id()
def getBufferID(self):
return self.bufferID
def getName(self):
return self.name
def getUid(self):
return self.uid
def getRegion(self):
return sublime.Region(self.regionA, self.regionB)
def getFilePath(self):
return self.filePath
def getProjectPath(self):
return self.projectPath
def getLineNumber(self):
return self.lineNumber
def getLineStr(self):
return self.lineStr
def getGroup(self):
return self.group
def setLineStr(self, newLineStr):
self.lineStr = str(newLineStr)
def setRegion(self, region):
self.regionA = region.a
self.regionB = region.b
def setGroup(self, group):
self.group = int(group)
def isTemporary(self):
return self.filePath == "None"
def goto(self, window):
view = getBookmarkView(window, self)
assert (view is not None)
region = self.getRegion()
view.show_at_center(region)
#move cursor to the middle of the bookmark's region
bookmarkRegionMid = 0.5 * (region.begin() + region.end())
moveRegion = sublime.Region(bookmarkRegionMid, bookmarkRegionMid)
view.sel().clear()
view.sel().add(moveRegion)
def isEmpty(self, view):
region = self.getRegion()
line = view.substr(region)
return isLineEmpty(line)
#the bookmark is associated with the current view
def isMyView(self, window, view):
#I bloody hate python for this madness
if view is None:
return False
bufferID = view.buffer_id()
filePath = view.file_name()
if self.isTemporary():
return self.getBufferID() == bufferID
else:
return self.getFilePath() == filePath
#updates the bookmark's data
#1) moved region to cover whole line
#2) updates the group info
#3) updates the current line string
def updateData(self, window, myView):
regions = myView.get_regions(str(self.uid))
#the region is not loaded yet
if len(regions) == 0:
return
lines = myView.split_by_newlines(regions[0])
region = lines[0]
self.regionA = region.a
self.regionB = region.b
self.lineStr = myView.substr(region)
self.group = window.get_view_index(myView)[0]
def getBookmarkView(window, bookmark):
view = None
if bookmark.isTemporary():
#mimic behavior of open_file. so w have to focus the view too...
view = getViewByBufferID(window, bookmark.getBufferID())
window.focus_view(view)
else:
view = window.open_file(bookmark.getFilePath())
assert view is not None
return view
def shouldRemoveTempBookmark(window, bookmark):
assert(bookmark.isTemporary())
return getViewByBufferID(window, bookmark.getBufferID()) is None
#I hate this function.
def moveBookmarkToGroup(window, bookmark, group):
def moveViewToGroup(window, view, group):
(viewGroup, viewIndex) = window.get_view_index(view)
#the view is not in the required group so move it
#we have to move the view to the other group and give it a new index
if group != viewGroup or viewGroup == -1 or viewIndex == -1:
#SUBLIME_BUG
#if the group the view is currently in has only one element - i.e this view,
#sublime text goes crazy and closes our options selector. So, we have to create
#a new file in the old group and *only then* move the view.
if len(window.views_in_group(viewGroup)) == 1:
window.focus_group(viewGroup)
window.new_file()
#if there are 0 views, then the moved view will have index 0
#similarly, if there are n views, the last view will have index (n-1), and
#so the new view will have index n
newIndex = len (window.views_in_group(group))
#move the view to the highlighted group and assign a
#correct index
window.set_view_index(view, group, newIndex)
#the view is in the right group, so chill
else:
pass
view = None
if bookmark.isTemporary():
view = getViewByBufferID(window, bookmark.getBufferID())
assert (view is not None)
else:
view = window.open_file(bookmark.getFilePath())
assert (view is not None)
#move the bookmark's view to the correct group
moveViewToGroup(window, view, group)