aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/path.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/path.rs')
-rw-r--r--crates/ra_hir/src/path.rs18
1 files changed, 14 insertions, 4 deletions
diff --git a/crates/ra_hir/src/path.rs b/crates/ra_hir/src/path.rs
index 0b260072c..93f7203fe 100644
--- a/crates/ra_hir/src/path.rs
+++ b/crates/ra_hir/src/path.rs
@@ -1,9 +1,11 @@
1use ra_syntax::{SmolStr, ast, AstNode, TextRange}; 1use ra_syntax::{ast, AstNode, TextRange};
2
3use crate::{Name, AsName};
2 4
3#[derive(Debug, Clone, PartialEq, Eq, Hash)] 5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4pub struct Path { 6pub struct Path {
5 pub kind: PathKind, 7 pub kind: PathKind,
6 pub segments: Vec<SmolStr>, 8 pub segments: Vec<Name>,
7} 9}
8 10
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -29,7 +31,7 @@ impl Path {
29 loop { 31 loop {
30 let segment = path.segment()?; 32 let segment = path.segment()?;
31 match segment.kind()? { 33 match segment.kind()? {
32 ast::PathSegmentKind::Name(name) => segments.push(name.text()), 34 ast::PathSegmentKind::Name(name) => segments.push(name.as_name()),
33 ast::PathSegmentKind::CrateKw => { 35 ast::PathSegmentKind::CrateKw => {
34 kind = PathKind::Crate; 36 kind = PathKind::Crate;
35 break; 37 break;
@@ -67,6 +69,14 @@ impl Path {
67 pub fn is_ident(&self) -> bool { 69 pub fn is_ident(&self) -> bool {
68 self.kind == PathKind::Plain && self.segments.len() == 1 70 self.kind == PathKind::Plain && self.segments.len() == 1
69 } 71 }
72
73 /// If this path is a single identifier, like `foo`, return its name.
74 pub fn as_ident(&self) -> Option<&Name> {
75 if self.kind != PathKind::Plain || self.segments.len() > 1 {
76 return None;
77 }
78 self.segments.first()
79 }
70} 80}
71 81
72fn expand_use_tree( 82fn expand_use_tree(
@@ -130,7 +140,7 @@ fn convert_path(prefix: Option<Path>, path: ast::Path) -> Option<Path> {
130 kind: PathKind::Plain, 140 kind: PathKind::Plain,
131 segments: Vec::with_capacity(1), 141 segments: Vec::with_capacity(1),
132 }); 142 });
133 res.segments.push(name.text()); 143 res.segments.push(name.as_name());
134 res 144 res
135 } 145 }
136 ast::PathSegmentKind::CrateKw => { 146 ast::PathSegmentKind::CrateKw => {