diff --git a/OpenRSpecFile.py b/OpenRSpecFile.py index 0971916..badfae7 100644 --- a/OpenRSpecFile.py +++ b/OpenRSpecFile.py @@ -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) @@ -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): @@ -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)