aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor')
-rw-r--r--crates/libeditor/src/lib.rs2
-rw-r--r--crates/libeditor/src/typing.rs31
-rw-r--r--crates/libeditor/tests/test.rs4
3 files changed, 34 insertions, 3 deletions
diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs
index f8bc73ae9..b29603da3 100644
--- a/crates/libeditor/src/lib.rs
+++ b/crates/libeditor/src/lib.rs
@@ -12,7 +12,7 @@ mod typing;
12use libsyntax2::{ 12use libsyntax2::{
13 ast::{self, NameOwner}, 13 ast::{self, NameOwner},
14 AstNode, 14 AstNode,
15 algo::{walk, find_leaf_at_offset, find_covering_node}, 15 algo::{walk, find_leaf_at_offset},
16 SyntaxKind::{self, *}, 16 SyntaxKind::{self, *},
17}; 17};
18pub use libsyntax2::{ParsedFile, TextRange, TextUnit}; 18pub use libsyntax2::{ParsedFile, TextRange, TextUnit};
diff --git a/crates/libeditor/src/typing.rs b/crates/libeditor/src/typing.rs
index f49dd0fdc..918f2325c 100644
--- a/crates/libeditor/src/typing.rs
+++ b/crates/libeditor/src/typing.rs
@@ -5,6 +5,7 @@ use libsyntax2::{
5 walk::preorder, 5 walk::preorder,
6 find_covering_node, 6 find_covering_node,
7 }, 7 },
8 SyntaxKind::*,
8}; 9};
9 10
10use {ActionResult, EditBuilder}; 11use {ActionResult, EditBuilder};
@@ -68,6 +69,24 @@ fn remove_newline(
68 node_text: &str, 69 node_text: &str,
69 offset: TextUnit, 70 offset: TextUnit,
70) { 71) {
72 if node.kind() == WHITESPACE && node_text.bytes().filter(|&b| b == b'\n').count() == 1 {
73 match (node.prev_sibling(), node.next_sibling()) {
74 (Some(prev), Some(next)) => {
75 if prev.kind() == COMMA && (next.kind() == R_PAREN || next.kind() == R_BRACK) {
76 let range = TextRange::from_to(prev.range().start(), node.range().end());
77 edit.delete(range);
78 } else {
79 edit.replace(
80 node.range(),
81 compute_ws(prev, next).to_string(),
82 );
83 }
84 return;
85 }
86 _ => (),
87 }
88 }
89
71 let suff = &node_text[TextRange::from_to( 90 let suff = &node_text[TextRange::from_to(
72 offset - node.range().start() + TextUnit::of_char('\n'), 91 offset - node.range().start() + TextUnit::of_char('\n'),
73 TextUnit::of_str(node_text), 92 TextUnit::of_str(node_text),
@@ -79,3 +98,15 @@ fn remove_newline(
79 " ".to_string(), 98 " ".to_string(),
80 ); 99 );
81} 100}
101
102fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str {
103 match left.kind() {
104 L_PAREN | L_BRACK => return "",
105 _ => (),
106 }
107 match right.kind() {
108 R_PAREN | R_BRACK => return "",
109 _ => (),
110 }
111 " "
112}
diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs
index 6aa260a86..e5088ad27 100644
--- a/crates/libeditor/tests/test.rs
+++ b/crates/libeditor/tests/test.rs
@@ -195,7 +195,7 @@ fn foo() {
195} 195}
196", r" 196", r"
197fn foo() { 197fn foo() {
198 <|>foo(1, ) 198 <|>foo(1)
199} 199}
200"); 200");
201} 201}
@@ -221,7 +221,7 @@ fn foo() {
221} 221}
222", r" 222", r"
223fn foo() { 223fn foo() {
224 foo(1, 2, 3, ) 224 foo(1, 2, 3)
225} 225}
226"); 226");
227} 227}