diff options
author | Aleksey Kladov <[email protected]> | 2019-01-05 13:20:48 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-05 16:30:49 +0000 |
commit | c2a0f5e50f6deb2e15bbfa6056a3cc0866c203a5 (patch) | |
tree | c2b1d13dc1cbae2c99e69b2cd9ec63d0f04e2996 /crates/ra_analysis/src/goto_defenition.rs | |
parent | 4551155073d8e12dd7aa467f6cd90e8705a115b3 (diff) |
move goto_defenition to a separate file
Diffstat (limited to 'crates/ra_analysis/src/goto_defenition.rs')
-rw-r--r-- | crates/ra_analysis/src/goto_defenition.rs | 80 |
1 files changed, 80 insertions, 0 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 @@ | |||
1 | use ra_db::{FileId, Cancelable, SyntaxDatabase}; | ||
2 | use ra_syntax::{TextRange, AstNode, ast, SyntaxKind::{NAME, MODULE}}; | ||
3 | |||
4 | use ra_editor::find_node_at_offset; | ||
5 | |||
6 | use crate::{FilePosition, NavigationTarget, db::RootDatabase}; | ||
7 | |||
8 | pub(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 | |||
23 | fn 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 | |||
53 | fn 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 | } | ||