1208: [WIP] Goto for Macro's r=matklad a=Lapz

Adds goto definition for macros. Currently only works for macros in the current crate ~~otherwise it panics~~. Proper macro resolution needs to be added for it to resolve macros in other crates.

Todo
- [X] Allow goto from macro calls
- [X] Fix panics
- [x] Add tests



![Screen Recording 2019-04-25 at 18 00 24](https://user-images.githubusercontent.com/19998186/56754499-1dd01c00-6785-11e9-9e9a-1e36de70cfa3.gif)



Co-authored-by: Lenard Pratt <l3np27@gmail.com>
This commit is contained in:
bors[bot] 2019-05-04 18:38:10 +00:00
commit aa7bdfd37f
9 changed files with 97 additions and 6 deletions

View file

@ -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<ast::MacroCall>) {
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<Documentation> {
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<MacroByExampleDef> {
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,