From db248885f148ad4d95ceada7eafdc440b9c6bb1e Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Mon, 17 Mar 2025 18:39:27 +0100 Subject: [PATCH] Show the Autofill completion case as what would be auto-filled (#22819) Partial Fix for scalameta/metals#7274 We have in our completion provider an option to automatically fill a case class constructor with named parameters: ```scala case class A(x: Int, y: Int) val a = A(@@) ^^ // would fill in here val a = A(x = ???, y = ???) // ^^^ ^^^ cursor marks added, so tabs can be used to move between the fields ``` However previously it was labeled `Autofill with default values`, which would never pass editors' filters based on the current state of input (most likely the name of a field). The only way to find this option was to type something matching the `Autofill with default values` label, which explains the `auto` in the above issue. We now provide the actual provided fields as the label, so the above would show `x = ???, y = ???` instead, which is both more intuitive for the user, and more friendly to editors. [Cherry-picked 744ba922a1729e7670a0db6de981932e60b2b56e] --- .../tools/pc/completions/CompletionValue.scala | 4 ++-- .../tools/pc/completions/NamedArgCompletions.scala | 9 ++++++++- .../pc/tests/completion/CompletionArgSuite.scala | 13 +++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala index 90b285bffb3a..05d97972d76e 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala @@ -261,13 +261,13 @@ object CompletionValue: end NamedArg case class Autofill( - value: String + value: String, + override val label: String, ) extends CompletionValue: override def completionItemKind(using Context): CompletionItemKind = CompletionItemKind.Enum override def completionItemDataKind: Integer = CompletionSource.OverrideKind.ordinal override def insertText: Option[String] = Some(value) - override def label: String = "Autofill with default values" case class Keyword(label: String, override val insertText: Option[String]) extends CompletionValue: diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/NamedArgCompletions.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/NamedArgCompletions.scala index dd3a910beb4f..a21706b9e36e 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/NamedArgCompletions.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/NamedArgCompletions.scala @@ -339,9 +339,16 @@ object NamedArgCompletions: s"${param.nameBackticked.replace("$", "$$")} = $${${index + 1}${findDefaultValue(param)}}" } .mkString(", ") + val labelText = allParams + .collect { + case param if !param.symbol.is(Flags.HasDefault) => + s"${param.nameBackticked.replace("$", "$$")} = ???" + } + .mkString(", ") List( CompletionValue.Autofill( - editText + editText, + labelText, ) ) else List.empty diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionArgSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionArgSuite.scala index dc81d2596c6f..e5f2d31ad808 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionArgSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionArgSuite.scala @@ -1128,3 +1128,16 @@ class CompletionArgSuite extends BaseCompletionSuite: """x: Int |x = : Any""".stripMargin, ) + + @Test def `autofill-arguments-case-class` = + check( + """ + |case class A(x: Int, y: Int) + | + |def main() = + | A(x@@) + |""".stripMargin, + """x = : Int + |x = ???, y = ???""".stripMargin, + // this looks strange due to the Autofill message belonging to the description + )