File tree Expand file tree Collapse file tree 2 files changed +41
-10
lines changed Expand file tree Collapse file tree 2 files changed +41
-10
lines changed Original file line number Diff line number Diff line change @@ -45,29 +45,35 @@ impl Namespace {
45
45
)
46
46
(call
47
47
function: (identifier) @fn_name
48
- arguments: (arguments (argument value: (identifier) @bulk_imported ))
48
+ arguments: (arguments (argument value: (identifier) @imported_pkgs ))
49
49
(#eq? @fn_name "import")
50
50
)
51
51
"# ;
52
52
let mut ts_query = TsQuery :: new ( query_str) ?;
53
53
54
- let all_captures = ts_query. all_captures ( root_node, contents. as_bytes ( ) ) ;
54
+ let captures = ts_query. captures_by (
55
+ root_node,
56
+ & [ "exported" , "imported" , "imported_pkgs" ] ,
57
+ contents. as_bytes ( ) ,
58
+ ) ;
55
59
56
- let filter_captures = |capture_name : & str | -> Vec < String > {
57
- all_captures
60
+ let as_strings = |nodes : & Vec < tree_sitter :: Node > | {
61
+ nodes
58
62
. iter ( )
59
- . filter ( |( name, _) | name == capture_name)
60
- . map ( |( _, node) | {
63
+ . map ( |node| {
61
64
node. utf8_text ( contents. as_bytes ( ) )
62
65
. unwrap_or ( "" )
63
66
. to_string ( )
64
67
} )
65
- . collect ( )
68
+ . collect :: < Vec < _ > > ( )
66
69
} ;
67
70
68
- let mut exports = filter_captures ( "exported" ) ;
69
- let mut imports = filter_captures ( "imported" ) ;
70
- let mut package_imports = filter_captures ( "bulk_imported" ) ;
71
+ let mut exports = captures. get ( "exported" ) . map ( as_strings) . unwrap_or_default ( ) ;
72
+ let mut imports = captures. get ( "imported" ) . map ( as_strings) . unwrap_or_default ( ) ;
73
+ let mut package_imports = captures
74
+ . get ( "imported_pkgs" )
75
+ . map ( as_strings)
76
+ . unwrap_or_default ( ) ;
71
77
72
78
// Take unique values of imports and exports. In the future we'll lint
73
79
// this but for now just be defensive.
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+
1
3
use anyhow:: anyhow;
2
4
use tree_sitter:: Node ;
3
5
@@ -694,6 +696,29 @@ impl TsQuery {
694
696
} )
695
697
. collect ( )
696
698
}
699
+
700
+ /// Run the query on `contents` and filter captures that match `capture_names`.
701
+ /// They are returned in a hashmap keyed by capture name.
702
+ pub ( crate ) fn captures_by < ' tree > (
703
+ & mut self ,
704
+ node : tree_sitter:: Node < ' tree > ,
705
+ capture_names : & [ & str ] ,
706
+ contents : & [ u8 ] ,
707
+ ) -> HashMap < String , Vec < tree_sitter:: Node < ' tree > > > {
708
+ let mut result: HashMap < String , Vec < tree_sitter:: Node < ' tree > > > = HashMap :: new ( ) ;
709
+
710
+ for & name in capture_names {
711
+ result. insert ( name. to_string ( ) , Vec :: new ( ) ) ;
712
+ }
713
+
714
+ for ( name, node) in self . all_captures ( node, contents) {
715
+ if let Some ( nodes) = result. get_mut ( & name) {
716
+ nodes. push ( node) ;
717
+ }
718
+ }
719
+
720
+ result
721
+ }
697
722
}
698
723
699
724
#[ cfg( test) ]
You can’t perform that action at this time.
0 commit comments