fix: lookup

This commit is contained in:
rainy-me 2022-04-22 00:07:42 +09:00
parent a58f7acc97
commit 8f8f20fda5
2 changed files with 26 additions and 11 deletions

View File

@ -30,11 +30,10 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
let mk_item = |label: &str, range: TextRange| { let mk_item = |label: &str, range: TextRange| {
CompletionItem::new(CompletionItemKind::Binding, range, label) CompletionItem::new(CompletionItemKind::Binding, range, label)
}; };
let mut item = match &comma_wrapper { let item = match &comma_wrapper {
Some((fmt, range)) => mk_item(&fmt(label), *range), Some((fmt, range, lookup)) => mk_item(&fmt(label), *range).lookup_by(lookup).to_owned(),
None => mk_item(label, ctx.source_range()), None => mk_item(label, ctx.source_range()).lookup_by(lookup).to_owned(),
}; };
item.lookup_by(lookup);
item.add_to(acc) item.add_to(acc)
}; };
@ -162,7 +161,7 @@ fn should_add_self_completions(ctx: &CompletionContext, param_list: &ast::ParamL
inside_impl && no_params inside_impl && no_params
} }
fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, TextRange)> { fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, TextRange, String)> {
let param = ctx.token.ancestors().find(|node| node.kind() == SyntaxKind::PARAM)?; let param = ctx.token.ancestors().find(|node| node.kind() == SyntaxKind::PARAM)?;
let next_token_kind = { let next_token_kind = {
@ -184,5 +183,9 @@ fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, Te
matches!(prev_token_kind, SyntaxKind::COMMA | SyntaxKind::L_PAREN | SyntaxKind::PIPE); matches!(prev_token_kind, SyntaxKind::COMMA | SyntaxKind::L_PAREN | SyntaxKind::PIPE);
let leading = if has_leading_comma { "" } else { ", " }; let leading = if has_leading_comma { "" } else { ", " };
Some((move |label: &_| (format!("{}{}{}", leading, label, trailing)), param.text_range())) Some((
move |label: &_| (format!("{}{}{}", leading, label, trailing)),
param.text_range(),
format!("{}{}", leading, param.text()),
))
} }

View File

@ -570,7 +570,7 @@ fn main() {
fn complete_fn_param() { fn complete_fn_param() {
// has mut kw // has mut kw
check_edit( check_edit(
"mut bar", "mut ba",
r#" r#"
fn f(foo: (), mut bar: u32) {} fn f(foo: (), mut bar: u32) {}
fn g(foo: (), mut ba$0) fn g(foo: (), mut ba$0)
@ -583,7 +583,7 @@ fn g(foo: (), mut bar: u32)
// has type param // has type param
check_edit( check_edit(
"mut bar", "mut ba: u32",
r#" r#"
fn g(foo: (), mut ba$0: u32) fn g(foo: (), mut ba$0: u32)
fn f(foo: (), mut bar: u32) {} fn f(foo: (), mut bar: u32) {}
@ -599,7 +599,7 @@ fn f(foo: (), mut bar: u32) {}
fn complete_fn_mut_param_add_comma() { fn complete_fn_mut_param_add_comma() {
// add leading and trailing comma // add leading and trailing comma
check_edit( check_edit(
"mut bar", ", mut ba",
r#" r#"
fn f(foo: (), mut bar: u32) {} fn f(foo: (), mut bar: u32) {}
fn g(foo: ()mut ba$0 baz: ()) fn g(foo: ()mut ba$0 baz: ())
@ -614,7 +614,7 @@ fn g(foo: (), mut bar: u32, baz: ())
#[test] #[test]
fn complete_fn_mut_param_has_attribute() { fn complete_fn_mut_param_has_attribute() {
check_edit( check_edit(
"mut bar", "mut ba",
r#" r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {} fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(foo: (), mut ba$0) fn g(foo: (), mut ba$0)
@ -626,7 +626,7 @@ fn g(foo: (), #[baz = "qux"] mut bar: u32)
); );
check_edit( check_edit(
"mut bar", r#"#[baz = "qux"] mut ba"#,
r#" r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {} fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(foo: (), #[baz = "qux"] mut ba$0) fn g(foo: (), #[baz = "qux"] mut ba$0)
@ -634,6 +634,18 @@ fn g(foo: (), #[baz = "qux"] mut ba$0)
r#" r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {} fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(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)
"#, "#,
); );
} }