1+ use std:: sync:: LazyLock ;
2+
13use tree_sitter:: Node ;
4+ use tree_sitter:: StreamingIterator ;
25
36pub ( crate ) static OBJECT_REFERENCE_QUERY : LazyLock < tree_sitter:: Query > = LazyLock :: new ( || {
47 static QUERY_STR : & str = r#"
5- (object_reference
6- object_reference_1of1: (any_identifier) @tail
7- )
8- (object_reference
9- object_reference_1of2: (any_identifier) @head
10- object_reference_2of2: (any_identifier) @tail
11- )
12- (object_reference
13- object_reference_1of1: (any_identifier) @head
14- object_reference_1of2: (any_identifier) @middle
15- object_reference_1of3: (any_identifier) @tail
16- )
8+ (object_reference
9+ object_reference_1of1: (any_identifier) @tail
10+ )
11+ (object_reference
12+ object_reference_1of2: (any_identifier) @head
13+ object_reference_2of2: (any_identifier) @tail
14+ )
15+ (object_reference
16+ object_reference_1of3: (any_identifier) @head
17+ object_reference_2of3: (any_identifier) @middle
18+ object_reference_3of3: (any_identifier) @tail
1719 )
1820"# ;
1921 tree_sitter:: Query :: new ( & pgt_treesitter_grammar:: LANGUAGE . into ( ) , QUERY_STR )
@@ -25,25 +27,22 @@ pub(crate) fn object_reference_query<'a>(
2527 stmt : & ' a str ,
2628) -> Option < ( Option < Node < ' a > > , Option < Node < ' a > > , Node < ' a > ) > {
2729 let mut cursor = tree_sitter:: QueryCursor :: new ( ) ;
28- let matches = cursor. matches ( & TS_QUERY , root_node , stmt. as_bytes ( ) ) ;
30+ let mut matches = cursor. matches ( & OBJECT_REFERENCE_QUERY , node , stmt. as_bytes ( ) ) ;
2931
30- assert ! (
31- matches. len( ) <= 1 ,
32- "Please pass a single `object_reference` node into the `object_reference_query`!"
33- ) ;
34-
35- if matches[ 0 ] . len ( ) == 0 {
36- None
37- } else if matches[ 0 ] . captures . len ( ) == 1 {
38- Some ( ( None , None , m. captures [ 0 ] . node ) )
39- } else if matches[ 0 ] . captures . len ( ) == 2 {
40- Some ( ( None , m. captures [ 0 ] . node , m. captures [ 1 ] . node ) )
41- } else if matches[ 0 ] . captures . len ( ) == 3 {
42- Some ( (
43- Some ( m. captures [ 0 ] . node ) ,
44- Some ( m. captures [ 1 ] . node ) ,
45- m. captures [ 2 ] . node ,
46- ) )
32+ if let Some ( next) = matches. next ( ) {
33+ if next. captures . len ( ) == 1 {
34+ Some ( ( None , None , next. captures [ 0 ] . node ) )
35+ } else if next. captures . len ( ) == 2 {
36+ Some ( ( None , Some ( next. captures [ 0 ] . node ) , next. captures [ 1 ] . node ) )
37+ } else if next. captures . len ( ) == 3 {
38+ Some ( (
39+ Some ( next. captures [ 0 ] . node ) ,
40+ Some ( next. captures [ 1 ] . node ) ,
41+ next. captures [ 2 ] . node ,
42+ ) )
43+ } else {
44+ None
45+ }
4746 } else {
4847 None
4948 }
0 commit comments