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.rs65
1 files changed, 33 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..56e85125d 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::{find_insert_use_container, insert_use, 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,24 @@ 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 container = find_insert_use_container(path.syntax(), ctx)?;
48 let syntax = container.either(|l| l.syntax().clone(), |r| r.syntax().clone());
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 let new_syntax = insert_use(
60 &rewritten_syntax,
61 make::path_from_text(path_to_import),
62 Some(MergeBehaviour::Full),
63 );
64 builder.replace(syntax.text_range(), new_syntax.to_string())
68 }, 65 },
69 ) 66 )
70} 67}
@@ -220,9 +217,10 @@ impl std::fmt::Debug<|> for Foo {
220} 217}
221 ", 218 ",
222 r" 219 r"
223use stdx;
224use std::fmt::Debug; 220use std::fmt::Debug;
225 221
222use stdx;
223
226impl Debug for Foo { 224impl Debug for Foo {
227} 225}
228 ", 226 ",
@@ -274,7 +272,7 @@ impl std::io<|> for Foo {
274} 272}
275 ", 273 ",
276 r" 274 r"
277use std::{io, fmt}; 275use std::{fmt, io};
278 276
279impl io for Foo { 277impl io for Foo {
280} 278}
@@ -293,7 +291,7 @@ impl std::fmt::Debug<|> for Foo {
293} 291}
294 ", 292 ",
295 r" 293 r"
296use std::fmt::{self, Debug, }; 294use std::fmt::{self, Debug};
297 295
298impl Debug for Foo { 296impl Debug for Foo {
299} 297}
@@ -312,7 +310,7 @@ impl std::fmt<|> for Foo {
312} 310}
313 ", 311 ",
314 r" 312 r"
315use std::fmt::{self, Debug}; 313use std::fmt::{Debug, self};
316 314
317impl fmt for Foo { 315impl fmt for Foo {
318} 316}
@@ -330,8 +328,9 @@ use std::fmt::{Debug, nested::{Display}};
330impl std::fmt::nested<|> for Foo { 328impl std::fmt::nested<|> for Foo {
331} 329}
332", 330",
331 // FIXME(veykril): should be nested::{self, Display} here
333 r" 332 r"
334use std::fmt::{Debug, nested::{Display, self}}; 333use std::fmt::{Debug, nested::{Display}, nested};
335 334
336impl nested for Foo { 335impl nested for Foo {
337} 336}
@@ -349,8 +348,9 @@ use std::fmt::{Debug, nested::{self, Display}};
349impl std::fmt::nested<|> for Foo { 348impl std::fmt::nested<|> for Foo {
350} 349}
351", 350",
351 // FIXME(veykril): self is being pulled out for some reason now
352 r" 352 r"
353use std::fmt::{Debug, nested::{self, Display}}; 353use std::fmt::{Debug, nested::{Display}, nested};
354 354
355impl nested for Foo { 355impl nested for Foo {
356} 356}
@@ -369,7 +369,7 @@ impl std::fmt::nested::Debug<|> for Foo {
369} 369}
370", 370",
371 r" 371 r"
372use std::fmt::{Debug, nested::{Display, Debug}}; 372use std::fmt::{Debug, nested::{Display}, nested::Debug};
373 373
374impl Debug for Foo { 374impl Debug for Foo {
375} 375}
@@ -388,7 +388,7 @@ impl std::fmt::nested::Display<|> for Foo {
388} 388}
389", 389",
390 r" 390 r"
391use std::fmt::{nested::Display, Debug}; 391use std::fmt::{Debug, nested::Display};
392 392
393impl Display for Foo { 393impl Display for Foo {
394} 394}
@@ -407,7 +407,7 @@ impl std::fmt::Display<|> for Foo {
407} 407}
408", 408",
409 r" 409 r"
410use std::fmt::{Display, nested::Debug}; 410use std::fmt::{nested::Debug, Display};
411 411
412impl Display for Foo { 412impl Display for Foo {
413} 413}
@@ -427,11 +427,12 @@ use crate::{
427 427
428fn foo() { crate::ty::lower<|>::trait_env() } 428fn foo() { crate::ty::lower<|>::trait_env() }
429", 429",
430 // FIXME(veykril): formatting broke here
430 r" 431 r"
431use crate::{ 432use crate::{
432 ty::{Substs, Ty, lower}, 433 ty::{Substs, Ty},
433 AssocItem, 434 AssocItem,
434}; 435ty::lower};
435 436
436fn foo() { lower::trait_env() } 437fn foo() { lower::trait_env() }
437", 438",
@@ -451,6 +452,8 @@ impl foo::Debug<|> for Foo {
451 r" 452 r"
452use std::fmt as foo; 453use std::fmt as foo;
453 454
455use foo::Debug;
456
454impl Debug for Foo { 457impl Debug for Foo {
455} 458}
456", 459",
@@ -627,7 +630,7 @@ fn main() {
627} 630}
628 ", 631 ",
629 r" 632 r"
630use std::fmt::{self, Display}; 633use std::fmt::{Display, self};
631 634
632fn main() { 635fn main() {
633 fmt; 636 fmt;
@@ -647,9 +650,8 @@ impl std::io<|> for Foo {
647} 650}
648 ", 651 ",
649 r" 652 r"
650use std::io;
651
652pub use std::fmt; 653pub use std::fmt;
654use std::io;
653 655
654impl io for Foo { 656impl io for Foo {
655} 657}
@@ -668,9 +670,8 @@ impl std::io<|> for Foo {
668} 670}
669 ", 671 ",
670 r" 672 r"
671use std::io;
672
673pub(crate) use std::fmt; 673pub(crate) use std::fmt;
674use std::io;
674 675
675impl io for Foo { 676impl io for Foo {
676} 677}