From 8198e13c26fe985af5893af7bdac04041880b461 Mon Sep 17 00:00:00 2001 From: Lenard Pratt Date: Wed, 24 Apr 2019 21:16:50 +0100 Subject: Added local macro goto --- crates/ra_hir/src/lib.rs | 4 ++-- crates/ra_hir/src/source_binder.rs | 43 +++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 4411715de..03b1063b6 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -69,7 +69,7 @@ pub use self::{ expr::ExprScopes, resolve::Resolution, generics::{GenericParams, GenericParam, HasGenericParams}, - source_binder::{SourceAnalyzer, PathResolution, ScopeEntryWithSyntax}, + source_binder::{SourceAnalyzer, PathResolution, ScopeEntryWithSyntax,MacroByExampleDef}, }; pub use self::code_model_api::{ @@ -80,5 +80,5 @@ pub use self::code_model_api::{ Function, FnSignature, StructField, FieldSource, Static, Const, ConstSignature, - Trait, TypeAlias, Container, + Trait, TypeAlias, Container }; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 2959e3eca..06d99351e 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -10,7 +10,7 @@ use std::sync::Arc; use rustc_hash::{FxHashSet, FxHashMap}; use ra_db::{FileId, FilePosition}; use ra_syntax::{ - SyntaxNode, AstPtr, TextUnit, SyntaxNodePtr, TextRange, + SyntaxNode, AstPtr, TextUnit, SyntaxNodePtr, TextRange,TreeArc, ast::{self, AstNode, NameOwner}, algo::find_node_at_offset, SyntaxKind::*, @@ -18,9 +18,10 @@ use ra_syntax::{ use crate::{ HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, PerNs, Name, - AsName, Module, HirFileId, Crate, Trait, Resolver, Ty, + AsName, Module, HirFileId, Crate, Trait, Resolver, Ty,Path, expr::{BodySourceMap, scope::{ScopeId, ExprScopes}}, - ids::LocationCtx, + ids::{LocationCtx,MacroCallId}, + docs::{docs_from_ast,Documentation}, expr, AstId, }; @@ -184,9 +185,28 @@ pub enum PathResolution { /// A generic parameter GenericParam(u32), SelfType(crate::ImplBlock), + Macro(MacroByExampleDef), AssocItem(crate::ImplItem), } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct MacroByExampleDef { + pub(crate) id: MacroCallId, +} + +impl MacroByExampleDef { + pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { + let loc = self.id.loc(db); + (self.id.into(), loc.def.0.to_node(db)) + } +} + +impl crate::Docs for MacroByExampleDef { + fn docs(&self, db: &impl HirDatabase) -> Option { + docs_from_ast(&*self.source(db).1) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct ScopeEntryWithSyntax { pub(crate) name: Name, @@ -264,6 +284,23 @@ impl SourceAnalyzer { self.infer.as_ref()?.field_resolution(expr_id) } + pub fn resolve_macro_call( + &self, + db: &impl HirDatabase, + file_id: FileId, + macro_call: &ast::MacroCall, + ) -> Option { + let hir_id = file_id.into(); + let ast_id = db.ast_id_map(hir_id).ast_id(macro_call).with_file_id(hir_id); + let call_id = self.resolver.resolve_macro_call( + db, + macro_call.path().and_then(Path::from_ast), + ast_id, + ); + + call_id.map(|id| MacroByExampleDef { id }) + } + pub fn resolve_hir_path( &self, db: &impl HirDatabase, -- cgit v1.2.3