@@ -58,13 +58,11 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
5858 let function_builder = FunctionBuilder :: from_call ( & ctx, & call, & path, target_module) ?;
5959
6060 let target = call. syntax ( ) . text_range ( ) ;
61- // TODO: assert here?
6261 ctx. add_assist ( AssistId ( "add_function" ) , "Add function" , target, |edit| {
63- if let Some ( function_template) = function_builder. render ( ) {
64- edit. set_file ( function_template. file ) ;
65- edit. set_cursor ( function_template. cursor_offset ) ;
66- edit. insert ( function_template. insert_offset , function_template. fn_def . to_string ( ) ) ;
67- }
62+ let function_template = function_builder. render ( ) ;
63+ edit. set_file ( function_template. file ) ;
64+ edit. set_cursor ( function_template. cursor_offset ) ;
65+ edit. insert ( function_template. insert_offset , function_template. fn_def . to_string ( ) ) ;
6866 } )
6967}
7068
@@ -107,7 +105,7 @@ impl FunctionBuilder {
107105 Some ( Self { target, fn_name, type_params, params, file, needs_pub } )
108106 }
109107
110- fn render ( self ) -> Option < FunctionTemplate > {
108+ fn render ( self ) -> FunctionTemplate {
111109 let placeholder_expr = ast:: make:: expr_todo ( ) ;
112110 let fn_body = ast:: make:: block_expr ( vec ! [ ] , Some ( placeholder_expr) ) ;
113111 let mut fn_def = ast:: make:: fn_def ( self . fn_name , self . type_params , self . params , fn_body) ;
@@ -133,15 +131,11 @@ impl FunctionBuilder {
133131 }
134132 } ;
135133
136- let cursor_offset_from_fn_start = fn_def
137- . syntax ( )
138- . descendants ( )
139- . find_map ( ast:: MacroCall :: cast) ?
140- . syntax ( )
141- . text_range ( )
142- . start ( ) ;
134+ let placeholder_expr =
135+ fn_def. syntax ( ) . descendants ( ) . find_map ( ast:: MacroCall :: cast) . unwrap ( ) ;
136+ let cursor_offset_from_fn_start = placeholder_expr. syntax ( ) . text_range ( ) . start ( ) ;
143137 let cursor_offset = insert_offset + cursor_offset_from_fn_start;
144- Some ( FunctionTemplate { insert_offset, cursor_offset, fn_def, file : self . file } )
138+ FunctionTemplate { insert_offset, cursor_offset, fn_def, file : self . file }
145139 }
146140}
147141
0 commit comments