-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
switch_file.py
67 lines (48 loc) · 1.97 KB
/
switch_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
import os.path
import platform
import sublime
import sublime_plugin
def compare_extension(extension, fname):
if platform.system() == 'Windows' or platform.system() == 'Darwin':
extension = extension.lower()
fname = fname.lower()
return fname.lower().endswith('.' + extension.lower())
class SwitchFileCommand(sublime_plugin.WindowCommand):
def run(self, extensions=[], side_by_side=False, event=None):
if not self.window.active_view():
return
fname = self.window.active_view().file_name()
if not fname:
return
if event and 'shift' in event.get('modifier_keys', {}):
side_by_side = True
replace_mru = False
if len(self.window.selected_sheets()) > 1 \
and not side_by_side:
replace_mru = True
base = os.path.splitext(fname)[0]
start = 0
count = len(extensions)
if '.' in fname:
# Extensions can be overlapping, like "ts" and "spec.ts". So instead
# of finding the first matching extension we find the longest.
ext_len = 0
for i in range(0, len(extensions)):
if compare_extension(extensions[i], fname) and len(extensions[i]) > ext_len:
start = i + 1
count -= 1
ext_len = len(extensions[i])
base = fname[:-ext_len - 1]
for i in range(0, count):
idx = (start + i) % len(extensions)
new_path = base + '.' + extensions[idx]
if os.path.exists(new_path):
flags = sublime.FORCE_GROUP
if side_by_side:
flags |= sublime.ADD_TO_SELECTION | sublime.SEMI_TRANSIENT
elif replace_mru:
flags |= sublime.REPLACE_MRU | sublime.SEMI_TRANSIENT
self.window.open_file(new_path, flags=flags)
break
def want_event(self):
return True