-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_switch_file.py
89 lines (65 loc) · 2.37 KB
/
my_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import sublime
import sublime_plugin
import os.path
import platform
def compare_file_names(x, y):
if platform.system() == 'Windows' or platform.system() == 'Darwin':
return x.lower() == y.lower()
else:
return x == y
class MySwitchFileCommand(sublime_plugin.WindowCommand):
def run(self, extensions=[]):
if not self.window.active_view():
return
fname = self.window.active_view().file_name()
if not fname:
return
pData = sublime.active_window().project_data()
if pData is not None:
file = ProjectSearch(pData, fname, extensions)
else:
file = DefaultSearch(fname, extensions)
if file is not None:
OpenFile(file)
def SearchFolder(searchFolder, fname, extensions):
ext = ExtractSearchExtensions(extensions, fname)
base, _ = os.path.splitext(fname)
filename = os.path.basename(base)
if not os.path.isabs(searchFolder):
pfn = sublime.active_window().project_file_name()
if pfn is None:
return None
projectPath = os.path.dirname(pfn)
searchFolder = os.path.join(projectPath, searchFolder)
for root, dirs, files in os.walk(searchFolder):
for e in ext:
newFile = root + "\\" + filename + "." + e
if os.path.exists(newFile):
return newFile
return None
def ExtractSearchExtensions(extensions, fname):
base, ext = os.path.splitext(fname)
ext = ext[1:]
return [e for e in extensions if e != ext]
def ProjectSearch(projectData, fname, extensions):
paths = [f["path"] for f in projectData["folders"]]
for p in paths:
match = SearchFolder(p, fname, extensions)
if match is not None:
return match
return None
def DefaultSearch(fname, extensions):
base = os.path.dirname(fname)
return SearchFolder(base, fname, extensions)
def OpenFile(fname):
windows = sublime.windows()
for w in windows:
view = w.find_open_file(fname)
if view is not None:
w.focus_view(view)
if w != sublime.active_window():
# Hack to get focus on other window
# https://github.com/SublimeTextIssues/Core/issues/444
w.run_command("focus_neighboring_group")
w.focus_view(view)
sublime.active_window().open_file(fname)