aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-28 20:37:49 +0100
committerAleksey Kladov <[email protected]>2018-08-28 20:52:51 +0100
commit69eeae0c9903931f13038f4e350fd53b1903530d (patch)
tree1797b4d897e4f65a1aa12baa43329a50a35611a1 /crates/libeditor
parent6effddb18c8c7e0d500e7c17482642f1c3ebcfc8 (diff)
polish join
Diffstat (limited to 'crates/libeditor')
-rw-r--r--crates/libeditor/Cargo.toml1
-rw-r--r--crates/libeditor/src/code_actions.rs2
-rw-r--r--crates/libeditor/src/lib.rs1
-rw-r--r--crates/libeditor/src/typing.rs30
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]
8itertools = "0.7.8" 8itertools = "0.7.8"
9superslice = "0.1.0" 9superslice = "0.1.0"
10join_to_string = "0.1.1"
10 11
11libsyntax2 = { path = "../libsyntax2" } 12libsyntax2 = { 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
5use join_to_string::join;
6
5use libsyntax2::{ 7use 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 @@
1extern crate libsyntax2; 1extern crate libsyntax2;
2extern crate superslice; 2extern crate superslice;
3extern crate itertools; 3extern crate itertools;
4extern crate join_to_string;
4#[cfg(test)] 5#[cfg(test)]
5#[macro_use] 6#[macro_use]
6extern crate test_utils as _test_utils; 7extern 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 @@
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]