From 2eaa8c94a8a6b5cd86139c5e010ae95268b28658 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 16 Nov 2019 16:49:26 +0300 Subject: Goto definition works inside macros --- crates/ra_hir/src/source_binder.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 5764dc26d..75a467fb3 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -11,7 +11,7 @@ use hir_def::{ expr::{ExprId, PatId}, path::known, }; -use hir_expand::{name::AsName, Source}; +use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source}; use ra_syntax::{ ast::{self, AstNode}, match_ast, AstPtr, @@ -126,6 +126,20 @@ pub struct ReferenceDescriptor { pub name: String, } +pub struct Expansion { + macro_call_id: MacroCallId, +} + +impl Expansion { + pub fn translate_offset(&self, db: &impl HirDatabase, offset: TextUnit) -> Option { + let exp_info = self.file_id().expansion_info(db)?; + exp_info.translate_offset(offset) + } + pub fn file_id(&self) -> HirFileId { + self.macro_call_id.as_file(MacroFileKind::Items) + } +} + impl SourceAnalyzer { pub fn new( db: &impl HirDatabase, @@ -386,6 +400,13 @@ impl SourceAnalyzer { implements_trait(&canonical_ty, db, &self.resolver, krate, std_future_trait) } + pub fn expand(&self, db: &impl HirDatabase, macro_call: &ast::MacroCall) -> Option { + let def = self.resolve_macro_call(db, macro_call)?.id; + let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(macro_call)); + let macro_call_loc = MacroCallLoc { def, ast_id }; + Some(Expansion { macro_call_id: db.intern_macro(macro_call_loc) }) + } + #[cfg(test)] pub(crate) fn body_source_map(&self) -> Arc { self.body_source_map.clone().unwrap() -- cgit v1.2.3