aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-05 13:20:48 +0000
committerAleksey Kladov <[email protected]>2019-01-05 16:30:49 +0000
commitc2a0f5e50f6deb2e15bbfa6056a3cc0866c203a5 (patch)
treec2b1d13dc1cbae2c99e69b2cd9ec63d0f04e2996 /crates/ra_analysis
parent4551155073d8e12dd7aa467f6cd90e8705a115b3 (diff)
move goto_defenition to a separate file
Diffstat (limited to 'crates/ra_analysis')
-rw-r--r--crates/ra_analysis/src/goto_defenition.rs80
-rw-r--r--crates/ra_analysis/src/imp.rs2
-rw-r--r--crates/ra_analysis/src/lib.rs20
3 files changed, 91 insertions, 11 deletions
diff --git a/crates/ra_analysis/src/goto_defenition.rs b/crates/ra_analysis/src/goto_defenition.rs
new file mode 100644
index 000000000..91de7ef65
--- /dev/null
+++ b/crates/ra_analysis/src/goto_defenition.rs
@@ -0,0 +1,80 @@
1use ra_db::{FileId, Cancelable, SyntaxDatabase};
2use ra_syntax::{TextRange, AstNode, ast, SyntaxKind::{NAME, MODULE}};
3
4use ra_editor::find_node_at_offset;
5
6use crate::{FilePosition, NavigationTarget, db::RootDatabase};
7
8pub(crate) fn goto_defenition(
9 db: &RootDatabase,
10 position: FilePosition,
11) -> Cancelable<Option<Vec<NavigationTarget>>> {
12 let file = db.source_file(position.file_id);
13 let syntax = file.syntax();
14 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) {
15 return Ok(Some(reference_defenition(db, position.file_id, name_ref)?));
16 }
17 if let Some(name) = find_node_at_offset::<ast::Name>(syntax, position.offset) {
18 return name_defenition(db, position.file_id, name);
19 }
20 Ok(None)
21}
22
23fn reference_defenition(
24 db: &RootDatabase,
25 file_id: FileId,
26 name_ref: ast::NameRef,
27) -> Cancelable<Vec<NavigationTarget>> {
28 if let Some(fn_descr) =
29 hir::source_binder::function_from_child_node(db, file_id, name_ref.syntax())?
30 {
31 let scope = fn_descr.scopes(db);
32 // First try to resolve the symbol locally
33 if let Some(entry) = scope.resolve_local_name(name_ref) {
34 let nav = NavigationTarget {
35 file_id,
36 name: entry.name().to_string().into(),
37 range: entry.ptr().range(),
38 kind: NAME,
39 ptr: None,
40 };
41 return Ok(vec![nav]);
42 };
43 }
44 // If that fails try the index based approach.
45 let navs = db
46 .index_resolve(name_ref)?
47 .into_iter()
48 .map(NavigationTarget::from_symbol)
49 .collect();
50 Ok(navs)
51}
52
53fn name_defenition(
54 db: &RootDatabase,
55 file_id: FileId,
56 name: ast::Name,
57) -> Cancelable<Option<Vec<NavigationTarget>>> {
58 if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
59 if module.has_semi() {
60 if let Some(child_module) =
61 hir::source_binder::module_from_declaration(db, file_id, module)?
62 {
63 let file_id = child_module.file_id();
64 let name = match child_module.name() {
65 Some(name) => name.to_string().into(),
66 None => "".into(),
67 };
68 let nav = NavigationTarget {
69 file_id,
70 name,
71 range: TextRange::offset_len(0.into(), 0.into()),
72 kind: MODULE,
73 ptr: None,
74 };
75 return Ok(Some(vec![nav]));
76 }
77 }
78 }
79 Ok(None)
80}
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs
index e2871451c..6df118c20 100644
--- a/crates/ra_analysis/src/imp.rs
+++ b/crates/ra_analysis/src/imp.rs
@@ -416,7 +416,7 @@ impl db::RootDatabase {
416 .collect::<Vec<_>>(); 416 .collect::<Vec<_>>();
417 Ok(res) 417 Ok(res)
418 } 418 }
419 fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<FileSymbol>> { 419 pub(crate) fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<FileSymbol>> {
420 let name = name_ref.text(); 420 let name = name_ref.text();
421 let mut query = Query::new(name.to_string()); 421 let mut query = Query::new(name.to_string());
422 query.exact(); 422 query.exact();
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs
index 70ee448fc..0dac9f268 100644
--- a/crates/ra_analysis/src/lib.rs
+++ b/crates/ra_analysis/src/lib.rs
@@ -15,6 +15,7 @@ macro_rules! ctry {
15mod db; 15mod db;
16mod imp; 16mod imp;
17mod completion; 17mod completion;
18mod goto_defenition;
18mod symbol_index; 19mod symbol_index;
19pub mod mock_analysis; 20pub mod mock_analysis;
20mod runnables; 21mod runnables;
@@ -396,16 +397,15 @@ impl Analysis {
396 &self, 397 &self,
397 position: FilePosition, 398 position: FilePosition,
398 ) -> Cancelable<Option<Vec<NavigationTarget>>> { 399 ) -> Cancelable<Option<Vec<NavigationTarget>>> {
399 let r = self.approximately_resolve_symbol(position)?; 400 goto_defenition::goto_defenition(&*self.db, position)
400 Ok(r.map(|it| it.resolves_to)) 401 }
401 } 402 // /// Resolves reference to definition, but does not gurantee correctness.
402 /// Resolves reference to definition, but does not gurantee correctness. 403 // pub fn approximately_resolve_symbol(
403 pub fn approximately_resolve_symbol( 404 // &self,
404 &self, 405 // position: FilePosition,
405 position: FilePosition, 406 // ) -> Cancelable<Option<ReferenceResolution>> {
406 ) -> Cancelable<Option<ReferenceResolution>> { 407 // self.db.approximately_resolve_symbol(position)
407 self.db.approximately_resolve_symbol(position) 408 // }
408 }
409 /// Finds all usages of the reference at point. 409 /// Finds all usages of the reference at point.
410 pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> { 410 pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
411 self.db.find_all_refs(position) 411 self.db.find_all_refs(position)