2019-01-11 10:01:35 +00:00
|
|
|
use ra_db::{FileId, LocalSyntaxPtr, Cancelable};
|
2019-01-11 10:28:59 +00:00
|
|
|
use ra_syntax::{
|
|
|
|
SyntaxNode, AstNode, SmolStr,
|
|
|
|
ast
|
|
|
|
};
|
|
|
|
use hir::{Def, ModuleSource};
|
2019-01-11 10:01:35 +00:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
NavigationTarget,
|
|
|
|
FileSymbol,
|
|
|
|
db::RootDatabase,
|
|
|
|
};
|
|
|
|
|
|
|
|
impl NavigationTarget {
|
|
|
|
pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget {
|
|
|
|
NavigationTarget {
|
|
|
|
file_id: symbol.file_id,
|
|
|
|
name: symbol.name.clone(),
|
|
|
|
kind: symbol.ptr.kind(),
|
|
|
|
range: symbol.ptr.range(),
|
|
|
|
ptr: Some(symbol.ptr.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO once Def::Item is gone, this should be able to always return a NavigationTarget
|
|
|
|
pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> {
|
2019-01-11 10:29:53 +00:00
|
|
|
let res = match def {
|
2019-01-11 10:01:35 +00:00
|
|
|
Def::Struct(s) => {
|
|
|
|
let (file_id, node) = s.source(db)?;
|
2019-01-11 10:29:53 +00:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-11 10:01:35 +00:00
|
|
|
}
|
|
|
|
Def::Enum(e) => {
|
|
|
|
let (file_id, node) = e.source(db)?;
|
2019-01-11 10:29:53 +00:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-11 10:01:35 +00:00
|
|
|
}
|
|
|
|
Def::EnumVariant(ev) => {
|
|
|
|
let (file_id, node) = ev.source(db)?;
|
2019-01-11 10:29:53 +00:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-11 10:01:35 +00:00
|
|
|
}
|
|
|
|
Def::Function(f) => {
|
|
|
|
let (file_id, node) = f.source(db)?;
|
2019-01-11 10:29:53 +00:00
|
|
|
NavigationTarget::from_named(file_id.original_file(db), &*node)
|
2019-01-11 10:01:35 +00:00
|
|
|
}
|
|
|
|
Def::Module(m) => {
|
|
|
|
let (file_id, source) = m.definition_source(db)?;
|
2019-01-11 10:28:59 +00:00
|
|
|
let name = m
|
|
|
|
.name(db)?
|
|
|
|
.map(|it| it.to_string().into())
|
2019-01-11 10:31:21 +00:00
|
|
|
.unwrap_or_default();
|
2019-01-11 10:01:35 +00:00
|
|
|
match source {
|
|
|
|
ModuleSource::SourceFile(node) => {
|
2019-01-11 10:29:53 +00:00
|
|
|
NavigationTarget::from_syntax(file_id, name, node.syntax())
|
2019-01-11 10:01:35 +00:00
|
|
|
}
|
|
|
|
ModuleSource::Module(node) => {
|
2019-01-11 10:29:53 +00:00
|
|
|
NavigationTarget::from_syntax(file_id, name, node.syntax())
|
2019-01-11 10:01:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-11 10:29:53 +00:00
|
|
|
Def::Item => return Ok(None),
|
|
|
|
};
|
|
|
|
Ok(Some(res))
|
2019-01-11 10:01:35 +00:00
|
|
|
}
|
2019-01-11 10:05:45 +00:00
|
|
|
|
2019-01-11 10:28:59 +00:00
|
|
|
fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
|
2019-01-11 10:31:21 +00:00
|
|
|
let name = node.name().map(|it| it.text().clone()).unwrap_or_default();
|
2019-01-11 10:28:59 +00:00
|
|
|
NavigationTarget::from_syntax(file_id, name, node.syntax())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_syntax(file_id: FileId, name: SmolStr, node: &SyntaxNode) -> NavigationTarget {
|
2019-01-11 10:05:45 +00:00
|
|
|
NavigationTarget {
|
|
|
|
file_id,
|
2019-01-11 10:28:59 +00:00
|
|
|
name,
|
2019-01-11 10:05:45 +00:00
|
|
|
kind: node.kind(),
|
|
|
|
range: node.range(),
|
|
|
|
ptr: Some(LocalSyntaxPtr::new(node)),
|
|
|
|
}
|
|
|
|
}
|
2019-01-11 10:01:35 +00:00
|
|
|
}
|