Skip to content

Commit f8c97d7

Browse files
IvarWithoutBonesjoriskleiber
authored andcommitted
feature: add the "Recursively add derive" assist
This introduces a new assist that adds the derive attributes from a struct or enum to the types of its fields, recursively. It is recursive in the sense that the fields of field types are also considered. The derive attributes will be copied over if the field type meets the following criteria: * It resides within the same workspace as the "top-level" type, to avoid editing the crate registry. * It does not already have the same derive attribute. * It does not have a manual implementation of the trait. The last criteria is a bit of a guess since it isn't possible to know what trait implementation(s) a derive macro generates. If a trait has the same name as the derive macro and the top-level type (which already has the derive attribute) implements it, it'll be used to check for manual impls. This seems to work well in practice.
1 parent 2393637 commit f8c97d7

File tree

6 files changed

+1209
-0
lines changed

6 files changed

+1209
-0
lines changed

crates/hir/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,20 @@ impl ModuleDef {
450450
acc
451451
}
452452

453+
pub fn as_trait(self) -> Option<Trait> {
454+
match self {
455+
ModuleDef::Trait(it) => Some(it),
456+
_ => None,
457+
}
458+
}
459+
460+
pub fn as_macro(self) -> Option<Macro> {
461+
match self {
462+
ModuleDef::Macro(it) => Some(it),
463+
_ => None,
464+
}
465+
}
466+
453467
pub fn as_def_with_body(self) -> Option<DefWithBody> {
454468
match self {
455469
ModuleDef::Function(it) => Some(it.into()),
@@ -3343,6 +3357,13 @@ impl ItemInNs {
33433357
}
33443358
}
33453359

3360+
pub fn as_macro(self) -> Option<Macro> {
3361+
match self {
3362+
ItemInNs::Types(_) | ItemInNs::Values(_) => None,
3363+
ItemInNs::Macros(id) => Some(id),
3364+
}
3365+
}
3366+
33463367
/// Returns the crate defining this item (or `None` if `self` is built-in).
33473368
pub fn krate(&self, db: &dyn HirDatabase) -> Option<Crate> {
33483369
match self {

crates/hir/src/semantics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ impl PathResolution {
9999
PathResolution::SelfType(impl_def) => Some(TypeNs::SelfType((*impl_def).into())),
100100
}
101101
}
102+
103+
pub fn as_module_def(&self) -> Option<ModuleDef> {
104+
match self {
105+
PathResolution::Def(it) => Some(*it),
106+
_ => None,
107+
}
108+
}
102109
}
103110

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

0 commit comments

Comments
 (0)