Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions crates/ide-db/src/imports/insert_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
28 changes: 27 additions & 1 deletion crates/syntax/src/ast/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down