Skip to content
Browse files

Merge pull request #11 from rexikan/improved_open_rspec_file

Improved open rspec file
  • Loading branch information...
2 parents 34c92a5 + bde30be commit e4eec33e4c3a0b52d5658f26b1e83564ecab30d2 Greg Williams committed Jun 24, 2012
Showing with 93 additions and 111 deletions.
  1. +12 −15 Default (OSX).sublime-keymap
  2. +12 −15 Default (Windows).sublime-keymap
  3. +48 −61 OpenRSpecFile.py
  4. +14 −20 RspecCreateModule.py → RSpecCreateModule.py
  5. +7 −0 shared.py
View
27 Default (OSX).sublime-keymap
@@ -1,15 +1,12 @@
-[
- { "keys": ["super+alt+b"],
- "command": "exec",
- "args": {"cmd": ["rspec", "*"]},
- "file_regex": "# ([A-Za-z0-9_./ ]+rb):([0-9]+)",
- "working_dir": "${folder}"
- },
-
- { "keys": ["super+alt+period"], "command": "open_rspec_file", "args": {"option": "test_and_source"} },
- { "keys": ["super+period"], "command": "open_rspec_file", "args": {"option": "next"} },
- { "keys": ["super+alt+s"], "command": "open_rspec_file", "args": {"option": "source"} },
- { "keys": ["super+alt+t"], "command": "open_rspec_file", "args": {"option": "test"} },
-
- { "keys": ["super+alt+n"], "command": "rspec_create_module", "args": {} }
-]
+[
+ { "keys": ["super+alt+b"],
+ "command": "exec",
+ "args": {"cmd": ["rspec", "*"]},
+ "file_regex": "# ([A-Za-z0-9_./ ]+rb):([0-9]+)",
+ "working_dir": "${folder}"
+ },
+
+ { "keys": ["super+period"], "command": "open_rspec_file", "args": {} },
+
+ { "keys": ["super+alt+n"], "command": "rspec_create_module", "args": {} }
+]
View
27 Default (Windows).sublime-keymap
@@ -1,15 +1,12 @@
-[
- { "keys": ["ctrl+f7"],
- "command": "exec",
- "args": {"cmd": ["rspec.bat", "*"]},
- "file_regex": "# ([A-Za-z0-9_./ ]+rb):([0-9]+)",
- "working_dir": "${folder}"
- },
-
- { "keys": ["ctrl+alt+period"], "command": "open_rspec_file", "args": {"option": "test_and_source"} },
- { "keys": ["ctrl+period"], "command": "open_rspec_file", "args": {"option": "next"} },
- { "keys": ["ctrl+alt+s"], "command": "open_rspec_file", "args": {"option": "source"} },
- { "keys": ["ctrl+alt+t"], "command": "open_rspec_file", "args": {"option": "test"} },
-
- { "keys": ["ctrl+alt+n"], "command": "rspec_create_module", "args": {} }
-]
+[
+ { "keys": ["ctrl+f7"],
+ "command": "exec",
+ "args": {"cmd": ["rspec.bat", "*"]},
+ "file_regex": "# ([A-Za-z0-9_./ ]+rb):([0-9]+)",
+ "working_dir": "${folder}"
+ },
+
+ { "keys": ["ctrl+period"], "command": "open_rspec_file", "args": {} },
+
+ { "keys": ["ctrl+alt+n"], "command": "rspec_create_module", "args": {} }
+]
View
109 OpenRSpecFile.py
@@ -1,61 +1,48 @@
-import sublime
-import sublime_plugin
-import re, inspect, os
-
-class OpenRspecFileCommand(sublime_plugin.WindowCommand):
-
- def run(self, option):
- if not self.window.active_view():
- return
-
- self.views = []
- window = self.window
- current_file_path = self.window.active_view().file_name()
-
- if re.search(r"\w+\.rb$", current_file_path):
-
- current_file = re.search(r"([\w\.]+)$", current_file_path).group(1)
- base_name = re.search(r"(\w+)\.(\w+)$", current_file).group(1)
- base_name = re.sub('_spec', '', base_name)
-
- source_matcher = re.compile("[/\\\\]" + base_name + "\.rb$")
- test_matcher = re.compile("[/\\\\]" + base_name + "_spec\.rb$")
-
- if option == 'next':
- print "Current file: " + current_file
- if re.search(re.compile(base_name + "_spec\.rb$"), current_file):
- self.open_project_file(source_matcher, window)
- elif re.search(re.compile(base_name + "\.rb$"), current_file):
- self.open_project_file(test_matcher, window)
- else:
- print "Current file is not valid for RSpec switch file!"
- elif option == 'source':
- self.open_project_file(source_matcher, window)
- elif option == 'test':
- self.open_project_file(test_matcher, window)
- elif option == 'test_and_source':
- window.run_command('set_layout', {
- "cols": [0.0, 0.5, 1.0],
- "rows": [0.0, 1.0],
- "cells": [[0, 0, 1, 1], [1, 0, 2, 1]]
- })
- self.open_project_file(test_matcher, window, 0)
- self.open_project_file(source_matcher, window, 1)
-
- for v in self.views:
- window.focus_view(v)
-
- def open_project_file(self, file_matcher, window, auto_set_view=-1):
- for root, dirs, files in os.walk(window.folders()[0]):
- for f in files:
- if re.search(r"\.rb$", f):
- cur_file = os.path.join(root, f)
- # print "Assessing: " + cur_file
- if file_matcher.search(cur_file):
- file_view = window.open_file(os.path.join(root, f))
- if auto_set_view >= 0: # don't set the view unless specified
- window.run_command('move_to_group', {'group': auto_set_view})
- self.views.append(file_view)
- print("Opened: " + f)
- return
- print("No matching files!")
+import sublime
+import sublime_plugin
+import re, inspect, os
+import shared
+
+class OpenRspecFileCommand(sublime_plugin.WindowCommand):
+
+ def run(self):
+ if not self.window.active_view():
+ return
+
+ self.views = []
+ window = self.window
+ current_file_path = self.window.active_view().file_name()
+
+ if re.search(r"\w+\.rb$", current_file_path):
+
+ current_file = re.search(r"([\w\.]+)$", current_file_path).group(1)
+ base_name = re.search(r"(\w+)\.(\w+)$", current_file).group(1)
+ base_name = re.sub('_spec', '', base_name)
+
+ source_matcher = re.compile("[/\\\\]" + base_name + "\.rb$")
+ test_matcher = re.compile("[/\\\\]" + base_name + "_spec\.rb$")
+
+ target_group = shared.other_group_in_pair(window)
+
+ print "Current file: " + current_file
+ if re.search(re.compile(base_name + "_spec\.rb$"), current_file):
+ self.open_project_file(source_matcher, window, target_group)
+ elif re.search(re.compile(base_name + "\.rb$"), current_file):
+ self.open_project_file(test_matcher, window, target_group)
+ else:
+ print "Current file is not valid for RSpec switch file!"
+
+ def open_project_file(self, file_matcher, window, group=-1):
+ for root, dirs, files in os.walk(window.folders()[0]):
+ for f in files:
+ if re.search(r"\.rb$", f):
+ cur_file = os.path.join(root, f)
+ # print "Assessing: " + cur_file
+ if file_matcher.search(cur_file):
+ file_view = window.open_file(os.path.join(root, f))
+ if group >= 0: # don't set the view unless specified
+ window.run_command('move_to_group', {'group': group})
+ self.views.append(file_view)
+ print("Opened: " + f)
+ return
+ print("No matching files!")
View
34 RspecCreateModule.py → RSpecCreateModule.py
@@ -1,5 +1,6 @@
import sublime, sublime_plugin, time
import re
+import shared
_patterns = dict((k, re.compile('_*' + v)) for (k, v)
in dict(allcamel=r'(?:[A-Z]+[a-z0-9]*)+$',
@@ -35,19 +36,23 @@ class RspecCreateModuleCommand(sublime_plugin.WindowCommand):
def run(self):
# self.view.insert(edit, 0, "Hello, World!")
self.window.show_input_panel("Enter module name:", "", self.on_done, None, None)
-
+
def on_done(self, text):
- # configure 2-paned layout (spec, module)
- self.window.run_command('set_layout', {
- "cols": [0.0, 0.5, 1.0],
- "rows": [0.0, 1.0],
- "cells": [[0, 0, 1, 1], [1, 0, 2, 1]]
- })
-
+ # create the module
+ module = self.window.new_file()
+ module.set_syntax_file('Packages/Ruby/Ruby.tmLanguage')
+ module.set_name(translate(text, 'allcamel', 'underscores') + '.rb')
+ module_template = "\n\
+class " + text + "\n\
+end"
+ edit = module.begin_edit()
+ module.insert(edit, 0, module_template)
+ module.end_edit(edit)
+
# create the spec
spec = self.window.new_file()
- self.window.run_command('move_to_group', {'group': 0})
+ self.window.run_command('move_to_group', {'group': shared.other_group_in_pair(self.window)})
spec.set_syntax_file('Packages/Ruby/Ruby.tmLanguage')
spec.set_name(translate(text, 'allcamel', 'underscores') + '_spec.rb')
spec_template = "require 'spec_helper'\n\
@@ -59,17 +64,6 @@ def on_done(self, text):
spec.insert(edit, 0, spec_template)
spec.end_edit(edit)
- # create the module
- module = self.window.new_file()
- self.window.run_command('move_to_group', {'group': 1})
- module.set_syntax_file('Packages/Ruby/Ruby.tmLanguage')
- module.set_name(translate(text, 'allcamel', 'underscores') + '.rb')
- module_template = "\n\
-class " + text + "\n\
-end"
- edit = module.begin_edit()
- module.insert(edit, 0, module_template)
- module.end_edit(edit)
# try:
# except ValueError:
# pass
View
7 shared.py
@@ -0,0 +1,7 @@
+"""Returns the neighbour focus group for the current window."""
+def other_group_in_pair(window):
+ if window.active_group() % 2 == 0:
+ target_group = window.active_group()+1
+ else:
+ target_group = window.active_group()-1
+ return min(target_group, window.num_groups()-1)

0 comments on commit e4eec33

Please sign in to comment.
Something went wrong with that request. Please try again.