Skip to content

Commit 74b755d

Browse files
committed
Allow merge_imports assists to merge imports of equal visibility
1 parent 7ccb198 commit 74b755d

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

crates/assists/src/handlers/merge_imports.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,56 @@ pub use std::fmt::Display;
149149
);
150150
}
151151

152+
#[test]
153+
fn skip_pub_crate_pub() {
154+
check_assist_not_applicable(
155+
merge_imports,
156+
r"
157+
pub(crate) use std::fmt<|>::Debug;
158+
pub use std::fmt::Display;
159+
",
160+
);
161+
}
162+
163+
#[test]
164+
fn skip_pub_pub_crate() {
165+
check_assist_not_applicable(
166+
merge_imports,
167+
r"
168+
pub use std::fmt<|>::Debug;
169+
pub(crate) use std::fmt::Display;
170+
",
171+
);
172+
}
173+
174+
#[test]
175+
fn merge_pub() {
176+
check_assist(
177+
merge_imports,
178+
r"
179+
pub use std::fmt<|>::Debug;
180+
pub use std::fmt::Display;
181+
",
182+
r"
183+
pub use std::fmt::{Debug, Display};
184+
",
185+
)
186+
}
187+
188+
#[test]
189+
fn merge_pub_crate() {
190+
check_assist(
191+
merge_imports,
192+
r"
193+
pub(crate) use std::fmt<|>::Debug;
194+
pub(crate) use std::fmt::Display;
195+
",
196+
r"
197+
pub(crate) use std::fmt::{Debug, Display};
198+
",
199+
)
200+
}
201+
152202
#[test]
153203
fn test_merge_nested() {
154204
check_assist(

crates/assists/src/utils/insert_use.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,23 @@ pub(crate) fn insert_use(
138138
algo::insert_children(scope.as_syntax_node(), insert_position, to_insert)
139139
}
140140

141+
fn eq_visibility(vis0: Option<ast::Visibility>, vis1: Option<ast::Visibility>) -> bool {
142+
match (vis0, vis1) {
143+
(None, None) => true,
144+
// FIXME: Don't use the string representation to check for equality
145+
// spaces inside of the node would break this comparison
146+
(Some(vis0), Some(vis1)) => vis0.to_string() == vis1.to_string(),
147+
_ => false,
148+
}
149+
}
150+
141151
pub(crate) fn try_merge_imports(
142152
old: &ast::Use,
143153
new: &ast::Use,
144154
merge_behaviour: MergeBehaviour,
145155
) -> Option<ast::Use> {
146156
// don't merge imports with different visibilities
147-
if old
148-
.visibility()
149-
.and_then(|vis| vis.pub_token())
150-
.or_else(|| new.visibility().and_then(|vis| vis.pub_token()))
151-
.is_some()
152-
{
157+
if !eq_visibility(old.visibility(), new.visibility()) {
153158
return None;
154159
}
155160
let old_tree = old.use_tree()?;

0 commit comments

Comments
 (0)