Skip to content

Commit

Permalink
Performance refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Po Chen committed Nov 5, 2014
1 parent 4d5d9a1 commit be6048e
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions OpenRSpecFile.py
Expand Up @@ -6,11 +6,12 @@
class OpenRspecFileCommand(sublime_plugin.WindowCommand):

def run(self):
if not self.window.active_view(): return
if not self.window.active_view():
return

current_file_path = self.window.active_view().file_name()

if re.search(r"\w+\.rb$", current_file_path):
if current_file_path.endswith(".rb"):
current_file_name = re.search(r"[/\\]([\w.]+)$", current_file_path).group(1)
base_name = re.search(r"(\w+)\.rb$", current_file_name).group(1)
base_name = re.sub(r"_spec$", "", base_name)
Expand All @@ -20,17 +21,15 @@ def run(self):
print("Current file: " + current_file_name)
if current_file_name.endswith("_spec.rb"):
source_matcher = re.compile(r"[/\\]" + base_name + "\.rb$")
self.open_project_file(source_matcher, target_group)
elif current_file_name.endswith(".rb"):
test_matcher = re.compile(r"[/\\]" + base_name + "_spec\.rb$")
self.open_project_file(test_matcher, target_group)
self.open_project_file(source_matcher, current_file_path, target_group)
else:
print("Error: current file is not valid for RSpec to switch file")
test_matcher = re.compile(r"[/\\]" + base_name + "_spec\.rb$")
self.open_project_file(test_matcher, current_file_path, target_group)
else:
print("Error: current file is not a ruby file")

def open_project_file(self, file_matcher, group):
for path, dirs, filenames in self.walk_all_folders():
def open_project_file(self, file_matcher, file_path, group):
for path, dirs, filenames in self.walk_project_folder(file_path):
for filename in filter(lambda f: f.endswith(".rb"), filenames):
current_file = os.path.join(path, filename)
if file_matcher.search(current_file):
Expand All @@ -39,6 +38,8 @@ def open_project_file(self, file_matcher, group):
return print("Opened: " + filename)
print("RSpec: No matching files found")

def walk_all_folders(self):
def walk_project_folder(self, file_path):
for folder in self.window.folders():
if not file_path.startswith(folder):
continue
yield from os.walk(folder)

0 comments on commit be6048e

Please sign in to comment.