From 0a80d9685a1db1bb39104a741922f6c9526eaf94 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 3 Jan 2019 13:47:28 +0300 Subject: visibility owner --- crates/ra_syntax/src/ast.rs | 6 +++++ crates/ra_syntax/src/ast/generated.rs | 47 +++++++++++++++++++++++++++++++++++ crates/ra_syntax/src/grammar.ron | 15 ++++++++--- 3 files changed, 64 insertions(+), 4 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 3e948800e..e968c9728 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -30,6 +30,12 @@ pub trait NameOwner<'a>: AstNode<'a> { } } +pub trait VisibilityOwner<'a>: AstNode<'a> { + fn visibility(self) -> Option> { + child_opt(self) + } +} + pub trait LoopBodyOwner<'a>: AstNode<'a> { fn loop_body(self) -> Option> { child_opt(self) diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index c5ac90a62..c619fc130 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -695,6 +695,7 @@ impl> ConstDefNode { } +impl<'a> ast::VisibilityOwner<'a> for ConstDef<'a> {} impl<'a> ast::NameOwner<'a> for ConstDef<'a> {} impl<'a> ast::TypeParamsOwner<'a> for ConstDef<'a> {} impl<'a> ast::AttrsOwner<'a> for ConstDef<'a> {} @@ -810,6 +811,7 @@ impl> EnumDefNode { } +impl<'a> ast::VisibilityOwner<'a> for EnumDef<'a> {} impl<'a> ast::NameOwner<'a> for EnumDef<'a> {} impl<'a> ast::TypeParamsOwner<'a> for EnumDef<'a> {} impl<'a> ast::AttrsOwner<'a> for EnumDef<'a> {} @@ -1213,6 +1215,7 @@ impl> FnDefNode { } +impl<'a> ast::VisibilityOwner<'a> for FnDef<'a> {} impl<'a> ast::NameOwner<'a> for FnDef<'a> {} impl<'a> ast::TypeParamsOwner<'a> for FnDef<'a> {} impl<'a> ast::AttrsOwner<'a> for FnDef<'a> {} @@ -2136,6 +2139,7 @@ impl> ModuleNode { } +impl<'a> ast::VisibilityOwner<'a> for Module<'a> {} impl<'a> ast::NameOwner<'a> for Module<'a> {} impl<'a> ast::AttrsOwner<'a> for Module<'a> {} impl<'a> ast::DocCommentsOwner<'a> for Module<'a> {} @@ -2351,6 +2355,7 @@ impl> NamedFieldDefNode { } +impl<'a> ast::VisibilityOwner<'a> for NamedFieldDef<'a> {} impl<'a> ast::NameOwner<'a> for NamedFieldDef<'a> {} impl<'a> ast::AttrsOwner<'a> for NamedFieldDef<'a> {} impl<'a> NamedFieldDef<'a> { @@ -3082,6 +3087,7 @@ impl> PosFieldNode { } +impl<'a> ast::VisibilityOwner<'a> for PosField<'a> {} impl<'a> ast::AttrsOwner<'a> for PosField<'a> {} impl<'a> PosField<'a> { pub fn type_ref(self) -> Option> { @@ -3639,6 +3645,7 @@ impl> StaticDefNode { } +impl<'a> ast::VisibilityOwner<'a> for StaticDef<'a> {} impl<'a> ast::NameOwner<'a> for StaticDef<'a> {} impl<'a> ast::TypeParamsOwner<'a> for StaticDef<'a> {} impl<'a> ast::AttrsOwner<'a> for StaticDef<'a> {} @@ -3742,6 +3749,7 @@ impl> StructDefNode { } +impl<'a> ast::VisibilityOwner<'a> for StructDef<'a> {} impl<'a> ast::NameOwner<'a> for StructDef<'a> {} impl<'a> ast::TypeParamsOwner<'a> for StructDef<'a> {} impl<'a> ast::AttrsOwner<'a> for StructDef<'a> {} @@ -3902,6 +3910,7 @@ impl> TraitDefNode { } +impl<'a> ast::VisibilityOwner<'a> for TraitDef<'a> {} impl<'a> ast::NameOwner<'a> for TraitDef<'a> {} impl<'a> ast::AttrsOwner<'a> for TraitDef<'a> {} impl<'a> ast::DocCommentsOwner<'a> for TraitDef<'a> {} @@ -4135,6 +4144,7 @@ impl> TypeDefNode { } +impl<'a> ast::VisibilityOwner<'a> for TypeDef<'a> {} impl<'a> ast::NameOwner<'a> for TypeDef<'a> {} impl<'a> ast::TypeParamsOwner<'a> for TypeDef<'a> {} impl<'a> ast::AttrsOwner<'a> for TypeDef<'a> {} @@ -4409,6 +4419,43 @@ impl<'a> UseTreeList<'a> { } } +// Visibility +#[derive(Debug, Clone, Copy,)] +pub struct VisibilityNode = OwnedRoot> { + pub(crate) syntax: SyntaxNode, +} +pub type Visibility<'a> = VisibilityNode>; + +impl, R2: TreeRoot> PartialEq> for VisibilityNode { + fn eq(&self, other: &VisibilityNode) -> bool { self.syntax == other.syntax } +} +impl> Eq for VisibilityNode {} +impl> Hash for VisibilityNode { + fn hash(&self, state: &mut H) { self.syntax.hash(state) } +} + +impl<'a> AstNode<'a> for Visibility<'a> { + fn cast(syntax: SyntaxNodeRef<'a>) -> Option { + match syntax.kind() { + VISIBILITY => Some(Visibility { syntax }), + _ => None, + } + } + fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax } +} + +impl> VisibilityNode { + pub fn borrowed(&self) -> Visibility { + VisibilityNode { syntax: self.syntax.borrowed() } + } + pub fn owned(&self) -> VisibilityNode { + VisibilityNode { syntax: self.syntax.owned() } + } +} + + +impl<'a> Visibility<'a> {} + // WhereClause #[derive(Debug, Clone, Copy,)] pub struct WhereClauseNode = OwnedRoot> { diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index aab4839a9..2abb9da61 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -247,6 +247,7 @@ Grammar( ), "FnDef": ( traits: [ + "VisibilityOwner", "NameOwner", "TypeParamsOwner", "AttrsOwner", @@ -257,6 +258,7 @@ Grammar( "RetType": (options: ["TypeRef"]), "StructDef": ( traits: [ + "VisibilityOwner", "NameOwner", "TypeParamsOwner", "AttrsOwner", @@ -264,10 +266,11 @@ Grammar( ] ), "NamedFieldDefList": (collections: [["fields", "NamedFieldDef"]]), - "NamedFieldDef": ( traits: ["NameOwner", "AttrsOwner"], options: ["TypeRef"] ), + "NamedFieldDef": ( traits: ["VisibilityOwner", "NameOwner", "AttrsOwner"], options: ["TypeRef"] ), "PosFieldList": (collections: [["fields", "PosField"]]), - "PosField": ( traits: ["AttrsOwner"], options: ["TypeRef"]), + "PosField": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]), "EnumDef": ( traits: [ + "VisibilityOwner", "NameOwner", "TypeParamsOwner", "AttrsOwner", @@ -275,27 +278,30 @@ Grammar( ], options: [["variant_list", "EnumVariantList"]] ), "EnumVariantList": ( collections: [["variants", "EnumVariant"]] ), "EnumVariant": ( traits: ["NameOwner"], options: ["Expr"] ), - "TraitDef": ( traits: ["NameOwner", "AttrsOwner", "DocCommentsOwner"] ), + "TraitDef": ( traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner"] ), "Module": ( - traits: ["NameOwner", "AttrsOwner", "DocCommentsOwner" ], + traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner" ], options: [ "ItemList" ] ), "ItemList": ( traits: [ "FnDefOwner", "ModuleItemOwner" ], ), "ConstDef": ( traits: [ + "VisibilityOwner", "NameOwner", "TypeParamsOwner", "AttrsOwner", "DocCommentsOwner" ] ), "StaticDef": ( traits: [ + "VisibilityOwner", "NameOwner", "TypeParamsOwner", "AttrsOwner", "DocCommentsOwner" ] ), "TypeDef": ( traits: [ + "VisibilityOwner", "NameOwner", "TypeParamsOwner", "AttrsOwner", @@ -482,6 +488,7 @@ Grammar( ], ), + "Visibility": (), "Name": (), "NameRef": (), "MacroCall": ( options: [ "TokenTree", "Path" ] ), -- cgit v1.2.3