aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r--crates/ra_syntax/src/ast/generated.rs4
-rw-r--r--crates/ra_syntax/src/ast/mod.rs30
2 files changed, 34 insertions, 0 deletions
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> {
1371 pub fn segment(self) -> Option<PathSegment<'a>> { 1371 pub fn segment(self) -> Option<PathSegment<'a>> {
1372 super::child_opt(self) 1372 super::child_opt(self)
1373 } 1373 }
1374
1375 pub fn qualifier(self) -> Option<Path<'a>> {
1376 super::child_opt(self)
1377 }
1374} 1378}
1375 1379
1376// PathExpr 1380// 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> {
232 } 232 }
233} 233}
234 234
235
236#[derive(Debug, Clone, Copy)]
237pub enum PathSegmentKind<'a> {
238 Name(NameRef<'a>),
239 SelfKw,
240 SuperKw,
241 CrateKw,
242}
243
244impl<'a> PathSegment<'a> {
245 pub fn parent_path(self) -> Path<'a> {
246 self.syntax().parent().and_then(Path::cast)
247 .expect("segments are always nested in paths")
248 }
249
250 pub fn kind(self) -> Option<PathSegmentKind<'a>> {
251 let res = if let Some(name_ref) = self.name_ref() {
252 PathSegmentKind::Name(name_ref)
253 } else {
254 match self.syntax().first_child()?.kind() {
255 SELF_KW => PathSegmentKind::SelfKw,
256 SUPER_KW => PathSegmentKind::SuperKw,
257 CRATE_KW => PathSegmentKind::CrateKw,
258 _ => return None,
259 }
260 };
261 Some(res)
262 }
263}
264
235fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option<C> { 265fn child_opt<'a, P: AstNode<'a>, C: AstNode<'a>>(parent: P) -> Option<C> {
236 children(parent).next() 266 children(parent).next()
237} 267}