Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8fab92f

Browse files
committed
normalize use trees when merging imports
1 parent da798bc commit 8fab92f

File tree

5 files changed

+350
-26
lines changed

5 files changed

+350
-26
lines changed

crates/ide-assists/src/handlers/merge_imports.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ use std::fmt$0::{Display, Debug};
201201
use std::fmt::{Display, Debug};
202202
",
203203
r"
204-
use std::fmt::{Display, Debug};
204+
use std::fmt::{Debug, Display};
205205
",
206206
);
207207

@@ -214,7 +214,7 @@ use std::fmt::{Display, Debug};
214214
check_assist_import_one_variations!(
215215
"std::fmt$0::{Display, Debug}",
216216
"std::fmt::{Display, Debug}",
217-
"use {std::fmt::{Display, Debug}};"
217+
"use {std::fmt::{Debug, Display}};"
218218
);
219219
}
220220

@@ -419,13 +419,13 @@ use std$0::{fmt::{Write, Display}};
419419
use std::{fmt::{self, Debug}};
420420
",
421421
r"
422-
use std::{fmt::{self, Debug, Display, Write}};
422+
use std::fmt::{self, Debug, Display, Write};
423423
",
424424
);
425425
check_assist_import_one_variations!(
426426
"std$0::{fmt::{Write, Display}}",
427427
"std::{fmt::{self, Debug}}",
428-
"use {std::{fmt::{self, Debug, Display, Write}}};"
428+
"use {std::fmt::{self, Debug, Display, Write}};"
429429
);
430430
}
431431

@@ -438,13 +438,13 @@ use std$0::{fmt::{self, Debug}};
438438
use std::{fmt::{Write, Display}};
439439
",
440440
r"
441-
use std::{fmt::{self, Debug, Display, Write}};
441+
use std::fmt::{self, Debug, Display, Write};
442442
",
443443
);
444444
check_assist_import_one_variations!(
445445
"std$0::{fmt::{self, Debug}}",
446446
"std::{fmt::{Write, Display}}",
447-
"use {std::{fmt::{self, Debug, Display, Write}}};"
447+
"use {std::fmt::{self, Debug, Display, Write}};"
448448
);
449449
}
450450

@@ -470,13 +470,13 @@ use foo::$0{bar::{self}};
470470
use foo::{bar};
471471
",
472472
r"
473-
use foo::{bar::{self}};
473+
use foo::bar;
474474
",
475475
);
476476
check_assist_import_one_variations!(
477477
"foo::$0{bar::{self}}",
478478
"foo::{bar}",
479-
"use {foo::{bar::{self}}};"
479+
"use {foo::bar};"
480480
);
481481
}
482482

@@ -489,13 +489,13 @@ use foo::$0{bar};
489489
use foo::{bar::{self}};
490490
",
491491
r"
492-
use foo::{bar::{self}};
492+
use foo::bar;
493493
",
494494
);
495495
check_assist_import_one_variations!(
496496
"foo::$0{bar}",
497497
"foo::{bar::{self}}",
498-
"use {foo::{bar::{self}}};"
498+
"use {foo::bar};"
499499
);
500500
}
501501

@@ -508,13 +508,13 @@ use std$0::{fmt::*};
508508
use std::{fmt::{self, Display}};
509509
",
510510
r"
511-
use std::{fmt::{self, Display, *}};
511+
use std::fmt::{self, Display, *};
512512
",
513513
);
514514
check_assist_import_one_variations!(
515515
"std$0::{fmt::*}",
516516
"std::{fmt::{self, Display}}",
517-
"use {std::{fmt::{self, Display, *}}};"
517+
"use {std::fmt::{self, Display, *}};"
518518
);
519519
}
520520

crates/ide-completion/src/tests/flyimport.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn main() {
106106
}
107107
"#,
108108
r#"
109-
use dep::{FirstStruct, some_module::{SecondStruct, ThirdStruct}};
109+
use dep::{some_module::{SecondStruct, ThirdStruct}, FirstStruct};
110110
111111
fn main() {
112112
ThirdStruct

crates/ide-db/src/imports/insert_use.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use syntax::{
1717
use crate::{
1818
imports::merge_imports::{
1919
common_prefix, eq_attrs, eq_visibility, try_merge_imports, use_tree_cmp, MergeBehavior,
20+
NormalizationStyle,
2021
},
2122
RootDatabase,
2223
};
@@ -40,6 +41,15 @@ pub enum ImportGranularity {
4041
One,
4142
}
4243

44+
impl From<ImportGranularity> for NormalizationStyle {
45+
fn from(granularity: ImportGranularity) -> Self {
46+
match granularity {
47+
ImportGranularity::One => NormalizationStyle::One,
48+
_ => NormalizationStyle::Default,
49+
}
50+
}
51+
}
52+
4353
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4454
pub struct InsertUseConfig {
4555
pub granularity: ImportGranularity,

crates/ide-db/src/imports/insert_use/tests.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ use std::io;",
635635
check_one(
636636
"std::io",
637637
r"use {std::fmt::{Result, Display}};",
638-
r"use {std::{fmt::{Result, Display}, io}};",
638+
r"use {std::{fmt::{Display, Result}, io}};",
639639
);
640640
}
641641

@@ -650,12 +650,12 @@ fn merge_groups_full() {
650650
check_crate(
651651
"std::io",
652652
r"use std::fmt::{Result, Display};",
653-
r"use std::{fmt::{Result, Display}, io};",
653+
r"use std::{fmt::{Display, Result}, io};",
654654
);
655655
check_one(
656656
"std::io",
657657
r"use {std::fmt::{Result, Display}};",
658-
r"use {std::{fmt::{Result, Display}, io}};",
658+
r"use {std::{fmt::{Display, Result}, io}};",
659659
);
660660
}
661661

@@ -749,12 +749,12 @@ fn merge_groups_full_nested_deep() {
749749
check_crate(
750750
"std::foo::bar::quux::Baz",
751751
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
752-
r"use std::foo::bar::{Qux, quux::{Baz, Fez, Fizz}};",
752+
r"use std::foo::bar::{quux::{Baz, Fez, Fizz}, Qux};",
753753
);
754754
check_one(
755755
"std::foo::bar::quux::Baz",
756756
r"use {std::foo::bar::{Qux, quux::{Fez, Fizz}}};",
757-
r"use {std::foo::bar::{Qux, quux::{Baz, Fez, Fizz}}};",
757+
r"use {std::foo::bar::{quux::{Baz, Fez, Fizz}, Qux}};",
758758
);
759759
}
760760

@@ -763,7 +763,7 @@ fn merge_groups_full_nested_long() {
763763
check_crate(
764764
"std::foo::bar::Baz",
765765
r"use std::{foo::bar::Qux};",
766-
r"use std::{foo::bar::{Baz, Qux}};",
766+
r"use std::foo::bar::{Baz, Qux};",
767767
);
768768
}
769769

@@ -772,12 +772,12 @@ fn merge_groups_last_nested_long() {
772772
check_crate(
773773
"std::foo::bar::Baz",
774774
r"use std::{foo::bar::Qux};",
775-
r"use std::{foo::bar::{Baz, Qux}};",
775+
r"use std::foo::bar::{Baz, Qux};",
776776
);
777777
check_one(
778778
"std::foo::bar::Baz",
779779
r"use {std::{foo::bar::Qux}};",
780-
r"use {std::{foo::bar::{Baz, Qux}}};",
780+
r"use {std::foo::bar::{Baz, Qux}};",
781781
);
782782
}
783783

@@ -898,7 +898,7 @@ fn merge_glob() {
898898
r"
899899
use syntax::{SyntaxKind::*};",
900900
r"
901-
use syntax::{SyntaxKind::{self, *}};",
901+
use syntax::SyntaxKind::{self, *};",
902902
)
903903
}
904904

@@ -907,7 +907,7 @@ fn merge_glob_nested() {
907907
check_crate(
908908
"foo::bar::quux::Fez",
909909
r"use foo::bar::{Baz, quux::*};",
910-
r"use foo::bar::{Baz, quux::{Fez, *}};",
910+
r"use foo::bar::{quux::{Fez, *}, Baz};",
911911
)
912912
}
913913

@@ -1211,7 +1211,7 @@ fn insert_with_renamed_import_complex_use() {
12111211
use self::foo::{self, Foo as _, Bar};
12121212
"#,
12131213
r#"
1214-
use self::foo::{self, Foo, Bar};
1214+
use self::foo::{self, Bar, Foo};
12151215
"#,
12161216
&InsertUseConfig {
12171217
granularity: ImportGranularity::Crate,

0 commit comments

Comments
 (0)