forked from skyronic/DogEars
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDogEars.py
More file actions
121 lines (81 loc) · 3.42 KB
/
DogEars.py
File metadata and controls
121 lines (81 loc) · 3.42 KB
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
import sublime, sublime_plugin
import os
import string, random
def id_generator(size=5, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
BOOKMARKS = {}
class NewBookmarkCommand(sublime_plugin.TextCommand):
def run(self, edit):
sel = self.view.sel()
if not len(sel) == 1:
# Work only on single selections
return
if not sel[0].begin() == sel[0].end():
# Only on single caret selections
return
point = sel[0].begin()
self.point = point
self.fileName = self.view.file_name()
window = self.view.window()
defaultString = os.path.basename(self.fileName) + " - "
window.show_input_panel("Enter Bookmark Name: ", defaultString, self.on_bookmark_name_entered, None, None)
def on_bookmark_name_entered(self, bookmarkName):
# Create a unique ID for the bookmark
key = id_generator()
bookmark = {}
bookmark['fileName'] = self.fileName
bookmark['baseName'] = os.path.basename(self.fileName)
bookmark['name'] = bookmarkName
bookmark['viewId'] = self.view.id()
BOOKMARKS[key] = bookmark
# Create a region to behave like a bookmark
self.view.add_regions('dogears_' + key, [s for s in self.view.sel()], "", "bookmark")
print("Saving bookmark {0} at key {1}".format(bookmarkName, key))
class BrowseBookmarksCommand(sublime_plugin.TextCommand):
def run(self, edit):
bookmarkOpts = []
self.panelKeys = []
baseName = os.path.basename(self.view.file_name())
for key, val in BOOKMARKS.iteritems():
bookmarkOpts.append(val['name'])
self.panelKeys.append(key)
window = self.view.window()
window.show_quick_panel(bookmarkOpts, self.on_bookmark_selected)
def on_bookmark_selected(self, idx):
if(idx == -1):
print("No bookmark selected. Returning ")
return
# Get the key for the bookmark
key = self.panelKeys[idx]
# Set focus on the which the bookmark was set on
viewId = BOOKMARKS[key]['viewId']
for v in self.view.window().views():
if v.id() == viewId:
self.view.window().focus_view(v)
break
# Retrieve the region for the bookmark
bmRegion = self.view.get_regions("dogears_" + key)
if len(bmRegion) == 0:
return
self.view.run_command("select_all_bookmarks", {'name':"dogears_" + key})
class DeleteBookmarkCommand(sublime_plugin.TextCommand):
def run(self, edit):
bookmarkOpts = []
self.panelKeys = []
baseName = os.path.basename(self.view.file_name())
for key, val in BOOKMARKS.iteritems():
bookmarkOpts.append(val['name'])
self.panelKeys.append(key)
window = self.view.window()
window.show_quick_panel(bookmarkOpts, self.on_bookmark_selected)
def on_bookmark_selected(self, idx):
if(idx == -1):
print("No bookmark selected. Returning ")
return
# Get the key for the bookmark
key = self.panelKeys[idx]
bookmarkName = BOOKMARKS[key]['name']
del BOOKMARKS[key]
# Delete the region for the bookmark
self.view.erase_regions("dogears_" + key)
print("Deleting bookmark {0} at key {1}".format(bookmarkName, key))