1 use ra_db::{FileId, LocalSyntaxPtr, Cancelable};
3 SyntaxNode, AstNode, SmolStr,
6 use hir::{Def, ModuleSource};
14 impl NavigationTarget {
15 pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget {
17 file_id: symbol.file_id,
18 name: symbol.name.clone(),
19 kind: symbol.ptr.kind(),
20 range: symbol.ptr.range(),
21 ptr: Some(symbol.ptr.clone()),
25 // TODO once Def::Item is gone, this should be able to always return a NavigationTarget
26 pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> {
29 let (file_id, node) = s.source(db)?;
30 NavigationTarget::from_named(file_id.original_file(db), &*node)
33 let (file_id, node) = e.source(db)?;
34 NavigationTarget::from_named(file_id.original_file(db), &*node)
36 Def::EnumVariant(ev) => {
37 let (file_id, node) = ev.source(db)?;
38 NavigationTarget::from_named(file_id.original_file(db), &*node)
41 let (file_id, node) = f.source(db)?;
42 NavigationTarget::from_named(file_id.original_file(db), &*node)
45 let (file_id, source) = m.definition_source(db)?;
48 .map(|it| it.to_string().into())
49 .unwrap_or_else(|| SmolStr::new(""));
51 ModuleSource::SourceFile(node) => {
52 NavigationTarget::from_syntax(file_id, name, node.syntax())
54 ModuleSource::Module(node) => {
55 NavigationTarget::from_syntax(file_id, name, node.syntax())
59 Def::Item => return Ok(None),
64 fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
67 .map(|it| it.text().clone())
68 .unwrap_or_else(|| SmolStr::new(""));
69 NavigationTarget::from_syntax(file_id, name, node.syntax())
72 fn from_syntax(file_id: FileId, name: SmolStr, node: &SyntaxNode) -> NavigationTarget {
78 ptr: Some(LocalSyntaxPtr::new(node)),