mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
simplify
This commit is contained in:
parent
98531dc785
commit
9cba67b2ad
5 changed files with 12 additions and 34 deletions
|
@ -10,7 +10,7 @@ use ra_syntax::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId,
|
Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId, MacroCallLoc,
|
||||||
name::AsName,
|
name::AsName,
|
||||||
type_ref::{Mutability, TypeRef},
|
type_ref::{Mutability, TypeRef},
|
||||||
};
|
};
|
||||||
|
@ -828,7 +828,8 @@ where
|
||||||
.ast_id(e)
|
.ast_id(e)
|
||||||
.with_file_id(self.current_file_id);
|
.with_file_id(self.current_file_id);
|
||||||
|
|
||||||
if let Some(call_id) = self.resolver.resolve_macro_call(self.db, path, ast_id) {
|
if let Some(def) = self.resolver.resolve_macro_call(path) {
|
||||||
|
let call_id = MacroCallLoc { def, ast_id }.id(self.db);
|
||||||
if let Some(tt) = self.db.macro_expand(call_id).ok() {
|
if let Some(tt) = self.db.macro_expand(call_id).ok() {
|
||||||
if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() {
|
if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() {
|
||||||
log::debug!("macro expansion {}", expr.syntax().debug_dump());
|
log::debug!("macro expansion {}", expr.syntax().debug_dump());
|
||||||
|
|
|
@ -272,8 +272,8 @@ impl CrateDefMap {
|
||||||
(res.resolved_def, res.segment_index)
|
(res.resolved_def, res.segment_index)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn find_macro(&self, name: &Name) -> Option<&MacroDefId> {
|
pub(crate) fn find_macro(&self, name: &Name) -> Option<MacroDefId> {
|
||||||
self.public_macros.get(name).or(self.local_macros.get(name))
|
self.public_macros.get(name).or(self.local_macros.get(name)).map(|it| *it)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns Yes if we are sure that additions to `ItemMap` wouldn't change
|
// Returns Yes if we are sure that additions to `ItemMap` wouldn't change
|
||||||
|
|
|
@ -1,16 +1,12 @@
|
||||||
//! Name resolution.
|
//! Name resolution.
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use ra_syntax::ast;
|
|
||||||
|
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ModuleDef, Trait,
|
ModuleDef, Trait,
|
||||||
code_model_api::Crate,
|
code_model_api::Crate,
|
||||||
MacroCallId,
|
MacroDefId,
|
||||||
MacroCallLoc,
|
|
||||||
AstId,
|
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
name::{Name, KnownName},
|
name::{Name, KnownName},
|
||||||
nameres::{PerNs, CrateDefMap, CrateModuleId},
|
nameres::{PerNs, CrateDefMap, CrateModuleId},
|
||||||
|
@ -134,16 +130,9 @@ impl Resolver {
|
||||||
resolution
|
resolution
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn resolve_macro_call(
|
pub(crate) fn resolve_macro_call(&self, path: Option<Path>) -> Option<MacroDefId> {
|
||||||
&self,
|
|
||||||
db: &impl HirDatabase,
|
|
||||||
path: Option<Path>,
|
|
||||||
ast_id: AstId<ast::MacroCall>,
|
|
||||||
) -> Option<MacroCallId> {
|
|
||||||
let name = path.and_then(|path| path.expand_macro_expr()).unwrap_or_else(Name::missing);
|
let name = path.and_then(|path| path.expand_macro_expr()).unwrap_or_else(Name::missing);
|
||||||
let def_id = self.module().and_then(|(module, _)| module.find_macro(&name))?;
|
self.module()?.0.find_macro(&name)
|
||||||
let call_loc = MacroCallLoc { def: *def_id, ast_id }.id(db);
|
|
||||||
Some(call_loc)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the resolved path segments
|
/// Returns the resolved path segments
|
||||||
|
|
|
@ -283,21 +283,9 @@ impl SourceAnalyzer {
|
||||||
self.infer.as_ref()?.field_resolution(expr_id)
|
self.infer.as_ref()?.field_resolution(expr_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resolve_macro_call(
|
pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<MacroByExampleDef> {
|
||||||
&self,
|
let id = self.resolver.resolve_macro_call(macro_call.path().and_then(Path::from_ast))?;
|
||||||
db: &impl HirDatabase,
|
Some(MacroByExampleDef { id })
|
||||||
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,
|
|
||||||
)?;
|
|
||||||
let loc = call_id.loc(db);
|
|
||||||
Some(MacroByExampleDef { id: loc.def })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resolve_hir_path(
|
pub fn resolve_hir_path(
|
||||||
|
|
|
@ -69,7 +69,7 @@ pub(crate) fn reference_definition(
|
||||||
.and_then(ast::MacroCall::cast)
|
.and_then(ast::MacroCall::cast)
|
||||||
{
|
{
|
||||||
tested_by!(goto_definition_works_for_macros);
|
tested_by!(goto_definition_works_for_macros);
|
||||||
if let Some(macro_call) = analyzer.resolve_macro_call(db, file_id, macro_call) {
|
if let Some(macro_call) = analyzer.resolve_macro_call(macro_call) {
|
||||||
return Exact(NavigationTarget::from_macro_def(db, macro_call));
|
return Exact(NavigationTarget::from_macro_def(db, macro_call));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue