From 1c3d6725e28e3e85acb804799e7daca02daeae86 Mon Sep 17 00:00:00 2001 From: Tianyi Song <42670338+tysg@users.noreply.github.com> Date: Fri, 28 Jan 2022 16:15:23 +0800 Subject: [PATCH] Drop generic args in path before insert use --- .../replace_qualified_name_with_use.rs | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs index 3121e22981..71c674a8dd 100644 --- a/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs @@ -84,7 +84,8 @@ pub(crate) fn replace_qualified_name_with_use( ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)), ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)), }; - shorten_paths(scope.as_syntax_node(), &path.clone_for_update()); + shorten_paths(scope.as_syntax_node(), &path); + let path = drop_generic_args(&path); // stick the found import in front of the to be replaced path let path = match path_to_qualifier.and_then(|it| mod_path_to_ast(&it).qualifier()) { Some(qualifier) => make::path_concat(qualifier, path), @@ -95,7 +96,17 @@ pub(crate) fn replace_qualified_name_with_use( ) } -/// Adds replacements to `re` that shorten `path` in all descendants of `node`. +fn drop_generic_args(path: &ast::Path) -> ast::Path { + let path = path.clone_for_update(); + if let Some(segment) = path.segment() { + if let Some(generic_args) = segment.generic_arg_list() { + ted::remove(generic_args.syntax()); + } + } + path +} + +/// Mutates `node` to shorten `path` in all descendants of `node`. fn shorten_paths(node: &SyntaxNode, path: &ast::Path) { for child in node.children() { match_ast! { @@ -389,6 +400,37 @@ mod std { fn main() { drop(0); } +", + ); + } + + #[test] + fn replace_should_drop_generic_args_in_use() { + check_assist( + replace_qualified_name_with_use, + r" +mod std { + pub mod mem { + pub fn drop(_: T) {} + } +} + +fn main() { + std::mem::drop::$0(0); +} +", + r" +use std::mem::drop; + +mod std { + pub mod mem { + pub fn drop(_: T) {} + } +} + +fn main() { + drop::(0); +} ", ); }