mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Implement ToDef for ast::Attr
This commit is contained in:
parent
6b7b09d329
commit
aeb5d64912
2 changed files with 21 additions and 16 deletions
|
@ -476,12 +476,12 @@ impl<'db> SemanticsImpl<'db> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn derive_macro_calls(&self, attr: &ast::Attr) -> Option<Vec<Option<MacroCallId>>> {
|
fn derive_macro_calls(&self, attr: &ast::Attr) -> Option<Vec<Option<MacroCallId>>> {
|
||||||
let item = attr.syntax().parent().and_then(ast::Item::cast)?;
|
let adt = attr.syntax().parent().and_then(ast::Adt::cast)?;
|
||||||
let file_id = self.find_file(item.syntax()).file_id;
|
let file_id = self.find_file(adt.syntax()).file_id;
|
||||||
let item = InFile::new(file_id, &item);
|
let adt = InFile::new(file_id, &adt);
|
||||||
let src = InFile::new(file_id, attr.clone());
|
let src = InFile::new(file_id, attr.clone());
|
||||||
self.with_ctx(|ctx| {
|
self.with_ctx(|ctx| {
|
||||||
let res = ctx.attr_to_derive_macro_call(item, src)?;
|
let res = ctx.attr_to_derive_macro_call(adt, src)?;
|
||||||
Some(res.to_vec())
|
Some(res.to_vec())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -909,17 +909,8 @@ impl<'db> SemanticsImpl<'db> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch hir::Attr definition
|
let attr_def =
|
||||||
// FIXME: Move this to ToDef impl?
|
ast::Attr::to_def(self, self.find_file(attr.syntax()).with_value(attr.clone()))?;
|
||||||
let adt = attr.syntax().parent().and_then(ast::Adt::cast)?;
|
|
||||||
let attr_pos = adt.attrs().position(|it| it == attr)?;
|
|
||||||
let attrs = {
|
|
||||||
let file_id = self.find_file(adt.syntax()).file_id;
|
|
||||||
let adt = InFile::new(file_id, adt);
|
|
||||||
let def = self.with_ctx(|ctx| ctx.adt_to_def(adt))?;
|
|
||||||
self.db.attrs(def.into())
|
|
||||||
};
|
|
||||||
let attr_def = attrs.get(attr_pos)?;
|
|
||||||
|
|
||||||
let mut derive_paths = attr_def.parse_path_comma_token_tree()?;
|
let mut derive_paths = attr_def.parse_path_comma_token_tree()?;
|
||||||
let derives = self.resolve_derive_macro(&attr)?;
|
let derives = self.resolve_derive_macro(&attr)?;
|
||||||
|
@ -1214,6 +1205,7 @@ to_def_impls![
|
||||||
(crate::Local, ast::SelfParam, self_param_to_def),
|
(crate::Local, ast::SelfParam, self_param_to_def),
|
||||||
(crate::Label, ast::Label, label_to_def),
|
(crate::Label, ast::Label, label_to_def),
|
||||||
(crate::Adt, ast::Adt, adt_to_def),
|
(crate::Adt, ast::Adt, adt_to_def),
|
||||||
|
(crate::Attr, ast::Attr, attr_to_def),
|
||||||
];
|
];
|
||||||
|
|
||||||
fn find_root(node: &SyntaxNode) -> SyntaxNode {
|
fn find_root(node: &SyntaxNode) -> SyntaxNode {
|
||||||
|
|
|
@ -210,6 +210,19 @@ impl SourceToDefCtx<'_, '_> {
|
||||||
ast::Adt::Union(it) => self.union_to_def(InFile::new(file_id, it)).map(AdtId::UnionId),
|
ast::Adt::Union(it) => self.union_to_def(InFile::new(file_id, it)).map(AdtId::UnionId),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub(super) fn attr_to_def(
|
||||||
|
&mut self,
|
||||||
|
InFile { file_id, value }: InFile<ast::Attr>,
|
||||||
|
) -> Option<crate::Attr> {
|
||||||
|
// FIXME: Use dynmap?
|
||||||
|
let adt = value.syntax().parent().and_then(ast::Adt::cast)?;
|
||||||
|
let attr_pos = ast::HasAttrs::attrs(&adt).position(|it| it == value)?;
|
||||||
|
let attrs = {
|
||||||
|
let def = self.adt_to_def(InFile::new(file_id, adt))?;
|
||||||
|
self.db.attrs(def.into())
|
||||||
|
};
|
||||||
|
attrs.get(attr_pos).cloned()
|
||||||
|
}
|
||||||
pub(super) fn bind_pat_to_def(
|
pub(super) fn bind_pat_to_def(
|
||||||
&mut self,
|
&mut self,
|
||||||
src: InFile<ast::IdentPat>,
|
src: InFile<ast::IdentPat>,
|
||||||
|
@ -246,7 +259,7 @@ impl SourceToDefCtx<'_, '_> {
|
||||||
|
|
||||||
pub(super) fn attr_to_derive_macro_call(
|
pub(super) fn attr_to_derive_macro_call(
|
||||||
&mut self,
|
&mut self,
|
||||||
item: InFile<&ast::Item>,
|
item: InFile<&ast::Adt>,
|
||||||
src: InFile<ast::Attr>,
|
src: InFile<ast::Attr>,
|
||||||
) -> Option<&[Option<MacroCallId>]> {
|
) -> Option<&[Option<MacroCallId>]> {
|
||||||
let map = self.dyn_map(item)?;
|
let map = self.dyn_map(item)?;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue