Skip to content

Commit a9669a5

Browse files
Merge imports when auto importing
1 parent 96bd4f5 commit a9669a5

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

crates/ra_assists/src/assists/auto_import.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,8 @@ pub(crate) fn auto_import<F: ImportsLocator>(
7979
fn import_to_action(import: String, position: &SyntaxNode, anchor: &SyntaxNode) -> ActionBuilder {
8080
let mut action_builder = ActionBuilder::default();
8181
action_builder.label(format!("Import `{}`", &import));
82-
auto_import_text_edit(
83-
position,
84-
anchor,
85-
&[SmolStr::new(import)],
86-
action_builder.text_edit_builder(),
87-
);
82+
let import_segments = import.split("::").map(SmolStr::new).collect::<Vec<_>>();
83+
auto_import_text_edit(position, anchor, &import_segments, action_builder.text_edit_builder());
8884
action_builder
8985
}
9086

@@ -120,6 +116,34 @@ mod tests {
120116
);
121117
}
122118

119+
#[test]
120+
fn auto_imports_are_merged() {
121+
check_assist_with_imports_locator(
122+
auto_import,
123+
TestImportsLocator::new,
124+
r"
125+
use PubMod::PubStruct1;
126+
127+
PubStruct2<|>
128+
129+
pub mod PubMod {
130+
pub struct PubStruct1;
131+
pub struct PubStruct2;
132+
}
133+
",
134+
r"
135+
use PubMod::{PubStruct2, PubStruct1};
136+
137+
PubStruct2<|>
138+
139+
pub mod PubMod {
140+
pub struct PubStruct1;
141+
pub struct PubStruct2;
142+
}
143+
",
144+
);
145+
}
146+
123147
#[test]
124148
fn applicable_when_found_multiple_imports() {
125149
check_assist_with_imports_locator(

0 commit comments

Comments
 (0)