Skip to content

Commit 0233b77

Browse files
committed
Auto merge of rust-lang#18050 - rust-lang:davidbarsky/push-uyvtlsvoqrxw, r=Veykril
assist: ensure `replace_qualified_name_with_use` applies to the first path segment This change helps a bit with the discoverability of `replace_qualified_name_with_use`. Specifically, it ensures that a cursor on the first path segment (e.g., `$0std::fmt::Debug`, where `$0` is the cursor) would result in an import along the lines of `use std::fmt;` and `fmt::Debug;` at the usage sites.
2 parents 7d8c5ad + 38514ba commit 0233b77

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@ pub(crate) fn replace_qualified_name_with_use(
2929
acc: &mut Assists,
3030
ctx: &AssistContext<'_>,
3131
) -> Option<()> {
32-
let original_path: ast::Path = ctx.find_node_at_offset()?;
32+
let mut original_path: ast::Path = ctx.find_node_at_offset()?;
3333
// We don't want to mess with use statements
3434
if original_path.syntax().ancestors().find_map(ast::UseTree::cast).is_some() {
3535
cov_mark::hit!(not_applicable_in_use);
3636
return None;
3737
}
3838

3939
if original_path.qualifier().is_none() {
40-
cov_mark::hit!(dont_import_trivial_paths);
41-
return None;
40+
original_path = original_path.parent_path()?;
4241
}
4342

4443
// only offer replacement for non assoc items
@@ -236,12 +235,6 @@ fs::Path
236235
);
237236
}
238237

239-
#[test]
240-
fn dont_import_trivial_paths() {
241-
cov_mark::check!(dont_import_trivial_paths);
242-
check_assist_not_applicable(replace_qualified_name_with_use, r"impl foo$0 for () {}");
243-
}
244-
245238
#[test]
246239
fn test_replace_not_applicable_in_use() {
247240
cov_mark::check!(not_applicable_in_use);
@@ -271,6 +264,29 @@ fn main() {
271264
);
272265
}
273266

267+
#[test]
268+
fn assist_runs_on_first_segment() {
269+
check_assist(
270+
replace_qualified_name_with_use,
271+
r"
272+
mod std { pub mod fmt { pub trait Debug {} } }
273+
fn main() {
274+
$0std::fmt::Debug;
275+
let x: std::fmt::Debug = std::fmt::Debug;
276+
}
277+
",
278+
r"
279+
use std::fmt;
280+
281+
mod std { pub mod fmt { pub trait Debug {} } }
282+
fn main() {
283+
fmt::Debug;
284+
let x: fmt::Debug = fmt::Debug;
285+
}
286+
",
287+
);
288+
}
289+
274290
#[test]
275291
fn does_not_replace_in_submodules() {
276292
check_assist(

0 commit comments

Comments
 (0)