Skip to content

Commit 5855bd8

Browse files
committed
Auto merge of #15587 - dfireBird:fix-15128, r=Veykril
Fix autoimport does nothing when importing trait that is as _ imports Potentially fixes #15128 There are two cases of imports: 1. With simple path 2. With use tree list (or say complex path). On deeper inspection, the [`recursive_merge`](https://github.com/rust-lang/rust-analyzer/blob/994df3d6a31d39f11600f30a6df0b744b13937c1/crates/ide-db/src/imports/merge_imports.rs#L87) function (called by [`try_merge_trees_mut`)](https://github.com/rust-lang/rust-analyzer/blob/994df3d6a31d39f11600f30a6df0b744b13937c1/crates/ide-db/src/imports/merge_imports.rs#L69) is meaningful only in the case of complex path (i.e when the UseTree contains a UseTreeList). The [`recursive_merge`](https://github.com/rust-lang/rust-analyzer/blob/994df3d6a31d39f11600f30a6df0b744b13937c1/crates/ide-db/src/imports/merge_imports.rs#L87) function has [match with `Ok` arm](https://github.com/rust-lang/rust-analyzer/blob/994df3d6a31d39f11600f30a6df0b744b13937c1/crates/ide-db/src/imports/merge_imports.rs#L106), that is only executed when both LHS and RHS has `PathSegment` with same `NameRef`. The removal of underscore is implemented in this arm in the case of complex path. For simple paths, the underscore is removed by checking if both LHS and RHS are simple paths and if their `Path` is same (the check is done [here](https://github.com/rust-lang/rust-analyzer/blob/994df3d6a31d39f11600f30a6df0b744b13937c1/crates/ide-db/src/imports/merge_imports.rs#L74)) and remove the underscore if one is found (I made an assumption here that RHS will always be what rust-analyzer suggests to import, because at this point I'm not sure how to remove underscore with help of `ted::replace`).
2 parents df75809 + df1239b commit 5855bd8

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

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

+40
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,46 @@ use foo::bar::qux;
993993
);
994994
}
995995

996+
#[test]
997+
fn insert_with_renamed_import_simple_use() {
998+
check_with_config(
999+
"use self::foo::Foo",
1000+
r#"
1001+
use self::foo::Foo as _;
1002+
"#,
1003+
r#"
1004+
use self::foo::Foo;
1005+
"#,
1006+
&InsertUseConfig {
1007+
granularity: ImportGranularity::Crate,
1008+
prefix_kind: hir::PrefixKind::BySelf,
1009+
enforce_granularity: true,
1010+
group: true,
1011+
skip_glob_imports: true,
1012+
},
1013+
);
1014+
}
1015+
1016+
#[test]
1017+
fn insert_with_renamed_import_complex_use() {
1018+
check_with_config(
1019+
"use self::foo::Foo;",
1020+
r#"
1021+
use self::foo::{self, Foo as _, Bar};
1022+
"#,
1023+
r#"
1024+
use self::foo::{self, Foo, Bar};
1025+
"#,
1026+
&InsertUseConfig {
1027+
granularity: ImportGranularity::Crate,
1028+
prefix_kind: hir::PrefixKind::BySelf,
1029+
enforce_granularity: true,
1030+
group: true,
1031+
skip_glob_imports: true,
1032+
},
1033+
);
1034+
}
1035+
9961036
fn check_with_config(
9971037
path: &str,
9981038
ra_fixture_before: &str,

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

+11
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ fn try_merge_trees_mut(lhs: &ast::UseTree, rhs: &ast::UseTree, merge: MergeBehav
7878
{
7979
lhs.split_prefix(&lhs_prefix);
8080
rhs.split_prefix(&rhs_prefix);
81+
} else {
82+
ted::replace(lhs.syntax(), rhs.syntax());
83+
// we can safely return here, in this case `recursive_merge` doesn't do anything
84+
return Some(());
8185
}
8286
recursive_merge(lhs, rhs, merge)
8387
}
@@ -123,6 +127,13 @@ fn recursive_merge(lhs: &ast::UseTree, rhs: &ast::UseTree, merge: MergeBehavior)
123127
// so they need to be handled explicitly
124128
.or_else(|| tree.star_token().map(|_| false))
125129
};
130+
131+
if lhs_t.rename().and_then(|x| x.underscore_token()).is_some() {
132+
ted::replace(lhs_t.syntax(), rhs_t.syntax());
133+
*lhs_t = rhs_t;
134+
continue;
135+
}
136+
126137
match (tree_contains_self(lhs_t), tree_contains_self(&rhs_t)) {
127138
(Some(true), None) => continue,
128139
(None, Some(true)) => {

0 commit comments

Comments
 (0)