-
Notifications
You must be signed in to change notification settings - Fork 23
/
SyncedSideBar.py
178 lines (139 loc) · 6.18 KB
/
SyncedSideBar.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
import sublime
import sublime_plugin
# ++++ old hacks ++++
# before ST3 build 3098 we assume sidebar is visible by default on every window (there's no way to check, unfortunately)
DEFAULT_VISIBILITY = True
sidebarVisible = DEFAULT_VISIBILITY
# Keep track of active windows so we remember sidebarVisible for each one
lastWindow = None
# ++++ end old hacks ++++
# Used to know whether we've run reveal_all for a window
windows = {}
# preference from plugin settings file
pluginPref = DEFAULT_VISIBILITY
# flag for alt-tab focus check
lastView = None
def plugin_loaded():
userSettings = sublime.load_settings('Preferences.sublime-settings')
packageSettings = sublime.load_settings('SyncedSideBar.sublime-settings')
def read_pref_user():
vis = userSettings.get('reveal-on-activate')
if vis is not None:
global pluginPref
pluginPref = vis
def read_pref_package():
vis = packageSettings.get('reveal-on-activate')
if vis is not None:
global pluginPref
pluginPref = vis
# read initial setting
read_pref_package()
read_pref_user()
# listen for changes
userSettings.add_on_change('Preferences', read_pref_user)
packageSettings.add_on_change('SyncedSideBar', read_pref_package)
# ST2 backwards compatibility
if (int(sublime.version()) < 3000):
plugin_loaded()
def reveal_all(view):
visUser = view.settings().get('reveal-all-tabs')
#print( 'view.settings().get(reveal-all-tabs): ' + str( visUser ) )
if visUser is None:
packageSettings = sublime.load_settings('SyncedSideBar.sublime-settings')
visPackage = packageSettings.get('reveal-all-tabs')
if visPackage is False:
return
elif visUser is False:
return
activeWindow = view.window();
viewList = activeWindow.views();
# Use set_timeout to give sublime a chance to fire normal events between tab changes
def reveal():
if (len(viewList) > 0):
target = viewList.pop()
activeWindow.focus_view(target)
sublime.set_timeout(reveal, 25);
else:
# all tabs have been activated in this window, restore focus to the tab that was first active
activeWindow.focus_view(view)
sublime.set_timeout(reveal, 50);
def manage_state(view):
activeWindow = view.window()
if not activeWindow.id() in windows:
# first activation in this window, use default (unused for 3098 and above, but we need to store *something*)
windows[activeWindow.id()] = DEFAULT_VISIBILITY
# fire 'reveal all' in the background
reveal_all(view)
# ++++ old hacks ++++
if (int(sublime.version()) < 3098):
global sidebarVisible, lastWindow
if lastWindow is None:
# plugin just loaded
lastWindow = activeWindow
elif lastWindow.id() != activeWindow.id():
# store the old window state
windows[lastWindow.id()] = sidebarVisible
# load the new window state
sidebarVisible = windows[activeWindow.id()]
lastWindow = activeWindow
# ++++ end old hacks ++++
def show_view(view):
userPref = view.settings().get('reveal-on-activate')
reveal = userPref if userPref is not None else pluginPref
win = view.window()
def revealLater():
# Some versions of Sublime Text crash when revealing files under `.git/` in the side bar:
# https://github.com/sublimehq/sublime_text/issues/5881
if int(sublime.version()) < 4148:
active_view = win.active_view()
if active_view and '/.git/' in str(active_view.file_name()):
return
if (int(sublime.version()) >= 3098):
# API provided by sublime
shouldReveal = win.is_sidebar_visible()
else:
# backwards compatibility
shouldReveal = sidebarVisible
if shouldReveal and reveal != False:
win.run_command('reveal_in_side_bar')
# When using quick switch project, the view activates before the sidebar is ready.
# This tiny delay is imperceptible but works around the issue.
sublime.set_timeout(revealLater, 100);
class SideBarListener(sublime_plugin.EventListener):
def on_activated(self, view):
# don't even consider updating state if we don't have a window.
# reveal in side bar is a window command only.
# 'goto anything' activates views but doesn't set a window until the file is selected.
if view.settings().get("is_widget", False):
return
global lastView
if lastView is not None and lastView.id() == view.id():
# this view has just been processed, likely an alt-tab focus event
return
lastView = view
manage_state(view)
show_view(view)
# Sublime text v3 window command listener, safe to include unconditionally as it's simply ignored by v2.
# Eventually, v3 support below 3098 will be dropped and this can be deleted.
def on_window_command(self, window, command, args):
if command == 'toggle_side_bar':
global sidebarVisible
sidebarVisible = not sidebarVisible
def on_post_window_command(self, window, command, args):
# v4 leaves focus on the sidebar after a `reveal_in_side_bar` command
# So we need to manually force focus back to the file window
if (int(sublime.version()) > 4000 and int(sublime.version()) < 4099):
if command == 'reveal_in_side_bar':
window.focus_view(lastView)
class SideBarUpdateSync(sublime_plugin.ApplicationCommand):
# Update user preferences with the new value
def run(self, enable):
userSettings = sublime.load_settings('Preferences.sublime-settings')
vis = userSettings.get('reveal-on-activate')
if vis is not None:
userSettings.set('reveal-on-activate', enable)
sublime.save_settings('Preferences.sublime-settings')
else:
packageSettings = sublime.load_settings('SyncedSideBar.sublime-settings')
packageSettings.set('reveal-on-activate', enable)
sublime.save_settings('SyncedSideBar.sublime-settings')