From 069bf55cca1e1be1f6cdd28b638f691e059858dc Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 24 Dec 2019 20:32:42 +0100 Subject: Add infrastructure for visibility on syntax and hir_def level --- crates/ra_syntax/src/ast.rs | 4 +++- crates/ra_syntax/src/ast/extensions.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 277532a8c..89cb9a9f3 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -17,7 +17,9 @@ use crate::{ pub use self::{ expr_extensions::{ArrayExprKind, BinOp, ElseBranch, LiteralKind, PrefixOp, RangeOp}, - extensions::{FieldKind, PathSegmentKind, SelfParamKind, StructKind, TypeBoundKind}, + extensions::{ + FieldKind, PathSegmentKind, SelfParamKind, StructKind, TypeBoundKind, VisibilityKind, + }, generated::*, tokens::*, traits::*, diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index baaef3023..d9666cdca 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -413,3 +413,32 @@ impl ast::TraitDef { self.syntax().children_with_tokens().any(|t| t.kind() == T![auto]) } } + +pub enum VisibilityKind { + In(ast::Path), + PubCrate, + PubSuper, + Pub, +} + +impl ast::Visibility { + pub fn kind(&self) -> VisibilityKind { + if let Some(path) = children(self).next() { + VisibilityKind::In(path) + } else if self.is_pub_crate() { + VisibilityKind::PubCrate + } else if self.is_pub_super() { + VisibilityKind::PubSuper + } else { + VisibilityKind::Pub + } + } + + fn is_pub_crate(&self) -> bool { + self.syntax().children_with_tokens().any(|it| it.kind() == T![crate]) + } + + fn is_pub_super(&self) -> bool { + self.syntax().children_with_tokens().any(|it| it.kind() == T![super]) + } +} -- cgit v1.2.3 From 79c90b5641d2934864c587380e4f050ab63ac029 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 24 Dec 2019 23:45:14 +0100 Subject: Collect visibility of items during nameres --- crates/ra_syntax/src/ast/generated.rs | 3 +++ crates/ra_syntax/src/grammar.ron | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 9f9d6e63c..e64c83d33 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -1064,6 +1064,7 @@ impl AstNode for ExternCrateItem { } } impl ast::AttrsOwner for ExternCrateItem {} +impl ast::VisibilityOwner for ExternCrateItem {} impl ExternCrateItem { pub fn name_ref(&self) -> Option { AstChildren::new(&self.syntax).next() @@ -2006,6 +2007,7 @@ impl AstNode for ModuleItem { } } impl ast::AttrsOwner for ModuleItem {} +impl ast::VisibilityOwner for ModuleItem {} impl ModuleItem {} #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Name { @@ -3893,6 +3895,7 @@ impl AstNode for UseItem { } } impl ast::AttrsOwner for UseItem {} +impl ast::VisibilityOwner for UseItem {} impl UseItem { pub fn use_tree(&self) -> Option { AstChildren::new(&self.syntax).next() diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 08aafb610..e43a724f0 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -412,7 +412,7 @@ Grammar( "ModuleItem": ( enum: ["StructDef", "UnionDef", "EnumDef", "FnDef", "TraitDef", "TypeAliasDef", "ImplBlock", "UseItem", "ExternCrateItem", "ConstDef", "StaticDef", "Module" ], - traits: ["AttrsOwner"], + traits: ["AttrsOwner", "VisibilityOwner"], ), "ImplItem": ( enum: ["FnDef", "TypeAliasDef", "ConstDef"], @@ -683,7 +683,7 @@ Grammar( ] ), "UseItem": ( - traits: ["AttrsOwner"], + traits: ["AttrsOwner", "VisibilityOwner"], options: [ "UseTree" ], ), "UseTree": ( @@ -696,7 +696,7 @@ Grammar( collections: [("use_trees", "UseTree")] ), "ExternCrateItem": ( - traits: ["AttrsOwner"], + traits: ["AttrsOwner", "VisibilityOwner"], options: ["NameRef", "Alias"], ), "ArgList": ( -- cgit v1.2.3