diff options
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.rs | 68 |
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 | |||
2 | use test_utils::mark; | 2 | use test_utils::mark; |
3 | 3 | ||
4 | use crate::{ | 4 | use 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 | }; |
8 | use 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" |
223 | use stdx; | ||
224 | use std::fmt::Debug; | 222 | use std::fmt::Debug; |
225 | 223 | ||
224 | use stdx; | ||
225 | |||
226 | impl Debug for Foo { | 226 | impl Debug for Foo { |
227 | } | 227 | } |
228 | ", | 228 | ", |
@@ -274,7 +274,7 @@ impl std::io<|> for Foo { | |||
274 | } | 274 | } |
275 | ", | 275 | ", |
276 | r" | 276 | r" |
277 | use std::{io, fmt}; | 277 | use std::{fmt, io}; |
278 | 278 | ||
279 | impl io for Foo { | 279 | impl io for Foo { |
280 | } | 280 | } |
@@ -293,7 +293,7 @@ impl std::fmt::Debug<|> for Foo { | |||
293 | } | 293 | } |
294 | ", | 294 | ", |
295 | r" | 295 | r" |
296 | use std::fmt::{self, Debug, }; | 296 | use std::fmt::{self, Debug}; |
297 | 297 | ||
298 | impl Debug for Foo { | 298 | impl Debug for Foo { |
299 | } | 299 | } |
@@ -312,7 +312,7 @@ impl std::fmt<|> for Foo { | |||
312 | } | 312 | } |
313 | ", | 313 | ", |
314 | r" | 314 | r" |
315 | use std::fmt::{self, Debug}; | 315 | use std::fmt::{Debug, self}; |
316 | 316 | ||
317 | impl fmt for Foo { | 317 | impl fmt for Foo { |
318 | } | 318 | } |
@@ -330,8 +330,9 @@ use std::fmt::{Debug, nested::{Display}}; | |||
330 | impl std::fmt::nested<|> for Foo { | 330 | impl std::fmt::nested<|> for Foo { |
331 | } | 331 | } |
332 | ", | 332 | ", |
333 | // FIXME(veykril): should be nested::{self, Display} here | ||
333 | r" | 334 | r" |
334 | use std::fmt::{Debug, nested::{Display, self}}; | 335 | use std::fmt::{Debug, nested::{Display}, nested}; |
335 | 336 | ||
336 | impl nested for Foo { | 337 | impl nested for Foo { |
337 | } | 338 | } |
@@ -349,8 +350,9 @@ use std::fmt::{Debug, nested::{self, Display}}; | |||
349 | impl std::fmt::nested<|> for Foo { | 350 | impl std::fmt::nested<|> for Foo { |
350 | } | 351 | } |
351 | ", | 352 | ", |
353 | // FIXME(veykril): nested is duplicated now | ||
352 | r" | 354 | r" |
353 | use std::fmt::{Debug, nested::{self, Display}}; | 355 | use std::fmt::{Debug, nested::{self, Display}, nested}; |
354 | 356 | ||
355 | impl nested for Foo { | 357 | impl nested for Foo { |
356 | } | 358 | } |
@@ -369,7 +371,7 @@ impl std::fmt::nested::Debug<|> for Foo { | |||
369 | } | 371 | } |
370 | ", | 372 | ", |
371 | r" | 373 | r" |
372 | use std::fmt::{Debug, nested::{Display, Debug}}; | 374 | use std::fmt::{Debug, nested::{Display}, nested::Debug}; |
373 | 375 | ||
374 | impl Debug for Foo { | 376 | impl Debug for Foo { |
375 | } | 377 | } |
@@ -388,7 +390,7 @@ impl std::fmt::nested::Display<|> for Foo { | |||
388 | } | 390 | } |
389 | ", | 391 | ", |
390 | r" | 392 | r" |
391 | use std::fmt::{nested::Display, Debug}; | 393 | use std::fmt::{Debug, nested::Display}; |
392 | 394 | ||
393 | impl Display for Foo { | 395 | impl Display for Foo { |
394 | } | 396 | } |
@@ -407,7 +409,7 @@ impl std::fmt::Display<|> for Foo { | |||
407 | } | 409 | } |
408 | ", | 410 | ", |
409 | r" | 411 | r" |
410 | use std::fmt::{Display, nested::Debug}; | 412 | use std::fmt::{nested::Debug, Display}; |
411 | 413 | ||
412 | impl Display for Foo { | 414 | impl Display for Foo { |
413 | } | 415 | } |
@@ -427,11 +429,12 @@ use crate::{ | |||
427 | 429 | ||
428 | fn foo() { crate::ty::lower<|>::trait_env() } | 430 | fn foo() { crate::ty::lower<|>::trait_env() } |
429 | ", | 431 | ", |
432 | // FIXME(veykril): formatting broke here | ||
430 | r" | 433 | r" |
431 | use crate::{ | 434 | use crate::{ |
432 | ty::{Substs, Ty, lower}, | 435 | ty::{Substs, Ty}, |
433 | AssocItem, | 436 | AssocItem, |
434 | }; | 437 | ty::lower}; |
435 | 438 | ||
436 | fn foo() { lower::trait_env() } | 439 | fn foo() { lower::trait_env() } |
437 | ", | 440 | ", |
@@ -451,6 +454,8 @@ impl foo::Debug<|> for Foo { | |||
451 | r" | 454 | r" |
452 | use std::fmt as foo; | 455 | use std::fmt as foo; |
453 | 456 | ||
457 | use foo::Debug; | ||
458 | |||
454 | impl Debug for Foo { | 459 | impl 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 | |||
518 | use std::fmt::Debug; | 524 | use std::fmt::Debug; |
519 | 525 | ||
520 | fn main() { | 526 | fn main() { |
@@ -627,7 +633,7 @@ fn main() { | |||
627 | } | 633 | } |
628 | ", | 634 | ", |
629 | r" | 635 | r" |
630 | use std::fmt::{self, Display}; | 636 | use std::fmt::{Display, self}; |
631 | 637 | ||
632 | fn main() { | 638 | fn main() { |
633 | fmt; | 639 | fmt; |
@@ -647,9 +653,8 @@ impl std::io<|> for Foo { | |||
647 | } | 653 | } |
648 | ", | 654 | ", |
649 | r" | 655 | r" |
650 | use std::io; | ||
651 | |||
652 | pub use std::fmt; | 656 | pub use std::fmt; |
657 | use std::io; | ||
653 | 658 | ||
654 | impl io for Foo { | 659 | impl io for Foo { |
655 | } | 660 | } |
@@ -668,9 +673,8 @@ impl std::io<|> for Foo { | |||
668 | } | 673 | } |
669 | ", | 674 | ", |
670 | r" | 675 | r" |
671 | use std::io; | ||
672 | |||
673 | pub(crate) use std::fmt; | 676 | pub(crate) use std::fmt; |
677 | use std::io; | ||
674 | 678 | ||
675 | impl io for Foo { | 679 | impl io for Foo { |
676 | } | 680 | } |