Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit adf13c2

Browse files
author
Pierre Avital
committed
Changed test detection regex to expand its scope
1 parent 317c2b5 commit adf13c2

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

rls/src/actions/run.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,26 @@ pub fn collect_run_actions(ctx: &InitActionContext, file: &Path) -> Vec<RunActio
2323
}
2424

2525
lazy_static! {
26-
static ref TEST_FN_RE: Regex = Regex::new(r"#\[test\]\s*\n\s*fn\s*(?P<name>\w+)").unwrap();
26+
/// __(a):__ `\#\[test\]` matches `#[test]`
27+
///
28+
/// __(b):__ `^[^\/]*?fn\s+(?P<name>\w+)` matches any line which contains `fn name` before any comment is started and captures the word after fn.
29+
/// The laziness of the quantifier is there to make the regex quicker (about 5 times less steps)
30+
///
31+
/// __(c):__ `(\n|.)*?` will match anything lazilly, matching whatever shortest string exists between __(a)__ and __(b)__, ensuring
32+
/// that whatever sits in between `#[test]` and the next function declaration doesn't interfere. It MUST be lazy, both for performance,
33+
/// as well as to prevent matches with further declared functions.
34+
///
35+
/// __(d):__ `(?m)` sets the and `m` regex flags to allow `^` to match line starts.
36+
///
37+
/// This regex is still imperfect, for example:
38+
/// ```rust
39+
/// #[test] /*
40+
/// But at this point it's pretty much a deliberate attempt
41+
/// to make `fn wrong_function` be matched instead of */
42+
/// fn right_function() {}
43+
/// ```
44+
static ref TEST_FN_RE: Regex =
45+
Regex::new(r"(?m)#\[test\](\n|.)*?^[^/]*?fn\s+(?P<name>\w+)").unwrap();
2746
}
2847

2948
let line_index = LineIndex::new(&text);

0 commit comments

Comments
 (0)