aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/reparsing.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/reparsing.rs')
-rw-r--r--crates/ra_syntax/src/reparsing.rs38
1 files changed, 8 insertions, 30 deletions
diff --git a/crates/ra_syntax/src/reparsing.rs b/crates/ra_syntax/src/reparsing.rs
index 732fb0e4a..208cae5c8 100644
--- a/crates/ra_syntax/src/reparsing.rs
+++ b/crates/ra_syntax/src/reparsing.rs
@@ -6,33 +6,11 @@ use crate::parser_impl;
6use crate::text_utils::replace_range; 6use crate::text_utils::replace_range;
7use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNodeRef}; 7use crate::yellow::{self, GreenNode, SyntaxError, SyntaxNodeRef};
8use crate::{SyntaxKind::*, TextRange, TextUnit}; 8use crate::{SyntaxKind::*, TextRange, TextUnit};
9 9use ra_text_edit::AtomTextEdit;
10#[derive(Debug, Clone)]
11pub struct AtomEdit {
12 pub delete: TextRange,
13 pub insert: String,
14}
15
16impl AtomEdit {
17 pub fn replace(range: TextRange, replace_with: String) -> AtomEdit {
18 AtomEdit {
19 delete: range,
20 insert: replace_with,
21 }
22 }
23
24 pub fn delete(range: TextRange) -> AtomEdit {
25 AtomEdit::replace(range, String::new())
26 }
27
28 pub fn insert(offset: TextUnit, text: String) -> AtomEdit {
29 AtomEdit::replace(TextRange::offset_len(offset, 0.into()), text)
30 }
31}
32 10
33pub(crate) fn incremental_reparse( 11pub(crate) fn incremental_reparse(
34 node: SyntaxNodeRef, 12 node: SyntaxNodeRef,
35 edit: &AtomEdit, 13 edit: &AtomTextEdit,
36 errors: Vec<SyntaxError>, 14 errors: Vec<SyntaxError>,
37) -> Option<(GreenNode, Vec<SyntaxError>)> { 15) -> Option<(GreenNode, Vec<SyntaxError>)> {
38 let (node, green, new_errors) = 16 let (node, green, new_errors) =
@@ -44,7 +22,7 @@ pub(crate) fn incremental_reparse(
44 22
45fn reparse_leaf<'node>( 23fn reparse_leaf<'node>(
46 node: SyntaxNodeRef<'node>, 24 node: SyntaxNodeRef<'node>,
47 edit: &AtomEdit, 25 edit: &AtomTextEdit,
48) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> { 26) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> {
49 let node = algo::find_covering_node(node, edit.delete); 27 let node = algo::find_covering_node(node, edit.delete);
50 match node.kind() { 28 match node.kind() {
@@ -70,7 +48,7 @@ fn reparse_leaf<'node>(
70 48
71fn reparse_block<'node>( 49fn reparse_block<'node>(
72 node: SyntaxNodeRef<'node>, 50 node: SyntaxNodeRef<'node>,
73 edit: &AtomEdit, 51 edit: &AtomTextEdit,
74) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> { 52) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> {
75 let (node, reparser) = find_reparsable_node(node, edit.delete)?; 53 let (node, reparser) = find_reparsable_node(node, edit.delete)?;
76 let text = get_text_after_edit(node, &edit); 54 let text = get_text_after_edit(node, &edit);
@@ -83,7 +61,7 @@ fn reparse_block<'node>(
83 Some((node, green, new_errors)) 61 Some((node, green, new_errors))
84} 62}
85 63
86fn get_text_after_edit(node: SyntaxNodeRef, edit: &AtomEdit) -> String { 64fn get_text_after_edit(node: SyntaxNodeRef, edit: &AtomTextEdit) -> String {
87 replace_range( 65 replace_range(
88 node.text().to_string(), 66 node.text().to_string(),
89 edit.delete - node.range().start(), 67 edit.delete - node.range().start(),
@@ -161,7 +139,7 @@ fn merge_errors(
161 old_errors: Vec<SyntaxError>, 139 old_errors: Vec<SyntaxError>,
162 new_errors: Vec<SyntaxError>, 140 new_errors: Vec<SyntaxError>,
163 old_node: SyntaxNodeRef, 141 old_node: SyntaxNodeRef,
164 edit: &AtomEdit, 142 edit: &AtomTextEdit,
165) -> Vec<SyntaxError> { 143) -> Vec<SyntaxError> {
166 let mut res = Vec::new(); 144 let mut res = Vec::new();
167 for e in old_errors { 145 for e in old_errors {
@@ -188,7 +166,7 @@ mod tests {
188 where 166 where
189 for<'a> F: Fn( 167 for<'a> F: Fn(
190 SyntaxNodeRef<'a>, 168 SyntaxNodeRef<'a>,
191 &AtomEdit, 169 &AtomTextEdit,
192 ) -> Option<(SyntaxNodeRef<'a>, GreenNode, Vec<SyntaxError>)>, 170 ) -> Option<(SyntaxNodeRef<'a>, GreenNode, Vec<SyntaxError>)>,
193 { 171 {
194 let (range, before) = extract_range(before); 172 let (range, before) = extract_range(before);
@@ -197,7 +175,7 @@ mod tests {
197 let fully_reparsed = SourceFileNode::parse(&after); 175 let fully_reparsed = SourceFileNode::parse(&after);
198 let incrementally_reparsed = { 176 let incrementally_reparsed = {
199 let f = SourceFileNode::parse(&before); 177 let f = SourceFileNode::parse(&before);
200 let edit = AtomEdit { 178 let edit = AtomTextEdit {
201 delete: range, 179 delete: range,
202 insert: replace_with.to_string(), 180 insert: replace_with.to_string(),
203 }; 181 };