Skip to content

Commit ffc5749

Browse files
committed
Add the possibility to search node by their number
1 parent 0674626 commit ffc5749

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

src/ts_parser.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ pub struct TSParser<
2424
phantom: PhantomData<T>,
2525
}
2626

27-
type FilterFn = fn(&Node) -> bool;
27+
type FilterFn = dyn Fn(&Node) -> bool;
2828

2929
pub struct Filter {
30-
filters: Vec<FilterFn>,
30+
filters: Vec<Box<FilterFn>>,
3131
}
3232

3333
impl Filter {
@@ -69,8 +69,18 @@ fn get_fake_code<T: TSLanguage>(
6969
}
7070
}
7171

72-
impl<T: TSLanguage + Checker + Getter + Alterator + Cyclomatic + Exit + Halstead + Loc + NArgs>
73-
TSParserTrait for TSParser<T>
72+
impl<
73+
T: 'static
74+
+ TSLanguage
75+
+ Checker
76+
+ Getter
77+
+ Alterator
78+
+ Cyclomatic
79+
+ Exit
80+
+ Halstead
81+
+ Loc
82+
+ NArgs,
83+
> TSParserTrait for TSParser<T>
7484
{
7585
type Checker = T;
7686
type Getter = T;
@@ -120,20 +130,24 @@ impl<T: TSLanguage + Checker + Getter + Alterator + Cyclomatic + Exit + Halstead
120130
}
121131

122132
fn get_filters(&self, filters: &[String]) -> Filter {
123-
let mut res = Vec::new();
133+
let mut res: Vec<Box<FilterFn>> = Vec::new();
124134
for f in filters.iter() {
125135
let f = f.as_str();
126-
res.push(match f {
127-
"call" => T::is_call,
128-
"comment" => T::is_comment,
129-
"error" => T::is_error,
130-
"string" => T::is_string,
131-
"function" => T::is_func,
132-
_ => |_: &Node| -> bool { true },
133-
});
136+
match f {
137+
"call" => res.push(Box::new(T::is_call)),
138+
"comment" => res.push(Box::new(T::is_comment)),
139+
"error" => res.push(Box::new(T::is_error)),
140+
"string" => res.push(Box::new(T::is_string)),
141+
"function" => res.push(Box::new(T::is_func)),
142+
_ => {
143+
if let Ok(n) = f.parse::<u16>() {
144+
res.push(Box::new(move |node: &Node| -> bool { node.kind_id() == n }));
145+
}
146+
}
147+
}
134148
}
135149
if res.is_empty() {
136-
res.push(|_: &Node| -> bool { true })
150+
res.push(Box::new(|_: &Node| -> bool { true }))
137151
}
138152

139153
Filter { filters: res }

0 commit comments

Comments
 (0)