Skip to content

Commit

Permalink
Merge #3432
Browse files Browse the repository at this point in the history
3432: Feature/snippet completions r=matklad a=iTZAvishay

This PR implements #1705 and is based on #3430 to avoid future conflicts.

The completions are placing default values with the names of the parameters, demo: 
![call_me_demo](https://user-images.githubusercontent.com/5567310/75828341-461ca400-5db4-11ea-88d8-88e59ac1a197.gif)


Co-authored-by: Avishay Matayev <me@avishay.dev>
  • Loading branch information
bors[bot] and Avishayy committed Mar 4, 2020
2 parents 79d05aa + fb34a5b commit 66ec6bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/ra_ide/src/completion/complete_scope.rs
Expand Up @@ -38,7 +38,7 @@ mod tests {
label: "quux(…)",
source_range: [91; 91),
delete: [91; 91),
insert: "quux($0)",
insert: "quux(${1:x})$0",
kind: Function,
lookup: "quux",
detail: "fn quux(x: i32)",
Expand Down
34 changes: 21 additions & 13 deletions crates/ra_ide/src/completion/presentation.rs
Expand Up @@ -9,7 +9,7 @@ use crate::completion::{
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
};

use crate::display::{const_label, function_label, macro_label, type_label};
use crate::display::{const_label, macro_label, type_label, FunctionSignature};

impl Completions {
pub(crate) fn add_field(
Expand Down Expand Up @@ -198,7 +198,7 @@ impl Completions {

let name = name.unwrap_or_else(|| func.name(ctx.db).to_string());
let ast_node = func.source(ctx.db).value;
let detail = function_label(&ast_node);
let function_signature = FunctionSignature::from(&ast_node);

let mut builder =
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone())
Expand All @@ -209,19 +209,27 @@ impl Completions {
})
.set_documentation(func.docs(ctx.db))
.set_deprecated(is_deprecated(func, ctx.db))
.detail(detail);
.detail(function_signature.to_string());

// Add `<>` for generic types
if ctx.use_item_syntax.is_none()
&& !ctx.is_call
&& ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
{
tested_by!(inserts_parens_for_function_calls);
let (snippet, label) = if params.is_empty() || has_self_param && params.len() == 1 {
(format!("{}()$0", name), format!("{}()", name))
} else {
(format!("{}($0)", name), format!("{}(…)", name))
};

let (snippet, label) =
if params.is_empty() || has_self_param && params.len() == 1 {
(format!("{}()$0", name), format!("{}()", name))
} else {
let function_params_snippet =
join(function_signature.parameter_names.iter().enumerate().map(
|(index, param_name)| format!("${{{}:{}}}", index + 1, param_name),
))
.separator(", ")
.to_string();
(format!("{}({})$0", name, function_params_snippet), format!("{}(…)", name))
};
builder = builder.lookup_by(name).label(label).insert_snippet(snippet);
}

Expand Down Expand Up @@ -486,7 +494,7 @@ mod tests {
label: "with_args(…)",
source_range: [80; 85),
delete: [80; 85),
insert: "with_args($0)",
insert: "with_args(${1:x}, ${2:y})$0",
kind: Function,
lookup: "with_args",
detail: "fn with_args(x: i32, y: String)",
Expand Down Expand Up @@ -630,7 +638,7 @@ mod tests {
label: "foo(…)",
source_range: [61; 63),
delete: [61; 63),
insert: "foo($0)",
insert: "foo(${1:xs})$0",
kind: Function,
lookup: "foo",
detail: "fn foo(xs: Ve)",
Expand Down Expand Up @@ -659,7 +667,7 @@ mod tests {
label: "foo(…)",
source_range: [64; 66),
delete: [64; 66),
insert: "foo($0)",
insert: "foo(${1:xs})$0",
kind: Function,
lookup: "foo",
detail: "fn foo(xs: Ve)",
Expand Down Expand Up @@ -687,7 +695,7 @@ mod tests {
label: "foo(…)",
source_range: [68; 70),
delete: [68; 70),
insert: "foo($0)",
insert: "foo(${1:xs})$0",
kind: Function,
lookup: "foo",
detail: "fn foo(xs: Ve)",
Expand Down Expand Up @@ -715,7 +723,7 @@ mod tests {
label: "foo(…)",
source_range: [61; 63),
delete: [61; 63),
insert: "foo($0)",
insert: "foo(${1:xs})$0",
kind: Function,
lookup: "foo",
detail: "fn foo(xs: Ve<i128>)",
Expand Down

0 comments on commit 66ec6bd

Please sign in to comment.