Allow merge_imports assists to merge imports of equal visibility

This commit is contained in:
Lukas Wirth 2020-09-05 16:15:16 +02:00
parent 7ccb198af8
commit 74b755d233
2 changed files with 61 additions and 6 deletions

View File

@ -149,6 +149,56 @@ fn skip_pub_last() {
);
}
#[test]
fn skip_pub_crate_pub() {
check_assist_not_applicable(
merge_imports,
r"
pub(crate) use std::fmt<|>::Debug;
pub use std::fmt::Display;
",
);
}
#[test]
fn skip_pub_pub_crate() {
check_assist_not_applicable(
merge_imports,
r"
pub use std::fmt<|>::Debug;
pub(crate) use std::fmt::Display;
",
);
}
#[test]
fn merge_pub() {
check_assist(
merge_imports,
r"
pub use std::fmt<|>::Debug;
pub use std::fmt::Display;
",
r"
pub use std::fmt::{Debug, Display};
",
)
}
#[test]
fn merge_pub_crate() {
check_assist(
merge_imports,
r"
pub(crate) use std::fmt<|>::Debug;
pub(crate) use std::fmt::Display;
",
r"
pub(crate) use std::fmt::{Debug, Display};
",
)
}
#[test]
fn test_merge_nested() {
check_assist(

View File

@ -138,18 +138,23 @@ pub(crate) fn insert_use(
algo::insert_children(scope.as_syntax_node(), insert_position, to_insert)
}
fn eq_visibility(vis0: Option<ast::Visibility>, vis1: Option<ast::Visibility>) -> bool {
match (vis0, vis1) {
(None, None) => true,
// FIXME: Don't use the string representation to check for equality
// spaces inside of the node would break this comparison
(Some(vis0), Some(vis1)) => vis0.to_string() == vis1.to_string(),
_ => false,
}
}
pub(crate) fn try_merge_imports(
old: &ast::Use,
new: &ast::Use,
merge_behaviour: MergeBehaviour,
) -> Option<ast::Use> {
// don't merge imports with different visibilities
if old
.visibility()
.and_then(|vis| vis.pub_token())
.or_else(|| new.visibility().and_then(|vis| vis.pub_token()))
.is_some()
{
if !eq_visibility(old.visibility(), new.visibility()) {
return None;
}
let old_tree = old.use_tree()?;