Auto merge of #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.
This commit is contained in:
bors 2024-09-11 10:04:56 +00:00
commit a90d78f327

View file

@ -29,7 +29,7 @@ pub(crate) fn replace_qualified_name_with_use(
acc: &mut Assists,
ctx: &AssistContext<'_>,
) -> Option<()> {
let original_path: ast::Path = ctx.find_node_at_offset()?;
let mut original_path: ast::Path = ctx.find_node_at_offset()?;
// We don't want to mess with use statements
if original_path.syntax().ancestors().find_map(ast::UseTree::cast).is_some() {
cov_mark::hit!(not_applicable_in_use);
@ -37,8 +37,7 @@ pub(crate) fn replace_qualified_name_with_use(
}
if original_path.qualifier().is_none() {
cov_mark::hit!(dont_import_trivial_paths);
return None;
original_path = original_path.parent_path()?;
}
// only offer replacement for non assoc items
@ -236,12 +235,6 @@ fs::Path
);
}
#[test]
fn dont_import_trivial_paths() {
cov_mark::check!(dont_import_trivial_paths);
check_assist_not_applicable(replace_qualified_name_with_use, r"impl foo$0 for () {}");
}
#[test]
fn test_replace_not_applicable_in_use() {
cov_mark::check!(not_applicable_in_use);
@ -271,6 +264,29 @@ fn main() {
);
}
#[test]
fn assist_runs_on_first_segment() {
check_assist(
replace_qualified_name_with_use,
r"
mod std { pub mod fmt { pub trait Debug {} } }
fn main() {
$0std::fmt::Debug;
let x: std::fmt::Debug = std::fmt::Debug;
}
",
r"
use std::fmt;
mod std { pub mod fmt { pub trait Debug {} } }
fn main() {
fmt::Debug;
let x: fmt::Debug = fmt::Debug;
}
",
);
}
#[test]
fn does_not_replace_in_submodules() {
check_assist(