diff options
author | Aleksey Kladov <[email protected]> | 2019-11-18 11:23:24 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-18 11:23:24 +0000 |
commit | 9fcd98e956a46d90c708abb9739f067a88ae3c4a (patch) | |
tree | ffc9afa4bd6f436e51eaa3de9758916a1bdc7124 /crates/ra_ide_api/src/goto_definition.rs | |
parent | 6d8ca870b3d8a12e7b2460532712f9a6daf80afb (diff) |
Add ra_ide_api::expand
This module should handle all tricky bits with mapping macro-expanded
HirFileId to original files the user actually can see in the editor
Diffstat (limited to 'crates/ra_ide_api/src/goto_definition.rs')
-rw-r--r-- | crates/ra_ide_api/src/goto_definition.rs | 27 |
1 files changed, 5 insertions, 22 deletions
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index b693a4c31..1a8db0ea0 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs | |||
@@ -1,16 +1,15 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use std::iter::successors; | ||
4 | |||
5 | use hir::{db::AstDatabase, Source}; | 3 | use hir::{db::AstDatabase, Source}; |
6 | use ra_syntax::{ | 4 | use ra_syntax::{ |
7 | ast::{self, DocCommentsOwner}, | 5 | ast::{self, DocCommentsOwner}, |
8 | match_ast, AstNode, SyntaxNode, SyntaxToken, | 6 | match_ast, AstNode, SyntaxNode, |
9 | }; | 7 | }; |
10 | 8 | ||
11 | use crate::{ | 9 | use crate::{ |
12 | db::RootDatabase, | 10 | db::RootDatabase, |
13 | display::{ShortLabel, ToNav}, | 11 | display::{ShortLabel, ToNav}, |
12 | expand::descend_into_macros, | ||
14 | references::{classify_name_ref, NameKind::*}, | 13 | references::{classify_name_ref, NameKind::*}, |
15 | FilePosition, NavigationTarget, RangeInfo, | 14 | FilePosition, NavigationTarget, RangeInfo, |
16 | }; | 15 | }; |
@@ -19,7 +18,9 @@ pub(crate) fn goto_definition( | |||
19 | db: &RootDatabase, | 18 | db: &RootDatabase, |
20 | position: FilePosition, | 19 | position: FilePosition, |
21 | ) -> Option<RangeInfo<Vec<NavigationTarget>>> { | 20 | ) -> Option<RangeInfo<Vec<NavigationTarget>>> { |
22 | let token = descend_into_macros(db, position)?; | 21 | let file = db.parse_or_expand(position.file_id.into())?; |
22 | let token = file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?; | ||
23 | let token = descend_into_macros(db, position.file_id, token); | ||
23 | 24 | ||
24 | let res = match_ast! { | 25 | let res = match_ast! { |
25 | match (token.ast.parent()) { | 26 | match (token.ast.parent()) { |
@@ -39,24 +40,6 @@ pub(crate) fn goto_definition( | |||
39 | Some(res) | 40 | Some(res) |
40 | } | 41 | } |
41 | 42 | ||
42 | fn descend_into_macros(db: &RootDatabase, position: FilePosition) -> Option<Source<SyntaxToken>> { | ||
43 | let file = db.parse_or_expand(position.file_id.into())?; | ||
44 | let token = file.token_at_offset(position.offset).filter(|it| !it.kind().is_trivia()).next()?; | ||
45 | |||
46 | successors(Some(Source::new(position.file_id.into(), token)), |token| { | ||
47 | let macro_call = token.ast.ancestors().find_map(ast::MacroCall::cast)?; | ||
48 | let tt = macro_call.token_tree()?; | ||
49 | if !token.ast.text_range().is_subrange(&tt.syntax().text_range()) { | ||
50 | return None; | ||
51 | } | ||
52 | let source_analyzer = | ||
53 | hir::SourceAnalyzer::new(db, token.with_ast(token.ast.parent()).as_ref(), None); | ||
54 | let exp = source_analyzer.expand(db, ¯o_call)?; | ||
55 | exp.map_token_down(db, token.as_ref()) | ||
56 | }) | ||
57 | .last() | ||
58 | } | ||
59 | |||
60 | #[derive(Debug)] | 43 | #[derive(Debug)] |
61 | pub(crate) enum ReferenceResult { | 44 | pub(crate) enum ReferenceResult { |
62 | Exact(NavigationTarget), | 45 | Exact(NavigationTarget), |