From 42604c673d159edc1571732c6be1dc00a365b7be Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 16 Nov 2019 22:18:07 +0300 Subject: Better factoring of macro expansion machinery in goto def --- crates/ra_ide_api/src/goto_definition.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 4b1581499..70baa294f 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -1,5 +1,7 @@ //! FIXME: write short doc here +use std::iter::successors; + use hir::{db::AstDatabase, Source}; use ra_syntax::{ algo::find_node_at_offset, @@ -18,10 +20,8 @@ pub(crate) fn goto_definition( db: &RootDatabase, position: FilePosition, ) -> Option>> { - go(db, Source::new(position.file_id.into(), position.offset)) -} + let offset = descend_into_macros(db, position); -fn go(db: &RootDatabase, offset: Source) -> Option>> { let syntax = db.parse_or_expand(offset.file_id)?; if let Some(name_ref) = find_node_at_offset::(&syntax, offset.ast) { @@ -32,16 +32,25 @@ fn go(db: &RootDatabase, offset: Source) -> Option(&syntax, offset.ast) { + None +} + +fn descend_into_macros(db: &RootDatabase, position: FilePosition) -> Source { + successors(Some(Source::new(position.file_id.into(), position.offset)), |offset| { + let syntax = db.parse_or_expand(offset.file_id)?; + let macro_call = find_node_at_offset::(&syntax, offset.ast)?; + let tt = macro_call.token_tree()?; + if !tt.syntax().text_range().contains(offset.ast) { + return None; + } let source_analyzer = hir::SourceAnalyzer::new(db, offset.with_ast(macro_call.syntax()), None); - if let Some(exp) = source_analyzer.expand(db, ¯o_call) { - if let Some(offset) = exp.translate_offset(db, offset.ast) { - return go(db, Source::new(exp.file_id(), offset)); - } - } - } - None + let exp = source_analyzer.expand(db, ¯o_call)?; + let next_offset = exp.translate_offset(db, offset.ast)?; + Some(Source::new(exp.file_id(), next_offset)) + }) + .last() + .unwrap() } #[derive(Debug)] -- cgit v1.2.3