Skip to content

Commit d7be1a4

Browse files
committed
Use attr location for builtin macro goto-imp
1 parent 86d2af9 commit d7be1a4

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ use hir_ty::{
2626
};
2727
use ra_db::{CrateId, Edition, FileId};
2828
use ra_prof::profile;
29-
use ra_syntax::ast;
29+
use ra_syntax::ast::{self, AttrsOwner};
3030

3131
use crate::{
3232
db::{DefDatabase, HirDatabase},
33+
has_source::HasSource,
3334
CallableDef, HirDisplay, InFile, Name,
3435
};
3536

@@ -805,6 +806,27 @@ impl ImplBlock {
805806
pub fn krate(&self, db: &impl DefDatabase) -> Crate {
806807
Crate { id: self.module(db).id.krate }
807808
}
809+
810+
pub fn is_builtin_derive(&self, db: &impl DefDatabase) -> Option<InFile<ast::Attr>> {
811+
let src = self.source(db);
812+
let item = src.file_id.is_builtin_derive(db)?;
813+
let hygenic = hir_expand::hygiene::Hygiene::new(db, item.file_id);
814+
815+
let attr = item
816+
.value
817+
.attrs()
818+
.filter_map(|it| {
819+
let path = hir_def::path::ModPath::from_src(it.path()?, &hygenic)?;
820+
if path.as_ident()?.to_string() == "derive" {
821+
Some(it)
822+
} else {
823+
None
824+
}
825+
})
826+
.last()?;
827+
828+
Some(item.with_value(attr))
829+
}
808830
}
809831

810832
#[derive(Clone, PartialEq, Eq, Debug)]

crates/ra_hir_expand/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ impl HirFileId {
112112
}
113113
}
114114
}
115+
116+
/// Indicate it is macro file generated for builtin derive
117+
pub fn is_builtin_derive(&self, db: &dyn db::AstDatabase) -> Option<InFile<ast::ModuleItem>> {
118+
match self.0 {
119+
HirFileIdRepr::FileId(_) => None,
120+
HirFileIdRepr::MacroFile(macro_file) => {
121+
let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id);
122+
let item = match loc.def.kind {
123+
MacroDefKind::BuiltInDerive(_) => loc.kind.node(db),
124+
_ => return None,
125+
};
126+
Some(item.with_value(ast::ModuleItem::cast(item.value.clone())?))
127+
}
128+
}
129+
}
115130
}
116131

117132
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

crates/ra_ide/src/display/navigation_target.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,11 @@ impl ToNav for hir::Module {
251251
impl ToNav for hir::ImplBlock {
252252
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
253253
let src = self.source(db);
254-
let frange = original_range(db, src.as_ref().map(|it| it.syntax()));
254+
let frange = if let Some(item) = self.is_builtin_derive(db) {
255+
original_range(db, item.syntax())
256+
} else {
257+
original_range(db, src.as_ref().map(|it| it.syntax()))
258+
};
255259

256260
NavigationTarget::from_syntax(
257261
frange.file_id,

0 commit comments

Comments
 (0)