Skip to content

Commit 0636e7c

Browse files
committed
Auto merge of rust-lang#17059 - Kohei316:refactor-generate-function, r=Veykril
internal: make function builder create ast directly I am working on rust-lang#17050. In the process, I noticed a place in the code that could be refactored. Currently, the `function builder` creates the `ast` through the `function template` , but those two processes can be combined into one function. I thought I should work on this first and created a PR.
2 parents 07ae540 + 145078e commit 0636e7c

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

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

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ fn add_func_to_accumulator(
175175
edit.edit_file(file);
176176

177177
let target = function_builder.target.clone();
178-
let function_template = function_builder.render();
179-
let func = function_template.to_ast(ctx.config.snippet_cap, edit);
178+
let func = function_builder.render(ctx.config.snippet_cap, edit);
180179

181180
if let Some(name) = adt_name {
182181
let name = make::ty_path(make::ext::ident_path(&format!("{}", name.display(ctx.db()))));
@@ -205,37 +204,6 @@ fn get_adt_source(
205204
find_struct_impl(ctx, &adt_source, &[fn_name.to_owned()]).map(|impl_| (impl_, range.file_id))
206205
}
207206

208-
struct FunctionTemplate {
209-
fn_def: ast::Fn,
210-
ret_type: Option<ast::RetType>,
211-
should_focus_return_type: bool,
212-
tail_expr: ast::Expr,
213-
}
214-
215-
impl FunctionTemplate {
216-
fn to_ast(&self, cap: Option<SnippetCap>, edit: &mut SourceChangeBuilder) -> ast::Fn {
217-
let Self { fn_def, ret_type, should_focus_return_type, tail_expr } = self;
218-
219-
if let Some(cap) = cap {
220-
if *should_focus_return_type {
221-
// Focus the return type if there is one
222-
match ret_type {
223-
Some(ret_type) => {
224-
edit.add_placeholder_snippet(cap, ret_type.clone());
225-
}
226-
None => {
227-
edit.add_placeholder_snippet(cap, tail_expr.clone());
228-
}
229-
}
230-
} else {
231-
edit.add_placeholder_snippet(cap, tail_expr.clone());
232-
}
233-
}
234-
235-
fn_def.clone()
236-
}
237-
}
238-
239207
struct FunctionBuilder {
240208
target: GeneratedFunctionTarget,
241209
fn_name: ast::Name,
@@ -339,7 +307,7 @@ impl FunctionBuilder {
339307
})
340308
}
341309

342-
fn render(self) -> FunctionTemplate {
310+
fn render(self, cap: Option<SnippetCap>, edit: &mut SourceChangeBuilder) -> ast::Fn {
343311
let placeholder_expr = make::ext::expr_todo();
344312
let fn_body = make::block_expr(vec![], Some(placeholder_expr));
345313
let visibility = match self.visibility {
@@ -361,17 +329,31 @@ impl FunctionBuilder {
361329
)
362330
.clone_for_update();
363331

364-
FunctionTemplate {
365-
ret_type: fn_def.ret_type(),
366-
// PANIC: we guarantee we always create a function body with a tail expr
367-
tail_expr: fn_def
368-
.body()
369-
.expect("generated function should have a body")
370-
.tail_expr()
371-
.expect("function body should have a tail expression"),
372-
should_focus_return_type: self.should_focus_return_type,
373-
fn_def,
332+
let ret_type = fn_def.ret_type();
333+
// PANIC: we guarantee we always create a function body with a tail expr
334+
let tail_expr = fn_def
335+
.body()
336+
.expect("generated function should have a body")
337+
.tail_expr()
338+
.expect("function body should have a tail expression");
339+
340+
if let Some(cap) = cap {
341+
if self.should_focus_return_type {
342+
// Focus the return type if there is one
343+
match ret_type {
344+
Some(ret_type) => {
345+
edit.add_placeholder_snippet(cap, ret_type.clone());
346+
}
347+
None => {
348+
edit.add_placeholder_snippet(cap, tail_expr.clone());
349+
}
350+
}
351+
} else {
352+
edit.add_placeholder_snippet(cap, tail_expr.clone());
353+
}
374354
}
355+
356+
fn_def
375357
}
376358
}
377359

0 commit comments

Comments
 (0)