rust/crates/ra_hir/src/path.rs

239 lines
7.6 KiB
Rust
Raw Normal View History

use std::sync::Arc;
2019-02-01 17:18:10 -06:00
use ra_syntax::{ast::{self, NameOwner}, AstNode};
2018-12-27 11:07:21 -06:00
use crate::{Name, AsName, type_ref::TypeRef};
2018-11-27 18:42:26 -06:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2018-11-27 19:09:44 -06:00
pub struct Path {
pub kind: PathKind,
pub segments: Vec<PathSegment>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PathSegment {
pub name: Name,
pub args_and_bindings: Option<Arc<GenericArgs>>,
}
/// Generic arguments to a path segment (e.g. the `i32` in `Option<i32>`). This
/// can (in the future) also include bindings of associated types, like in
/// `Iterator<Item = Foo>`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GenericArgs {
pub args: Vec<GenericArg>,
// someday also bindings
}
/// A single generic argument.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GenericArg {
Type(TypeRef),
// or lifetime...
2018-11-27 18:42:26 -06:00
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2018-11-27 19:09:44 -06:00
pub enum PathKind {
2018-11-27 18:42:26 -06:00
Plain,
Self_,
Super,
Crate,
2019-01-22 23:21:29 -06:00
// Absolute path
Abs,
2018-11-27 18:42:26 -06:00
}
impl Path {
/// Calls `cb` with all paths, represented by this use item.
2019-01-18 07:36:56 -06:00
pub fn expand_use_item<'a>(
item: &'a ast::UseItem,
mut cb: impl FnMut(Path, &'a ast::UseTree, bool, Option<Name>),
2019-01-18 07:36:56 -06:00
) {
2018-11-27 18:42:26 -06:00
if let Some(tree) = item.use_tree() {
expand_use_tree(None, tree, &mut cb);
}
}
/// Converts an `ast::Path` to `Path`. Works with use trees.
2019-01-08 02:28:42 -06:00
pub fn from_ast(mut path: &ast::Path) -> Option<Path> {
2018-11-27 18:42:26 -06:00
let mut kind = PathKind::Plain;
let mut segments = Vec::new();
loop {
let segment = path.segment()?;
2019-01-22 23:21:29 -06:00
if segment.has_colon_colon() {
kind = PathKind::Abs;
}
2018-11-27 18:42:26 -06:00
match segment.kind()? {
ast::PathSegmentKind::Name(name) => {
2019-02-08 05:49:43 -06:00
let args =
segment.type_arg_list().and_then(GenericArgs::from_ast).map(Arc::new);
let segment = PathSegment { name: name.as_name(), args_and_bindings: args };
segments.push(segment);
}
2018-11-27 18:42:26 -06:00
ast::PathSegmentKind::CrateKw => {
kind = PathKind::Crate;
break;
}
ast::PathSegmentKind::SelfKw => {
kind = PathKind::Self_;
break;
}
ast::PathSegmentKind::SuperKw => {
kind = PathKind::Super;
break;
}
}
path = match qualifier(path) {
Some(it) => it,
None => break,
};
}
segments.reverse();
return Some(Path { kind, segments });
2019-01-08 02:28:42 -06:00
fn qualifier(path: &ast::Path) -> Option<&ast::Path> {
2018-11-27 18:42:26 -06:00
if let Some(q) = path.qualifier() {
return Some(q);
}
2019-03-23 02:53:48 -05:00
// FIXME: this bottom up traversal is not too precise.
2019-02-11 10:18:27 -06:00
// Should we handle do a top-down analysis, recording results?
2018-11-27 18:42:26 -06:00
let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
let use_tree = use_tree_list.parent_use_tree();
use_tree.path()
}
}
2019-01-05 09:32:07 -06:00
/// Converts an `ast::NameRef` into a single-identifier `Path`.
2019-01-08 02:28:42 -06:00
pub fn from_name_ref(name_ref: &ast::NameRef) -> Path {
2019-01-05 18:00:34 -06:00
name_ref.as_name().into()
2019-01-05 09:32:07 -06:00
}
2018-11-27 18:42:26 -06:00
/// `true` is this path is a single identifier, like `foo`
2018-11-27 19:09:44 -06:00
pub fn is_ident(&self) -> bool {
2018-11-27 18:42:26 -06:00
self.kind == PathKind::Plain && self.segments.len() == 1
}
2018-12-27 11:07:21 -06:00
/// `true` if this path is just a standalone `self`
pub fn is_self(&self) -> bool {
2019-06-04 01:28:22 -05:00
self.kind == PathKind::Self_ && self.segments.is_empty()
}
2018-12-27 11:07:21 -06:00
/// 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().map(|s| &s.name)
}
2019-04-15 05:01:29 -05:00
pub fn expand_macro_expr(&self) -> Option<Name> {
self.as_ident().and_then(|name| Some(name.clone()))
}
}
impl GenericArgs {
2019-02-17 07:54:48 -06:00
pub(crate) fn from_ast(node: &ast::TypeArgList) -> Option<GenericArgs> {
let mut args = Vec::new();
for type_arg in node.type_args() {
let type_ref = TypeRef::from_ast_opt(type_arg.type_ref());
args.push(GenericArg::Type(type_ref));
}
// lifetimes and assoc type args ignored for now
2019-06-04 01:28:22 -05:00
if !args.is_empty() {
Some(GenericArgs { args })
} else {
None
}
2018-12-27 11:07:21 -06:00
}
2018-11-27 18:42:26 -06:00
}
2019-01-05 18:00:34 -06:00
impl From<Name> for Path {
fn from(name: Name) -> Path {
Path {
kind: PathKind::Plain,
2019-02-08 05:49:43 -06:00
segments: vec![PathSegment { name, args_and_bindings: None }],
2019-01-05 18:00:34 -06:00
}
}
}
2019-01-18 07:36:56 -06:00
fn expand_use_tree<'a>(
2018-11-27 18:42:26 -06:00
prefix: Option<Path>,
2019-01-18 07:36:56 -06:00
tree: &'a ast::UseTree,
cb: &mut impl FnMut(Path, &'a ast::UseTree, bool, Option<Name>),
2018-11-27 18:42:26 -06:00
) {
if let Some(use_tree_list) = tree.use_tree_list() {
let prefix = match tree.path() {
2018-12-21 16:29:59 -06:00
// E.g. use something::{{{inner}}};
2018-11-27 18:42:26 -06:00
None => prefix,
2018-12-21 16:29:59 -06:00
// E.g. `use something::{inner}` (prefix is `None`, path is `something`)
// or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
2018-11-27 18:42:26 -06:00
Some(path) => match convert_path(prefix, path) {
Some(it) => Some(it),
2019-03-23 02:53:48 -05:00
None => return, // FIXME: report errors somewhere
2018-11-27 18:42:26 -06:00
},
};
2018-12-21 16:29:59 -06:00
for child_tree in use_tree_list.use_trees() {
expand_use_tree(prefix.clone(), child_tree, cb);
}
} else {
2019-02-01 17:18:10 -06:00
let alias = tree.alias().and_then(|a| a.name()).map(|a| a.as_name());
if let Some(ast_path) = tree.path() {
2018-12-21 16:29:59 -06:00
// Handle self in a path.
// E.g. `use something::{self, <...>}`
if ast_path.qualifier().is_none() {
if let Some(segment) = ast_path.segment() {
if segment.kind() == Some(ast::PathSegmentKind::SelfKw) {
if let Some(prefix) = prefix {
cb(prefix, tree, false, alias);
return;
2018-12-21 16:29:59 -06:00
}
}
}
}
2018-11-27 18:42:26 -06:00
if let Some(path) = convert_path(prefix, ast_path) {
let is_glob = tree.has_star();
cb(path, tree, is_glob, alias)
2018-11-27 18:42:26 -06:00
}
2019-03-23 02:53:48 -05:00
// FIXME: report errors somewhere
// We get here if we do
2018-11-27 18:42:26 -06:00
}
}
}
2019-01-08 02:28:42 -06:00
fn convert_path(prefix: Option<Path>, path: &ast::Path) -> Option<Path> {
2019-02-08 05:49:43 -06:00
let prefix =
if let Some(qual) = path.qualifier() { Some(convert_path(prefix, qual)?) } else { prefix };
2018-11-27 18:42:26 -06:00
let segment = path.segment()?;
let res = match segment.kind()? {
ast::PathSegmentKind::Name(name) => {
2019-02-08 05:49:43 -06:00
let mut res = prefix
.unwrap_or_else(|| Path { kind: PathKind::Plain, segments: Vec::with_capacity(1) });
res.segments.push(PathSegment {
name: name.as_name(),
args_and_bindings: None, // no type args in use
});
2018-11-27 18:42:26 -06:00
res
}
ast::PathSegmentKind::CrateKw => {
if prefix.is_some() {
return None;
}
2019-02-08 05:49:43 -06:00
Path { kind: PathKind::Crate, segments: Vec::new() }
2018-11-27 18:42:26 -06:00
}
ast::PathSegmentKind::SelfKw => {
if prefix.is_some() {
return None;
}
2019-02-08 05:49:43 -06:00
Path { kind: PathKind::Self_, segments: Vec::new() }
2018-11-27 18:42:26 -06:00
}
ast::PathSegmentKind::SuperKw => {
if prefix.is_some() {
return None;
}
2019-02-08 05:49:43 -06:00
Path { kind: PathKind::Super, segments: Vec::new() }
2018-11-27 18:42:26 -06:00
}
};
Some(res)
}