diff options
author | Aleksey Kladov <[email protected]> | 2018-12-21 18:06:01 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-12-21 18:06:01 +0000 |
commit | 67ac0a423f767ce8973348d5188e8baa6979d380 (patch) | |
tree | 61a81b86d3a08038641439cbba19b500619beb56 /crates/ra_editor | |
parent | a106784115b5f55b9bbc4d9f72464bdc31cf5cda (diff) |
join lines collapses use_trees
Diffstat (limited to 'crates/ra_editor')
-rw-r--r-- | crates/ra_editor/src/typing.rs | 58 |
1 files changed, 58 insertions, 0 deletions
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( | |||
164 | if join_single_expr_block(edit, node).is_some() { | 164 | if join_single_expr_block(edit, node).is_some() { |
165 | return; | 165 | return; |
166 | } | 166 | } |
167 | // ditto for | ||
168 | // | ||
169 | // ``` | ||
170 | // use foo::{<|> | ||
171 | // bar | ||
172 | // }; | ||
173 | // ``` | ||
174 | if join_single_use_tree(edit, node).is_some() { | ||
175 | return; | ||
176 | } | ||
167 | 177 | ||
168 | // The node is between two other nodes | 178 | // The node is between two other nodes |
169 | let prev = node.prev_sibling().unwrap(); | 179 | let prev = node.prev_sibling().unwrap(); |
@@ -228,6 +238,36 @@ fn single_expr(block: ast::Block) -> Option<ast::Expr> { | |||
228 | res | 238 | res |
229 | } | 239 | } |
230 | 240 | ||
241 | fn join_single_use_tree(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Option<()> { | ||
242 | let use_tree_list = ast::UseTreeList::cast(node.parent()?)?; | ||
243 | let tree = single_use_tree(use_tree_list)?; | ||
244 | edit.replace( | ||
245 | use_tree_list.syntax().range(), | ||
246 | tree.syntax().text().to_string(), | ||
247 | ); | ||
248 | Some(()) | ||
249 | } | ||
250 | |||
251 | fn single_use_tree(tree_list: ast::UseTreeList) -> Option<ast::UseTree> { | ||
252 | let mut res = None; | ||
253 | for child in tree_list.syntax().children() { | ||
254 | if let Some(tree) = ast::UseTree::cast(child) { | ||
255 | if tree.syntax().text().contains('\n') { | ||
256 | return None; | ||
257 | } | ||
258 | if mem::replace(&mut res, Some(tree)).is_some() { | ||
259 | return None; | ||
260 | } | ||
261 | } else { | ||
262 | match child.kind() { | ||
263 | WHITESPACE | L_CURLY | R_CURLY | COMMA => (), | ||
264 | _ => return None, | ||
265 | } | ||
266 | } | ||
267 | } | ||
268 | res | ||
269 | } | ||
270 | |||
231 | fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str { | 271 | fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str { |
232 | match left.kind() { | 272 | match left.kind() { |
233 | L_PAREN | L_BRACK => return "", | 273 | L_PAREN | L_BRACK => return "", |
@@ -306,6 +346,24 @@ fn foo() { | |||
306 | } | 346 | } |
307 | 347 | ||
308 | #[test] | 348 | #[test] |
349 | fn test_join_lines_use_tree() { | ||
350 | check_join_lines( | ||
351 | r" | ||
352 | use ra_syntax::{ | ||
353 | algo::<|>{ | ||
354 | find_leaf_at_offset, | ||
355 | }, | ||
356 | ast, | ||
357 | };", | ||
358 | r" | ||
359 | use ra_syntax::{ | ||
360 | algo::<|>find_leaf_at_offset, | ||
361 | ast, | ||
362 | };", | ||
363 | ); | ||
364 | } | ||
365 | |||
366 | #[test] | ||
309 | fn test_join_lines_normal_comments() { | 367 | fn test_join_lines_normal_comments() { |
310 | check_join_lines( | 368 | check_join_lines( |
311 | r" | 369 | r" |