-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab_switcher.py
62 lines (48 loc) · 1.76 KB
/
tab_switcher.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
import sublime
import sublime_plugin
import os
class TabSwitcherCommand(sublime_plugin.WindowCommand):
def run(self):
items = self.__get_items()
self.window.show_quick_panel(items, self.__select, 0)
def __get_views(self):
views = list(self.window.views())
for view in views:
if view.id() == self.window.active_view().id():
views.remove(view)
break
for vid in recent_view_ids[:]:
for view in views:
if view.id() == vid:
views.remove(view)
views.insert(0, view)
break
else:
recent_view_ids.remove(vid)
return views
def __get_items(self):
self.__item_view_ids = []
items = []
for view in self.__get_views():
items.append([self.__get_display_name(view), view.file_name() or ''])
self.__item_view_ids.append(view.id())
return items
def __select(self, index):
if index != -1:
for view in self.window.views():
if view.id() == self.__item_view_ids[index]:
self.window.focus_view(view)
break
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() or 'untitled'
else:
disp_name = os.path.basename(view.file_name())
return '%s%s' % (disp_name, mod_star)
recent_view_ids = []
class Listener(sublime_plugin.EventListener):
def on_deactivated(self, view):
if view.id() in recent_view_ids:
recent_view_ids.remove(view.id())
recent_view_ids.append(view.id())