aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/navigation_target.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-11 10:01:35 +0000
committerAleksey Kladov <[email protected]>2019-01-11 13:01:57 +0000
commit2aa125251ebd74c0e2a119b351caec27a9e1da46 (patch)
treec556663ffa90d7c8164d010666ece7fb0a7e2f55 /crates/ra_ide_api/src/navigation_target.rs
parentf848aa97ab9d9789f72828d28619dd4227c352b9 (diff)
move nav to a separate file
Diffstat (limited to 'crates/ra_ide_api/src/navigation_target.rs')
-rw-r--r--crates/ra_ide_api/src/navigation_target.rs86
1 files changed, 86 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/navigation_target.rs b/crates/ra_ide_api/src/navigation_target.rs
new file mode 100644
index 000000000..bacb7329f
--- /dev/null
+++ b/crates/ra_ide_api/src/navigation_target.rs
@@ -0,0 +1,86 @@
1use ra_db::{FileId, LocalSyntaxPtr, Cancelable};
2use ra_syntax::{SyntaxNode, AstNode};
3use hir::{Name, Def, ModuleSource};
4
5use crate::{
6 NavigationTarget,
7 FileSymbol,
8 db::RootDatabase,
9};
10
11impl 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 pub(crate) fn from_syntax(
23 name: Option<Name>,
24 file_id: FileId,
25 node: &SyntaxNode,
26 ) -> NavigationTarget {
27 NavigationTarget {
28 file_id,
29 name: name.map(|n| n.to_string().into()).unwrap_or("".into()),
30 kind: node.kind(),
31 range: node.range(),
32 ptr: Some(LocalSyntaxPtr::new(node)),
33 }
34 }
35 // TODO once Def::Item is gone, this should be able to always return a NavigationTarget
36 pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> {
37 Ok(match def {
38 Def::Struct(s) => {
39 let (file_id, node) = s.source(db)?;
40 Some(NavigationTarget::from_syntax(
41 s.name(db)?,
42 file_id.original_file(db),
43 node.syntax(),
44 ))
45 }
46 Def::Enum(e) => {
47 let (file_id, node) = e.source(db)?;
48 Some(NavigationTarget::from_syntax(
49 e.name(db)?,
50 file_id.original_file(db),
51 node.syntax(),
52 ))
53 }
54 Def::EnumVariant(ev) => {
55 let (file_id, node) = ev.source(db)?;
56 Some(NavigationTarget::from_syntax(
57 ev.name(db)?,
58 file_id.original_file(db),
59 node.syntax(),
60 ))
61 }
62 Def::Function(f) => {
63 let (file_id, node) = f.source(db)?;
64 let name = f.signature(db).name().clone();
65 Some(NavigationTarget::from_syntax(
66 Some(name),
67 file_id.original_file(db),
68 node.syntax(),
69 ))
70 }
71 Def::Module(m) => {
72 let (file_id, source) = m.definition_source(db)?;
73 let name = m.name(db)?;
74 match source {
75 ModuleSource::SourceFile(node) => {
76 Some(NavigationTarget::from_syntax(name, file_id, node.syntax()))
77 }
78 ModuleSource::Module(node) => {
79 Some(NavigationTarget::from_syntax(name, file_id, node.syntax()))
80 }
81 }
82 }
83 Def::Item => None,
84 })
85 }
86}