11import os
22import re
33from pathlib import Path
4- from typing import Iterator
4+ from typing import Iterator , cast
55
66import sublime
77import sublime_plugin
@@ -25,21 +25,26 @@ def run(self):
2525 path = Path (current_file_path )
2626 current_file_name = path .name
2727 base_name = path .stem
28- base_name = re .sub (r"_spec$" , "" , base_name )
2928
3029 if current_file_name .endswith ("_spec.rb" ):
31- source_matcher = re .compile (r"[/\\]" + base_name + r"\.rb$" )
32- self .open_project_file (source_matcher , current_file_path )
30+ base_name = re .sub (r"_spec$" , "" , base_name )
31+ target = base_name + ".rb"
32+ self .open_project_file (target , current_file_path )
3333 else :
34- test_matcher = re .compile (r"[/\\]" + base_name + r"_spec\.rb$" )
35- self .open_project_file (test_matcher , current_file_path )
36-
37- def open_project_file (self , file_matcher : re .Pattern [str ], file_path : str ):
38- for path , _ , filenames in self .walk_project_folder (file_path ):
39- for filename in filter (lambda f : f .endswith (".rb" ), filenames ):
40- current_file = os .path .join (path , filename )
41- if file_matcher .search (current_file ):
42- return self .switch_to (os .path .join (path , filename ))
34+ target = base_name + "_spec.rb"
35+ self .open_project_file (target , current_file_path )
36+
37+ def open_project_file (self , target : "str" , file_path : str ):
38+ excluded = cast (
39+ "list[str]" ,
40+ sublime .load_settings ("Preferences.sublime-settings" ).get (
41+ "folder_exclude_patterns"
42+ ),
43+ )
44+ for path , dirs , filenames in self .walk_project_folder (file_path ):
45+ dirs [:] = [d for d in dirs if d not in excluded ]
46+ if target in filenames :
47+ return self .switch_to (os .path .join (path , target ))
4348 print ("RSpec: No matching files found" )
4449
4550 def spec_paths (self , file_path : str ):
@@ -62,7 +67,12 @@ def code_paths(self, file_path: str):
6267 re .sub (r"\b{}\b" .format (os .path .join ("spec" , "lib" )), "lib" , file_path ),
6368 ]
6469
65- def quick_find (self , file_path : str ):
70+ def quick_find (self , file_path : str ) -> bool :
71+ """Guesses location of the target based on common Ruby project layouts
72+
73+ SIDE EFFECT: Opens/focuses a file
74+
75+ Returns a boolean representing whether or not the file was found"""
6676 if re .search (r"\bspec\b|_spec\.rb$" , file_path ):
6777 for path in self .code_paths (file_path ):
6878 if os .path .exists (path ):
@@ -72,9 +82,9 @@ def quick_find(self, file_path: str):
7282 if os .path .exists (path ):
7383 return self .switch_to (path )
7484 print ("RSpec: quick find failed, doing regular find" )
85+ return False
7586
7687 def batch_replace (self , string : str , * pairs : "tuple[str, str]" ) -> str :
77- string = ""
7888 for target , replacement in pairs :
7989 string = re .sub (target , replacement , string )
8090 return str (string )
0 commit comments