From 69eeae0c9903931f13038f4e350fd53b1903530d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 28 Aug 2018 22:37:49 +0300 Subject: polish join --- crates/libeditor/src/code_actions.rs | 2 ++ crates/libeditor/src/lib.rs | 1 + crates/libeditor/src/typing.rs | 30 ++++++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) (limited to 'crates/libeditor/src') diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs index 79530f257..cef3a12f9 100644 --- a/crates/libeditor/src/code_actions.rs +++ b/crates/libeditor/src/code_actions.rs @@ -2,6 +2,8 @@ use std::{ fmt::{self, Write}, }; +use join_to_string::join; + use libsyntax2::{ File, TextUnit, ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner}, diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs index 06dac9d6d..4895f6fa9 100644 --- a/crates/libeditor/src/lib.rs +++ b/crates/libeditor/src/lib.rs @@ -1,6 +1,7 @@ extern crate libsyntax2; extern crate superslice; extern crate itertools; +extern crate join_to_string; #[cfg(test)] #[macro_use] extern crate test_utils as _test_utils; diff --git a/crates/libeditor/src/typing.rs b/crates/libeditor/src/typing.rs index 65a8933a4..5008b8d49 100644 --- a/crates/libeditor/src/typing.rs +++ b/crates/libeditor/src/typing.rs @@ -1,7 +1,7 @@ use std::mem; use libsyntax2::{ - TextUnit, TextRange, SyntaxNodeRef, File, AstNode, + TextUnit, TextRange, SyntaxNodeRef, File, AstNode, SyntaxKind, ast, algo::{ walk::preorder, @@ -48,6 +48,7 @@ pub fn join_lines(file: &File, range: TextRange) -> ActionResult { remove_newline(&mut edit, node, text.as_str(), off); } } + eprintln!("{:?}", edit); ActionResult { edit: edit.finish(), @@ -93,8 +94,10 @@ fn remove_newline( match (node.prev_sibling(), node.next_sibling()) { (Some(prev), Some(next)) => { let range = TextRange::from_to(prev.range().start(), node.range().end()); - if prev.kind() == COMMA && (next.kind() == R_PAREN || next.kind() == R_BRACK) { + if is_trailing_comma(prev.kind(), next.kind()) { edit.delete(range); + } else if no_space_required(prev.kind(), next.kind()) { + edit.delete(node.range()); } else if prev.kind() == COMMA && next.kind() == R_CURLY { edit.replace(range, " ".to_string()); } else { @@ -121,6 +124,20 @@ fn remove_newline( ); } +fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool { + match (left, right) { + (COMMA, R_PAREN) | (COMMA, R_BRACK) => true, + _ => false + } +} + +fn no_space_required(left: SyntaxKind, right: SyntaxKind) -> bool { + match (left, right) { + (_, DOT) => true, + _ => false + } +} + fn join_single_expr_block( edit: &mut EditBuilder, node: SyntaxNodeRef, @@ -252,6 +269,15 @@ struct Foo <|>{ ", r" struct Foo { f: u32 } "); + do_check(r" +fn foo() { + join(<|>type_params.type_params() + .filter_map(|it| it.name()) + .map(|it| it.text())<|>) +}", r" +fn foo() { + join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text())) +}") } #[test] -- cgit v1.2.3