]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide_api/src/navigation_target.rs
Make from_syntax private
[rust.git] / crates / ra_ide_api / src / navigation_target.rs
1 use ra_db::{FileId, LocalSyntaxPtr, Cancelable};
2 use ra_syntax::{SyntaxNode, AstNode};
3 use hir::{Name, Def, ModuleSource};
4
5 use crate::{
6     NavigationTarget,
7     FileSymbol,
8     db::RootDatabase,
9 };
10
11 impl NavigationTarget {
12     pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget {
13         NavigationTarget {
14             file_id: symbol.file_id,
15             name: symbol.name.clone(),
16             kind: symbol.ptr.kind(),
17             range: symbol.ptr.range(),
18             ptr: Some(symbol.ptr.clone()),
19         }
20     }
21
22     // TODO once Def::Item is gone, this should be able to always return a NavigationTarget
23     pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> {
24         Ok(match def {
25             Def::Struct(s) => {
26                 let (file_id, node) = s.source(db)?;
27                 Some(NavigationTarget::from_syntax(
28                     s.name(db)?,
29                     file_id.original_file(db),
30                     node.syntax(),
31                 ))
32             }
33             Def::Enum(e) => {
34                 let (file_id, node) = e.source(db)?;
35                 Some(NavigationTarget::from_syntax(
36                     e.name(db)?,
37                     file_id.original_file(db),
38                     node.syntax(),
39                 ))
40             }
41             Def::EnumVariant(ev) => {
42                 let (file_id, node) = ev.source(db)?;
43                 Some(NavigationTarget::from_syntax(
44                     ev.name(db)?,
45                     file_id.original_file(db),
46                     node.syntax(),
47                 ))
48             }
49             Def::Function(f) => {
50                 let (file_id, node) = f.source(db)?;
51                 let name = f.signature(db).name().clone();
52                 Some(NavigationTarget::from_syntax(
53                     Some(name),
54                     file_id.original_file(db),
55                     node.syntax(),
56                 ))
57             }
58             Def::Module(m) => {
59                 let (file_id, source) = m.definition_source(db)?;
60                 let name = m.name(db)?;
61                 match source {
62                     ModuleSource::SourceFile(node) => {
63                         Some(NavigationTarget::from_syntax(name, file_id, node.syntax()))
64                     }
65                     ModuleSource::Module(node) => {
66                         Some(NavigationTarget::from_syntax(name, file_id, node.syntax()))
67                     }
68                 }
69             }
70             Def::Item => None,
71         })
72     }
73
74     fn from_syntax(name: Option<Name>, file_id: FileId, node: &SyntaxNode) -> NavigationTarget {
75         NavigationTarget {
76             file_id,
77             name: name.map(|n| n.to_string().into()).unwrap_or("".into()),
78             kind: node.kind(),
79             range: node.range(),
80             ptr: Some(LocalSyntaxPtr::new(node)),
81         }
82     }
83 }