@@ -6,7 +6,7 @@ use crate::{nodekind::NodeKind, utils::lsp_to_ts_point};
66use super :: ParsedTree ;
77
88impl ParsedTree {
9- pub ( super ) fn walk_and_collect_filter < ' a > (
9+ pub ( super ) fn walk_and_filter < ' a > (
1010 cursor : & mut TreeCursor < ' a > ,
1111 f : fn ( & Node ) -> bool ,
1212 early : bool ,
@@ -24,7 +24,7 @@ impl ParsedTree {
2424 }
2525
2626 if cursor. goto_first_child ( ) {
27- v. extend ( Self :: walk_and_collect_filter ( cursor, f, early) ) ;
27+ v. extend ( Self :: walk_and_filter ( cursor, f, early) ) ;
2828 cursor. goto_parent ( ) ;
2929 }
3030
@@ -110,29 +110,41 @@ impl ParsedTree {
110110 self . tree . root_node ( ) . descendant_for_point_range ( pos, pos)
111111 }
112112
113- pub fn filter_nodes ( & self , f : fn ( & Node ) -> bool ) -> Vec < Node > {
114- self . filter_nodes_from ( self . tree . root_node ( ) , f)
113+ pub fn find_all_nodes ( & self , f : fn ( & Node ) -> bool ) -> Vec < Node > {
114+ self . find_all_nodes_from ( self . tree . root_node ( ) , f)
115115 }
116116
117- pub fn filter_nodes_from < ' a > ( & self , n : Node < ' a > , f : fn ( & Node ) -> bool ) -> Vec < Node < ' a > > {
117+ pub fn find_all_nodes_from < ' a > ( & self , n : Node < ' a > , f : fn ( & Node ) -> bool ) -> Vec < Node < ' a > > {
118118 let mut cursor = n. walk ( ) ;
119- Self :: walk_and_collect_filter ( & mut cursor, f, false )
119+ Self :: walk_and_filter ( & mut cursor, f, false )
120120 }
121121
122- pub fn find_node ( & self , f : fn ( & Node ) -> bool ) -> Vec < Node > {
122+ pub fn find_first_node ( & self , f : fn ( & Node ) -> bool ) -> Vec < Node > {
123123 self . find_node_from ( self . tree . root_node ( ) , f)
124124 }
125125
126126 pub fn find_node_from < ' a > ( & self , n : Node < ' a > , f : fn ( & Node ) -> bool ) -> Vec < Node < ' a > > {
127127 let mut cursor = n. walk ( ) ;
128- Self :: walk_and_collect_filter ( & mut cursor, f, true )
128+ Self :: walk_and_filter ( & mut cursor, f, true )
129129 }
130130
131131 pub fn get_package_name < ' a > ( & self , content : & ' a [ u8 ] ) -> Option < & ' a str > {
132- self . find_node ( NodeKind :: is_package_name)
132+ self . find_first_node ( NodeKind :: is_package_name)
133133 . first ( )
134134 . map ( |n| n. utf8_text ( content) . expect ( "utf-8 parse error" ) )
135135 }
136+ pub fn get_import_path < ' a > ( & self , content : & ' a [ u8 ] ) -> Vec < & ' a str > {
137+ self . find_all_nodes ( NodeKind :: is_import_path)
138+ . into_iter ( )
139+ . filter_map ( |n| {
140+ n. child_by_field_name ( "path" ) . map ( |c| {
141+ c. utf8_text ( content)
142+ . expect ( "utf-8 parse error" )
143+ . trim_matches ( '"' )
144+ } )
145+ } )
146+ . collect ( )
147+ }
136148}
137149
138150#[ cfg( test) ]
@@ -150,7 +162,7 @@ mod test {
150162
151163 assert ! ( parsed. is_some( ) ) ;
152164 let tree = parsed. unwrap ( ) ;
153- let nodes = tree. filter_nodes ( NodeKind :: is_message_name) ;
165+ let nodes = tree. find_all_nodes ( NodeKind :: is_message_name) ;
154166
155167 assert_eq ! ( nodes. len( ) , 2 ) ;
156168
@@ -163,5 +175,7 @@ mod test {
163175
164176 let package_name = tree. get_package_name ( contents. as_ref ( ) ) ;
165177 assert_yaml_snapshot ! ( package_name) ;
178+ let imports = tree. get_import_path ( contents. as_ref ( ) ) ;
179+ assert_yaml_snapshot ! ( imports) ;
166180 }
167181}
0 commit comments