Skip to content

Commit 0a8c784

Browse files
committed
Auto merge of rust-lang#16352 - davidsemakula:rustfmt-import-sort-algo, r=Veykril
internal: Follow rustfmt's algorithm for ordering imports when ordering and merging use trees Updates use tree ordering and merging utilities to follow rustfmt's algorithm for ordering imports. The [rustfmt implementation](https://github.com/rust-lang/rustfmt/blob/6356fca675bd756d71f5c123cd053d17b16c573e/src/imports.rs) was used as reference.
2 parents 63c4e69 + 1f91c48 commit 0a8c784

File tree

7 files changed

+260
-115
lines changed

7 files changed

+260
-115
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ fn foo() {
986986
}
987987
988988
//- /main.rs
989-
use foo::{Foo, Bool};
989+
use foo::{Bool, Foo};
990990
991991
mod foo;
992992
@@ -1662,7 +1662,7 @@ impl Foo {
16621662
}
16631663
16641664
//- /foo.rs
1665-
use crate::{Foo, Bool};
1665+
use crate::{Bool, Foo};
16661666
16671667
fn foo() -> bool {
16681668
Foo::BOOL == Bool::True

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ use std::fmt::Debug;
179179
use std::fmt$0::Display;
180180
",
181181
r"
182-
use std::fmt::{Display, Debug};
182+
use std::fmt::{Debug, Display};
183183
",
184184
);
185185
}
@@ -206,7 +206,7 @@ use std::fmt::{self, Display};
206206
use std::{fmt, $0fmt::Display};
207207
",
208208
r"
209-
use std::{fmt::{Display, self}};
209+
use std::{fmt::{self, Display}};
210210
",
211211
);
212212
}
@@ -318,7 +318,7 @@ use std::{fmt::{Debug, Display}};
318318
use std::{fmt::Debug, fmt$0::Display};
319319
",
320320
r"
321-
use std::{fmt::{Display, Debug}};
321+
use std::{fmt::{Debug, Display}};
322322
",
323323
);
324324
}
@@ -332,7 +332,7 @@ use std$0::{fmt::{Write, Display}};
332332
use std::{fmt::{self, Debug}};
333333
",
334334
r"
335-
use std::{fmt::{Write, Display, self, Debug}};
335+
use std::{fmt::{self, Debug, Display, Write}};
336336
",
337337
);
338338
}
@@ -346,7 +346,7 @@ use std$0::{fmt::{self, Debug}};
346346
use std::{fmt::{Write, Display}};
347347
",
348348
r"
349-
use std::{fmt::{self, Debug, Write, Display}};
349+
use std::{fmt::{self, Debug, Display, Write}};
350350
",
351351
);
352352
}
@@ -359,7 +359,7 @@ use std::{fmt::{self, Debug, Write, Display}};
359359
use std::{fmt$0::{self, Debug}, fmt::{Write, Display}};
360360
",
361361
r"
362-
use std::{fmt::{self, Debug, Write, Display}};
362+
use std::{fmt::{self, Debug, Display, Write}};
363363
",
364364
);
365365
}
@@ -401,7 +401,7 @@ use std$0::{fmt::*};
401401
use std::{fmt::{self, Display}};
402402
",
403403
r"
404-
use std::{fmt::{*, self, Display}};
404+
use std::{fmt::{self, Display, *}};
405405
",
406406
)
407407
}
@@ -496,7 +496,7 @@ use foo::$0{
496496
",
497497
r"
498498
use foo::{
499-
FooBar, bar::baz,
499+
bar::baz, FooBar
500500
};
501501
",
502502
)
@@ -521,7 +521,7 @@ use foo::$0*;
521521
use foo::bar::Baz;
522522
",
523523
r"
524-
use foo::{*, bar::Baz};
524+
use foo::{bar::Baz, *};
525525
",
526526
);
527527
}
@@ -539,7 +539,7 @@ $0use std::fmt::Result;
539539
",
540540
r"
541541
use std::fmt::Error;
542-
use std::fmt::{Display, Debug, Write};
542+
use std::fmt::{Debug, Display, Write};
543543
use std::fmt::Result;
544544
",
545545
);
@@ -560,15 +560,15 @@ use std::{
560560
r"
561561
use std::{
562562
fmt::Error,
563-
fmt::{Display, Debug, Write},
563+
fmt::{Debug, Display, Write},
564564
fmt::Result,
565565
};",
566566
);
567567
// FIXME: Remove redundant braces. See also unnecessary-braces diagnostic.
568568
check_assist(
569569
merge_imports,
570570
r"use std::$0{fmt::Display, fmt::Debug}$0;",
571-
r"use std::{fmt::{Display, Debug}};",
571+
r"use std::{fmt::{Debug, Display}};",
572572
);
573573
}
574574
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ fn main() {
313313
",
314314
r"
315315
mod std { pub mod fmt { pub trait Display {} } }
316-
use std::fmt::{Display, self};
316+
use std::fmt::{self, Display};
317317
318318
fn main() {
319319
fmt;

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use syntax::{
1616

1717
use crate::{
1818
imports::merge_imports::{
19-
common_prefix, eq_attrs, eq_visibility, try_merge_imports, use_tree_path_cmp, MergeBehavior,
19+
common_prefix, eq_attrs, eq_visibility, try_merge_imports, use_tree_cmp, MergeBehavior,
2020
},
2121
RootDatabase,
2222
};
@@ -357,15 +357,16 @@ fn insert_use_(
357357
use_item: ast::Use,
358358
) {
359359
let scope_syntax = scope.as_syntax_node();
360+
let insert_use_tree =
361+
use_item.use_tree().expect("`use_item` should have a use tree for `insert_path`");
360362
let group = ImportGroup::new(insert_path);
361363
let path_node_iter = scope_syntax
362364
.children()
363365
.filter_map(|node| ast::Use::cast(node.clone()).zip(Some(node)))
364366
.flat_map(|(use_, node)| {
365367
let tree = use_.use_tree()?;
366368
let path = tree.path()?;
367-
let has_tl = tree.use_tree_list().is_some();
368-
Some((path, has_tl, node))
369+
Some((path, tree, node))
369370
});
370371

371372
if group_imports {
@@ -381,9 +382,7 @@ fn insert_use_(
381382
// find the element that would come directly after our new import
382383
let post_insert: Option<(_, _, SyntaxNode)> = group_iter
383384
.inspect(|(.., node)| last = Some(node.clone()))
384-
.find(|&(ref path, has_tl, _)| {
385-
use_tree_path_cmp(insert_path, false, path, has_tl) != Ordering::Greater
386-
});
385+
.find(|(_, use_tree, _)| use_tree_cmp(&insert_use_tree, use_tree) != Ordering::Greater);
387386

388387
if let Some((.., node)) = post_insert {
389388
cov_mark::hit!(insert_group);

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

+13-15
Original file line numberDiff line numberDiff line change
@@ -596,15 +596,15 @@ fn merge_groups_full() {
596596

597597
#[test]
598598
fn merge_groups_long_full() {
599-
check_crate("std::foo::bar::Baz", r"use std::foo::bar::Qux;", r"use std::foo::bar::{Qux, Baz};")
599+
check_crate("std::foo::bar::Baz", r"use std::foo::bar::Qux;", r"use std::foo::bar::{Baz, Qux};")
600600
}
601601

602602
#[test]
603603
fn merge_groups_long_last() {
604604
check_module(
605605
"std::foo::bar::Baz",
606606
r"use std::foo::bar::Qux;",
607-
r"use std::foo::bar::{Qux, Baz};",
607+
r"use std::foo::bar::{Baz, Qux};",
608608
)
609609
}
610610

@@ -613,7 +613,7 @@ fn merge_groups_long_full_list() {
613613
check_crate(
614614
"std::foo::bar::Baz",
615615
r"use std::foo::bar::{Qux, Quux};",
616-
r"use std::foo::bar::{Qux, Quux, Baz};",
616+
r"use std::foo::bar::{Baz, Quux, Qux};",
617617
)
618618
}
619619

@@ -622,7 +622,7 @@ fn merge_groups_long_last_list() {
622622
check_module(
623623
"std::foo::bar::Baz",
624624
r"use std::foo::bar::{Qux, Quux};",
625-
r"use std::foo::bar::{Qux, Quux, Baz};",
625+
r"use std::foo::bar::{Baz, Quux, Qux};",
626626
)
627627
}
628628

@@ -631,7 +631,7 @@ fn merge_groups_long_full_nested() {
631631
check_crate(
632632
"std::foo::bar::Baz",
633633
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
634-
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}, Baz};",
634+
r"use std::foo::bar::{quux::{Fez, Fizz}, Baz, Qux};",
635635
)
636636
}
637637

@@ -650,7 +650,7 @@ fn merge_groups_full_nested_deep() {
650650
check_crate(
651651
"std::foo::bar::quux::Baz",
652652
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
653-
r"use std::foo::bar::{Qux, quux::{Fez, Fizz, Baz}};",
653+
r"use std::foo::bar::{Qux, quux::{Baz, Fez, Fizz}};",
654654
)
655655
}
656656

@@ -659,7 +659,7 @@ fn merge_groups_full_nested_long() {
659659
check_crate(
660660
"std::foo::bar::Baz",
661661
r"use std::{foo::bar::Qux};",
662-
r"use std::{foo::bar::{Qux, Baz}};",
662+
r"use std::{foo::bar::{Baz, Qux}};",
663663
);
664664
}
665665

@@ -668,7 +668,7 @@ fn merge_groups_last_nested_long() {
668668
check_crate(
669669
"std::foo::bar::Baz",
670670
r"use std::{foo::bar::Qux};",
671-
r"use std::{foo::bar::{Qux, Baz}};",
671+
r"use std::{foo::bar::{Baz, Qux}};",
672672
);
673673
}
674674

@@ -733,7 +733,7 @@ fn merge_mod_into_glob() {
733733
check_with_config(
734734
"token::TokenKind",
735735
r"use token::TokenKind::*;",
736-
r"use token::TokenKind::{*, self};",
736+
r"use token::TokenKind::{self, *};",
737737
&InsertUseConfig {
738738
granularity: ImportGranularity::Crate,
739739
enforce_granularity: true,
@@ -742,15 +742,14 @@ fn merge_mod_into_glob() {
742742
skip_glob_imports: false,
743743
},
744744
)
745-
// FIXME: have it emit `use token::TokenKind::{self, *}`?
746745
}
747746

748747
#[test]
749748
fn merge_self_glob() {
750749
check_with_config(
751750
"self",
752751
r"use self::*;",
753-
r"use self::{*, self};",
752+
r"use self::{self, *};",
754753
&InsertUseConfig {
755754
granularity: ImportGranularity::Crate,
756755
enforce_granularity: true,
@@ -759,7 +758,6 @@ fn merge_self_glob() {
759758
skip_glob_imports: false,
760759
},
761760
)
762-
// FIXME: have it emit `use {self, *}`?
763761
}
764762

765763
#[test]
@@ -769,7 +767,7 @@ fn merge_glob() {
769767
r"
770768
use syntax::{SyntaxKind::*};",
771769
r"
772-
use syntax::{SyntaxKind::{*, self}};",
770+
use syntax::{SyntaxKind::{self, *}};",
773771
)
774772
}
775773

@@ -778,7 +776,7 @@ fn merge_glob_nested() {
778776
check_crate(
779777
"foo::bar::quux::Fez",
780778
r"use foo::bar::{Baz, quux::*};",
781-
r"use foo::bar::{Baz, quux::{*, Fez}};",
779+
r"use foo::bar::{Baz, quux::{Fez, *}};",
782780
)
783781
}
784782

@@ -787,7 +785,7 @@ fn merge_nested_considers_first_segments() {
787785
check_crate(
788786
"hir_ty::display::write_bounds_like_dyn_trait",
789787
r"use hir_ty::{autoderef, display::{HirDisplayError, HirFormatter}, method_resolution};",
790-
r"use hir_ty::{autoderef, display::{HirDisplayError, HirFormatter, write_bounds_like_dyn_trait}, method_resolution};",
788+
r"use hir_ty::{autoderef, display::{write_bounds_like_dyn_trait, HirDisplayError, HirFormatter}, method_resolution};",
791789
);
792790
}
793791

0 commit comments

Comments
 (0)