|
1 |
| -import sublime |
2 |
| -import sublime_plugin |
3 |
| -import re, inspect, os |
4 |
| - |
5 |
| -class OpenRspecFileCommand(sublime_plugin.WindowCommand): |
6 |
| - |
7 |
| - def run(self, option): |
8 |
| - if not self.window.active_view(): |
9 |
| - return |
10 |
| - |
11 |
| - self.views = [] |
12 |
| - window = self.window |
13 |
| - current_file_path = self.window.active_view().file_name() |
14 |
| - |
15 |
| - if re.search(r"\w+\.rb$", current_file_path): |
16 |
| - |
17 |
| - current_file = re.search(r"([\w\.]+)$", current_file_path).group(1) |
18 |
| - base_name = re.search(r"(\w+)\.(\w+)$", current_file).group(1) |
19 |
| - base_name = re.sub('_spec', '', base_name) |
20 |
| - |
21 |
| - source_matcher = re.compile("[/\\\\]" + base_name + "\.rb$") |
22 |
| - test_matcher = re.compile("[/\\\\]" + base_name + "_spec\.rb$") |
23 |
| - |
24 |
| - if option == 'next': |
25 |
| - print "Current file: " + current_file |
26 |
| - if re.search(re.compile(base_name + "_spec\.rb$"), current_file): |
27 |
| - self.open_project_file(source_matcher, window) |
28 |
| - elif re.search(re.compile(base_name + "\.rb$"), current_file): |
29 |
| - self.open_project_file(test_matcher, window) |
30 |
| - else: |
31 |
| - print "Current file is not valid for RSpec switch file!" |
32 |
| - elif option == 'source': |
33 |
| - self.open_project_file(source_matcher, window) |
34 |
| - elif option == 'test': |
35 |
| - self.open_project_file(test_matcher, window) |
36 |
| - elif option == 'test_and_source': |
37 |
| - window.run_command('set_layout', { |
38 |
| - "cols": [0.0, 0.5, 1.0], |
39 |
| - "rows": [0.0, 1.0], |
40 |
| - "cells": [[0, 0, 1, 1], [1, 0, 2, 1]] |
41 |
| - }) |
42 |
| - self.open_project_file(test_matcher, window, 0) |
43 |
| - self.open_project_file(source_matcher, window, 1) |
44 |
| - |
45 |
| - for v in self.views: |
46 |
| - window.focus_view(v) |
47 |
| - |
48 |
| - def open_project_file(self, file_matcher, window, auto_set_view=-1): |
49 |
| - for root, dirs, files in os.walk(window.folders()[0]): |
50 |
| - for f in files: |
51 |
| - if re.search(r"\.rb$", f): |
52 |
| - cur_file = os.path.join(root, f) |
53 |
| - # print "Assessing: " + cur_file |
54 |
| - if file_matcher.search(cur_file): |
55 |
| - file_view = window.open_file(os.path.join(root, f)) |
56 |
| - if auto_set_view >= 0: # don't set the view unless specified |
57 |
| - window.run_command('move_to_group', {'group': auto_set_view}) |
58 |
| - self.views.append(file_view) |
59 |
| - print("Opened: " + f) |
60 |
| - return |
61 |
| - print("No matching files!") |
| 1 | +import sublime |
| 2 | +import sublime_plugin |
| 3 | +import re, inspect, os |
| 4 | +import shared |
| 5 | + |
| 6 | +class OpenRspecFileCommand(sublime_plugin.WindowCommand): |
| 7 | + |
| 8 | + def run(self): |
| 9 | + if not self.window.active_view(): |
| 10 | + return |
| 11 | + |
| 12 | + self.views = [] |
| 13 | + window = self.window |
| 14 | + current_file_path = self.window.active_view().file_name() |
| 15 | + |
| 16 | + if re.search(r"\w+\.rb$", current_file_path): |
| 17 | + |
| 18 | + current_file = re.search(r"([\w\.]+)$", current_file_path).group(1) |
| 19 | + base_name = re.search(r"(\w+)\.(\w+)$", current_file).group(1) |
| 20 | + base_name = re.sub('_spec', '', base_name) |
| 21 | + |
| 22 | + source_matcher = re.compile("[/\\\\]" + base_name + "\.rb$") |
| 23 | + test_matcher = re.compile("[/\\\\]" + base_name + "_spec\.rb$") |
| 24 | + |
| 25 | + target_group = shared.other_group_in_pair(window) |
| 26 | + |
| 27 | + print "Current file: " + current_file |
| 28 | + if re.search(re.compile(base_name + "_spec\.rb$"), current_file): |
| 29 | + self.open_project_file(source_matcher, window, target_group) |
| 30 | + elif re.search(re.compile(base_name + "\.rb$"), current_file): |
| 31 | + self.open_project_file(test_matcher, window, target_group) |
| 32 | + else: |
| 33 | + print "Current file is not valid for RSpec switch file!" |
| 34 | + |
| 35 | + def open_project_file(self, file_matcher, window, group=-1): |
| 36 | + for root, dirs, files in os.walk(window.folders()[0]): |
| 37 | + for f in files: |
| 38 | + if re.search(r"\.rb$", f): |
| 39 | + cur_file = os.path.join(root, f) |
| 40 | + # print "Assessing: " + cur_file |
| 41 | + if file_matcher.search(cur_file): |
| 42 | + file_view = window.open_file(os.path.join(root, f)) |
| 43 | + if group >= 0: # don't set the view unless specified |
| 44 | + window.run_command('move_to_group', {'group': group}) |
| 45 | + self.views.append(file_view) |
| 46 | + print("Opened: " + f) |
| 47 | + return |
| 48 | + print("No matching files!") |
0 commit comments