aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/render/builder_ext.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/completion/src/render/builder_ext.rs')
-rw-r--r--crates/completion/src/render/builder_ext.rs94
1 files changed, 0 insertions, 94 deletions
diff --git a/crates/completion/src/render/builder_ext.rs b/crates/completion/src/render/builder_ext.rs
deleted file mode 100644
index d053a988b..000000000
--- a/crates/completion/src/render/builder_ext.rs
+++ /dev/null
@@ -1,94 +0,0 @@
1//! Extensions for `Builder` structure required for item rendering.
2
3use itertools::Itertools;
4use test_utils::mark;
5
6use crate::{item::Builder, CompletionContext};
7
8#[derive(Debug)]
9pub(super) enum Params {
10 Named(Vec<String>),
11 Anonymous(usize),
12}
13
14impl Params {
15 pub(super) fn len(&self) -> usize {
16 match self {
17 Params::Named(xs) => xs.len(),
18 Params::Anonymous(len) => *len,
19 }
20 }
21
22 pub(super) fn is_empty(&self) -> bool {
23 self.len() == 0
24 }
25}
26
27impl Builder {
28 fn should_add_parens(&self, ctx: &CompletionContext) -> bool {
29 if !ctx.config.add_call_parenthesis {
30 return false;
31 }
32 if ctx.use_item_syntax.is_some() {
33 mark::hit!(no_parens_in_use_item);
34 return false;
35 }
36 if ctx.is_pattern_call {
37 return false;
38 }
39 if ctx.is_call {
40 return false;
41 }
42
43 // Don't add parentheses if the expected type is some function reference.
44 if let Some(ty) = &ctx.expected_type {
45 if ty.is_fn() {
46 mark::hit!(no_call_parens_if_fn_ptr_needed);
47 return false;
48 }
49 }
50
51 // Nothing prevents us from adding parentheses
52 true
53 }
54
55 pub(super) fn add_call_parens(
56 mut self,
57 ctx: &CompletionContext,
58 name: String,
59 params: Params,
60 ) -> Builder {
61 if !self.should_add_parens(ctx) {
62 return self;
63 }
64
65 let cap = match ctx.config.snippet_cap {
66 Some(it) => it,
67 None => return self,
68 };
69 // If not an import, add parenthesis automatically.
70 mark::hit!(inserts_parens_for_function_calls);
71
72 let (snippet, label) = if params.is_empty() {
73 (format!("{}()$0", name), format!("{}()", name))
74 } else {
75 self = self.trigger_call_info();
76 let snippet = match (ctx.config.add_call_argument_snippets, params) {
77 (true, Params::Named(params)) => {
78 let function_params_snippet =
79 params.iter().enumerate().format_with(", ", |(index, param_name), f| {
80 f(&format_args!("${{{}:{}}}", index + 1, param_name))
81 });
82 format!("{}({})$0", name, function_params_snippet)
83 }
84 _ => {
85 mark::hit!(suppress_arg_snippets);
86 format!("{}($0)", name)
87 }
88 };
89
90 (snippet, format!("{}(…)", name))
91 };
92 self.lookup_by(name).label(label).insert_snippet(cap, snippet)
93 }
94}