diff options
Diffstat (limited to 'crates/ra_editor/src/typing.rs')
-rw-r--r-- | crates/ra_editor/src/typing.rs | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index 01acdda7c..46a6e2d62 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs | |||
@@ -3,13 +3,14 @@ use std::mem; | |||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, | 4 | algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, |
5 | ast, | 5 | ast, |
6 | text_utils::{contains_offset_nonstrict, intersect}, | 6 | text_utils::intersect, |
7 | AstNode, SourceFileNode, SyntaxKind, | 7 | AstNode, SourceFileNode, SyntaxKind, |
8 | SyntaxKind::*, | 8 | SyntaxKind::*, |
9 | SyntaxNodeRef, TextRange, TextUnit, | 9 | SyntaxNodeRef, TextRange, TextUnit, |
10 | }; | 10 | }; |
11 | use ra_text_edit::text_utils::contains_offset_nonstrict; | ||
11 | 12 | ||
12 | use crate::{find_node_at_offset, EditBuilder, LocalEdit}; | 13 | use crate::{find_node_at_offset, TextEditBuilder, LocalEdit}; |
13 | 14 | ||
14 | pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit { | 15 | pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit { |
15 | let range = if range.is_empty() { | 16 | let range = if range.is_empty() { |
@@ -18,7 +19,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit { | |||
18 | let pos = match text.find('\n') { | 19 | let pos = match text.find('\n') { |
19 | None => { | 20 | None => { |
20 | return LocalEdit { | 21 | return LocalEdit { |
21 | edit: EditBuilder::new().finish(), | 22 | edit: TextEditBuilder::new().finish(), |
22 | cursor_position: None, | 23 | cursor_position: None, |
23 | }; | 24 | }; |
24 | } | 25 | } |
@@ -30,7 +31,7 @@ pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit { | |||
30 | }; | 31 | }; |
31 | 32 | ||
32 | let node = find_covering_node(file.syntax(), range); | 33 | let node = find_covering_node(file.syntax(), range); |
33 | let mut edit = EditBuilder::new(); | 34 | let mut edit = TextEditBuilder::new(); |
34 | for node in node.descendants() { | 35 | for node in node.descendants() { |
35 | let text = match node.leaf_text() { | 36 | let text = match node.leaf_text() { |
36 | Some(text) => text, | 37 | Some(text) => text, |
@@ -72,7 +73,7 @@ pub fn on_enter(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit> { | |||
72 | let indent = node_indent(file, comment.syntax())?; | 73 | let indent = node_indent(file, comment.syntax())?; |
73 | let inserted = format!("\n{}{} ", indent, prefix); | 74 | let inserted = format!("\n{}{} ", indent, prefix); |
74 | let cursor_position = offset + TextUnit::of_str(&inserted); | 75 | let cursor_position = offset + TextUnit::of_str(&inserted); |
75 | let mut edit = EditBuilder::new(); | 76 | let mut edit = TextEditBuilder::new(); |
76 | edit.insert(offset, inserted); | 77 | edit.insert(offset, inserted); |
77 | Some(LocalEdit { | 78 | Some(LocalEdit { |
78 | edit: edit.finish(), | 79 | edit: edit.finish(), |
@@ -122,7 +123,7 @@ pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit> | |||
122 | return None; | 123 | return None; |
123 | } | 124 | } |
124 | let offset = let_stmt.syntax().range().end(); | 125 | let offset = let_stmt.syntax().range().end(); |
125 | let mut edit = EditBuilder::new(); | 126 | let mut edit = TextEditBuilder::new(); |
126 | edit.insert(offset, ";".to_string()); | 127 | edit.insert(offset, ";".to_string()); |
127 | Some(LocalEdit { | 128 | Some(LocalEdit { |
128 | edit: edit.finish(), | 129 | edit: edit.finish(), |
@@ -130,7 +131,12 @@ pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit> | |||
130 | }) | 131 | }) |
131 | } | 132 | } |
132 | 133 | ||
133 | fn remove_newline(edit: &mut EditBuilder, node: SyntaxNodeRef, node_text: &str, offset: TextUnit) { | 134 | fn remove_newline( |
135 | edit: &mut TextEditBuilder, | ||
136 | node: SyntaxNodeRef, | ||
137 | node_text: &str, | ||
138 | offset: TextUnit, | ||
139 | ) { | ||
134 | if node.kind() != WHITESPACE || node_text.bytes().filter(|&b| b == b'\n').count() != 1 { | 140 | if node.kind() != WHITESPACE || node_text.bytes().filter(|&b| b == b'\n').count() != 1 { |
135 | // The node is either the first or the last in the file | 141 | // The node is either the first or the last in the file |
136 | let suff = &node_text[TextRange::from_to( | 142 | let suff = &node_text[TextRange::from_to( |
@@ -191,7 +197,7 @@ fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool { | |||
191 | } | 197 | } |
192 | } | 198 | } |
193 | 199 | ||
194 | fn join_single_expr_block(edit: &mut EditBuilder, node: SyntaxNodeRef) -> Option<()> { | 200 | fn join_single_expr_block(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Option<()> { |
195 | let block = ast::Block::cast(node.parent()?)?; | 201 | let block = ast::Block::cast(node.parent()?)?; |
196 | let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; | 202 | let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; |
197 | let expr = single_expr(block)?; | 203 | let expr = single_expr(block)?; |
@@ -269,14 +275,14 @@ fn foo() { | |||
269 | fn test_join_lines_lambda_block() { | 275 | fn test_join_lines_lambda_block() { |
270 | check_join_lines( | 276 | check_join_lines( |
271 | r" | 277 | r" |
272 | pub fn reparse(&self, edit: &AtomEdit) -> File { | 278 | pub fn reparse(&self, edit: &AtomTextEdit) -> File { |
273 | <|>self.incremental_reparse(edit).unwrap_or_else(|| { | 279 | <|>self.incremental_reparse(edit).unwrap_or_else(|| { |
274 | self.full_reparse(edit) | 280 | self.full_reparse(edit) |
275 | }) | 281 | }) |
276 | } | 282 | } |
277 | ", | 283 | ", |
278 | r" | 284 | r" |
279 | pub fn reparse(&self, edit: &AtomEdit) -> File { | 285 | pub fn reparse(&self, edit: &AtomTextEdit) -> File { |
280 | <|>self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) | 286 | <|>self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) |
281 | } | 287 | } |
282 | ", | 288 | ", |