Skip to content

Commit e4eec33

Browse files
author
Greg Williams
committed
Merge pull request #11 from rexikan/improved_open_rspec_file
Improved open rspec file
2 parents 34c92a5 + bde30be commit e4eec33

5 files changed

+93
-111
lines changed

Default (OSX).sublime-keymap

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
[
2-
{ "keys": ["super+alt+b"],
3-
"command": "exec",
4-
"args": {"cmd": ["rspec", "*"]},
5-
"file_regex": "# ([A-Za-z0-9_./ ]+rb):([0-9]+)",
6-
"working_dir": "${folder}"
7-
},
8-
9-
{ "keys": ["super+alt+period"], "command": "open_rspec_file", "args": {"option": "test_and_source"} },
10-
{ "keys": ["super+period"], "command": "open_rspec_file", "args": {"option": "next"} },
11-
{ "keys": ["super+alt+s"], "command": "open_rspec_file", "args": {"option": "source"} },
12-
{ "keys": ["super+alt+t"], "command": "open_rspec_file", "args": {"option": "test"} },
13-
14-
{ "keys": ["super+alt+n"], "command": "rspec_create_module", "args": {} }
15-
]
1+
[
2+
{ "keys": ["super+alt+b"],
3+
"command": "exec",
4+
"args": {"cmd": ["rspec", "*"]},
5+
"file_regex": "# ([A-Za-z0-9_./ ]+rb):([0-9]+)",
6+
"working_dir": "${folder}"
7+
},
8+
9+
{ "keys": ["super+period"], "command": "open_rspec_file", "args": {} },
10+
11+
{ "keys": ["super+alt+n"], "command": "rspec_create_module", "args": {} }
12+
]

Default (Windows).sublime-keymap

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
[
2-
{ "keys": ["ctrl+f7"],
3-
"command": "exec",
4-
"args": {"cmd": ["rspec.bat", "*"]},
5-
"file_regex": "# ([A-Za-z0-9_./ ]+rb):([0-9]+)",
6-
"working_dir": "${folder}"
7-
},
8-
9-
{ "keys": ["ctrl+alt+period"], "command": "open_rspec_file", "args": {"option": "test_and_source"} },
10-
{ "keys": ["ctrl+period"], "command": "open_rspec_file", "args": {"option": "next"} },
11-
{ "keys": ["ctrl+alt+s"], "command": "open_rspec_file", "args": {"option": "source"} },
12-
{ "keys": ["ctrl+alt+t"], "command": "open_rspec_file", "args": {"option": "test"} },
13-
14-
{ "keys": ["ctrl+alt+n"], "command": "rspec_create_module", "args": {} }
15-
]
1+
[
2+
{ "keys": ["ctrl+f7"],
3+
"command": "exec",
4+
"args": {"cmd": ["rspec.bat", "*"]},
5+
"file_regex": "# ([A-Za-z0-9_./ ]+rb):([0-9]+)",
6+
"working_dir": "${folder}"
7+
},
8+
9+
{ "keys": ["ctrl+period"], "command": "open_rspec_file", "args": {} },
10+
11+
{ "keys": ["ctrl+alt+n"], "command": "rspec_create_module", "args": {} }
12+
]

OpenRSpecFile.py

+48-61
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,48 @@
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!")

RspecCreateModule.py renamed to RSpecCreateModule.py

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sublime, sublime_plugin, time
22
import re
3+
import shared
34

45
_patterns = dict((k, re.compile('_*' + v)) for (k, v)
56
in dict(allcamel=r'(?:[A-Z]+[a-z0-9]*)+$',
@@ -35,19 +36,23 @@ class RspecCreateModuleCommand(sublime_plugin.WindowCommand):
3536
def run(self):
3637
# self.view.insert(edit, 0, "Hello, World!")
3738
self.window.show_input_panel("Enter module name:", "", self.on_done, None, None)
38-
39+
3940
def on_done(self, text):
4041

41-
# configure 2-paned layout (spec, module)
42-
self.window.run_command('set_layout', {
43-
"cols": [0.0, 0.5, 1.0],
44-
"rows": [0.0, 1.0],
45-
"cells": [[0, 0, 1, 1], [1, 0, 2, 1]]
46-
})
47-
42+
# create the module
43+
module = self.window.new_file()
44+
module.set_syntax_file('Packages/Ruby/Ruby.tmLanguage')
45+
module.set_name(translate(text, 'allcamel', 'underscores') + '.rb')
46+
module_template = "\n\
47+
class " + text + "\n\
48+
end"
49+
edit = module.begin_edit()
50+
module.insert(edit, 0, module_template)
51+
module.end_edit(edit)
52+
4853
# create the spec
4954
spec = self.window.new_file()
50-
self.window.run_command('move_to_group', {'group': 0})
55+
self.window.run_command('move_to_group', {'group': shared.other_group_in_pair(self.window)})
5156
spec.set_syntax_file('Packages/Ruby/Ruby.tmLanguage')
5257
spec.set_name(translate(text, 'allcamel', 'underscores') + '_spec.rb')
5358
spec_template = "require 'spec_helper'\n\
@@ -59,17 +64,6 @@ def on_done(self, text):
5964
spec.insert(edit, 0, spec_template)
6065
spec.end_edit(edit)
6166

62-
# create the module
63-
module = self.window.new_file()
64-
self.window.run_command('move_to_group', {'group': 1})
65-
module.set_syntax_file('Packages/Ruby/Ruby.tmLanguage')
66-
module.set_name(translate(text, 'allcamel', 'underscores') + '.rb')
67-
module_template = "\n\
68-
class " + text + "\n\
69-
end"
70-
edit = module.begin_edit()
71-
module.insert(edit, 0, module_template)
72-
module.end_edit(edit)
7367
# try:
7468
# except ValueError:
7569
# pass

shared.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""Returns the neighbour focus group for the current window."""
2+
def other_group_in_pair(window):
3+
if window.active_group() % 2 == 0:
4+
target_group = window.active_group()+1
5+
else:
6+
target_group = window.active_group()-1
7+
return min(target_group, window.num_groups()-1)

0 commit comments

Comments
 (0)