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.rs31
1 files changed, 31 insertions, 0 deletions
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}