Skip to content

Commit 46318c1

Browse files
committed
Typing + performance + respect folder_exclude_patterns
1 parent 1692423 commit 46318c1

2 files changed

Lines changed: 28 additions & 15 deletions

File tree

RSpecToggleSourceOrSpec.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import re
33
from pathlib import Path
4-
from typing import Iterator
4+
from typing import Iterator, cast
55

66
import sublime
77
import 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)

messages/3.0.0.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Report bugs or request features in the Github repo: https://github.com/SublimeTe
2121

2222
# Other improvements and new features
2323

24+
- General performance improvements to the command to toggle between spec and source.
25+
- In the case where a slow search has to be performed (non-standard layouts), the command now
26+
respects your global "folder_exclude_patterns" setting.
2427
- Added improved syntax, courtesy of @themilkman
2528
- Extended to include all RSpec matchers available in the latest version (3.13)
2629
- Scope commands so they don't appear when they are not relevant.

0 commit comments

Comments
 (0)