Skip to content

fix: Improve parameter completion #12040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions crates/ide_completion/src/completions/fn_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_hash::FxHashMap;
use syntax::{
algo,
ast::{self, HasModuleItem},
match_ast, AstNode, Direction, SyntaxKind,
match_ast, AstNode, Direction, SyntaxKind, TextRange,
};

use crate::{
Expand All @@ -27,14 +27,13 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)

let comma_wrapper = comma_wrapper(ctx);
let mut add_new_item_to_acc = |label: &str, lookup: String| {
let mk_item = |label: &str| {
CompletionItem::new(CompletionItemKind::Binding, ctx.source_range(), label)
let mk_item = |label: &str, range: TextRange| {
CompletionItem::new(CompletionItemKind::Binding, range, label)
};
let mut item = match &comma_wrapper {
Some(fmt) => mk_item(&fmt(label)),
None => mk_item(label),
let item = match &comma_wrapper {
Some((fmt, range, lookup)) => mk_item(&fmt(label), *range).lookup_by(lookup).to_owned(),
None => mk_item(label, ctx.source_range()).lookup_by(lookup).to_owned(),
};
item.lookup_by(lookup);
item.add_to(acc)
};

Expand Down Expand Up @@ -162,14 +161,16 @@ fn should_add_self_completions(ctx: &CompletionContext, param_list: &ast::ParamL
inside_impl && no_params
}

fn comma_wrapper(ctx: &CompletionContext) -> Option<impl Fn(&str) -> String> {
fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, TextRange, String)> {
let param = ctx.token.ancestors().find(|node| node.kind() == SyntaxKind::PARAM)?;

let next_token_kind = {
let t = ctx.token.next_token()?;
let t = param.last_token()?.next_token()?;
let t = algo::skip_whitespace_token(t, Direction::Next)?;
t.kind()
};
let prev_token_kind = {
let t = ctx.previous_token.clone()?;
let t = param.first_token()?.prev_token()?;
let t = algo::skip_whitespace_token(t, Direction::Prev)?;
t.kind()
};
Expand All @@ -182,5 +183,9 @@ fn comma_wrapper(ctx: &CompletionContext) -> Option<impl Fn(&str) -> String> {
matches!(prev_token_kind, SyntaxKind::COMMA | SyntaxKind::L_PAREN | SyntaxKind::PIPE);
let leading = if has_leading_comma { "" } else { ", " };

Some(move |param: &_| format!("{}{}{}", leading, param, trailing))
Some((
move |label: &_| (format!("{}{}{}", leading, label, trailing)),
param.text_range(),
format!("{}{}", leading, param.text()),
))
}
84 changes: 84 additions & 0 deletions crates/ide_completion/src/render/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,90 @@ fn qux(Foo { bar }: Foo) {
fn main() {
qux(${1:foo})$0
}
"#,
);
}

#[test]
fn complete_fn_param() {
// has mut kw
check_edit(
"mut ba",
r#"
fn f(foo: (), mut bar: u32) {}
fn g(foo: (), mut ba$0)
"#,
r#"
fn f(foo: (), mut bar: u32) {}
fn g(foo: (), mut bar: u32)
"#,
);

// has type param
check_edit(
"mut ba: u32",
r#"
fn g(foo: (), mut ba$0: u32)
fn f(foo: (), mut bar: u32) {}
Comment on lines +588 to +589
Copy link
Contributor Author

@yue4u yue4u Apr 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only when editor's insert mode set to replace? Is there a way to test edit results with lsp cap and editor settings? Not seems possible with CompletionConfig yet

"#,
r#"
fn g(foo: (), mut bar: u32)
fn f(foo: (), mut bar: u32) {}
"#,
);
}

#[test]
fn complete_fn_mut_param_add_comma() {
// add leading and trailing comma
check_edit(
", mut ba",
r#"
fn f(foo: (), mut bar: u32) {}
fn g(foo: ()mut ba$0 baz: ())
"#,
r#"
fn f(foo: (), mut bar: u32) {}
fn g(foo: (), mut bar: u32, baz: ())
"#,
);
}

#[test]
fn complete_fn_mut_param_has_attribute() {
check_edit(
"mut ba",
r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(foo: (), mut ba$0)
Comment on lines +619 to +620
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests pass for both completion listing and edit, but doesn't work on my machine, not sure what's wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be the same bug that I fixed in #11967 - basically, the completion's lookup_by must start with the characters in its range, otherwise the editor will filter out the completion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, 8f8f20f should fix this? At least it became working in my environment.

"#,
r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(foo: (), #[baz = "qux"] mut bar: u32)
"#,
);

check_edit(
r#"#[baz = "qux"] mut ba"#,
r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(foo: (), #[baz = "qux"] mut ba$0)
"#,
r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(foo: (), #[baz = "qux"] mut bar: u32)
"#,
);

check_edit(
r#", #[baz = "qux"] mut ba"#,
r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(foo: ()#[baz = "qux"] mut ba$0)
"#,
r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(foo: (), #[baz = "qux"] mut bar: u32)
"#,
);
}
Expand Down
13 changes: 13 additions & 0 deletions crates/ide_completion/src/tests/fn_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,16 @@ fn bar(bar$0) {}
"#]],
)
}

#[test]
fn completes_for_params_with_attributes() {
check(
r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(foo: (), #[baz = "qux"] mut ba$0)
"#,
expect![[r##"
bn #[baz = "qux"] mut bar: u32
"##]],
)
}