@@ -4,7 +4,7 @@ use std::{usize, vec};
44use tree_sitter:: { Node , Parser , Point , Tree } ;
55
66use super :: tokens:: {
7- DrupalRoute , DrupalRouteDefaults , DrupalService , PhpClassName , PhpMethod , Token , TokenData
7+ DrupalRoute , DrupalRouteDefaults , DrupalService , PhpClassName , PhpMethod , Token , TokenData ,
88} ;
99
1010pub struct YamlParser {
@@ -31,8 +31,19 @@ impl YamlParser {
3131
3232 pub fn get_token_at_position < ' a > ( & self , position : Position ) -> Option < Token > {
3333 let tree = self . get_tree ( ) ?;
34- let node = self . get_node_at_position ( & tree, position) ?;
35- self . parse_node ( node, Some ( self . position_to_point ( position) ) )
34+ let mut node = self . get_node_at_position ( & tree, position) ?;
35+ let point = self . position_to_point ( position) ;
36+
37+ // Return the first "parseable" token in the parent chain.
38+ let mut parsed_node: Option < Token > ;
39+ loop {
40+ parsed_node = self . parse_node ( node, Some ( point) ) ;
41+ if parsed_node. is_some ( ) {
42+ break ;
43+ }
44+ node = node. parent ( ) ?;
45+ }
46+ parsed_node
3647 }
3748
3849 fn get_node_at_position < ' a > ( & self , tree : & ' a Tree , position : Position ) -> Option < Node < ' a > > {
@@ -58,8 +69,11 @@ impl YamlParser {
5869 match self . parse_node ( node, None ) {
5970 Some ( token) => tokens. push ( token) ,
6071 None => {
61- let mut cursor = node. walk ( ) ;
62- new_nodes. append ( & mut node. children ( & mut cursor) . collect :: < Vec < Node > > ( ) ) ;
72+ if node. child_count ( ) > 0 {
73+ let mut cursor = node. walk ( ) ;
74+ new_nodes
75+ . append ( & mut node. children ( & mut cursor) . collect :: < Vec < Node > > ( ) ) ;
76+ }
6377 }
6478 } ;
6579 }
@@ -71,7 +85,7 @@ impl YamlParser {
7185 fn parse_node < ' a > ( & self , node : Node , point : Option < Point > ) -> Option < Token > {
7286 match node. kind ( ) {
7387 "block_mapping_pair" => self . parse_block_mapping_pair ( node, point) ,
74- _ => self . parse_node ( node . parent ( ) ? , point ) ,
88+ _ => None ,
7589 }
7690 }
7791
0 commit comments