Skip to content

Commit b1cf95f

Browse files
committed
Generalize call parenthesis insertion
1 parent adbcedd commit b1cf95f

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

crates/ra_ide/src/completion/presentation.rs

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use test_utils::tested_by;
77

88
use crate::{
99
completion::{
10-
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
10+
completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind,
11+
CompletionKind, Completions,
1112
},
1213
display::{const_label, macro_label, type_label, FunctionSignature},
1314
RootDatabase,
@@ -193,7 +194,6 @@ impl Completions {
193194
func: hir::Function,
194195
) {
195196
let has_self_param = func.has_self_param(ctx.db);
196-
let params = func.params(ctx.db);
197197

198198
let name = name.unwrap_or_else(|| func.name(ctx.db).to_string());
199199
let ast_node = func.source(ctx.db).value;
@@ -210,32 +210,14 @@ impl Completions {
210210
.set_deprecated(is_deprecated(func, ctx.db))
211211
.detail(function_signature.to_string());
212212

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();
216219

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);
239221

240222
self.add(builder)
241223
}
@@ -300,6 +282,43 @@ impl Completions {
300282
}
301283
}
302284

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+
303322
fn is_deprecated(node: impl HasAttrs, db: &RootDatabase) -> bool {
304323
node.attrs(db).by_key("deprecated").exists()
305324
}

0 commit comments

Comments
 (0)