forked from neilsarkar/sublime_user_directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen_javascript_file.py
55 lines (44 loc) · 1.63 KB
/
open_javascript_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
import sublime
import sublime_plugin
import os
import re
class OpenJavascriptTwinCommand(sublime_plugin.WindowCommand):
def run(self):
self.views = []
window = self.window
current_file_path = self.window.active_view().file_name()
if current_file_path.find("/spec/") > 0:
twin_path = self.app_twin_path(current_file_path)
else:
twin_path = self.spec_twin_path(current_file_path)
if os.path.exists(twin_path) :
window.open_file(twin_path)
else :
if sublime.ok_cancel_dialog("Create file: "+twin_path, "Yeah, fuck it"):
open(twin_path,"w").close()
window.open_file(twin_path)
else:
sublime.status_message("Could not find " + twin_path)
def app_twin_path(self, spec_path):
file_path = re.search(r'scripts(/.*)', spec_path).group(1).replace(".spec.js", ".js")
return self.find_app_directory() + file_path
def find_app_directory(self):
return self.find_directory([
"/app/assets/javascripts",
"/app/javascripts"
])
def spec_twin_path(self, app_path):
file_path = re.search(r'scripts(/.*)', app_path).group(1).replace(".js", ".spec.js")
return self.find_spec_directory() + file_path
def find_spec_directory(self):
return self.find_directory([
"/spec/javascripts",
"/spec/assets/javascripts"
])
def find_directory(self, candidates):
root_path = self.window.folders()[0]
for candidate in candidates:
print root_path + candidate
if os.path.exists(root_path + candidate):
return root_path + candidate
raise Exception("Unable to find javascripts path in " + ','.join(map(str, candidates)))