aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-05 22:50:47 +0100
committerGitHub <[email protected]>2020-05-05 22:50:47 +0100
commit30eb458b4fa8adcecd8cbf731bd1cfa9a7a8b88b (patch)
tree1ee318a9568c8bf1d0b314ef996581bb4e518b9e /crates/ra_syntax
parent78c82eff95cae43b389dbd6e590d7f09bac8f3f1 (diff)
parentca9e0f5fe9ad29ab0c5a0771a0d0eaec97e4104b (diff)
Merge #4332
4332: Refactor TextEdit r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/fuzz.rs6
-rw-r--r--crates/ra_syntax/src/lib.rs15
-rw-r--r--crates/ra_syntax/src/parsing/reparsing.rs28
3 files changed, 27 insertions, 22 deletions
diff --git a/crates/ra_syntax/src/fuzz.rs b/crates/ra_syntax/src/fuzz.rs
index 10fbe3176..39f9b12ab 100644
--- a/crates/ra_syntax/src/fuzz.rs
+++ b/crates/ra_syntax/src/fuzz.rs
@@ -5,7 +5,7 @@ use std::{
5 str::{self, FromStr}, 5 str::{self, FromStr},
6}; 6};
7 7
8use ra_text_edit::AtomTextEdit; 8use ra_text_edit::Indel;
9 9
10use crate::{validation, AstNode, SourceFile, TextRange}; 10use crate::{validation, AstNode, SourceFile, TextRange};
11 11
@@ -22,7 +22,7 @@ pub fn check_parser(text: &str) {
22#[derive(Debug, Clone)] 22#[derive(Debug, Clone)]
23pub struct CheckReparse { 23pub struct CheckReparse {
24 text: String, 24 text: String,
25 edit: AtomTextEdit, 25 edit: Indel,
26 edited_text: String, 26 edited_text: String,
27} 27}
28 28
@@ -43,7 +43,7 @@ impl CheckReparse {
43 TextRange::at(delete_start.try_into().unwrap(), delete_len.try_into().unwrap()); 43 TextRange::at(delete_start.try_into().unwrap(), delete_len.try_into().unwrap());
44 let edited_text = 44 let edited_text =
45 format!("{}{}{}", &text[..delete_start], &insert, &text[delete_start + delete_len..]); 45 format!("{}{}{}", &text[..delete_start], &insert, &text[delete_start + delete_len..]);
46 let edit = AtomTextEdit { delete, insert }; 46 let edit = Indel { delete, insert };
47 Some(CheckReparse { text, edit, edited_text }) 47 Some(CheckReparse { text, edit, edited_text })
48 } 48 }
49 49
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index d0234cada..61e686da5 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -39,7 +39,7 @@ pub mod fuzz;
39 39
40use std::{marker::PhantomData, sync::Arc}; 40use std::{marker::PhantomData, sync::Arc};
41 41
42use ra_text_edit::AtomTextEdit; 42use ra_text_edit::Indel;
43use stdx::format_to; 43use stdx::format_to;
44 44
45use crate::syntax_node::GreenNode; 45use crate::syntax_node::GreenNode;
@@ -126,13 +126,13 @@ impl Parse<SourceFile> {
126 buf 126 buf
127 } 127 }
128 128
129 pub fn reparse(&self, edit: &AtomTextEdit) -> Parse<SourceFile> { 129 pub fn reparse(&self, indel: &Indel) -> Parse<SourceFile> {
130 self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) 130 self.incremental_reparse(indel).unwrap_or_else(|| self.full_reparse(indel))
131 } 131 }
132 132
133 fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<Parse<SourceFile>> { 133 fn incremental_reparse(&self, indel: &Indel) -> Option<Parse<SourceFile>> {
134 // FIXME: validation errors are not handled here 134 // FIXME: validation errors are not handled here
135 parsing::incremental_reparse(self.tree().syntax(), edit, self.errors.to_vec()).map( 135 parsing::incremental_reparse(self.tree().syntax(), indel, self.errors.to_vec()).map(
136 |(green_node, errors, _reparsed_range)| Parse { 136 |(green_node, errors, _reparsed_range)| Parse {
137 green: green_node, 137 green: green_node,
138 errors: Arc::new(errors), 138 errors: Arc::new(errors),
@@ -141,8 +141,9 @@ impl Parse<SourceFile> {
141 ) 141 )
142 } 142 }
143 143
144 fn full_reparse(&self, edit: &AtomTextEdit) -> Parse<SourceFile> { 144 fn full_reparse(&self, indel: &Indel) -> Parse<SourceFile> {
145 let text = edit.apply(self.tree().syntax().text().to_string()); 145 let mut text = self.tree().syntax().text().to_string();
146 indel.apply(&mut text);
146 SourceFile::parse(&text) 147 SourceFile::parse(&text)
147 } 148 }
148} 149}
diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs
index ffff0a7b2..edbc190f8 100644
--- a/crates/ra_syntax/src/parsing/reparsing.rs
+++ b/crates/ra_syntax/src/parsing/reparsing.rs
@@ -7,7 +7,7 @@
7//! and try to parse only this block. 7//! and try to parse only this block.
8 8
9use ra_parser::Reparser; 9use ra_parser::Reparser;
10use ra_text_edit::AtomTextEdit; 10use ra_text_edit::Indel;
11 11
12use crate::{ 12use crate::{
13 algo, 13 algo,
@@ -24,7 +24,7 @@ use crate::{
24 24
25pub(crate) fn incremental_reparse( 25pub(crate) fn incremental_reparse(
26 node: &SyntaxNode, 26 node: &SyntaxNode,
27 edit: &AtomTextEdit, 27 edit: &Indel,
28 errors: Vec<SyntaxError>, 28 errors: Vec<SyntaxError>,
29) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> { 29) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> {
30 if let Some((green, new_errors, old_range)) = reparse_token(node, &edit) { 30 if let Some((green, new_errors, old_range)) = reparse_token(node, &edit) {
@@ -39,7 +39,7 @@ pub(crate) fn incremental_reparse(
39 39
40fn reparse_token<'node>( 40fn reparse_token<'node>(
41 root: &'node SyntaxNode, 41 root: &'node SyntaxNode,
42 edit: &AtomTextEdit, 42 edit: &Indel,
43) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> { 43) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> {
44 let prev_token = algo::find_covering_element(root, edit.delete).as_token()?.clone(); 44 let prev_token = algo::find_covering_element(root, edit.delete).as_token()?.clone();
45 let prev_token_kind = prev_token.kind(); 45 let prev_token_kind = prev_token.kind();
@@ -88,7 +88,7 @@ fn reparse_token<'node>(
88 88
89fn reparse_block<'node>( 89fn reparse_block<'node>(
90 root: &'node SyntaxNode, 90 root: &'node SyntaxNode,
91 edit: &AtomTextEdit, 91 edit: &Indel,
92) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> { 92) -> Option<(GreenNode, Vec<SyntaxError>, TextRange)> {
93 let (node, reparser) = find_reparsable_node(root, edit.delete)?; 93 let (node, reparser) = find_reparsable_node(root, edit.delete)?;
94 let text = get_text_after_edit(node.clone().into(), edit); 94 let text = get_text_after_edit(node.clone().into(), edit);
@@ -108,15 +108,15 @@ fn reparse_block<'node>(
108 Some((node.replace_with(green), new_parser_errors, node.text_range())) 108 Some((node.replace_with(green), new_parser_errors, node.text_range()))
109} 109}
110 110
111fn get_text_after_edit(element: SyntaxElement, edit: &AtomTextEdit) -> String { 111fn get_text_after_edit(element: SyntaxElement, edit: &Indel) -> String {
112 let edit = 112 let edit = Indel::replace(edit.delete - element.text_range().start(), edit.insert.clone());
113 AtomTextEdit::replace(edit.delete - element.text_range().start(), edit.insert.clone());
114 113
115 let text = match element { 114 let mut text = match element {
116 NodeOrToken::Token(token) => token.text().to_string(), 115 NodeOrToken::Token(token) => token.text().to_string(),
117 NodeOrToken::Node(node) => node.text().to_string(), 116 NodeOrToken::Node(node) => node.text().to_string(),
118 }; 117 };
119 edit.apply(text) 118 edit.apply(&mut text);
119 text
120} 120}
121 121
122fn is_contextual_kw(text: &str) -> bool { 122fn is_contextual_kw(text: &str) -> bool {
@@ -167,7 +167,7 @@ fn merge_errors(
167 old_errors: Vec<SyntaxError>, 167 old_errors: Vec<SyntaxError>,
168 new_errors: Vec<SyntaxError>, 168 new_errors: Vec<SyntaxError>,
169 range_before_reparse: TextRange, 169 range_before_reparse: TextRange,
170 edit: &AtomTextEdit, 170 edit: &Indel,
171) -> Vec<SyntaxError> { 171) -> Vec<SyntaxError> {
172 let mut res = Vec::new(); 172 let mut res = Vec::new();
173 173
@@ -198,8 +198,12 @@ mod tests {
198 198
199 fn do_check(before: &str, replace_with: &str, reparsed_len: u32) { 199 fn do_check(before: &str, replace_with: &str, reparsed_len: u32) {
200 let (range, before) = extract_range(before); 200 let (range, before) = extract_range(before);
201 let edit = AtomTextEdit::replace(range, replace_with.to_owned()); 201 let edit = Indel::replace(range, replace_with.to_owned());
202 let after = edit.apply(before.clone()); 202 let after = {
203 let mut after = before.clone();
204 edit.apply(&mut after);
205 after
206 };
203 207
204 let fully_reparsed = SourceFile::parse(&after); 208 let fully_reparsed = SourceFile::parse(&after);
205 let incrementally_reparsed: Parse<SourceFile> = { 209 let incrementally_reparsed: Parse<SourceFile> = {