diff options
-rw-r--r-- | crates/libeditor/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/libeditor/src/code_actions.rs | 2 | ||||
-rw-r--r-- | crates/libeditor/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/libeditor/src/typing.rs | 30 |
4 files changed, 32 insertions, 2 deletions
diff --git a/crates/libeditor/Cargo.toml b/crates/libeditor/Cargo.toml index 265772204..b04da1abe 100644 --- a/crates/libeditor/Cargo.toml +++ b/crates/libeditor/Cargo.toml | |||
@@ -7,6 +7,7 @@ publish = false | |||
7 | [dependencies] | 7 | [dependencies] |
8 | itertools = "0.7.8" | 8 | itertools = "0.7.8" |
9 | superslice = "0.1.0" | 9 | superslice = "0.1.0" |
10 | join_to_string = "0.1.1" | ||
10 | 11 | ||
11 | libsyntax2 = { path = "../libsyntax2" } | 12 | libsyntax2 = { path = "../libsyntax2" } |
12 | 13 | ||
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::{ | |||
2 | fmt::{self, Write}, | 2 | fmt::{self, Write}, |
3 | }; | 3 | }; |
4 | 4 | ||
5 | use join_to_string::join; | ||
6 | |||
5 | use libsyntax2::{ | 7 | use libsyntax2::{ |
6 | File, TextUnit, | 8 | File, TextUnit, |
7 | ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner}, | 9 | 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 @@ | |||
1 | extern crate libsyntax2; | 1 | extern crate libsyntax2; |
2 | extern crate superslice; | 2 | extern crate superslice; |
3 | extern crate itertools; | 3 | extern crate itertools; |
4 | extern crate join_to_string; | ||
4 | #[cfg(test)] | 5 | #[cfg(test)] |
5 | #[macro_use] | 6 | #[macro_use] |
6 | extern crate test_utils as _test_utils; | 7 | 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 @@ | |||
1 | use std::mem; | 1 | use std::mem; |
2 | 2 | ||
3 | use libsyntax2::{ | 3 | use 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 | ||
127 | fn 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 | |||
134 | fn no_space_required(left: SyntaxKind, right: SyntaxKind) -> bool { | ||
135 | match (left, right) { | ||
136 | (_, DOT) => true, | ||
137 | _ => false | ||
138 | } | ||
139 | } | ||
140 | |||
124 | fn join_single_expr_block( | 141 | fn 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" |
253 | struct Foo { f: u32 } | 270 | struct Foo { f: u32 } |
254 | "); | 271 | "); |
272 | do_check(r" | ||
273 | fn foo() { | ||
274 | join(<|>type_params.type_params() | ||
275 | .filter_map(|it| it.name()) | ||
276 | .map(|it| it.text())<|>) | ||
277 | }", r" | ||
278 | fn 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] |