fields are part of the structure

This commit is contained in:
Aleksey Kladov 2018-08-14 14:45:56 +03:00
parent 1141d448d9
commit 199e3b73c7
4 changed files with 44 additions and 10 deletions

View file

@ -96,6 +96,7 @@ fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> {
visitor()
.visit(decl::<ast::FnDef<_>>)
.visit(decl::<ast::StructDef<_>>)
.visit(decl::<ast::NamedField<_>>)
.visit(decl::<ast::EnumDef<_>>)
.visit(decl::<ast::TraitDef<_>>)
.visit(decl::<ast::Module<_>>)

View file

@ -88,8 +88,9 @@ impl fmt::Debug for E {}
let symbols = file_structure(&file);
dbg_eq(
r#"[StructureNode { parent: None, label: "Foo", navigation_range: [8; 11), node_range: [1; 26), kind: STRUCT_DEF },
StructureNode { parent: Some(0), label: "x", navigation_range: [18; 19), node_range: [18; 24), kind: NAMED_FIELD },
StructureNode { parent: None, label: "m", navigation_range: [32; 33), node_range: [28; 53), kind: MODULE },
StructureNode { parent: Some(1), label: "bar", navigation_range: [43; 46), node_range: [40; 51), kind: FN_DEF },
StructureNode { parent: Some(2), label: "bar", navigation_range: [43; 46), node_range: [40; 51), kind: FN_DEF },
StructureNode { parent: None, label: "E", navigation_range: [60; 61), node_range: [55; 75), kind: ENUM_DEF },
StructureNode { parent: None, label: "T", navigation_range: [81; 82), node_range: [76; 88), kind: TYPE_DEF },
StructureNode { parent: None, label: "S", navigation_range: [96; 97), node_range: [89; 108), kind: STATIC_DEF },

View file

@ -249,6 +249,25 @@ impl<R: TreeRoot> AstNode<R> for NameRef<R> {
impl<R: TreeRoot> NameRef<R> {}
// NamedField
#[derive(Debug, Clone, Copy)]
pub struct NamedField<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>,
}
impl<R: TreeRoot> AstNode<R> for NamedField<R> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
match syntax.kind() {
NAMED_FIELD => Some(NamedField { syntax }),
_ => None,
}
}
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
}
impl<R: TreeRoot> ast::NameOwner<R> for NamedField<R> {}
impl<R: TreeRoot> NamedField<R> {}
// NeverType
#[derive(Debug, Clone, Copy)]
pub struct NeverType<R: TreeRoot = Arc<SyntaxRoot>> {
@ -436,7 +455,13 @@ impl<R: TreeRoot> AstNode<R> for StructDef<R> {
}
impl<R: TreeRoot> ast::NameOwner<R> for StructDef<R> {}
impl<R: TreeRoot> StructDef<R> {}
impl<R: TreeRoot> StructDef<R> {
pub fn fields<'a>(&'a self) -> impl Iterator<Item = NamedField<R>> + 'a {
self.syntax()
.children()
.filter_map(NamedField::cast)
}
}
// TraitDef
#[derive(Debug, Clone, Copy)]

View file

@ -222,7 +222,13 @@ Grammar(
]
),
"FnDef": ( traits: ["NameOwner"] ),
"StructDef": ( traits: ["NameOwner"] ),
"StructDef": (
traits: ["NameOwner"],
collections: [
["fields", "NamedField"]
]
),
"NamedField": ( traits: ["NameOwner"] ),
"EnumDef": ( traits: ["NameOwner"] ),
"TraitDef": ( traits: ["NameOwner"] ),
"Module": ( traits: ["NameOwner"] ),
@ -230,6 +236,7 @@ Grammar(
"StaticDef": ( traits: ["NameOwner"] ),
"TypeDef": ( traits: ["NameOwner"] ),
"ImplItem": (),
"Name": (),
"NameRef": (),