11use std:: collections:: HashMap ;
22
3- use tree_sitter:: Node ;
3+ use tree_sitter:: { Node , Query , QueryCursor } ;
44
55use super :: {
6- filter_parsers:: { extra, label, number} ,
6+ filter_parsers:: { extra, label, number, string } ,
77 All , Attribute , AttributeType , Attributes , Filter , FilterFunction , HasFilterInformation ,
88 Language ,
99} ;
@@ -45,7 +45,7 @@ impl FunctionInLines {
4545impl Filter for FunctionInLines {
4646 fn parse_filter ( & self , s : & str ) -> Result < FilterFunction , String > {
4747 let ( start, end) = Self :: from_str ( s) ?;
48- Ok ( Box :: new ( move |node : & Node < ' _ > | {
48+ Ok ( Box :: new ( move |node : & Node < ' _ > , _ | {
4949 node. range ( ) . start_point . row >= start && node. range ( ) . end_point . row <= end
5050 } ) )
5151 }
@@ -83,7 +83,7 @@ impl Filter for FunctionInImpl {
8383 return Err ( format ! ( "invalid options for function_in_impl, this filter accepts not options, but got {s}" ) ) ;
8484 }
8585
86- Ok ( Box :: new ( move |node : & Node < ' _ > | {
86+ Ok ( Box :: new ( move |node : & Node < ' _ > , _ | {
8787 node. parent ( ) . is_some_and ( |parent| {
8888 parent
8989 . parent ( )
@@ -129,7 +129,7 @@ impl HasFilterInformation for FunctionWithParameterRust {
129129 }
130130
131131 fn attributes ( & self ) -> Attributes {
132- HashMap :: from ( [ ( Attribute ( "Name " . to_string ( ) ) , AttributeType :: String ) ] )
132+ HashMap :: from ( [ ( Attribute ( "name " . to_string ( ) ) , AttributeType :: String ) ] )
133133 }
134134
135135 type Supports = Language ;
@@ -155,14 +155,69 @@ impl HasFilterInformation for FunctionWithParameterPython {
155155 }
156156
157157 fn attributes ( & self ) -> Attributes {
158- HashMap :: from ( [ ( Attribute ( "Name " . to_string ( ) ) , AttributeType :: String ) ] )
158+ HashMap :: from ( [ ( Attribute ( "name " . to_string ( ) ) , AttributeType :: String ) ] )
159159 }
160160
161161 type Supports = Language ;
162162}
163163
164164impl Filter for FunctionWithParameterPython {
165165 fn parse_filter ( & self , s : & str ) -> Result < FilterFunction , String > {
166- todo ! ( )
166+ let query = Query :: new (
167+ & tree_sitter_rust:: LANGUAGE . into ( ) ,
168+ "((function_item
169+ parameters: (parameters (parameter pattern: (identifier) @param)))
170+ )
171+ ((let_declaration
172+ value: (closure_expression
173+ parameters: (closure_parameters [((identifier) @param)
174+ (parameter pattern: (identifier) @param)])
175+ ))
176+ )
177+ ((const_item
178+ value: (closure_expression
179+ (closure_parameters [((identifier) @param)
180+ (parameter pattern: (identifier) @param)])
181+ ))
182+ )
183+ ((static_item
184+ value: (closure_expression
185+ (closure_parameters [((identifier) @param)
186+ (parameter pattern: (identifier) @param)])
187+ ))
188+ )" ,
189+ )
190+ . map_err ( |_| String :: from ( "Could not create tree sitter filter" ) ) ?;
191+ let name = parse_with_param ( s) ?;
192+ Ok ( Box :: new ( move |node : & Node < ' _ > , code| {
193+ let mut cursor = QueryCursor :: new ( ) ;
194+ cursor. set_max_start_depth ( Some ( 0 ) ) ;
195+ let text_provider = code. as_bytes ( ) ;
196+ cursor. matches ( & query, * node, text_provider) . any ( |c| {
197+ c. captures
198+ . iter ( )
199+ . any ( |c| c. node . utf8_text ( text_provider) . unwrap ( ) == name)
200+ } )
201+ } ) )
202+ }
203+ }
204+
205+ fn parse_with_param ( s : & str ) -> Result < String , String > {
206+ let mut substring = s. split ( ' ' ) . filter ( |s| * s != " " ) ;
207+ let fst = substring. next ( ) . ok_or (
208+ "invalid options for function_in_lines filter\n expected [string] or name: [string]" ,
209+ ) ?;
210+ match fst {
211+ "start:" => {
212+ let format = "name: [string]" ;
213+ let name = string ( & mut substring, format, "name:" ) ?;
214+ extra ( & mut substring, format) ?;
215+ Ok ( name)
216+ }
217+
218+ name => {
219+ extra ( & mut substring, "[string]" ) ?;
220+ Ok ( name. to_string ( ) )
221+ }
167222 }
168223}
0 commit comments