blob: 86de7bce8716e7fde0d57af513bfaa4ba39e75f6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
use std::sync::Arc;
use {
SyntaxNode, SyntaxRoot, TreeRoot, AstNode,
SyntaxKind::*,
};
{% for node in ast %}
{% set Name = node.kind | camel %}
#[derive(Debug, Clone, Copy)]
pub struct {{ Name }}<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>,
}
impl<R: TreeRoot> AstNode<R> for {{ Name }}<R> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
match syntax.kind() {
{{ node.kind }} => Some({{ Name }} { syntax }),
_ => None,
}
}
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
}
impl<R: TreeRoot> {{ Name }}<R> {
{%- if node.collections -%}
{%- for m in node.collections -%}
{%- set method_name = m.0 -%}
{%- set ChildName = m.1 | camel %}
pub fn {{ method_name }}<'a>(&'a self) -> impl Iterator<Item = {{ ChildName }}<R>> + 'a {
self.syntax()
.children()
.filter_map({{ ChildName }}::cast)
}
{% endfor -%}
{%- endif -%}
{%- if node.options -%}
{%- for m in node.options -%}
{%- set method_name = m.0 -%}
{%- set ChildName = m.1 | camel %}
pub fn {{ method_name }}(&self) -> Option<{{ ChildName }}<R>> {
self.syntax()
.children()
.filter_map({{ ChildName }}::cast)
.next()
}
{% endfor -%}
{%- endif -%}
}
{% endfor %}
|