Skip to content

Commit 3877421

Browse files
bors[bot]Veykril
andauthored
Merge #5989
5989: Rewrite import merging r=jonas-schievink a=Veykril Rewrites how import merging is being handled. It is now a recursive function to properly handle merging of intermediate levels in the import trees. With this ordering the imports is also now possible tho it doesn't quite order it the same way as `rustfmt` does yet, namely it orders lowercase identifiers after uppercase identifiers as that is the standard character order that rust uses. This also fixes a few weird behaviors that were visible in some of the `replace_qualified_name_with_use.rs` tests. This really took longer than I was hoping for, fighting with import trees is quite the exhausting task 😅 Co-authored-by: Lukas Wirth <[email protected]>
2 parents 4bc8015 + 45298b5 commit 3877421

File tree

5 files changed

+292
-103
lines changed

5 files changed

+292
-103
lines changed

crates/assists/src/handlers/merge_imports.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ use std::fmt::Debug;
9595
use std::fmt<|>::Display;
9696
",
9797
r"
98-
use std::fmt::{Display, Debug};
98+
use std::fmt::{Debug, Display};
9999
",
100100
);
101101
}
@@ -122,7 +122,7 @@ use std::fmt::{self, Display};
122122
use std::{fmt, <|>fmt::Display};
123123
",
124124
r"
125-
use std::{fmt::{Display, self}};
125+
use std::{fmt::{self, Display}};
126126
",
127127
);
128128
}
@@ -210,13 +210,17 @@ use std::{fmt<|>::Debug, fmt::Display};
210210
use std::{fmt::{Debug, Display}};
211211
",
212212
);
213+
}
214+
215+
#[test]
216+
fn test_merge_nested2() {
213217
check_assist(
214218
merge_imports,
215219
r"
216220
use std::{fmt::Debug, fmt<|>::Display};
217221
",
218222
r"
219-
use std::{fmt::{Display, Debug}};
223+
use std::{fmt::{Debug, Display}};
220224
",
221225
);
222226
}
@@ -310,9 +314,7 @@ use foo::<|>{
310314
};
311315
",
312316
r"
313-
use foo::{
314-
FooBar,
315-
bar::baz};
317+
use foo::{FooBar, bar::baz};
316318
",
317319
)
318320
}

crates/assists/src/handlers/replace_qualified_name_with_use.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl std::fmt<|> for Foo {
312312
}
313313
",
314314
r"
315-
use std::fmt::{Debug, self};
315+
use std::fmt::{self, Debug};
316316
317317
impl fmt for Foo {
318318
}
@@ -330,9 +330,8 @@ use std::fmt::{Debug, nested::{Display}};
330330
impl std::fmt::nested<|> for Foo {
331331
}
332332
",
333-
// FIXME(veykril): should be nested::{self, Display} here
334333
r"
335-
use std::fmt::{Debug, nested::{Display}, nested};
334+
use std::fmt::{Debug, nested::{self, Display}};
336335
337336
impl nested for Foo {
338337
}
@@ -350,9 +349,8 @@ use std::fmt::{Debug, nested::{self, Display}};
350349
impl std::fmt::nested<|> for Foo {
351350
}
352351
",
353-
// FIXME(veykril): nested is duplicated now
354352
r"
355-
use std::fmt::{Debug, nested::{self, Display}, nested};
353+
use std::fmt::{Debug, nested::{self, Display}};
356354
357355
impl nested for Foo {
358356
}
@@ -371,7 +369,7 @@ impl std::fmt::nested::Debug<|> for Foo {
371369
}
372370
",
373371
r"
374-
use std::fmt::{Debug, nested::{Display}, nested::Debug};
372+
use std::fmt::{Debug, nested::{Debug, Display}};
375373
376374
impl Debug for Foo {
377375
}
@@ -409,7 +407,7 @@ impl std::fmt::Display<|> for Foo {
409407
}
410408
",
411409
r"
412-
use std::fmt::{nested::Debug, Display};
410+
use std::fmt::{Display, nested::Debug};
413411
414412
impl Display for Foo {
415413
}
@@ -429,12 +427,8 @@ use crate::{
429427
430428
fn foo() { crate::ty::lower<|>::trait_env() }
431429
",
432-
// FIXME(veykril): formatting broke here
433430
r"
434-
use crate::{
435-
ty::{Substs, Ty},
436-
AssocItem,
437-
ty::lower};
431+
use crate::{AssocItem, ty::{Substs, Ty, lower}};
438432
439433
fn foo() { lower::trait_env() }
440434
",
@@ -633,7 +627,7 @@ fn main() {
633627
}
634628
",
635629
r"
636-
use std::fmt::{Display, self};
630+
use std::fmt::{self, Display};
637631
638632
fn main() {
639633
fmt;

0 commit comments

Comments
 (0)