aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/reparsing.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 09:05:55 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 09:05:55 +0000
commit3f4be819125ce4a22edd86721fa56b5caba99c2e (patch)
treebe93895ddc08c911585d9f7bc64623a3741f32c6 /crates/ra_syntax/src/reparsing.rs
parent4e444d2bc24d16284401444fd2154f63e0f96070 (diff)
parent122410d7aa34a32d468a3173858cbc8a2bbc68f5 (diff)
Merge #449
449: switch to new rowan API r=matklad a=matklad closes https://github.com/rust-analyzer/rust-analyzer/issues/448 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/reparsing.rs')
-rw-r--r--crates/ra_syntax/src/reparsing.rs35
1 files changed, 16 insertions, 19 deletions
diff --git a/crates/ra_syntax/src/reparsing.rs b/crates/ra_syntax/src/reparsing.rs
index 7ee71a1b6..d5d72e1f8 100644
--- a/crates/ra_syntax/src/reparsing.rs
+++ b/crates/ra_syntax/src/reparsing.rs
@@ -4,12 +4,12 @@ use crate::lexer::{tokenize, Token};
4use crate::parser_api::Parser; 4use crate::parser_api::Parser;
5use crate::parser_impl; 5use 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, SyntaxNode};
8use crate::{SyntaxKind::*, TextRange, TextUnit}; 8use crate::{SyntaxKind::*, TextRange, TextUnit};
9use ra_text_edit::AtomTextEdit; 9use ra_text_edit::AtomTextEdit;
10 10
11pub(crate) fn incremental_reparse( 11pub(crate) fn incremental_reparse(
12 node: SyntaxNodeRef, 12 node: &SyntaxNode,
13 edit: &AtomTextEdit, 13 edit: &AtomTextEdit,
14 errors: Vec<SyntaxError>, 14 errors: Vec<SyntaxError>,
15) -> Option<(GreenNode, Vec<SyntaxError>)> { 15) -> Option<(GreenNode, Vec<SyntaxError>)> {
@@ -21,9 +21,9 @@ pub(crate) fn incremental_reparse(
21} 21}
22 22
23fn reparse_leaf<'node>( 23fn reparse_leaf<'node>(
24 node: SyntaxNodeRef<'node>, 24 node: &'node SyntaxNode,
25 edit: &AtomTextEdit, 25 edit: &AtomTextEdit,
26) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> { 26) -> Option<(&'node SyntaxNode, GreenNode, Vec<SyntaxError>)> {
27 let node = algo::find_covering_node(node, edit.delete); 27 let node = algo::find_covering_node(node, edit.delete);
28 match node.kind() { 28 match node.kind() {
29 WHITESPACE | COMMENT | IDENT | STRING | RAW_STRING => { 29 WHITESPACE | COMMENT | IDENT | STRING | RAW_STRING => {
@@ -47,9 +47,9 @@ fn reparse_leaf<'node>(
47} 47}
48 48
49fn reparse_block<'node>( 49fn reparse_block<'node>(
50 node: SyntaxNodeRef<'node>, 50 node: &'node SyntaxNode,
51 edit: &AtomTextEdit, 51 edit: &AtomTextEdit,
52) -> Option<(SyntaxNodeRef<'node>, GreenNode, Vec<SyntaxError>)> { 52) -> Option<(&'node SyntaxNode, GreenNode, Vec<SyntaxError>)> {
53 let (node, reparser) = find_reparsable_node(node, edit.delete)?; 53 let (node, reparser) = find_reparsable_node(node, edit.delete)?;
54 let text = get_text_after_edit(node, &edit); 54 let text = get_text_after_edit(node, &edit);
55 let tokens = tokenize(&text); 55 let tokens = tokenize(&text);
@@ -61,7 +61,7 @@ fn reparse_block<'node>(
61 Some((node, green, new_errors)) 61 Some((node, green, new_errors))
62} 62}
63 63
64fn get_text_after_edit(node: SyntaxNodeRef, edit: &AtomTextEdit) -> String { 64fn get_text_after_edit(node: &SyntaxNode, edit: &AtomTextEdit) -> String {
65 replace_range( 65 replace_range(
66 node.text().to_string(), 66 node.text().to_string(),
67 edit.delete - node.range().start(), 67 edit.delete - node.range().start(),
@@ -77,17 +77,14 @@ fn is_contextual_kw(text: &str) -> bool {
77} 77}
78 78
79type ParseFn = fn(&mut Parser); 79type ParseFn = fn(&mut Parser);
80fn find_reparsable_node( 80fn find_reparsable_node(node: &SyntaxNode, range: TextRange) -> Option<(&SyntaxNode, ParseFn)> {
81 node: SyntaxNodeRef<'_>,
82 range: TextRange,
83) -> Option<(SyntaxNodeRef<'_>, ParseFn)> {
84 let node = algo::find_covering_node(node, range); 81 let node = algo::find_covering_node(node, range);
85 return node 82 return node
86 .ancestors() 83 .ancestors()
87 .filter_map(|node| reparser(node).map(|r| (node, r))) 84 .filter_map(|node| reparser(node).map(|r| (node, r)))
88 .next(); 85 .next();
89 86
90 fn reparser(node: SyntaxNodeRef) -> Option<ParseFn> { 87 fn reparser(node: &SyntaxNode) -> Option<ParseFn> {
91 let res = match node.kind() { 88 let res = match node.kind() {
92 BLOCK => grammar::block, 89 BLOCK => grammar::block,
93 NAMED_FIELD_DEF_LIST => grammar::named_field_def_list, 90 NAMED_FIELD_DEF_LIST => grammar::named_field_def_list,
@@ -138,7 +135,7 @@ fn is_balanced(tokens: &[Token]) -> bool {
138fn merge_errors( 135fn merge_errors(
139 old_errors: Vec<SyntaxError>, 136 old_errors: Vec<SyntaxError>,
140 new_errors: Vec<SyntaxError>, 137 new_errors: Vec<SyntaxError>,
141 old_node: SyntaxNodeRef, 138 old_node: &SyntaxNode,
142 edit: &AtomTextEdit, 139 edit: &AtomTextEdit,
143) -> Vec<SyntaxError> { 140) -> Vec<SyntaxError> {
144 let mut res = Vec::new(); 141 let mut res = Vec::new();
@@ -159,22 +156,22 @@ fn merge_errors(
159mod tests { 156mod tests {
160 use test_utils::{extract_range, assert_eq_text}; 157 use test_utils::{extract_range, assert_eq_text};
161 158
162 use crate::{SourceFileNode, text_utils::replace_range, utils::dump_tree }; 159 use crate::{SourceFile, AstNode, text_utils::replace_range, utils::dump_tree};
163 use super::*; 160 use super::*;
164 161
165 fn do_check<F>(before: &str, replace_with: &str, reparser: F) 162 fn do_check<F>(before: &str, replace_with: &str, reparser: F)
166 where 163 where
167 for<'a> F: Fn( 164 for<'a> F: Fn(
168 SyntaxNodeRef<'a>, 165 &'a SyntaxNode,
169 &AtomTextEdit, 166 &AtomTextEdit,
170 ) -> Option<(SyntaxNodeRef<'a>, GreenNode, Vec<SyntaxError>)>, 167 ) -> Option<(&'a SyntaxNode, GreenNode, Vec<SyntaxError>)>,
171 { 168 {
172 let (range, before) = extract_range(before); 169 let (range, before) = extract_range(before);
173 let after = replace_range(before.clone(), range, replace_with); 170 let after = replace_range(before.clone(), range, replace_with);
174 171
175 let fully_reparsed = SourceFileNode::parse(&after); 172 let fully_reparsed = SourceFile::parse(&after);
176 let incrementally_reparsed = { 173 let incrementally_reparsed = {
177 let f = SourceFileNode::parse(&before); 174 let f = SourceFile::parse(&before);
178 let edit = AtomTextEdit { 175 let edit = AtomTextEdit {
179 delete: range, 176 delete: range,
180 insert: replace_with.to_string(), 177 insert: replace_with.to_string(),
@@ -183,7 +180,7 @@ mod tests {
183 reparser(f.syntax(), &edit).expect("cannot incrementally reparse"); 180 reparser(f.syntax(), &edit).expect("cannot incrementally reparse");
184 let green_root = node.replace_with(green); 181 let green_root = node.replace_with(green);
185 let errors = super::merge_errors(f.errors(), new_errors, node, &edit); 182 let errors = super::merge_errors(f.errors(), new_errors, node, &edit);
186 SourceFileNode::new(green_root, errors) 183 SourceFile::new(green_root, errors)
187 }; 184 };
188 185
189 assert_eq_text!( 186 assert_eq_text!(