@@ -100,31 +100,7 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext) -> Opt
100
100
ast:: NameLike :: NameRef ( name_ref) => Some ( name_ref) ,
101
101
_ => None ,
102
102
} ) ;
103
- let call_infos = name_refs. filter_map ( |name_ref| {
104
- let parent = name_ref. syntax ( ) . parent ( ) ?;
105
- if let Some ( call) = ast:: MethodCallExpr :: cast ( parent. clone ( ) ) {
106
- let receiver = call. receiver ( ) ?;
107
- let mut arguments = vec ! [ receiver] ;
108
- arguments. extend ( call. arg_list ( ) ?. args ( ) ) ;
109
- Some ( CallInfo {
110
- generic_arg_list : call. generic_arg_list ( ) ,
111
- node : CallExprNode :: MethodCallExpr ( call) ,
112
- arguments,
113
- } )
114
- } else if let Some ( segment) = ast:: PathSegment :: cast ( parent) {
115
- let path = segment. syntax ( ) . parent ( ) . and_then ( ast:: Path :: cast) ?;
116
- let path = path. syntax ( ) . parent ( ) . and_then ( ast:: PathExpr :: cast) ?;
117
- let call = path. syntax ( ) . parent ( ) . and_then ( ast:: CallExpr :: cast) ?;
118
-
119
- Some ( CallInfo {
120
- arguments : call. arg_list ( ) ?. args ( ) . collect ( ) ,
121
- node : CallExprNode :: Call ( call) ,
122
- generic_arg_list : segment. generic_arg_list ( ) ,
123
- } )
124
- } else {
125
- None
126
- }
127
- } ) ;
103
+ let call_infos = name_refs. filter_map ( CallInfo :: from_name_ref) ;
128
104
let replaced = call_infos
129
105
. map ( |call_info| {
130
106
let replacement =
@@ -178,43 +154,25 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext) -> Opt
178
154
// }
179
155
// ```
180
156
pub ( crate ) fn inline_call ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
181
- let ( label, function, call_info) =
182
- if let Some ( path_expr) = ctx. find_node_at_offset :: < ast:: PathExpr > ( ) {
183
- // FIXME make applicable only on nameref
184
- let call = path_expr. syntax ( ) . parent ( ) . and_then ( ast:: CallExpr :: cast) ?;
185
- let path = path_expr. path ( ) ?;
186
-
157
+ let name_ref: ast:: NameRef = ctx. find_node_at_offset ( ) ?;
158
+ let call_info = CallInfo :: from_name_ref ( name_ref. clone ( ) ) ?;
159
+ let ( function, label) = match & call_info. node {
160
+ CallExprNode :: Call ( call) => {
161
+ let path = match call. expr ( ) ? {
162
+ ast:: Expr :: PathExpr ( path) => path. path ( ) ,
163
+ _ => None ,
164
+ } ?;
187
165
let function = match ctx. sema . resolve_path ( & path) ? {
188
- PathResolution :: Def ( hir:: ModuleDef :: Function ( f) )
189
- | PathResolution :: AssocItem ( hir:: AssocItem :: Function ( f) ) => f,
166
+ PathResolution :: Def ( hir:: ModuleDef :: Function ( f) ) => f ,
167
+ PathResolution :: AssocItem ( hir:: AssocItem :: Function ( f) ) => f,
190
168
_ => return None ,
191
169
} ;
192
- (
193
- format ! ( "Inline `{}`" , path) ,
194
- function,
195
- CallInfo {
196
- arguments : call. arg_list ( ) ?. args ( ) . collect ( ) ,
197
- node : CallExprNode :: Call ( call) ,
198
- generic_arg_list : path. segment ( ) . and_then ( |it| it. generic_arg_list ( ) ) ,
199
- } ,
200
- )
201
- } else {
202
- let name_ref: ast:: NameRef = ctx. find_node_at_offset ( ) ?;
203
- let call = name_ref. syntax ( ) . parent ( ) . and_then ( ast:: MethodCallExpr :: cast) ?;
204
- let receiver = call. receiver ( ) ?;
205
- let function = ctx. sema . resolve_method_call ( & call) ?;
206
- let mut arguments = vec ! [ receiver] ;
207
- arguments. extend ( call. arg_list ( ) ?. args ( ) ) ;
208
- (
209
- format ! ( "Inline `{}`" , name_ref) ,
210
- function,
211
- CallInfo {
212
- generic_arg_list : call. generic_arg_list ( ) ,
213
- node : CallExprNode :: MethodCallExpr ( call) ,
214
- arguments,
215
- } ,
216
- )
217
- } ;
170
+ ( function, format ! ( "Inline `{}`" , path) )
171
+ }
172
+ CallExprNode :: MethodCallExpr ( call) => {
173
+ ( ctx. sema . resolve_method_call ( call) ?, format ! ( "Inline `{}`" , name_ref) )
174
+ }
175
+ } ;
218
176
219
177
let fn_source = function. source ( ctx. db ( ) ) ?;
220
178
let fn_body = fn_source. value . body ( ) ?;
@@ -273,6 +231,34 @@ struct CallInfo {
273
231
generic_arg_list : Option < ast:: GenericArgList > ,
274
232
}
275
233
234
+ impl CallInfo {
235
+ fn from_name_ref ( name_ref : ast:: NameRef ) -> Option < CallInfo > {
236
+ let parent = name_ref. syntax ( ) . parent ( ) ?;
237
+ if let Some ( call) = ast:: MethodCallExpr :: cast ( parent. clone ( ) ) {
238
+ let receiver = call. receiver ( ) ?;
239
+ let mut arguments = vec ! [ receiver] ;
240
+ arguments. extend ( call. arg_list ( ) ?. args ( ) ) ;
241
+ Some ( CallInfo {
242
+ generic_arg_list : call. generic_arg_list ( ) ,
243
+ node : CallExprNode :: MethodCallExpr ( call) ,
244
+ arguments,
245
+ } )
246
+ } else if let Some ( segment) = ast:: PathSegment :: cast ( parent) {
247
+ let path = segment. syntax ( ) . parent ( ) . and_then ( ast:: Path :: cast) ?;
248
+ let path = path. syntax ( ) . parent ( ) . and_then ( ast:: PathExpr :: cast) ?;
249
+ let call = path. syntax ( ) . parent ( ) . and_then ( ast:: CallExpr :: cast) ?;
250
+
251
+ Some ( CallInfo {
252
+ arguments : call. arg_list ( ) ?. args ( ) . collect ( ) ,
253
+ node : CallExprNode :: Call ( call) ,
254
+ generic_arg_list : segment. generic_arg_list ( ) ,
255
+ } )
256
+ } else {
257
+ None
258
+ }
259
+ }
260
+ }
261
+
276
262
fn get_fn_params (
277
263
db : & dyn HirDatabase ,
278
264
function : hir:: Function ,
@@ -860,7 +846,7 @@ fn foo<T, const N: usize>() {
860
846
fn bar<U, const M: usize>() {}
861
847
862
848
fn main() {
863
- foo::<usize, {0}>$0 ();
849
+ foo$0 ::<usize, {0}>();
864
850
}
865
851
"# ,
866
852
r#"
0 commit comments