mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 06:03:58 +00:00
join lines collapses use_trees
This commit is contained in:
parent
a106784115
commit
67ac0a423f
1 changed files with 58 additions and 0 deletions
|
@ -164,6 +164,16 @@ fn remove_newline(
|
||||||
if join_single_expr_block(edit, node).is_some() {
|
if join_single_expr_block(edit, node).is_some() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// ditto for
|
||||||
|
//
|
||||||
|
// ```
|
||||||
|
// use foo::{<|>
|
||||||
|
// bar
|
||||||
|
// };
|
||||||
|
// ```
|
||||||
|
if join_single_use_tree(edit, node).is_some() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// The node is between two other nodes
|
// The node is between two other nodes
|
||||||
let prev = node.prev_sibling().unwrap();
|
let prev = node.prev_sibling().unwrap();
|
||||||
|
@ -228,6 +238,36 @@ fn single_expr(block: ast::Block) -> Option<ast::Expr> {
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn join_single_use_tree(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Option<()> {
|
||||||
|
let use_tree_list = ast::UseTreeList::cast(node.parent()?)?;
|
||||||
|
let tree = single_use_tree(use_tree_list)?;
|
||||||
|
edit.replace(
|
||||||
|
use_tree_list.syntax().range(),
|
||||||
|
tree.syntax().text().to_string(),
|
||||||
|
);
|
||||||
|
Some(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn single_use_tree(tree_list: ast::UseTreeList) -> Option<ast::UseTree> {
|
||||||
|
let mut res = None;
|
||||||
|
for child in tree_list.syntax().children() {
|
||||||
|
if let Some(tree) = ast::UseTree::cast(child) {
|
||||||
|
if tree.syntax().text().contains('\n') {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
if mem::replace(&mut res, Some(tree)).is_some() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match child.kind() {
|
||||||
|
WHITESPACE | L_CURLY | R_CURLY | COMMA => (),
|
||||||
|
_ => return None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str {
|
fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str {
|
||||||
match left.kind() {
|
match left.kind() {
|
||||||
L_PAREN | L_BRACK => return "",
|
L_PAREN | L_BRACK => return "",
|
||||||
|
@ -305,6 +345,24 @@ fn foo() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_join_lines_use_tree() {
|
||||||
|
check_join_lines(
|
||||||
|
r"
|
||||||
|
use ra_syntax::{
|
||||||
|
algo::<|>{
|
||||||
|
find_leaf_at_offset,
|
||||||
|
},
|
||||||
|
ast,
|
||||||
|
};",
|
||||||
|
r"
|
||||||
|
use ra_syntax::{
|
||||||
|
algo::<|>find_leaf_at_offset,
|
||||||
|
ast,
|
||||||
|
};",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_join_lines_normal_comments() {
|
fn test_join_lines_normal_comments() {
|
||||||
check_join_lines(
|
check_join_lines(
|
||||||
|
|
Loading…
Reference in a new issue