-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgoto_spec.py
More file actions
64 lines (43 loc) · 1.67 KB
/
goto_spec.py
File metadata and controls
64 lines (43 loc) · 1.67 KB
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
import os
import sublime_plugin
from itertools import takewhile
class GotoSpecCommand(sublime_plugin.WindowCommand):
def run(self):
if not self.window.folders():
return
file_path = self.window.active_view().file_name()
searching_spec = not file_path.endswith("_spec.rb")
if searching_spec:
searched_file_path = file_path.replace(".rb", "_spec.rb")
else:
searched_file_path = file_path.replace("_spec.rb", ".rb")
project_root = self.window.folders()[0]
if searching_spec:
search_path = "%s/spec" % project_root
else:
search_path = project_root
perfect_match = search_in_directory(searched_file_path, search_path)
if not perfect_match:
return
if self.window.num_groups() > 1:
self.window.focus_group(1 if searching_spec else 0)
self.window.open_file(perfect_match)
def list_files(dirname):
files = []
for dirname, dirnames, filenames in os.walk(dirname):
for filename in filenames:
files.append(os.path.join(dirname, filename))
return files
def search_in_directory(file_path, directory_path):
if not os.access(directory_path, os.R_OK):
return
file_name = file_path.split('/')[-1]
files = list_files(directory_path)
matches = [common_start_len(f[::-1], file_path[::-1]) for f in files]
if not matches:
return
perfect_match = files[matches.index(max(matches))]
if perfect_match.endswith('/' + file_name):
return perfect_match
def common_start_len(a, b):
return len(list(takewhile(lambda (x, y): x == y, zip(a, b))))