aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers/replace_qualified_name_with_use.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/assists/src/handlers/replace_qualified_name_with_use.rs')
-rw-r--r--crates/assists/src/handlers/replace_qualified_name_with_use.rs68
1 files changed, 36 insertions, 32 deletions
diff --git a/crates/assists/src/handlers/replace_qualified_name_with_use.rs b/crates/assists/src/handlers/replace_qualified_name_with_use.rs
index 470e5f8ff..85c70d16b 100644
--- a/crates/assists/src/handlers/replace_qualified_name_with_use.rs
+++ b/crates/assists/src/handlers/replace_qualified_name_with_use.rs
@@ -2,9 +2,10 @@ use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode, TextRang
2use test_utils::mark; 2use test_utils::mark;
3 3
4use crate::{ 4use crate::{
5 utils::{find_insert_use_container, insert_use_statement}, 5 utils::{insert_use, ImportScope, MergeBehaviour},
6 AssistContext, AssistId, AssistKind, Assists, 6 AssistContext, AssistId, AssistKind, Assists,
7}; 7};
8use ast::make;
8 9
9// Assist: replace_qualified_name_with_use 10// Assist: replace_qualified_name_with_use
10// 11//
@@ -32,7 +33,7 @@ pub(crate) fn replace_qualified_name_with_use(
32 mark::hit!(dont_import_trivial_paths); 33 mark::hit!(dont_import_trivial_paths);
33 return None; 34 return None;
34 } 35 }
35 let path_to_import = path.to_string().clone(); 36 let path_to_import = path.to_string();
36 let path_to_import = match path.segment()?.generic_arg_list() { 37 let path_to_import = match path.segment()?.generic_arg_list() {
37 Some(generic_args) => { 38 Some(generic_args) => {
38 let generic_args_start = 39 let generic_args_start =
@@ -43,28 +44,26 @@ pub(crate) fn replace_qualified_name_with_use(
43 }; 44 };
44 45
45 let target = path.syntax().text_range(); 46 let target = path.syntax().text_range();
47 let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?;
48 let syntax = scope.as_syntax_node();
46 acc.add( 49 acc.add(
47 AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite), 50 AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite),
48 "Replace qualified path with use", 51 "Replace qualified path with use",
49 target, 52 target,
50 |builder| { 53 |builder| {
51 let container = match find_insert_use_container(path.syntax(), ctx) {
52 Some(c) => c,
53 None => return,
54 };
55 insert_use_statement(
56 path.syntax(),
57 &path_to_import.to_string(),
58 ctx,
59 builder.text_edit_builder(),
60 );
61
62 // Now that we've brought the name into scope, re-qualify all paths that could be 54 // Now that we've brought the name into scope, re-qualify all paths that could be
63 // affected (that is, all paths inside the node we added the `use` to). 55 // affected (that is, all paths inside the node we added the `use` to).
64 let mut rewriter = SyntaxRewriter::default(); 56 let mut rewriter = SyntaxRewriter::default();
65 let syntax = container.either(|l| l.syntax().clone(), |r| r.syntax().clone()); 57 shorten_paths(&mut rewriter, syntax.clone(), path);
66 shorten_paths(&mut rewriter, syntax, path); 58 let rewritten_syntax = rewriter.rewrite(&syntax);
67 builder.rewrite(rewriter); 59 if let Some(ref import_scope) = ImportScope::from(rewritten_syntax) {
60 let new_syntax = insert_use(
61 import_scope,
62 make::path_from_text(path_to_import),
63 Some(MergeBehaviour::Full),
64 );
65 builder.replace(syntax.text_range(), new_syntax.to_string())
66 }
68 }, 67 },
69 ) 68 )
70} 69}
@@ -220,9 +219,10 @@ impl std::fmt::Debug<|> for Foo {
220} 219}
221 ", 220 ",
222 r" 221 r"
223use stdx;
224use std::fmt::Debug; 222use std::fmt::Debug;
225 223
224use stdx;
225
226impl Debug for Foo { 226impl Debug for Foo {
227} 227}
228 ", 228 ",
@@ -274,7 +274,7 @@ impl std::io<|> for Foo {
274} 274}
275 ", 275 ",
276 r" 276 r"
277use std::{io, fmt}; 277use std::{fmt, io};
278 278
279impl io for Foo { 279impl io for Foo {
280} 280}
@@ -293,7 +293,7 @@ impl std::fmt::Debug<|> for Foo {
293} 293}
294 ", 294 ",
295 r" 295 r"
296use std::fmt::{self, Debug, }; 296use std::fmt::{self, Debug};
297 297
298impl Debug for Foo { 298impl Debug for Foo {
299} 299}
@@ -312,7 +312,7 @@ impl std::fmt<|> for Foo {
312} 312}
313 ", 313 ",
314 r" 314 r"
315use std::fmt::{self, Debug}; 315use std::fmt::{Debug, self};
316 316
317impl fmt for Foo { 317impl fmt for Foo {
318} 318}
@@ -330,8 +330,9 @@ use std::fmt::{Debug, nested::{Display}};
330impl std::fmt::nested<|> for Foo { 330impl std::fmt::nested<|> for Foo {
331} 331}
332", 332",
333 // FIXME(veykril): should be nested::{self, Display} here
333 r" 334 r"
334use std::fmt::{Debug, nested::{Display, self}}; 335use std::fmt::{Debug, nested::{Display}, nested};
335 336
336impl nested for Foo { 337impl nested for Foo {
337} 338}
@@ -349,8 +350,9 @@ use std::fmt::{Debug, nested::{self, Display}};
349impl std::fmt::nested<|> for Foo { 350impl std::fmt::nested<|> for Foo {
350} 351}
351", 352",
353 // FIXME(veykril): nested is duplicated now
352 r" 354 r"
353use std::fmt::{Debug, nested::{self, Display}}; 355use std::fmt::{Debug, nested::{self, Display}, nested};
354 356
355impl nested for Foo { 357impl nested for Foo {
356} 358}
@@ -369,7 +371,7 @@ impl std::fmt::nested::Debug<|> for Foo {
369} 371}
370", 372",
371 r" 373 r"
372use std::fmt::{Debug, nested::{Display, Debug}}; 374use std::fmt::{Debug, nested::{Display}, nested::Debug};
373 375
374impl Debug for Foo { 376impl Debug for Foo {
375} 377}
@@ -388,7 +390,7 @@ impl std::fmt::nested::Display<|> for Foo {
388} 390}
389", 391",
390 r" 392 r"
391use std::fmt::{nested::Display, Debug}; 393use std::fmt::{Debug, nested::Display};
392 394
393impl Display for Foo { 395impl Display for Foo {
394} 396}
@@ -407,7 +409,7 @@ impl std::fmt::Display<|> for Foo {
407} 409}
408", 410",
409 r" 411 r"
410use std::fmt::{Display, nested::Debug}; 412use std::fmt::{nested::Debug, Display};
411 413
412impl Display for Foo { 414impl Display for Foo {
413} 415}
@@ -427,11 +429,12 @@ use crate::{
427 429
428fn foo() { crate::ty::lower<|>::trait_env() } 430fn foo() { crate::ty::lower<|>::trait_env() }
429", 431",
432 // FIXME(veykril): formatting broke here
430 r" 433 r"
431use crate::{ 434use crate::{
432 ty::{Substs, Ty, lower}, 435 ty::{Substs, Ty},
433 AssocItem, 436 AssocItem,
434}; 437ty::lower};
435 438
436fn foo() { lower::trait_env() } 439fn foo() { lower::trait_env() }
437", 440",
@@ -451,6 +454,8 @@ impl foo::Debug<|> for Foo {
451 r" 454 r"
452use std::fmt as foo; 455use std::fmt as foo;
453 456
457use foo::Debug;
458
454impl Debug for Foo { 459impl Debug for Foo {
455} 460}
456", 461",
@@ -515,6 +520,7 @@ fn main() {
515 ", 520 ",
516 r" 521 r"
517#![allow(dead_code)] 522#![allow(dead_code)]
523
518use std::fmt::Debug; 524use std::fmt::Debug;
519 525
520fn main() { 526fn main() {
@@ -627,7 +633,7 @@ fn main() {
627} 633}
628 ", 634 ",
629 r" 635 r"
630use std::fmt::{self, Display}; 636use std::fmt::{Display, self};
631 637
632fn main() { 638fn main() {
633 fmt; 639 fmt;
@@ -647,9 +653,8 @@ impl std::io<|> for Foo {
647} 653}
648 ", 654 ",
649 r" 655 r"
650use std::io;
651
652pub use std::fmt; 656pub use std::fmt;
657use std::io;
653 658
654impl io for Foo { 659impl io for Foo {
655} 660}
@@ -668,9 +673,8 @@ impl std::io<|> for Foo {
668} 673}
669 ", 674 ",
670 r" 675 r"
671use std::io;
672
673pub(crate) use std::fmt; 676pub(crate) use std::fmt;
677use std::io;
674 678
675impl io for Foo { 679impl io for Foo {
676} 680}