1 use ide_db::helpers::insert_use::{insert_use, ImportScope};
2 use syntax::{ast, match_ast, ted, AstNode, SyntaxNode};
4 use crate::{AssistContext, AssistId, AssistKind, Assists};
6 // Assist: replace_qualified_name_with_use
8 // Adds a use statement for a given fully-qualified name.
11 // fn process(map: std::collections::$0HashMap<String, String>) {}
15 // use std::collections::HashMap;
17 // fn process(map: HashMap<String, String>) {}
19 pub(crate) fn replace_qualified_name_with_use(
23 let path: ast::Path = ctx.find_node_at_offset()?;
24 // We don't want to mess with use statements
25 if path.syntax().ancestors().find_map(ast::Use::cast).is_some() {
28 if path.qualifier().is_none() {
29 cov_mark::hit!(dont_import_trivial_paths);
33 let target = path.syntax().text_range();
34 let scope = ImportScope::find_insert_use_container(path.syntax(), &ctx.sema)?;
35 let syntax = scope.as_syntax_node();
37 AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite),
38 "Replace qualified path with use",
41 // Now that we've brought the name into scope, re-qualify all paths that could be
42 // affected (that is, all paths inside the node we added the `use` to).
43 let syntax = builder.make_mut(syntax.clone());
44 if let Some(ref import_scope) = ImportScope::from(syntax.clone()) {
45 insert_use(import_scope, path.clone(), ctx.config.insert_use);
47 shorten_paths(syntax.clone(), &path.clone_for_update());
52 /// Adds replacements to `re` that shorten `path` in all descendants of `node`.
53 fn shorten_paths(node: SyntaxNode, path: &ast::Path) {
54 for child in node.children() {
57 // Don't modify `use` items, as this can break the `use` item when injecting a new
58 // import into the use tree.
59 ast::Use(_it) => continue,
60 // Don't descend into submodules, they don't have the same `use` items in scope.
61 ast::Module(_it) => continue,
64 match maybe_replace_path(p.clone(), path.clone()) {
66 None => shorten_paths(p.syntax().clone(), path),
69 _ => shorten_paths(child, path),
75 fn maybe_replace_path(path: ast::Path, target: ast::Path) -> Option<()> {
76 if !path_eq(path.clone(), target) {
80 // Shorten `path`, leaving only its last segment.
81 if let Some(parent) = path.qualifier() {
82 ted::remove(parent.syntax());
84 if let Some(double_colon) = path.coloncolon_token() {
85 ted::remove(&double_colon);
91 fn path_eq(lhs: ast::Path, rhs: ast::Path) -> bool {
92 let mut lhs_curr = lhs;
93 let mut rhs_curr = rhs;
95 match (lhs_curr.segment(), rhs_curr.segment()) {
96 (Some(lhs), Some(rhs)) if lhs.syntax().text() == rhs.syntax().text() => (),
100 match (lhs_curr.qualifier(), rhs_curr.qualifier()) {
101 (Some(lhs), Some(rhs)) => {
105 (None, None) => return true,
113 use crate::tests::{check_assist, check_assist_not_applicable};
118 fn test_replace_already_imported() {
120 replace_qualified_name_with_use,
135 fn test_replace_add_use_no_anchor() {
137 replace_qualified_name_with_use,
150 fn test_replace_add_use_no_anchor_with_item_below() {
152 replace_qualified_name_with_use,
171 fn test_replace_add_use_no_anchor_with_item_above() {
173 replace_qualified_name_with_use,
192 fn test_replace_add_use_no_anchor_2seg() {
194 replace_qualified_name_with_use,
207 fn test_replace_add_use() {
209 replace_qualified_name_with_use,
213 impl std::fmt::Debug$0 for Foo {
228 fn test_replace_file_use_other_anchor() {
230 replace_qualified_name_with_use,
232 impl std::fmt::Debug$0 for Foo {
245 fn test_replace_add_use_other_anchor_indent() {
247 replace_qualified_name_with_use,
249 impl std::fmt::Debug$0 for Foo {
262 fn test_replace_split_different() {
264 replace_qualified_name_with_use,
268 impl std::io$0 for Foo {
281 fn test_replace_split_self_for_use() {
283 replace_qualified_name_with_use,
287 impl std::fmt::Debug$0 for Foo {
291 use std::fmt::{self, Debug};
300 fn test_replace_split_self_for_target() {
302 replace_qualified_name_with_use,
306 impl std::fmt$0 for Foo {
310 use std::fmt::{self, Debug};
319 fn test_replace_add_to_nested_self_nested() {
321 replace_qualified_name_with_use,
323 use std::fmt::{Debug, nested::{Display}};
325 impl std::fmt::nested$0 for Foo {
329 use std::fmt::{Debug, nested::{self, Display}};
331 impl nested for Foo {
338 fn test_replace_add_to_nested_self_already_included() {
340 replace_qualified_name_with_use,
342 use std::fmt::{Debug, nested::{self, Display}};
344 impl std::fmt::nested$0 for Foo {
348 use std::fmt::{Debug, nested::{self, Display}};
350 impl nested for Foo {
357 fn test_replace_add_to_nested_nested() {
359 replace_qualified_name_with_use,
361 use std::fmt::{Debug, nested::{Display}};
363 impl std::fmt::nested::Debug$0 for Foo {
367 use std::fmt::{Debug, nested::{Debug, Display}};
376 fn test_replace_split_common_target_longer() {
378 replace_qualified_name_with_use,
382 impl std::fmt::nested::Display$0 for Foo {
386 use std::fmt::{Debug, nested::Display};
388 impl Display for Foo {
395 fn test_replace_split_common_use_longer() {
397 replace_qualified_name_with_use,
399 use std::fmt::nested::Debug;
401 impl std::fmt::Display$0 for Foo {
405 use std::fmt::{Display, nested::Debug};
407 impl Display for Foo {
414 fn test_replace_use_nested_import() {
416 replace_qualified_name_with_use,
423 fn foo() { crate::ty::lower$0::trait_env() }
426 use crate::{AssocItem, ty::{Substs, Ty, lower}};
428 fn foo() { lower::trait_env() }
434 fn test_replace_alias() {
436 replace_qualified_name_with_use,
440 impl foo::Debug$0 for Foo {
455 fn dont_import_trivial_paths() {
456 cov_mark::check!(dont_import_trivial_paths);
457 check_assist_not_applicable(
458 replace_qualified_name_with_use,
467 fn test_replace_not_applicable_in_use() {
468 check_assist_not_applicable(
469 replace_qualified_name_with_use,
477 fn test_replace_add_use_no_anchor_in_mod_mod() {
479 replace_qualified_name_with_use,
500 fn inserts_imports_after_inner_attributes() {
502 replace_qualified_name_with_use,
523 fn replaces_all_affected_paths() {
525 replace_qualified_name_with_use,
529 let x: std::fmt::Debug = std::fmt::Debug;
537 let x: Debug = Debug;
544 fn replaces_all_affected_paths_mod() {
546 replace_qualified_name_with_use,
551 let x: std::fmt::Debug = std::fmt::Debug;
568 let x: Debug = Debug;
583 fn does_not_replace_in_submodules() {
585 replace_qualified_name_with_use,
614 fn does_not_replace_in_use() {
616 replace_qualified_name_with_use,
618 use std::fmt::Display;
625 use std::fmt::{self, Display};
635 fn does_not_replace_pub_use() {
637 replace_qualified_name_with_use,
641 impl std::io$0 for Foo {
655 fn does_not_replace_pub_crate_use() {
657 replace_qualified_name_with_use,
659 pub(crate) use std::fmt;
661 impl std::io$0 for Foo {
665 pub(crate) use std::fmt;