Unnamed repository; edit this file 'description' to name the repository.
-rw-r--r--crates/ide-db/src/imports/insert_use.rs9
-rw-r--r--crates/syntax/src/ast/edit.rs28
2 files changed, 33 insertions, 4 deletions
diff --git a/crates/ide-db/src/imports/insert_use.rs b/crates/ide-db/src/imports/insert_use.rs
index 4b0373271c..c3949f8713 100644
--- a/crates/ide-db/src/imports/insert_use.rs
+++ b/crates/ide-db/src/imports/insert_use.rs
@@ -248,9 +248,12 @@ fn insert_use_with_alias_option_with_editor(
};
}
- let use_tree = make.use_tree(path, None, alias, false);
- if mb == Some(MergeBehavior::One) && use_tree.path().is_some() {
- use_tree.wrap_in_tree_list();
+ let mut use_tree = make.use_tree(path, None, alias, false);
+ if mb == Some(MergeBehavior::One)
+ && use_tree.path().is_some()
+ && let Some(wrapped) = use_tree.wrap_in_tree_list_with_editor()
+ {
+ use_tree = wrapped;
}
let use_item = make.use_(scope.required_cfgs.iter().cloned().rev(), None, use_tree);
diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs
index 0ffcbd212b..20f1aaf22d 100644
--- a/crates/syntax/src/ast/edit.rs
+++ b/crates/syntax/src/ast/edit.rs
@@ -2,7 +2,11 @@
//! immutable, all function here return a fresh copy of the tree, instead of
//! doing an in-place modification.
use parser::T;
-use std::{fmt, iter, ops};
+use std::{
+ fmt,
+ iter::{self, once},
+ ops,
+};
use crate::{
AstToken, NodeOrToken, SyntaxElement,
@@ -252,6 +256,28 @@ impl ast::IdentPat {
}
}
+impl ast::UseTree {
+ pub fn wrap_in_tree_list_with_editor(&self) -> Option<ast::UseTree> {
+ if self.use_tree_list().is_some()
+ && self.path().is_none()
+ && self.star_token().is_none()
+ && self.rename().is_none()
+ {
+ return None;
+ }
+
+ let (editor, use_tree) = SyntaxEditor::with_ast_node(self);
+ let make = editor.make();
+ let first_child = use_tree.syntax().first_child_or_token()?;
+ let last_child = use_tree.syntax().last_child_or_token()?;
+ let use_tree_list = make.use_tree_list(once(self.clone()));
+ editor.replace_all(first_child..=last_child, vec![use_tree_list.syntax().clone().into()]);
+
+ let edit = editor.finish();
+ ast::UseTree::cast(edit.new_root().clone())
+ }
+}
+
pub fn indent(node: &SyntaxNode, level: IndentLevel) -> SyntaxNode {
level.clone_increase_indent(node)
}