Skip to content

Commit

Permalink
Version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
alibektas committed Jun 17, 2023
1 parent 18024ec commit 4897297
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions crates/ide-assists/src/handlers/generate_delegate_trait.rs
Expand Up @@ -137,20 +137,18 @@ impl Field {

if let Some(tp) = type_param {
for tb in tp.trait_bounds(db) {
impls.push(Delegee::Bound(BoundCase { 0: tb }));
impls.push(Delegee::Bound(BoundCase(tb)));
}
};

for imp in type_impls {
match imp.trait_(db) {
Some(tr) => {
if tr.is_visible_from(db, module) {
impls.push(Delegee::Impls(ImplCase { 0: tr, 1: imp }))
impls.push(Delegee::Impls(ImplCase(tr, imp)))
}
}
None => {
continue;
}
None => (),
}
}

Expand All @@ -171,32 +169,19 @@ enum Delegee {
struct BoundCase(hir::Trait);
struct ImplCase(hir::Trait, hir::Impl);

/// When we list traits we can implement for the enclosing struct
/// we use the absolute path of a trait. This trait consists of a single
/// method that produces this path.
trait Signatured {
fn signature(&self, db: &dyn HirDatabase) -> String;
}

impl Signatured for Delegee {
impl Delegee {
fn signature(&self, db: &dyn HirDatabase) -> String {
let mut s = String::new();
let trait_: hir::Trait;

match self {
Delegee::Bound(b) => trait_ = b.0,
Delegee::Impls(i) => trait_ = i.0,
};
let (Delegee::Bound(BoundCase(it)) | Delegee::Impls(ImplCase(it, _))) = self;

for m in trait_.module(db).path_to_root(db).iter().rev() {
for m in it.module(db).path_to_root(db).iter().rev() {
if let Some(name) = m.name(db) {
s.push_str(&format!("{}::", name.to_smol_str()));
} else {
continue;
}
}

s.push_str(&trait_.name(db).to_smol_str());
s.push_str(&it.name(db).to_smol_str());
s
}
}
Expand Down Expand Up @@ -231,7 +216,7 @@ impl Struct {
let delegate = generate_impl(ctx, self, &field.ty, &field.name, delegee);

acc.add_group(
&GroupLabel("Generate delegate traits...".to_owned()),
&GroupLabel("Delegate trait impl for field...".to_owned()),
AssistId("generate_delegate_trait", ide_db::assists::AssistKind::Generate),
format!("Generate delegate impl `{}` for `{}`", signature, field.name),
field.range,
Expand Down

0 comments on commit 4897297

Please sign in to comment.