Skip to content

Commit 8e8c5a7

Browse files
bors[bot]matklad
andauthored
Merge #3498
3498: Trigger parameter info automatically r=matklad a=matklad See microsoft/vscode#64023 bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents ce7496e + 3ff170d commit 8e8c5a7

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

crates/ra_ide/src/completion/completion_item.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ pub struct CompletionItem {
4747

4848
/// Whether this item is marked as deprecated
4949
deprecated: bool,
50+
51+
/// If completing a function call, ask the editor to show parameter popup
52+
/// after completion.
53+
trigger_call_info: bool,
5054
}
5155

5256
// We use custom debug for CompletionItem to make `insta`'s diffs more readable.
@@ -139,6 +143,7 @@ impl CompletionItem {
139143
kind: None,
140144
text_edit: None,
141145
deprecated: None,
146+
trigger_call_info: None,
142147
}
143148
}
144149
/// What user sees in pop-up in the UI.
@@ -177,6 +182,10 @@ impl CompletionItem {
177182
pub fn deprecated(&self) -> bool {
178183
self.deprecated
179184
}
185+
186+
pub fn trigger_call_info(&self) -> bool {
187+
self.trigger_call_info
188+
}
180189
}
181190

182191
/// A helper to make `CompletionItem`s.
@@ -193,6 +202,7 @@ pub(crate) struct Builder {
193202
kind: Option<CompletionItemKind>,
194203
text_edit: Option<TextEdit>,
195204
deprecated: Option<bool>,
205+
trigger_call_info: Option<bool>,
196206
}
197207

198208
impl Builder {
@@ -221,6 +231,7 @@ impl Builder {
221231
kind: self.kind,
222232
completion_kind: self.completion_kind,
223233
deprecated: self.deprecated.unwrap_or(false),
234+
trigger_call_info: self.trigger_call_info.unwrap_or(false),
224235
}
225236
}
226237
pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder {
@@ -271,6 +282,10 @@ impl Builder {
271282
self.deprecated = Some(deprecated);
272283
self
273284
}
285+
pub(crate) fn trigger_call_info(mut self) -> Builder {
286+
self.trigger_call_info = Some(true);
287+
self
288+
}
274289
}
275290

276291
impl<'a> Into<CompletionItem> for Builder {

crates/ra_ide/src/completion/presentation.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl Completions {
103103
}
104104
};
105105

106-
// If not an import, add parenthesis automatically.
106+
// Add `<>` for generic types
107107
if ctx.is_path_type
108108
&& !ctx.has_type_args
109109
&& ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
@@ -211,7 +211,7 @@ impl Completions {
211211
.set_deprecated(is_deprecated(func, ctx.db))
212212
.detail(function_signature.to_string());
213213

214-
// Add `<>` for generic types
214+
// If not an import, add parenthesis automatically.
215215
if ctx.use_item_syntax.is_none()
216216
&& !ctx.is_call
217217
&& ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
@@ -221,16 +221,26 @@ impl Completions {
221221
let (snippet, label) = if params.is_empty() || has_self_param && params.len() == 1 {
222222
(format!("{}()$0", name), format!("{}()", name))
223223
} else {
224-
let to_skip = if has_self_param { 1 } else { 0 };
225-
let function_params_snippet =
226-
join(
224+
builder = builder.trigger_call_info();
225+
let snippet = if ctx
226+
.db
227+
.feature_flags
228+
.get("completion.insertion.add-argument-sippets")
229+
{
230+
let to_skip = if has_self_param { 1 } else { 0 };
231+
let function_params_snippet = join(
227232
function_signature.parameter_names.iter().skip(to_skip).enumerate().map(
228233
|(index, param_name)| format!("${{{}:{}}}", index + 1, param_name),
229234
),
230235
)
231236
.separator(", ")
232237
.to_string();
233-
(format!("{}({})$0", name, function_params_snippet), format!("{}(…)", name))
238+
format!("{}({})$0", name, function_params_snippet)
239+
} else {
240+
format!("{}($0)", name)
241+
};
242+
243+
(snippet, format!("{}(…)", name))
234244
};
235245
builder = builder.lookup_by(name).label(label).insert_snippet(snippet);
236246
}

crates/ra_ide_db/src/feature_flags.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl Default for FeatureFlags {
5454
FeatureFlags::new(&[
5555
("lsp.diagnostics", true),
5656
("completion.insertion.add-call-parenthesis", true),
57+
("completion.insertion.add-argument-sippets", true),
5758
("completion.enable-postfix", true),
5859
("notifications.workspace-loaded", true),
5960
("notifications.cargo-toml-not-found", true),

crates/rust-analyzer/src/conv.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ impl ConvWith<(&LineIndex, LineEndings)> for CompletionItem {
150150
additional_text_edits: Some(additional_text_edits),
151151
documentation: self.documentation().map(|it| it.conv()),
152152
deprecated: Some(self.deprecated()),
153+
command: if self.trigger_call_info() {
154+
let cmd = lsp_types::Command {
155+
title: "triggerParameterHints".into(),
156+
command: "editor.action.triggerParameterHints".into(),
157+
arguments: None,
158+
};
159+
Some(cmd)
160+
} else {
161+
None
162+
},
153163
..Default::default()
154164
};
155165

editors/code/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@
197197
"type": "boolean",
198198
"description": "Whether to add parenthesis when completing functions"
199199
},
200+
"completion.insertion.add-argument-sippets": {
201+
"type": "boolean",
202+
"description": "Whether to add argument snippets when completing functions"
203+
},
200204
"completion.enable-postfix": {
201205
"type": "boolean",
202206
"description": "Whether to show postfix snippets like `dbg`, `if`, `not`, etc."

0 commit comments

Comments
 (0)