From 69d07df201307fb7c539cdb20b8f1c1c12840386 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 24 Oct 2018 18:37:25 +0300 Subject: Complete crate:: paths --- crates/ra_syntax/src/ast/generated.rs | 4 ++++ crates/ra_syntax/src/ast/mod.rs | 30 ++++++++++++++++++++++++++++++ crates/ra_syntax/src/grammar.ron | 3 ++- 3 files changed, 36 insertions(+), 1 deletion(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 98c7de361..096405a38 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -1371,6 +1371,10 @@ impl<'a> Path<'a> { pub fn segment(self) -> Option> { super::child_opt(self) } + + pub fn qualifier(self) -> Option> { + super::child_opt(self) + } } // PathExpr diff --git a/crates/ra_syntax/src/ast/mod.rs b/crates/ra_syntax/src/ast/mod.rs index 900426a8a..c033263a1 100644 --- a/crates/ra_syntax/src/ast/mod.rs +++ b/crates/ra_syntax/src/ast/mod.rs @@ -232,6 +232,36 @@ impl<'a> IfExpr<'a> { } } + +#[derive(Debug, Clone, Copy)] +pub enum PathSegmentKind<'a> { + Name(NameRef<'a>), + SelfKw, + SuperKw, + CrateKw, +} + +impl<'a> PathSegment<'a> { + pub fn parent_path(self) -> Path<'a> { + self.syntax().parent().and_then(Path::cast) + .expect("segments are always nested in paths") + } + + pub fn kind(self) -> Option> { + let res = if let Some(name_ref) = self.name_ref() { + PathSegmentKind::Name(name_ref) + } else { + match self.syntax().first_child()?.kind() { + SELF_KW => PathSegmentKind::SelfKw, + SUPER_KW => PathSegmentKind::SuperKw, + CRATE_KW => PathSegmentKind::CrateKw, + _ => return None, + } + }; + Some(res) + } +} + fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option { children(parent).next() } diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 0830e02f2..c1c215e0d 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -531,7 +531,8 @@ Grammar( ), "Path": ( options: [ - ["segment", "PathSegment"] + ["segment", "PathSegment"], + ["qualifier", "Path"], ] ), "PathSegment": ( -- cgit v1.2.3