forked from bollu/sublimeBookmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sublimebookmark.py
522 lines (402 loc) · 17.1 KB
/
sublimebookmark.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import sublime
import sublime_plugin
import os.path
from pickle import dump, load, UnpicklingError, PicklingError
from copy import deepcopy
from .common import *
from .bookmark import *
from .visibilityHandler import *
from .ui import *
BOOKMARKS = []
UID = None
# list of bookmarks that have been deleted.
# This is used to remove bookmarks' buffer highlights.
# Without this, if a bookmark is removed,
# when a file is revisited,
# the buffer will still be marked. This will keep track of bookmarks
# that have been removed.
ERASED_BOOKMARKS = []
# whether all bookmarks (even unrelated) should be shown
BOOKMARKS_MODE = SHOW_ALL_BOOKMARKS()
def removeBookmark(bookmark):
global BOOKMARKS
global ERASED_BOOKMARKS
ERASED_BOOKMARKS.append(deepcopy(bookmark))
BOOKMARKS.remove(bookmark)
def addBookmark(bookmark):
global BOOKMARKS
BOOKMARKS.append(deepcopy(bookmark))
class SublimeBookmarkCommand(sublime_plugin.WindowCommand):
def __init__(self, window):
self.window = window
self.activeGroup = self.window.active_group()
self.activeView = self.window.active_view()
global BOOKMARKS
global UID
BOOKMARKS = []
# Initialize to 0
UID = 0
# The bookmark to go to if the user cancels
self.revertBookmark = None
# Bookmarks that are being shown in the panel
self.displayedBookmarks = None
# The index used for goto next / goto previous
# not a believer of dynamic typing
self.global_bookmark_index = int(-1)
currentDir = os.path.dirname(sublime.packages_path())
self.SAVE_PATH = currentDir + '/sublimeBookmarks.pickle'
Log(currentDir)
self._Load()
def run(self, type, name=None):
global BOOKMARKS_MODE
self.activeGroup = self.window.active_group()
self.activeView = self.window.active_view()
# update bookmark positions. We need to do it anyway...
self._UpdateBookmarkPosition()
# delete any temp bookmarks that have been since destroyed
self._UpdateTemporaryBookmarks()
if type == "add":
self._addBookmark(name)
elif type == "goto":
# on highlighting, goto the current bookmark
# on option select, just center the selected item
self._createBookmarkPanel(self._HilightDoneCallback,
self._AutoMoveToBookmarkCallback)
elif type == "remove":
# on highlighting, goto the current bookmark
# on option select, remove the selected item
self._createBookmarkPanel(self._RemoveDoneCallback,
self._AutoMoveToBookmarkCallback)
elif type == "remove_all":
self._removeAllBookmarks()
elif type == "toggle_line":
self._toggleCurrentLine()
elif type == "goto_next":
self._quickGoto(True)
elif type == "goto_previous":
self._quickGoto(False)
# BOOKMARK MODES
elif type == "show_all_bookmarks":
BOOKMARKS_MODE = SHOW_ALL_BOOKMARKS()
self._Save()
# update buffer to show all bookmarks
self._updateBufferStatus()
elif type == "show_project_bookmarks":
BOOKMARKS_MODE = SHOW_ONLY_PROJECT_BOOKMARKS()
self._Save()
# update buffer to show only project bookmarks
self._updateBufferStatus()
elif type == "show_file_bookmarks":
BOOKMARKS_MODE = SHOW_ONLY_FILE_BOOKMARKS()
self._Save()
# update buffer to show only project bookmarks
self._updateBufferStatus()
# ASYNC OPERATIONS
elif type == "mark_buffer":
self._updateBufferStatus()
elif type == "move_bookmarks":
self._UpdateBookmarkPosition()
elif type == "update_temporary":
self._UpdateTemporaryBookmarks()
def _createBookmarkPanel(self, onHighlight, onDone):
def moveBookmarksToActiveGroup(activeGroup):
# move all open bookmark tabs to one group
# so that group switching does not occur.
for bookmark in BOOKMARKS:
moveBookmarkToGroup(self.window, bookmark, self.activeGroup)
def createPanel():
bookmarkPanelItems = \
createBookmarkPanelItems(self.window, self.displayedBookmarks)
# create a selection panel and launch it
selector = OptionsSelector(self.window,
bookmarkPanelItems,
onHighlight,
onDone)
selector.start()
return True
# load all visible bookmarks
self.displayedBookmarks = getVisibleBookmarks(BOOKMARKS,
self.window,
self.activeView,
BOOKMARKS_MODE)
# if no bookmarks are acceptable, don't show bookmarks
if len(self.displayedBookmarks) == 0:
return False
# create a revert bookmark to go back if the user cancels
self._createRevertBookmark(self.activeView)
# move all active bookmarks to the currently active group
moveBookmarksToActiveGroup(self.activeGroup)
# create the selection panel
panelCreated = createPanel()
if not panelCreated:
MESSAGE_NoBookmarkToGoto()
return False
# Event handlers
def _addBookmark(self, name):
if name is None:
window = self.window
view = window.active_view()
region = getCurrentLineRegion(view)
# copy whatever is on the line for the bookmark name
initialText = view.substr(region).strip()
input = OptionsInput(self.window,
"Add Bookmark",
initialText,
self._AddBookmarkCallback,
None)
input.start()
else:
self._AddBookmarkCallback(name)
def _removeAllBookmarks(self):
global ERASED_BOOKMARKS
global BOOKMARKS
for bookmark in BOOKMARKS: # noqa
# store erased bookmarks for delayed removal
ERASED_BOOKMARKS.append(deepcopy(bookmark))
# yep. nuke em
del BOOKMARKS
BOOKMARKS = [] # noqa
# update the buffer since we deleted a bookmark
self._updateBufferStatus()
# save to eternal storage
self._Save()
def _quickGoto(self, forward):
# Gather appropriate bookmarks
self.displayedBookmarks = getVisibleBookmarks(BOOKMARKS,
self.window,
self.activeView,
BOOKMARKS_MODE)
if 0 == len(self.displayedBookmarks):
MESSAGE_NoBookmarkToGoto()
return
# increment or decrement
if forward:
self.global_bookmark_index = self.global_bookmark_index + 1
else:
self.global_bookmark_index = self.global_bookmark_index - 1
# If we're pointing off the end, go to the first one instead
if self.global_bookmark_index >= len(self.displayedBookmarks):
self.global_bookmark_index = 0
# If we're pointing off the beginning, go to the last one instead
if self.global_bookmark_index < 0:
self.global_bookmark_index = len(self.displayedBookmarks) - 1
# Go there!
bookmark = BOOKMARKS[self.global_bookmark_index]
moveBookmarkToGroup(self.window, bookmark, self.activeGroup)
self._AutoMoveToBookmarkCallback(self.global_bookmark_index)
def _toggleCurrentLine(self):
def getLineBookmark(window):
currentFilePath = window.active_view().file_name()
cursorRegion = getCurrentLineRegion(window.active_view())
for bookmark in BOOKMARKS:
if bookmark.getFilePath() == currentFilePath and \
bookmark.getRegion().contains(cursorRegion):
return bookmark
# no bookmark
return None
bookmark = getLineBookmark(self.window)
if bookmark is not None:
global ERASED_BOOKMARKS
global BOOKMARKS
# add to list of erased bookmarks
ERASED_BOOKMARKS.append(deepcopy(bookmark))
BOOKMARKS.remove(bookmark)
self._updateBufferStatus()
# [File IO]
self._Save()
else:
region = getCurrentLineRegion(self.activeView)
# copy whatever is on the line for the bookmark name
name = self.activeView.substr(region).strip()
self._AddBookmarkCallback(name)
def _updateBufferStatus(self):
# marks the given bookmark on the buffer
def markBuffer(view, bookmark):
uid = bookmark.getUid()
region = bookmark.getRegion()
view.add_regions(str(uid), [region],
"text.plain", "bookmark",
sublime.DRAW_NO_FILL |
sublime.DRAW_EMPTY_AS_OVERWRITE)
# unmarks the given bookmark on the buffer
def unmarkBuffer(view, bookmark):
uid = bookmark.getUid()
view.erase_regions(str(uid))
if self.activeView is None:
return
# mark all bookmarks that are visible, and unmark invisible bookmarks
for bookmark in BOOKMARKS:
# if the bookmark should be shown
# according to the current bookmark mode
shouldShow = shouldShowBookmark(self.window,
self.activeView,
bookmark, BOOKMARKS_MODE)
# only mark if we are in the right view.
validContext = bookmark.isMyView(self.window, self.activeView)
if validContext and shouldShow:
markBuffer(self.activeView, bookmark)
else:
unmarkBuffer(self.activeView, bookmark)
# unmark all erased bookmarks
for bookmark in ERASED_BOOKMARKS:
validContext = bookmark.isMyView(self.window, self.activeView)
if validContext:
unmarkBuffer(self.activeView, bookmark)
# 1. move bookmarks
# 2. update their regions when text is entered into the buffer
def _UpdateBookmarkPosition(self):
# this bookmark (might) have been changed
# since it's in the current file
# We're on a thread anyway so update it.
for bookmark in BOOKMARKS:
# if the activeView is the bookmark's view, update r
if bookmark.isMyView(self.window, self.activeView):
bookmark.updateData(self.window, self.activeView)
# the bookmark is empty - it has no data in it.
if bookmark.isEmpty(self.activeView):
Log("EMPTY BOOKMARK. NAME: " + bookmark.getName())
removeBookmark(bookmark)
# we've moved regions around so update the buffer
self._updateBufferStatus()
# we've moved bookmarks around and may also have deleted them. So, save
self._Save()
# check if the buffers associated
# with temporary bookmark are still active or not, and remove
# unnecessary bookmarks
def _UpdateTemporaryBookmarks(self):
for bookmark in BOOKMARKS:
# if the bookmark is a temporary bookmark
# and the bookmark has been deleted, remove the bookmark
if bookmark.isTemporary() and \
shouldRemoveTempBookmark(self.window, bookmark):
Log("BOOKMARK IS TEMP AND BUFFER HAS BEEN REMOVED."
"REMOVING. "
"BUFFER: %s | NAME: %s" %
(bookmark.getBufferID(), bookmark.getName()))
removeBookmark(bookmark)
# helpers
# creates a bookmark that keeps track of where we were before opening
# n options menu.
def _createRevertBookmark(self, activeView):
# there's no file open.
# return None 'cause there's no place to return TO
if isViewTemporary(activeView):
self.revertBookmark = None
return
uid = -1
name = ""
self.revertBookmark = Bookmark(uid, name, self.window, activeView)
# goes to the revert bookmark
def _gotoRevertBookmark(self):
if self.revertBookmark is None:
return
self.revertBookmark.goto(self.window)
self.revertBookmark = None
def _restoreFiles(self):
for bookmark in BOOKMARKS:
moveBookmarkToGroup(self.window, bookmark, bookmark.getGroup())
# callbacks---------------------------------------------------
def _AddBookmarkCallback(self, name):
global UID
assert UID is not None
myUID = UID
myUID = REGION_BASE_TAG + myUID
UID = UID + 1
# get region and line data
region = getCurrentLineRegion(self.activeView)
lineStr = self.activeView.substr(region)
# there's no content
if isLineEmpty(lineStr):
MESSAGE_BookmarkEmpty()
return
bookmark = Bookmark(UID, name, self.window, self.activeView)
addBookmark(bookmark)
self._updateBufferStatus()
self._Save()
# display highlighted bookmark
def _AutoMoveToBookmarkCallback(self, index):
assert index < len(self.displayedBookmarks)
bookmark = self.displayedBookmarks[index]
assert bookmark is not None
# goto highlighted bookmark
bookmark.goto(self.window)
self._updateBufferStatus()
# if the user canceled, go back to the original file
def _HilightDoneCallback(self, index):
# restore all files back to their original places
self._restoreFiles()
# if the user canceled, then goto the revert bookmark
if index == -1:
self._gotoRevertBookmark()
# otherwise, goto the selected bookmark
else:
# now open the selected bookmark and scroll to bookmark
self._AutoMoveToBookmarkCallback(index)
# move the correct bookmark back to the active group -
# since all fails including the bookmark have been restored,
# we have to move the bookmark back
# ARRGH! this is __so__ hacky :(
bookmark = self.displayedBookmarks[index]
moveBookmarkToGroup(self.window, bookmark, self.activeGroup)
# IMPORTANT - not doing this will cause bookmark to think it is
# still in it's previous group.
bookmark.updateData(self.window, self.activeView)
self._updateBufferStatus()
# remove the selected bookmark or go back if user cancelled
def _RemoveDoneCallback(self, index):
# restore all files back to their original places
self._restoreFiles()
# if the user canceled, then goto the revert bookmark
if index == -1:
self._gotoRevertBookmark()
# otherwise, goto the selected bookmark
else:
assert index < len(self.displayedBookmarks)
bookmark = self.displayedBookmarks[index]
assert bookmark is not None
# goto the removed bookmark
bookmark.goto(self.window)
removeBookmark(bookmark)
# decrement global_bookmark_index so
# goto_next will not skip anything
if index <= self.global_bookmark_index:
self.global_bookmark_index = self.global_bookmark_index - 1
self._updateBufferStatus()
self._Save()
# Save Load
def _Load(self):
global BOOKMARKS
global BOOKMARKS_MODE
global UID
Log("LOADING BOOKMARKS")
try:
savefile = open(self.SAVE_PATH, "rb")
saveVersion = load(savefile)
if saveVersion != VERSION:
raise UnpicklingError("version difference in files")
BOOKMARKS_MODE = load(savefile)
UID = load(savefile)
BOOKMARKS = load(savefile)
except (OSError, IOError, UnpicklingError,
EOFError, BaseException) as e:
print("\nEXCEPTION:------- ")
print(e)
print("\nUNABLE TO LOAD BOOKMARKS. NUKING LOAD FILE")
# clear the load file :]
open(self.SAVE_PATH, "wb").close()
# if you can't load, try and save a "default" state
self._Save()
def _Save(self):
global BOOKMARKS
global BOOKMARKS_MODE
global UID
try:
savefile = open(self.SAVE_PATH, "wb")
dump(VERSION, savefile)
dump(BOOKMARKS_MODE, savefile)
dump(UID, savefile)
dump(BOOKMARKS, savefile)
savefile.close()
except (OSError, IOError, PicklingError) as e:
print(e)
print("\nUNABLE TO SAVE BOOKMARKS. PLEASE CONTACT DEV")