aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/source_binder.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r--crates/ra_hir/src/source_binder.rs23
1 files changed, 22 insertions, 1 deletions
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::{
11 expr::{ExprId, PatId}, 11 expr::{ExprId, PatId},
12 path::known, 12 path::known,
13}; 13};
14use hir_expand::{name::AsName, Source}; 14use hir_expand::{name::AsName, AstId, MacroCallId, MacroCallLoc, MacroFileKind, Source};
15use ra_syntax::{ 15use ra_syntax::{
16 ast::{self, AstNode}, 16 ast::{self, AstNode},
17 match_ast, AstPtr, 17 match_ast, AstPtr,
@@ -126,6 +126,20 @@ pub struct ReferenceDescriptor {
126 pub name: String, 126 pub name: String,
127} 127}
128 128
129pub struct Expansion {
130 macro_call_id: MacroCallId,
131}
132
133impl Expansion {
134 pub fn translate_offset(&self, db: &impl HirDatabase, offset: TextUnit) -> Option<TextUnit> {
135 let exp_info = self.file_id().expansion_info(db)?;
136 exp_info.translate_offset(offset)
137 }
138 pub fn file_id(&self) -> HirFileId {
139 self.macro_call_id.as_file(MacroFileKind::Items)
140 }
141}
142
129impl SourceAnalyzer { 143impl SourceAnalyzer {
130 pub fn new( 144 pub fn new(
131 db: &impl HirDatabase, 145 db: &impl HirDatabase,
@@ -386,6 +400,13 @@ impl SourceAnalyzer {
386 implements_trait(&canonical_ty, db, &self.resolver, krate, std_future_trait) 400 implements_trait(&canonical_ty, db, &self.resolver, krate, std_future_trait)
387 } 401 }
388 402
403 pub fn expand(&self, db: &impl HirDatabase, macro_call: &ast::MacroCall) -> Option<Expansion> {
404 let def = self.resolve_macro_call(db, macro_call)?.id;
405 let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(macro_call));
406 let macro_call_loc = MacroCallLoc { def, ast_id };
407 Some(Expansion { macro_call_id: db.intern_macro(macro_call_loc) })
408 }
409
389 #[cfg(test)] 410 #[cfg(test)]
390 pub(crate) fn body_source_map(&self) -> Arc<BodySourceMap> { 411 pub(crate) fn body_source_map(&self) -> Arc<BodySourceMap> {
391 self.body_source_map.clone().unwrap() 412 self.body_source_map.clone().unwrap()