forked from sublime-emacs/sublemacspro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbp_goto_file.py
69 lines (49 loc) · 1.88 KB
/
sbp_goto_file.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
import sublime, sublime_plugin, os
isSt2 = False
try:
import sublime_api
except ImportError:
isSt2 = True
# This code is inspired and modified from https://github.com/phildopus/sublime-goto-open-file/blob/master/GotoOpenFile.py
class GotoOpenFileCommand(sublime_plugin.TextCommand):
def run(self, edit, active_group=False):
window = sublime.active_window()
selector = ViewSelector(window, active_group)
window.show_quick_panel(selector.get_items(), selector.select)
class ViewSelector(object):
def __init__(self, window, active_group):
self.window = window
if active_group:
self.views = window.views_in_group(window.active_group())
else:
self.views = window.views()
def select(self, index):
if index != -1:
if not isSt2:
wid = sublime.active_window().id()
vid = sublime.active_window().views()[index].id()
sublime_api.window_focus_view(wid, vid)
else:
sublime.active_window().focus_view(self.views[index])
def get_items(self):
return [[self.__get_display_name(view), self.__get_path(view)] for view in self.views]
def __get_display_name(self, view):
mod_star = '*' if view.is_dirty() else ''
if view.is_scratch() or not view.file_name():
disp_name = view.name() if len(view.name()) > 0 else 'untitled'
else:
disp_name = os.path.basename(view.file_name())
return '%s%s' % (disp_name, mod_star)
def __get_path(self, view):
if view.is_scratch():
return ''
if not view.file_name():
return '<unsaved>'
folders = self.window.folders()
for folder in folders:
if os.path.commonprefix([folder, view.file_name()]) == folder:
relpath = os.path.relpath(view.file_name(), folder)
if len(folders) > 1:
return os.path.join(os.path.basename(folder), relpath)
return relpath
return view.file_name()