]> git.lizzy.rs Git - rust.git/commitdiff
make grammar independent of syntax tree
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 21 Feb 2019 09:12:04 +0000 (12:12 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 21 Feb 2019 09:12:04 +0000 (12:12 +0300)
crates/ra_syntax/src/parsing/grammar.rs
crates/ra_syntax/src/parsing/reparsing.rs

index 7ca9c223cc0ee1a4371488fba502152a065c5f31..800d5a4a29cffeaca33fdbddb0791a41c5b47272 100644 (file)
@@ -37,7 +37,6 @@
 mod types;
 
 use crate::{
-    SyntaxNode,
     SyntaxKind::{self, *},
     parsing::{
         token_set::TokenSet,
@@ -52,8 +51,12 @@ pub(super) fn root(p: &mut Parser) {
     m.complete(p, SOURCE_FILE);
 }
 
-pub(super) fn reparser(node: &SyntaxNode) -> Option<fn(&mut Parser)> {
-    let res = match node.kind() {
+pub(super) fn reparser(
+    node: SyntaxKind,
+    first_child: Option<SyntaxKind>,
+    parent: Option<SyntaxKind>,
+) -> Option<fn(&mut Parser)> {
+    let res = match node {
         BLOCK => expressions::block,
         NAMED_FIELD_DEF_LIST => items::named_field_def_list,
         NAMED_FIELD_LIST => items::named_field_list,
@@ -61,16 +64,13 @@ pub(super) fn reparser(node: &SyntaxNode) -> Option<fn(&mut Parser)> {
         MATCH_ARM_LIST => items::match_arm_list,
         USE_TREE_LIST => items::use_tree_list,
         EXTERN_ITEM_LIST => items::extern_item_list,
-        TOKEN_TREE if node.first_child().unwrap().kind() == L_CURLY => items::token_tree,
-        ITEM_LIST => {
-            let parent = node.parent().unwrap();
-            match parent.kind() {
-                IMPL_BLOCK => items::impl_item_list,
-                TRAIT_DEF => items::trait_item_list,
-                MODULE => items::mod_item_list,
-                _ => return None,
-            }
-        }
+        TOKEN_TREE if first_child? == L_CURLY => items::token_tree,
+        ITEM_LIST => match parent? {
+            IMPL_BLOCK => items::impl_item_list,
+            TRAIT_DEF => items::trait_item_list,
+            MODULE => items::mod_item_list,
+            _ => return None,
+        },
         _ => return None,
     };
     Some(res)
index f4c2251d7c76a33cd07e828703ea0989f83d6418..2c860b3df025aa6e2f4d86ed09640b22d6aea993 100644 (file)
@@ -83,7 +83,11 @@ fn find_reparsable_node(
     range: TextRange,
 ) -> Option<(&SyntaxNode, fn(&mut Parser))> {
     let node = algo::find_covering_node(node, range);
-    node.ancestors().find_map(|node| grammar::reparser(node).map(|r| (node, r)))
+    node.ancestors().find_map(|node| {
+        let first_child = node.first_child().map(|it| it.kind());
+        let parent = node.parent().map(|it| it.kind());
+        grammar::reparser(node.kind(), first_child, parent).map(|r| (node, r))
+    })
 }
 
 fn is_balanced(tokens: &[Token]) -> bool {