Skip to content

Commit 1d36aba

Browse files
committed
Auto merge of rust-lang#13045 - DorianListens:dscheidt/run-test-mod-outside, r=Veykril
feat: Run test mod from anywhere in parent file The "Run" feature of rust-analyzer is super useful, especially for running individual tests or test-modules during development. One common pattern in rust development is to develop tests in the same file as production code, inside a module (usually called `test` or `tests`) marked with `#[cfg(test)]`. Unforunately, this pattern is not well supported by r-a today, as a test module won't show up as a runnable unless the cursor is inside it. In my experience, it is quite common to want to run the tests associated with some production code immediately after editing it, not only after editing the tests themselves. As such it would be better if test modules were available from the "Run" menu even when the cursor is outside the test module. This change updates the filtration logic for runnables in `handlers::handle_runnables` to special case `RunnableKind::TestMod`, making test modules available regardless of the cursor location. Other `RunnableKind`s are unnaffected. Fixes rust-lang#9589
2 parents 1da9156 + 85b9568 commit 1d36aba

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

crates/rust-analyzer/src/handlers.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,8 @@ pub(crate) fn handle_runnables(
703703

704704
let mut res = Vec::new();
705705
for runnable in snap.analysis.runnables(file_id)? {
706-
if let Some(offset) = offset {
707-
if !runnable.nav.full_range.contains_inclusive(offset) {
708-
continue;
709-
}
706+
if should_skip_for_offset(&runnable, offset) {
707+
continue;
710708
}
711709
if should_skip_target(&runnable, cargo_spec.as_ref()) {
712710
continue;
@@ -772,6 +770,14 @@ pub(crate) fn handle_runnables(
772770
Ok(res)
773771
}
774772

773+
fn should_skip_for_offset(runnable: &Runnable, offset: Option<TextSize>) -> bool {
774+
match offset {
775+
None => false,
776+
_ if matches!(&runnable.kind, RunnableKind::TestMod { .. }) => false,
777+
Some(offset) => !runnable.nav.full_range.contains_inclusive(offset),
778+
}
779+
}
780+
775781
pub(crate) fn handle_related_tests(
776782
snap: GlobalStateSnapshot,
777783
params: lsp_types::TextDocumentPositionParams,

0 commit comments

Comments
 (0)