From d61707b4e1f0bdfc7f62b1abf78fdc45c0128699 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 3 Jan 2019 21:28:35 +0300 Subject: index stuff produced by macros --- crates/ra_hir/src/ids.rs | 7 +++++++ crates/ra_hir/src/module/nameres.rs | 8 ++++---- crates/ra_hir/src/source_binder.rs | 39 ++++++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index a09dee8b1..4c7ebe3ea 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -48,6 +48,13 @@ impl HirFileId { } } + pub(crate) fn as_macro_call_id(self) -> Option { + match self.0 { + HirFileIdRepr::Macro(it) => Some(it), + _ => None, + } + } + pub(crate) fn hir_source_file(db: &impl HirDatabase, file_id: HirFileId) -> SourceFileNode { match file_id.0 { HirFileIdRepr::File(file_id) => db.source_file(file_id), diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 40aa33ffa..8d1209626 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs @@ -64,14 +64,14 @@ impl ModuleScope { /// running name resolution. #[derive(Debug, Default, PartialEq, Eq)] pub struct InputModuleItems { - items: Vec, + pub(crate) items: Vec, imports: Vec, } #[derive(Debug, PartialEq, Eq)] -struct ModuleItem { - id: SourceItemId, - name: Name, +pub(crate) struct ModuleItem { + pub(crate) id: SourceItemId, + pub(crate) name: Name, kind: SyntaxKind, vis: Vis, } diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 24490d119..85bd84469 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -8,8 +8,8 @@ use ra_db::{FileId, FilePosition, Cancelable}; use ra_editor::find_node_at_offset; use ra_syntax::{ + SmolStr, TextRange, SyntaxNodeRef, ast::{self, AstNode, NameOwner}, - SyntaxNodeRef, }; use crate::{ @@ -126,3 +126,40 @@ pub fn function_from_child_node( let fn_def = ctry!(node.ancestors().find_map(ast::FnDef::cast)); function_from_source(db, file_id, fn_def) } + +pub fn macro_symbols( + db: &impl HirDatabase, + file_id: FileId, +) -> Cancelable> { + let module = match module_from_file_id(db, file_id)? { + Some(it) => it, + None => return Ok(Vec::new()), + }; + let items = db.input_module_items(module.source_root_id, module.module_id)?; + let mut res = Vec::new(); + + for macro_call_id in items + .items + .iter() + .filter_map(|it| it.id.file_id.as_macro_call_id()) + { + if let Some(exp) = db.expand_macro_invocation(macro_call_id) { + let loc = macro_call_id.loc(db); + let syntax = db.file_item(loc.source_item_id); + let syntax = syntax.borrowed(); + let macro_call = ast::MacroCall::cast(syntax).unwrap(); + let off = macro_call.token_tree().unwrap().syntax().range().start(); + let file = exp.file(); + for trait_def in file.syntax().descendants().filter_map(ast::TraitDef::cast) { + if let Some(name) = trait_def.name() { + let dst_range = name.syntax().range(); + if let Some(src_range) = exp.map_range_back(dst_range) { + res.push((name.text(), src_range + off)) + } + } + } + } + } + + Ok(res) +} -- cgit v1.2.3