introduce hir::Name

This commit is contained in:
Aleksey Kladov
2018-12-27 20:07:21 +03:00
parent 3b820bcca3
commit d963042ca9
11 changed files with 121 additions and 51 deletions

View File

@@ -1,9 +1,11 @@
use ra_syntax::{SmolStr, ast, AstNode, TextRange};
use ra_syntax::{ast, AstNode, TextRange};
use crate::{Name, AsName};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Path {
pub kind: PathKind,
pub segments: Vec<SmolStr>,
pub segments: Vec<Name>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -29,7 +31,7 @@ impl Path {
loop {
let segment = path.segment()?;
match segment.kind()? {
ast::PathSegmentKind::Name(name) => segments.push(name.text()),
ast::PathSegmentKind::Name(name) => segments.push(name.as_name()),
ast::PathSegmentKind::CrateKw => {
kind = PathKind::Crate;
break;
@@ -67,6 +69,14 @@ impl Path {
pub fn is_ident(&self) -> bool {
self.kind == PathKind::Plain && self.segments.len() == 1
}
/// If this path is a single identifier, like `foo`, return its name.
pub fn as_ident(&self) -> Option<&Name> {
if self.kind != PathKind::Plain || self.segments.len() > 1 {
return None;
}
self.segments.first()
}
}
fn expand_use_tree(
@@ -130,7 +140,7 @@ fn convert_path(prefix: Option<Path>, path: ast::Path) -> Option<Path> {
kind: PathKind::Plain,
segments: Vec::with_capacity(1),
});
res.segments.push(name.text());
res.segments.push(name.as_name());
res
}
ast::PathSegmentKind::CrateKw => {