From 67ac0a423f767ce8973348d5188e8baa6979d380 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 21 Dec 2018 21:06:01 +0300 Subject: join lines collapses use_trees --- crates/ra_editor/src/typing.rs | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'crates/ra_editor') diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index 46a6e2d62..5e412bcfa 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -164,6 +164,16 @@ fn remove_newline( if join_single_expr_block(edit, node).is_some() { return; } + // ditto for + // + // ``` + // use foo::{<|> + // bar + // }; + // ``` + if join_single_use_tree(edit, node).is_some() { + return; + } // The node is between two other nodes let prev = node.prev_sibling().unwrap(); @@ -228,6 +238,36 @@ fn single_expr(block: ast::Block) -> Option { 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 { + 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 { match left.kind() { 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] fn test_join_lines_normal_comments() { check_join_lines( -- cgit v1.2.3