11import os
22import re
33from pathlib import Path
4+ from typing import Iterator
45
56import sublime
67import sublime_plugin
@@ -33,15 +34,15 @@ def run(self):
3334 test_matcher = re .compile (r"[/\\]" + base_name + r"_spec\.rb$" )
3435 self .open_project_file (test_matcher , current_file_path )
3536
36- def open_project_file (self , file_matcher , file_path ):
37+ def open_project_file (self , file_matcher : re . Pattern [ str ] , file_path : str ):
3738 for path , _ , filenames in self .walk_project_folder (file_path ):
3839 for filename in filter (lambda f : f .endswith (".rb" ), filenames ):
3940 current_file = os .path .join (path , filename )
4041 if file_matcher .search (current_file ):
4142 return self .switch_to (os .path .join (path , filename ))
4243 print ("RSpec: No matching files found" )
4344
44- def spec_paths (self , file_path ):
45+ def spec_paths (self , file_path : str ):
4546 return [
4647 self .batch_replace (
4748 file_path , (r"\b(?:app|lib)\b" , "spec" ), (r"\b(\w+)\.rb" , r"\1_spec.rb" )
@@ -53,15 +54,15 @@ def spec_paths(self, file_path):
5354 ),
5455 ]
5556
56- def code_paths (self , file_path ):
57+ def code_paths (self , file_path : str ):
5758 file_path = re .sub (r"\b(\w+)_spec\.rb$" , r"\1.rb" , file_path )
5859 return [
5960 re .sub (r"\bspec\b" , "app" , file_path ),
6061 re .sub (r"\bspec\b" , "lib" , file_path ),
6162 re .sub (r"\b{}\b" .format (os .path .join ("spec" , "lib" )), "lib" , file_path ),
6263 ]
6364
64- def quick_find (self , file_path ):
65+ def quick_find (self , file_path : str ):
6566 if re .search (r"\bspec\b|_spec\.rb$" , file_path ):
6667 for path in self .code_paths (file_path ):
6768 if os .path .exists (path ):
@@ -72,28 +73,34 @@ def quick_find(self, file_path):
7273 return self .switch_to (path )
7374 print ("RSpec: quick find failed, doing regular find" )
7475
75- def batch_replace (self , string , * pairs ):
76+ def batch_replace (self , string : str , * pairs : "tuple[str, str]" ) -> str :
77+ string = ""
7678 for target , replacement in pairs :
7779 string = re .sub (target , replacement , string )
78- return string
80+ return str ( string )
7981
80- def switch_to (self , file_path ):
82+ def switch_to (self , file_path : str ):
8183 group = shared .other_group_in_pair (self .window )
8284 self .window .open_file (file_path )
8385 self .window .run_command ("move_to_group" , {"group" : group })
8486 print ("Opened: " + file_path )
8587 return True
8688
87- def walk_project_folder (self , file_path ):
89+ def walk_project_folder (
90+ self , file_path : str
91+ ) -> "Iterator[tuple[str, list[str], list[str]]]" :
8892 for folder in self .window .folders ():
8993 if not file_path .startswith (folder ):
9094 continue
9195 yield from os .walk (folder )
9296
9397 def is_enabled (self ):
9498 view = self .window .active_view ()
95- return view and self ._is_ruby_file (view )
99+ return view is not None and self ._is_ruby_file (view )
96100
97- def _is_ruby_file (self , view : sublime .View ):
101+ def _is_ruby_file (self , view : sublime .View ) -> bool :
98102 syntax = view .syntax ()
99- return syntax and syntax .scope in ("source.ruby" , "source.ruby.rspec" )
103+ return syntax is not None and syntax .scope in (
104+ "source.ruby" ,
105+ "source.ruby.rspec" ,
106+ )
0 commit comments