Skip to content

Commit bd093d1

Browse files
committed
Sort methods in generate_delegate_methods listing
1 parent 3484b5a commit bd093d1

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

crates/ide-assists/src/handlers/generate_delegate_methods.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,39 +72,36 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
7272
let krate = ty.krate(ctx.db());
7373
ty.iterate_assoc_items(ctx.db(), krate, |item| {
7474
if let hir::AssocItem::Function(f) = item {
75+
let name = f.name(ctx.db());
7576
if f.self_param(ctx.db()).is_some()
7677
&& f.is_visible_from(ctx.db(), current_module)
77-
&& seen_names.insert(f.name(ctx.db()))
78+
&& seen_names.insert(name.clone())
7879
{
79-
methods.push(f)
80+
methods.push((name, f))
8081
}
8182
}
8283
Option::<()>::None
8384
});
8485
}
85-
86-
for method in methods {
86+
methods.sort_by(|(a, _), (b, _)| a.cmp(b));
87+
for (name, method) in methods {
8788
let adt = ast::Adt::Struct(strukt.clone());
88-
let name = method.name(ctx.db()).display(ctx.db()).to_string();
89+
let name = name.display(ctx.db()).to_string();
8990
// if `find_struct_impl` returns None, that means that a function named `name` already exists.
90-
let Some(impl_def) = find_struct_impl(ctx, &adt, &[name]) else { continue; };
91+
let Some(impl_def) = find_struct_impl(ctx, &adt, std::slice::from_ref(&name)) else { continue; };
9192
acc.add_group(
9293
&GroupLabel("Generate delegate methods…".to_owned()),
9394
AssistId("generate_delegate_methods", AssistKind::Generate),
94-
format!(
95-
"Generate delegate for `{field_name}.{}()`",
96-
method.name(ctx.db()).display(ctx.db())
97-
),
95+
format!("Generate delegate for `{field_name}.{name}()`",),
9896
target,
9997
|builder| {
10098
// Create the function
10199
let method_source = match method.source(ctx.db()) {
102100
Some(source) => source.value,
103101
None => return,
104102
};
105-
let method_name = method.name(ctx.db());
106103
let vis = method_source.visibility();
107-
let name = make::name(&method.name(ctx.db()).display(ctx.db()).to_string());
104+
let fn_name = make::name(&name);
108105
let params =
109106
method_source.param_list().unwrap_or_else(|| make::param_list(None, []));
110107
let type_params = method_source.generic_param_list();
@@ -114,7 +111,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
114111
};
115112
let tail_expr = make::expr_method_call(
116113
make::ext::field_from_idents(["self", &field_name]).unwrap(), // This unwrap is ok because we have at least 1 arg in the list
117-
make::name_ref(&method_name.display(ctx.db()).to_string()),
114+
make::name_ref(&name),
118115
arg_list,
119116
);
120117
let ret_type = method_source.ret_type();
@@ -126,7 +123,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
126123
let body = make::block_expr([], Some(tail_expr_finished));
127124
let f = make::fn_(
128125
vis,
129-
name,
126+
fn_name,
130127
type_params,
131128
None,
132129
params,

0 commit comments

Comments
 (0)