@@ -58,13 +58,11 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
58
58
let function_builder = FunctionBuilder :: from_call ( & ctx, & call, & path, target_module) ?;
59
59
60
60
let target = call. syntax ( ) . text_range ( ) ;
61
- // TODO: assert here?
62
61
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 ( ) ) ;
68
66
} )
69
67
}
70
68
@@ -107,7 +105,7 @@ impl FunctionBuilder {
107
105
Some ( Self { target, fn_name, type_params, params, file, needs_pub } )
108
106
}
109
107
110
- fn render ( self ) -> Option < FunctionTemplate > {
108
+ fn render ( self ) -> FunctionTemplate {
111
109
let placeholder_expr = ast:: make:: expr_todo ( ) ;
112
110
let fn_body = ast:: make:: block_expr ( vec ! [ ] , Some ( placeholder_expr) ) ;
113
111
let mut fn_def = ast:: make:: fn_def ( self . fn_name , self . type_params , self . params , fn_body) ;
@@ -133,15 +131,11 @@ impl FunctionBuilder {
133
131
}
134
132
} ;
135
133
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 ( ) ;
143
137
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 }
145
139
}
146
140
}
147
141
0 commit comments