aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/libsyntax2/src/reparsing.rs116
1 files changed, 68 insertions, 48 deletions
diff --git a/crates/libsyntax2/src/reparsing.rs b/crates/libsyntax2/src/reparsing.rs
index 1df2ca094..da44913c5 100644
--- a/crates/libsyntax2/src/reparsing.rs
+++ b/crates/libsyntax2/src/reparsing.rs
@@ -188,29 +188,51 @@ fn merge_errors(
188} 188}
189 189
190#[cfg(test)] 190#[cfg(test)]
191use super::{File, text_utils, test_utils, utils};
192
193#[cfg(test)]
194mod tests { 191mod tests {
195 use super::{*, utils::dump_tree}; 192 use super::{
193 super::{
194 File,
195 test_utils::extract_range,
196 text_utils::replace_range,
197 utils::dump_tree,
198 },
199 reparse_leaf, reparse_block, AtomEdit, GreenNode, SyntaxError, SyntaxNodeRef,
200 };
196 201
197 #[test] 202 fn do_check<F>(
198 fn reparse_test() { 203 before: &str,
199 fn do_check(before: &str, replace_with: &str) { 204 replace_with: &str,
200 let (range, before) = test_utils::extract_range(before); 205 reparser: F,
201 let after = text_utils::replace_range(before.clone(), range, replace_with); 206 ) where
207 for<'a> F: Fn(
208 SyntaxNodeRef<'a>,
209 &AtomEdit,
210 ) -> Option<(SyntaxNodeRef<'a>, GreenNode, Vec<SyntaxError>)>
211 {
212 let (range, before) = extract_range(before);
213 let after = replace_range(before.clone(), range, replace_with);
202 214
203 let fully_reparsed = File::parse(&after); 215 let fully_reparsed = File::parse(&after);
204 let incrementally_reparsed = { 216 let incrementally_reparsed = {
205 let f = File::parse(&before); 217 let f = File::parse(&before);
206 let edit = AtomEdit { delete: range, insert: replace_with.to_string() }; 218 let edit = AtomEdit { delete: range, insert: replace_with.to_string() };
207 f.incremental_reparse(&edit).expect("cannot incrementally reparse") 219 let (node, green, new_errors) =
208 }; 220 reparser(f.syntax(), &edit).expect("cannot incrementally reparse");
209 assert_eq_text!( 221 let green_root = node.replace_with(green);
210 &dump_tree(fully_reparsed.syntax()), 222 let errors = super::merge_errors(f.errors(), new_errors, node, &edit);
211 &dump_tree(incrementally_reparsed.syntax()), 223 File::new(green_root, errors)
212 ) 224 };
213 } 225
226 assert_eq_text!(
227 &dump_tree(fully_reparsed.syntax()),
228 &dump_tree(incrementally_reparsed.syntax()),
229 )
230 }
231
232 #[test]
233 fn reparse_block_tests() {
234 let do_check = |before, replace_to|
235 do_check(before, replace_to, reparse_block);
214 236
215 do_check(r" 237 do_check(r"
216fn foo() { 238fn foo() {
@@ -245,11 +267,6 @@ trait Foo {
245} 267}
246", "Output"); 268", "Output");
247 do_check(r" 269 do_check(r"
248trait Foo {
249 type<|> Foo<|>;
250}
251", "Output");
252 do_check(r"
253impl IntoIterator<Item=i32> for Foo { 270impl IntoIterator<Item=i32> for Foo {
254 f<|><|> 271 f<|><|>
255} 272}
@@ -275,43 +292,37 @@ extern {
275 fn<|>;<|> 292 fn<|>;<|>
276} 293}
277", " exit(code: c_int)"); 294", " exit(code: c_int)");
295 }
296
297 #[test]
298 fn reparse_leaf_tests() {
299 let do_check = |before, replace_to|
300 do_check(before, replace_to, reparse_leaf);
301
278 do_check(r"<|><|> 302 do_check(r"<|><|>
279fn foo() -> i32 { 303fn foo() -> i32 { 1 }
280 1
281}
282", "\n\n\n \n"); 304", "\n\n\n \n");
283 do_check(r" 305 do_check(r"
284fn foo() -> <|><|> {} 306fn foo() -> <|><|> {}
285", " \n"); 307", " \n");
286 do_check(r" 308 do_check(r"
287fn <|>foo<|>() -> i32 { 309fn <|>foo<|>() -> i32 { 1 }
288 1
289}
290", "bar"); 310", "bar");
291 do_check(r" 311 do_check(r"
292fn aa<|><|>bb() { 312fn foo<|><|>foo() { }
293 313", "bar");
294}
295", "foofoo");
296 do_check(r" 314 do_check(r"
297fn aabb /* <|><|> */ () { 315fn foo /* <|><|> */ () {}
298
299}
300", "some comment"); 316", "some comment");
301 do_check(r" 317 do_check(r"
302fn aabb <|><|> () { 318fn baz <|><|> () {}
303
304}
305", " \t\t\n\n"); 319", " \t\t\n\n");
306 do_check(r" 320 do_check(r"
307trait foo { 321fn baz <|><|> () {}
308// comment <|><|> 322", " \t\t\n\n");
309}
310", "\n");
311 do_check(r" 323 do_check(r"
312/// good <|><|>omment 324/// foo <|><|>omment
313mod { 325mod { }
314}
315", "c"); 326", "c");
316 do_check(r#" 327 do_check(r#"
317fn -> &str { "Hello<|><|>" } 328fn -> &str { "Hello<|><|>" }
@@ -319,5 +330,14 @@ fn -> &str { "Hello<|><|>" }
319 do_check(r#" 330 do_check(r#"
320fn -> &str { // "Hello<|><|>" 331fn -> &str { // "Hello<|><|>"
321"#, ", world"); 332"#, ", world");
333 do_check(r##"
334fn -> &str { r#"Hello<|><|>"#
335"##, ", world");
336 do_check(r"
337#[derive(<|>Copy<|>)]
338enum Foo {
339
340}
341", "Clone");
322 } 342 }
323} \ No newline at end of file 343} \ No newline at end of file