Skip to content

Commit

Permalink
assist: ensure replace_qualified_name_with_use applies to the first p…
Browse files Browse the repository at this point in the history
…ath segment
  • Loading branch information
davidbarsky committed Sep 8, 2024
1 parent f16a03f commit 3bcc2b7
Showing 1 changed file with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ 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);
return None;
}

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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 3bcc2b7

Please sign in to comment.