Unnamed repository; edit this file 'description' to name the repository.
merge imports assist always applies to covering use item except for nested use tree selections
davidsemakula 2024-01-28
parent a8a18f3 · commit b241593
-rw-r--r--crates/ide-assists/src/handlers/merge_imports.rs99
1 files changed, 23 insertions, 76 deletions
diff --git a/crates/ide-assists/src/handlers/merge_imports.rs b/crates/ide-assists/src/handlers/merge_imports.rs
index 02dbde803e..ee6376f247 100644
--- a/crates/ide-assists/src/handlers/merge_imports.rs
+++ b/crates/ide-assists/src/handlers/merge_imports.rs
@@ -32,24 +32,13 @@ use Edit::*;
pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let (target, edits) = if ctx.has_empty_selection() {
// Merge a neighbor
- let mut tree: ast::UseTree = ctx.find_node_at_offset()?;
- if ctx.config.insert_use.granularity == ImportGranularity::One
- && tree.parent_use_tree_list().is_some()
- {
- cov_mark::hit!(resolve_top_use_tree_for_import_one);
- tree = tree.top_use_tree();
- }
+ cov_mark::hit!(merge_with_use_item_neighbors);
+ let tree = ctx.find_node_at_offset::<ast::UseTree>()?.top_use_tree();
let target = tree.syntax().text_range();
- let edits = if let Some(use_item) = tree.syntax().parent().and_then(ast::Use::cast) {
- cov_mark::hit!(merge_with_use_item_neighbors);
- let mut neighbor = next_prev().find_map(|dir| neighbor(&use_item, dir)).into_iter();
- use_item.try_merge_from(&mut neighbor, &ctx.config.insert_use)
- } else {
- cov_mark::hit!(merge_with_use_tree_neighbors);
- let mut neighbor = next_prev().find_map(|dir| neighbor(&tree, dir)).into_iter();
- tree.clone().try_merge_from(&mut neighbor, &ctx.config.insert_use)
- };
+ let use_item = tree.syntax().parent().and_then(ast::Use::cast)?;
+ let mut neighbor = next_prev().find_map(|dir| neighbor(&use_item, dir)).into_iter();
+ let edits = use_item.try_merge_from(&mut neighbor, &ctx.config.insert_use);
(target, edits?)
} else {
// Merge selected
@@ -159,10 +148,7 @@ impl Edit {
#[cfg(test)]
mod tests {
- use crate::tests::{
- check_assist, check_assist_import_one, check_assist_not_applicable,
- check_assist_not_applicable_for_import_one,
- };
+ use crate::tests::{check_assist, check_assist_import_one, check_assist_not_applicable};
use super::*;
@@ -206,10 +192,7 @@ use std::fmt::{Debug, Display};
);
// The assist macro below calls `check_assist_import_one` 4 times with different input
- // use item variations based on the first 2 input parameters, but only 2 calls
- // contain `use {std::fmt$0::{Display, Debug}};` for which the top use tree will need
- // to be resolved.
- cov_mark::check_count!(resolve_top_use_tree_for_import_one, 2);
+ // use item variations based on the first 2 input parameters.
cov_mark::check_count!(merge_with_use_item_neighbors, 4);
check_assist_import_one_variations!(
"std::fmt$0::{Display, Debug}",
@@ -257,7 +240,7 @@ use std::fmt::{Debug, Display};
}
#[test]
- fn merge_self1() {
+ fn merge_self() {
check_assist(
merge_imports,
r"
@@ -276,28 +259,6 @@ use std::fmt::{self, Display};
}
#[test]
- fn merge_self2() {
- check_assist(
- merge_imports,
- r"
-use std::{fmt, $0fmt::Display};
-",
- r"
-use std::{fmt::{self, Display}};
-",
- );
- }
-
- #[test]
- fn not_applicable_to_single_one_style_import() {
- cov_mark::check!(resolve_top_use_tree_for_import_one);
- check_assist_not_applicable_for_import_one(
- merge_imports,
- "use {std::{fmt, $0fmt::Display}};",
- );
- }
-
- #[test]
fn skip_pub1() {
check_assist_not_applicable(
merge_imports,
@@ -385,14 +346,14 @@ pub(in this::path) use std::fmt::{Debug, Display};
#[test]
fn test_merge_nested() {
- cov_mark::check!(merge_with_use_tree_neighbors);
check_assist(
merge_imports,
r"
-use std::{fmt$0::Debug, fmt::Display};
+use std::{fmt$0::Debug, fmt::Error};
+use std::{fmt::Write, fmt::Display};
",
r"
-use std::{fmt::{Debug, Display}};
+use std::fmt::{Debug, Display, Error, Write};
",
);
}
@@ -402,10 +363,11 @@ use std::{fmt::{Debug, Display}};
check_assist(
merge_imports,
r"
-use std::{fmt::Debug, fmt$0::Display};
+use std::{fmt::Debug, fmt$0::Error};
+use std::{fmt::Write, fmt::Display};
",
r"
-use std::{fmt::{Debug, Display}};
+use std::fmt::{Debug, Display, Error, Write};
",
);
}
@@ -449,19 +411,6 @@ use std::fmt::{self, Debug, Display, Write};
}
#[test]
- fn test_merge_self_with_nested_self_item() {
- check_assist(
- merge_imports,
- r"
-use std::{fmt$0::{self, Debug}, fmt::{Write, Display}};
-",
- r"
-use std::{fmt::{self, Debug, Display, Write}};
-",
- );
- }
-
- #[test]
fn test_merge_nested_self_and_empty() {
check_assist(
merge_imports,
@@ -579,29 +528,27 @@ use foo::{bar, baz};
check_assist(
merge_imports,
r"
-use {
- foo$0::bar,
- foo::baz,
+use foo$0::{
+ bar, baz,
};
+use foo::qux;
",
r"
-use {
- foo::{bar, baz},
+use foo::{
+ bar, baz, qux,
};
",
);
check_assist(
merge_imports,
r"
-use {
- foo::baz,
- foo$0::bar,
+use foo::{
+ baz, bar,
};
+use foo$0::qux;
",
r"
-use {
- foo::{bar, baz},
-};
+use foo::{bar, baz, qux};
",
);
}