Skip to content

Commit c1925df

Browse files
committed
Replace insert_use_statement with the new insert_use
1 parent 952f385 commit c1925df

File tree

4 files changed

+68
-54
lines changed

4 files changed

+68
-54
lines changed

crates/assists/src/handlers/auto_import.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ use syntax::{
1313
};
1414

1515
use crate::{
16-
utils::insert_use_statement, AssistContext, AssistId, AssistKind, Assists, GroupLabel,
16+
utils::{insert_use, MergeBehaviour},
17+
AssistContext, AssistId, AssistKind, Assists, GroupLabel,
1718
};
19+
use ast::make;
20+
use insert_use::find_insert_use_container;
1821

1922
// Assist: auto_import
2023
//
@@ -44,19 +47,21 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
4447

4548
let range = ctx.sema.original_range(&auto_import_assets.syntax_under_caret).range;
4649
let group = auto_import_assets.get_import_group_message();
50+
let container = find_insert_use_container(&auto_import_assets.syntax_under_caret, ctx)?;
51+
let syntax = container.either(|l| l.syntax().clone(), |r| r.syntax().clone());
4752
for import in proposed_imports {
4853
acc.add_group(
4954
&group,
5055
AssistId("auto_import", AssistKind::QuickFix),
5156
format!("Import `{}`", &import),
5257
range,
5358
|builder| {
54-
insert_use_statement(
55-
&auto_import_assets.syntax_under_caret,
56-
&import.to_string(),
57-
ctx,
58-
builder.text_edit_builder(),
59+
let new_syntax = insert_use(
60+
&syntax,
61+
make::path_from_text(&import.to_string()),
62+
Some(MergeBehaviour::Full),
5963
);
64+
builder.replace(syntax.text_range(), new_syntax.to_string())
6065
},
6166
);
6267
}
@@ -358,7 +363,7 @@ mod tests {
358363
}
359364
",
360365
r"
361-
use PubMod::{PubStruct2, PubStruct1};
366+
use PubMod::{PubStruct1, PubStruct2};
362367
363368
struct Test {
364369
test: PubStruct2<u8>,

crates/assists/src/handlers/extract_struct_from_enum_variant.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ use syntax::{
1010
};
1111

1212
use crate::{
13-
assist_context::AssistBuilder, utils::insert_use_statement, AssistContext, AssistId,
14-
AssistKind, Assists,
13+
assist_context::AssistBuilder,
14+
utils::{insert_use, MergeBehaviour},
15+
AssistContext, AssistId, AssistKind, Assists,
1516
};
17+
use ast::make;
18+
use insert_use::find_insert_use_container;
1619

1720
// Assist: extract_struct_from_enum_variant
1821
//
@@ -107,12 +110,15 @@ fn insert_import(
107110
if let Some(mut mod_path) = mod_path {
108111
mod_path.segments.pop();
109112
mod_path.segments.push(variant_hir_name.clone());
110-
insert_use_statement(
111-
path.syntax(),
112-
&mod_path.to_string(),
113-
ctx,
114-
builder.text_edit_builder(),
113+
let container = find_insert_use_container(path.syntax(), ctx)?;
114+
let syntax = container.either(|l| l.syntax().clone(), |r| r.syntax().clone());
115+
116+
let new_syntax = insert_use(
117+
&syntax,
118+
make::path_from_text(&mod_path.to_string()),
119+
Some(MergeBehaviour::Full),
115120
);
121+
builder.replace(syntax.text_range(), new_syntax.to_string())
116122
}
117123
Some(())
118124
}

crates/assists/src/handlers/replace_qualified_name_with_use.rs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode, TextRang
22
use test_utils::mark;
33

44
use crate::{
5-
utils::{find_insert_use_container, insert_use_statement},
5+
utils::{find_insert_use_container, insert_use, MergeBehaviour},
66
AssistContext, AssistId, AssistKind, Assists,
77
};
8+
use ast::make;
89

910
// Assist: replace_qualified_name_with_use
1011
//
@@ -32,7 +33,7 @@ pub(crate) fn replace_qualified_name_with_use(
3233
mark::hit!(dont_import_trivial_paths);
3334
return None;
3435
}
35-
let path_to_import = path.to_string().clone();
36+
let path_to_import = path.to_string();
3637
let path_to_import = match path.segment()?.generic_arg_list() {
3738
Some(generic_args) => {
3839
let generic_args_start =
@@ -43,28 +44,24 @@ pub(crate) fn replace_qualified_name_with_use(
4344
};
4445

4546
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());
4649
acc.add(
4750
AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite),
4851
"Replace qualified path with use",
4952
target,
5053
|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-
6254
// Now that we've brought the name into scope, re-qualify all paths that could be
6355
// affected (that is, all paths inside the node we added the `use` to).
6456
let mut rewriter = SyntaxRewriter::default();
65-
let syntax = container.either(|l| l.syntax().clone(), |r| r.syntax().clone());
66-
shorten_paths(&mut rewriter, syntax, path);
67-
builder.rewrite(rewriter);
57+
shorten_paths(&mut rewriter, syntax.clone(), path);
58+
let rewritten_syntax = rewriter.rewrite(&syntax);
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())
6865
},
6966
)
7067
}
@@ -220,9 +217,10 @@ impl std::fmt::Debug<|> for Foo {
220217
}
221218
",
222219
r"
223-
use stdx;
224220
use std::fmt::Debug;
225221
222+
use stdx;
223+
226224
impl Debug for Foo {
227225
}
228226
",
@@ -274,7 +272,7 @@ impl std::io<|> for Foo {
274272
}
275273
",
276274
r"
277-
use std::{io, fmt};
275+
use std::{fmt, io};
278276
279277
impl io for Foo {
280278
}
@@ -293,7 +291,7 @@ impl std::fmt::Debug<|> for Foo {
293291
}
294292
",
295293
r"
296-
use std::fmt::{self, Debug, };
294+
use std::fmt::{self, Debug};
297295
298296
impl Debug for Foo {
299297
}
@@ -312,7 +310,7 @@ impl std::fmt<|> for Foo {
312310
}
313311
",
314312
r"
315-
use std::fmt::{self, Debug};
313+
use std::fmt::{Debug, self};
316314
317315
impl fmt for Foo {
318316
}
@@ -330,8 +328,9 @@ use std::fmt::{Debug, nested::{Display}};
330328
impl std::fmt::nested<|> for Foo {
331329
}
332330
",
331+
// FIXME(veykril): should be nested::{self, Display} here
333332
r"
334-
use std::fmt::{Debug, nested::{Display, self}};
333+
use std::fmt::{Debug, nested::{Display}, nested};
335334
336335
impl nested for Foo {
337336
}
@@ -349,8 +348,9 @@ use std::fmt::{Debug, nested::{self, Display}};
349348
impl std::fmt::nested<|> for Foo {
350349
}
351350
",
351+
// FIXME(veykril): self is being pulled out for some reason now
352352
r"
353-
use std::fmt::{Debug, nested::{self, Display}};
353+
use std::fmt::{Debug, nested::{Display}, nested};
354354
355355
impl nested for Foo {
356356
}
@@ -369,7 +369,7 @@ impl std::fmt::nested::Debug<|> for Foo {
369369
}
370370
",
371371
r"
372-
use std::fmt::{Debug, nested::{Display, Debug}};
372+
use std::fmt::{Debug, nested::{Display}, nested::Debug};
373373
374374
impl Debug for Foo {
375375
}
@@ -388,7 +388,7 @@ impl std::fmt::nested::Display<|> for Foo {
388388
}
389389
",
390390
r"
391-
use std::fmt::{nested::Display, Debug};
391+
use std::fmt::{Debug, nested::Display};
392392
393393
impl Display for Foo {
394394
}
@@ -407,7 +407,7 @@ impl std::fmt::Display<|> for Foo {
407407
}
408408
",
409409
r"
410-
use std::fmt::{Display, nested::Debug};
410+
use std::fmt::{nested::Debug, Display};
411411
412412
impl Display for Foo {
413413
}
@@ -427,11 +427,12 @@ use crate::{
427427
428428
fn foo() { crate::ty::lower<|>::trait_env() }
429429
",
430+
// FIXME(veykril): formatting broke here
430431
r"
431432
use crate::{
432-
ty::{Substs, Ty, lower},
433+
ty::{Substs, Ty},
433434
AssocItem,
434-
};
435+
ty::lower};
435436
436437
fn foo() { lower::trait_env() }
437438
",
@@ -451,6 +452,8 @@ impl foo::Debug<|> for Foo {
451452
r"
452453
use std::fmt as foo;
453454
455+
use foo::Debug;
456+
454457
impl Debug for Foo {
455458
}
456459
",
@@ -627,7 +630,7 @@ fn main() {
627630
}
628631
",
629632
r"
630-
use std::fmt::{self, Display};
633+
use std::fmt::{Display, self};
631634
632635
fn main() {
633636
fmt;
@@ -647,9 +650,8 @@ impl std::io<|> for Foo {
647650
}
648651
",
649652
r"
650-
use std::io;
651-
652653
pub use std::fmt;
654+
use std::io;
653655
654656
impl io for Foo {
655657
}
@@ -668,9 +670,8 @@ impl std::io<|> for Foo {
668670
}
669671
",
670672
r"
671-
use std::io;
672-
673673
pub(crate) use std::fmt;
674+
use std::io;
674675
675676
impl io for Foo {
676677
}

crates/assists/src/utils/insert_use.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ pub(crate) fn find_insert_use_container(
1818
) -> Option<Either<ast::ItemList, ast::SourceFile>> {
1919
ctx.sema.ancestors_with_macros(position.clone()).find_map(|n| {
2020
if let Some(module) = ast::Module::cast(n.clone()) {
21-
return module.item_list().map(Either::Left);
21+
module.item_list().map(Either::Left)
22+
} else {
23+
Some(Either::Right(ast::SourceFile::cast(n)?))
2224
}
23-
Some(Either::Right(ast::SourceFile::cast(n)?))
2425
})
2526
}
2627

@@ -92,6 +93,7 @@ fn use_tree_list_is_nested(tl: &ast::UseTreeList) -> bool {
9293
})
9394
}
9495

96+
// FIXME: currently this merely prepends the new tree into old, ideally it would insert the items in a sorted fashion
9597
pub fn try_merge_trees(
9698
old: &ast::UseTree,
9799
new: &ast::UseTree,
@@ -486,7 +488,7 @@ use std::io;",
486488
check_full(
487489
"std::foo::bar::Baz",
488490
r"use std::foo::bar::Qux;",
489-
r"use std::foo::bar::{Baz, Qux};",
491+
r"use std::foo::bar::{Qux, Baz};",
490492
)
491493
}
492494

@@ -495,7 +497,7 @@ use std::io;",
495497
check_last(
496498
"std::foo::bar::Baz",
497499
r"use std::foo::bar::Qux;",
498-
r"use std::foo::bar::{Baz, Qux};",
500+
r"use std::foo::bar::{Qux, Baz};",
499501
)
500502
}
501503

@@ -504,7 +506,7 @@ use std::io;",
504506
check_full(
505507
"std::foo::bar::Baz",
506508
r"use std::foo::bar::{Qux, Quux};",
507-
r"use std::foo::bar::{Baz, Quux, Qux};",
509+
r"use std::foo::bar::{Qux, Quux, Baz};",
508510
)
509511
}
510512

@@ -513,7 +515,7 @@ use std::io;",
513515
check_last(
514516
"std::foo::bar::Baz",
515517
r"use std::foo::bar::{Qux, Quux};",
516-
r"use std::foo::bar::{Baz, Quux, Qux};",
518+
r"use std::foo::bar::{Qux, Quux, Baz};",
517519
)
518520
}
519521

@@ -522,7 +524,7 @@ use std::io;",
522524
check_full(
523525
"std::foo::bar::Baz",
524526
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
525-
r"use std::foo::bar::{Baz, quux::{Fez, Fizz}, Qux};",
527+
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}, Baz};",
526528
)
527529
}
528530

@@ -532,7 +534,7 @@ use std::io;",
532534
"std::foo::bar::Baz",
533535
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
534536
r"use std::foo::bar::Baz;
535-
use std::foo::bar::{quux::{Fez, Fizz}, Qux};",
537+
use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
536538
)
537539
}
538540

0 commit comments

Comments
 (0)