@@ -7,7 +7,8 @@ use test_utils::tested_by;
7
7
8
8
use crate :: {
9
9
completion:: {
10
- CompletionContext , CompletionItem , CompletionItemKind , CompletionKind , Completions ,
10
+ completion_item:: Builder , CompletionContext , CompletionItem , CompletionItemKind ,
11
+ CompletionKind , Completions ,
11
12
} ,
12
13
display:: { const_label, macro_label, type_label, FunctionSignature } ,
13
14
RootDatabase ,
@@ -193,7 +194,6 @@ impl Completions {
193
194
func : hir:: Function ,
194
195
) {
195
196
let has_self_param = func. has_self_param ( ctx. db ) ;
196
- let params = func. params ( ctx. db ) ;
197
197
198
198
let name = name. unwrap_or_else ( || func. name ( ctx. db ) . to_string ( ) ) ;
199
199
let ast_node = func. source ( ctx. db ) . value ;
@@ -210,32 +210,14 @@ impl Completions {
210
210
. set_deprecated ( is_deprecated ( func, ctx. db ) )
211
211
. detail ( function_signature. to_string ( ) ) ;
212
212
213
- // If not an import, add parenthesis automatically.
214
- if ctx. use_item_syntax . is_none ( ) && !ctx. is_call && ctx. config . add_call_parenthesis {
215
- tested_by ! ( inserts_parens_for_function_calls) ;
213
+ let params = function_signature
214
+ . parameter_names
215
+ . iter ( )
216
+ . skip ( if function_signature. has_self_param { 1 } else { 0 } )
217
+ . cloned ( )
218
+ . collect ( ) ;
216
219
217
- let ( snippet, label) = if params. is_empty ( ) || has_self_param && params. len ( ) == 1 {
218
- ( format ! ( "{}()$0" , name) , format ! ( "{}()" , name) )
219
- } else {
220
- builder = builder. trigger_call_info ( ) ;
221
- let snippet = if ctx. config . add_call_argument_snippets {
222
- let to_skip = if has_self_param { 1 } else { 0 } ;
223
- let function_params_snippet = function_signature
224
- . parameter_names
225
- . iter ( )
226
- . skip ( to_skip)
227
- . enumerate ( )
228
- . map ( |( index, param_name) | format ! ( "${{{}:{}}}" , index + 1 , param_name) )
229
- . sep_by ( ", " ) ;
230
- format ! ( "{}({})$0" , name, function_params_snippet)
231
- } else {
232
- format ! ( "{}($0)" , name)
233
- } ;
234
-
235
- ( snippet, format ! ( "{}(…)" , name) )
236
- } ;
237
- builder = builder. lookup_by ( name) . label ( label) . insert_snippet ( snippet) ;
238
- }
220
+ builder = builder. add_call_parens ( ctx, name, params) ;
239
221
240
222
self . add ( builder)
241
223
}
@@ -300,6 +282,43 @@ impl Completions {
300
282
}
301
283
}
302
284
285
+ impl Builder {
286
+ fn add_call_parens (
287
+ mut self ,
288
+ ctx : & CompletionContext ,
289
+ name : String ,
290
+ params : Vec < String > ,
291
+ ) -> Builder {
292
+ if !ctx. config . add_call_parenthesis {
293
+ return self ;
294
+ }
295
+ if ctx. use_item_syntax . is_some ( ) || ctx. is_call {
296
+ return self ;
297
+ }
298
+ // If not an import, add parenthesis automatically.
299
+ tested_by ! ( inserts_parens_for_function_calls) ;
300
+
301
+ let ( snippet, label) = if params. is_empty ( ) {
302
+ ( format ! ( "{}()$0" , name) , format ! ( "{}()" , name) )
303
+ } else {
304
+ self = self . trigger_call_info ( ) ;
305
+ let snippet = if ctx. config . add_call_argument_snippets {
306
+ let function_params_snippet = params
307
+ . iter ( )
308
+ . enumerate ( )
309
+ . map ( |( index, param_name) | format ! ( "${{{}:{}}}" , index + 1 , param_name) )
310
+ . sep_by ( ", " ) ;
311
+ format ! ( "{}({})$0" , name, function_params_snippet)
312
+ } else {
313
+ format ! ( "{}($0)" , name)
314
+ } ;
315
+
316
+ ( snippet, format ! ( "{}(…)" , name) )
317
+ } ;
318
+ self . lookup_by ( name) . label ( label) . insert_snippet ( snippet)
319
+ }
320
+ }
321
+
303
322
fn is_deprecated ( node : impl HasAttrs , db : & RootDatabase ) -> bool {
304
323
node. attrs ( db) . by_key ( "deprecated" ) . exists ( )
305
324
}
0 commit comments