-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtabfilter.py
111 lines (93 loc) · 3.73 KB
/
tabfilter.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
# Copyright (c) 2013 - 2021 Robin Malburn
# See the file license.txt for copying permission.
import sublime # type: ignore
import sublime_plugin # type: ignore
from typing import List, Tuple
from .lib.entities import Tab
from .lib.settings import (
TabSetting,
CommonPrefixTabSetting,
ShowCaptionsTabSetting,
IncludePathTabSetting,
ShowGroupCaptionTabSetting,
)
class TabFilterCommand(sublime_plugin.WindowCommand):
"""Provides a GoToAnything style interface for
searching and selecting open tabs.
"""
window: sublime.Window
views: List[sublime.View] = []
current_tab_idx: int = -1
settings: sublime.Settings
def gather_tabs(self, group_indexes: List[int]) -> List[Tab]:
"""Gather tabs from the given group indexes."""
tabs: List[Tab] = []
idx: int = 0
self.views = []
for group_idx in group_indexes:
for view in self.window.views_in_group(group_idx):
self.views.append(view)
if self.window.active_view().id() == view.id():
# save index for later usage
self.current_tab_idx = idx
tabs.append(Tab(view))
idx = idx + 1
return tabs
def format_tabs(
self,
tabs: List[Tab],
formatting_settings: Tuple[TabSetting, ...]
) -> List[List[str]]:
"""Formats tabs for display in the quick info panel."""
for setting in formatting_settings:
tabs = setting.apply(tabs)
return [tab.get_details() for tab in tabs]
def display_quick_info_panel(
self,
tabs: List[List[str]],
preview: bool
) -> None:
"""Displays the quick info panel with the formatted tabs."""
if preview is True:
self.window.show_quick_panel(
tabs,
self.on_done,
on_highlight=self.on_highlighted,
selected_index=self.current_tab_idx
)
return
self.window.show_quick_panel(tabs, self.on_done)
def on_done(self, index: int) -> None:
"""Callback handler to move focus to the selected tab index."""
if index == -1 and self.current_tab_idx != -1:
# If the selection was quit, re-focus the last selected Tab
self.window.focus_view(self.views[self.current_tab_idx])
elif index > -1 and index < len(self.views):
self.window.focus_view(self.views[index])
def on_highlighted(self, index: int) -> None:
"""Callback handler to focus the currently highlighted Tab."""
if index > -1 and index < len(self.views):
self.window.focus_view(self.views[index])
def run(self, active_group_only=False) -> None:
"""Shows a quick panel to filter and select tabs from
the active window.
"""
self.views = []
self.settings = sublime.load_settings("tabfilter.sublime-settings")
groups: List[int] = [self.window.active_group()]
if active_group_only is False:
groups = list(range(self.window.num_groups()))
tabs = self.gather_tabs(groups)
preview: bool = self.settings.get("preview_tab") is True
if active_group_only is False:
preview = preview and self.window.num_groups() == 1
formatting_settings: Tuple[TabSetting, ...] = (
CommonPrefixTabSetting(self.settings, self.window),
ShowGroupCaptionTabSetting(self.settings, self.window),
ShowCaptionsTabSetting(self.settings, self.window),
IncludePathTabSetting(self.settings, self.window),
)
self.display_quick_info_panel(
self.format_tabs(tabs, formatting_settings),
preview
)