aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/typing.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor/src/typing.rs')
-rw-r--r--crates/libeditor/src/typing.rs30
1 files changed, 28 insertions, 2 deletions
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 @@
1use std::mem; 1use std::mem;
2 2
3use libsyntax2::{ 3use libsyntax2::{
4 TextUnit, TextRange, SyntaxNodeRef, File, AstNode, 4 TextUnit, TextRange, SyntaxNodeRef, File, AstNode, SyntaxKind,
5 ast, 5 ast,
6 algo::{ 6 algo::{
7 walk::preorder, 7 walk::preorder,
@@ -48,6 +48,7 @@ pub fn join_lines(file: &File, range: TextRange) -> ActionResult {
48 remove_newline(&mut edit, node, text.as_str(), off); 48 remove_newline(&mut edit, node, text.as_str(), off);
49 } 49 }
50 } 50 }
51 eprintln!("{:?}", edit);
51 52
52 ActionResult { 53 ActionResult {
53 edit: edit.finish(), 54 edit: edit.finish(),
@@ -93,8 +94,10 @@ fn remove_newline(
93 match (node.prev_sibling(), node.next_sibling()) { 94 match (node.prev_sibling(), node.next_sibling()) {
94 (Some(prev), Some(next)) => { 95 (Some(prev), Some(next)) => {
95 let range = TextRange::from_to(prev.range().start(), node.range().end()); 96 let range = TextRange::from_to(prev.range().start(), node.range().end());
96 if prev.kind() == COMMA && (next.kind() == R_PAREN || next.kind() == R_BRACK) { 97 if is_trailing_comma(prev.kind(), next.kind()) {
97 edit.delete(range); 98 edit.delete(range);
99 } else if no_space_required(prev.kind(), next.kind()) {
100 edit.delete(node.range());
98 } else if prev.kind() == COMMA && next.kind() == R_CURLY { 101 } else if prev.kind() == COMMA && next.kind() == R_CURLY {
99 edit.replace(range, " ".to_string()); 102 edit.replace(range, " ".to_string());
100 } else { 103 } else {
@@ -121,6 +124,20 @@ fn remove_newline(
121 ); 124 );
122} 125}
123 126
127fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
128 match (left, right) {
129 (COMMA, R_PAREN) | (COMMA, R_BRACK) => true,
130 _ => false
131 }
132}
133
134fn no_space_required(left: SyntaxKind, right: SyntaxKind) -> bool {
135 match (left, right) {
136 (_, DOT) => true,
137 _ => false
138 }
139}
140
124fn join_single_expr_block( 141fn join_single_expr_block(
125 edit: &mut EditBuilder, 142 edit: &mut EditBuilder,
126 node: SyntaxNodeRef, 143 node: SyntaxNodeRef,
@@ -252,6 +269,15 @@ struct Foo <|>{
252 ", r" 269 ", r"
253struct Foo { f: u32 } 270struct Foo { f: u32 }
254 "); 271 ");
272 do_check(r"
273fn foo() {
274 join(<|>type_params.type_params()
275 .filter_map(|it| it.name())
276 .map(|it| it.text())<|>)
277}", r"
278fn foo() {
279 join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text()))
280}")
255 } 281 }
256 282
257 #[test] 283 #[test]