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 | 65 |
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 | |||
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::{find_insert_use_container, insert_use, 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,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" |
223 | use stdx; | ||
224 | use std::fmt::Debug; | 220 | use std::fmt::Debug; |
225 | 221 | ||
222 | use stdx; | ||
223 | |||
226 | impl Debug for Foo { | 224 | impl Debug for Foo { |
227 | } | 225 | } |
228 | ", | 226 | ", |
@@ -274,7 +272,7 @@ impl std::io<|> for Foo { | |||
274 | } | 272 | } |
275 | ", | 273 | ", |
276 | r" | 274 | r" |
277 | use std::{io, fmt}; | 275 | use std::{fmt, io}; |
278 | 276 | ||
279 | impl io for Foo { | 277 | impl io for Foo { |
280 | } | 278 | } |
@@ -293,7 +291,7 @@ impl std::fmt::Debug<|> for Foo { | |||
293 | } | 291 | } |
294 | ", | 292 | ", |
295 | r" | 293 | r" |
296 | use std::fmt::{self, Debug, }; | 294 | use std::fmt::{self, Debug}; |
297 | 295 | ||
298 | impl Debug for Foo { | 296 | impl Debug for Foo { |
299 | } | 297 | } |
@@ -312,7 +310,7 @@ impl std::fmt<|> for Foo { | |||
312 | } | 310 | } |
313 | ", | 311 | ", |
314 | r" | 312 | r" |
315 | use std::fmt::{self, Debug}; | 313 | use std::fmt::{Debug, self}; |
316 | 314 | ||
317 | impl fmt for Foo { | 315 | impl fmt for Foo { |
318 | } | 316 | } |
@@ -330,8 +328,9 @@ use std::fmt::{Debug, nested::{Display}}; | |||
330 | impl std::fmt::nested<|> for Foo { | 328 | impl std::fmt::nested<|> for Foo { |
331 | } | 329 | } |
332 | ", | 330 | ", |
331 | // FIXME(veykril): should be nested::{self, Display} here | ||
333 | r" | 332 | r" |
334 | use std::fmt::{Debug, nested::{Display, self}}; | 333 | use std::fmt::{Debug, nested::{Display}, nested}; |
335 | 334 | ||
336 | impl nested for Foo { | 335 | impl nested for Foo { |
337 | } | 336 | } |
@@ -349,8 +348,9 @@ use std::fmt::{Debug, nested::{self, Display}}; | |||
349 | impl std::fmt::nested<|> for Foo { | 348 | impl 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" |
353 | use std::fmt::{Debug, nested::{self, Display}}; | 353 | use std::fmt::{Debug, nested::{Display}, nested}; |
354 | 354 | ||
355 | impl nested for Foo { | 355 | impl nested for Foo { |
356 | } | 356 | } |
@@ -369,7 +369,7 @@ impl std::fmt::nested::Debug<|> for Foo { | |||
369 | } | 369 | } |
370 | ", | 370 | ", |
371 | r" | 371 | r" |
372 | use std::fmt::{Debug, nested::{Display, Debug}}; | 372 | use std::fmt::{Debug, nested::{Display}, nested::Debug}; |
373 | 373 | ||
374 | impl Debug for Foo { | 374 | impl Debug for Foo { |
375 | } | 375 | } |
@@ -388,7 +388,7 @@ impl std::fmt::nested::Display<|> for Foo { | |||
388 | } | 388 | } |
389 | ", | 389 | ", |
390 | r" | 390 | r" |
391 | use std::fmt::{nested::Display, Debug}; | 391 | use std::fmt::{Debug, nested::Display}; |
392 | 392 | ||
393 | impl Display for Foo { | 393 | impl Display for Foo { |
394 | } | 394 | } |
@@ -407,7 +407,7 @@ impl std::fmt::Display<|> for Foo { | |||
407 | } | 407 | } |
408 | ", | 408 | ", |
409 | r" | 409 | r" |
410 | use std::fmt::{Display, nested::Debug}; | 410 | use std::fmt::{nested::Debug, Display}; |
411 | 411 | ||
412 | impl Display for Foo { | 412 | impl Display for Foo { |
413 | } | 413 | } |
@@ -427,11 +427,12 @@ use crate::{ | |||
427 | 427 | ||
428 | fn foo() { crate::ty::lower<|>::trait_env() } | 428 | fn foo() { crate::ty::lower<|>::trait_env() } |
429 | ", | 429 | ", |
430 | // FIXME(veykril): formatting broke here | ||
430 | r" | 431 | r" |
431 | use crate::{ | 432 | use crate::{ |
432 | ty::{Substs, Ty, lower}, | 433 | ty::{Substs, Ty}, |
433 | AssocItem, | 434 | AssocItem, |
434 | }; | 435 | ty::lower}; |
435 | 436 | ||
436 | fn foo() { lower::trait_env() } | 437 | fn foo() { lower::trait_env() } |
437 | ", | 438 | ", |
@@ -451,6 +452,8 @@ impl foo::Debug<|> for Foo { | |||
451 | r" | 452 | r" |
452 | use std::fmt as foo; | 453 | use std::fmt as foo; |
453 | 454 | ||
455 | use foo::Debug; | ||
456 | |||
454 | impl Debug for Foo { | 457 | impl Debug for Foo { |
455 | } | 458 | } |
456 | ", | 459 | ", |
@@ -627,7 +630,7 @@ fn main() { | |||
627 | } | 630 | } |
628 | ", | 631 | ", |
629 | r" | 632 | r" |
630 | use std::fmt::{self, Display}; | 633 | use std::fmt::{Display, self}; |
631 | 634 | ||
632 | fn main() { | 635 | fn main() { |
633 | fmt; | 636 | fmt; |
@@ -647,9 +650,8 @@ impl std::io<|> for Foo { | |||
647 | } | 650 | } |
648 | ", | 651 | ", |
649 | r" | 652 | r" |
650 | use std::io; | ||
651 | |||
652 | pub use std::fmt; | 653 | pub use std::fmt; |
654 | use std::io; | ||
653 | 655 | ||
654 | impl io for Foo { | 656 | impl io for Foo { |
655 | } | 657 | } |
@@ -668,9 +670,8 @@ impl std::io<|> for Foo { | |||
668 | } | 670 | } |
669 | ", | 671 | ", |
670 | r" | 672 | r" |
671 | use std::io; | ||
672 | |||
673 | pub(crate) use std::fmt; | 673 | pub(crate) use std::fmt; |
674 | use std::io; | ||
674 | 675 | ||
675 | impl io for Foo { | 676 | impl io for Foo { |
676 | } | 677 | } |