]> git.lizzy.rs Git - rust.git/commitdiff
fix: lookup
authorrainy-me <github@yue.coffee>
Thu, 21 Apr 2022 15:07:42 +0000 (00:07 +0900)
committerrainy-me <github@yue.coffee>
Thu, 21 Apr 2022 15:07:42 +0000 (00:07 +0900)
crates/ide_completion/src/completions/fn_param.rs
crates/ide_completion/src/render/function.rs

index 5bfd2bc1df1b11295d53196e75c49e896ad663aa..ae09339e9516a2814634e410f36f8fd78d6589e3 100644 (file)
@@ -30,11 +30,10 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
         let mk_item = |label: &str, range: TextRange| {
             CompletionItem::new(CompletionItemKind::Binding, range, label)
         };
-        let mut item = match &comma_wrapper {
-            Some((fmt, range)) => mk_item(&fmt(label), *range),
-            None => mk_item(label, ctx.source_range()),
+        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)
     };
 
@@ -162,7 +161,7 @@ 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, 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 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);
     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()),
+    ))
 }
index 2ae300ab653e3f2e0d744e1c9cbdf3ca568bee01..bd6833567c3346ef400b54de757c5c6143c42279 100644 (file)
@@ -570,7 +570,7 @@ fn main() {
     fn complete_fn_param() {
         // has mut kw
         check_edit(
-            "mut bar",
+            "mut ba",
             r#"
 fn f(foo: (), mut bar: u32) {}
 fn g(foo: (), mut ba$0)
@@ -583,7 +583,7 @@ fn g(foo: (), mut bar: u32)
 
         // has type param
         check_edit(
-            "mut bar",
+            "mut ba: u32",
             r#"
 fn g(foo: (), mut ba$0: u32)
 fn f(foo: (), mut bar: u32) {}
@@ -599,7 +599,7 @@ fn f(foo: (), mut bar: u32) {}
     fn complete_fn_mut_param_add_comma() {
         // add leading and trailing comma
         check_edit(
-            "mut bar",
+            ", mut ba",
             r#"
 fn f(foo: (), mut bar: u32) {}
 fn g(foo: ()mut ba$0 baz: ())
@@ -614,7 +614,7 @@ fn g(foo: (), mut bar: u32, baz: ())
     #[test]
     fn complete_fn_mut_param_has_attribute() {
         check_edit(
-            "mut bar",
+            "mut ba",
             r#"
 fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
 fn g(foo: (), mut ba$0)
@@ -626,7 +626,7 @@ fn g(foo: (), #[baz = "qux"] mut bar: u32)
         );
 
         check_edit(
-            "mut bar",
+            r#"#[baz = "qux"] mut ba"#,
             r#"
 fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
 fn g(foo: (), #[baz = "qux"] mut ba$0)
@@ -634,6 +634,18 @@ 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)
 "#,
         );
     }