Skip to content
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

fix: disambiguate workspace completions for vals in Scala 3 #6625

Merged
merged 1 commit into from
Aug 1, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ object CompletionValue:
)(using Context): String =
if symbol.is(Method) then s"${label}${description(printer)}"
else if symbol.isConstructor then label
else if symbol.is(Mutable) then s"$label: ${description(printer)}"
else if symbol.is(Mutable) then s"$label${description(printer)}"
else if symbol.is(Package) || symbol.is(Module) || symbol.isClass then
if isFromWorkspace then
s"${labelWithSuffix(printer)} -${description(printer)}"
else s"${labelWithSuffix(printer)}${description(printer)}"
else if symbol.isType then labelWithSuffix(printer)
else s"$label: ${description(printer)}"
else s"$label${description(printer)}"

protected def labelWithSuffix(
printer: MetalsPrinter
Expand All @@ -98,7 +98,9 @@ object CompletionValue:
else label

override def description(printer: MetalsPrinter)(using Context): String =
printer.completionSymbol(symbol)
val isVal = !(symbol.is(Module) || symbol.is(Method) || symbol.isType)
val prefix = if isVal then ": " else ""
prefix ++ printer.completionSymbol(symbol)
end Symbolic

case class Compiler(
Expand All @@ -120,7 +122,8 @@ object CompletionValue:
override def labelWithDescription(
printer: MetalsPrinter
)(using Context): String =
if symbol.is(Method) && symbol.name != nme.apply then
val isMethodOrValue = !(symbol.isType || symbol.is(Module))
if isMethodOrValue && symbol.name != nme.apply then
s"${labelWithSuffix(printer)} - ${printer.fullName(symbol.effectiveOwner)}"
else super.labelWithDescription(printer)
override def isFromWorkspace: Boolean = true
Expand Down Expand Up @@ -151,7 +154,7 @@ object CompletionValue:
override def completionItemKind(using Context): CompletionItemKind =
CompletionItemKind.Method
override def description(printer: MetalsPrinter)(using Context): String =
s"${printer.completionSymbol(symbol)} (implicit)"
s"${super.description(printer)} (implicit)"

/**
* @param shortenedNames shortened type names by `Printer`. This field should be used for autoImports
Expand Down Expand Up @@ -269,6 +272,10 @@ object CompletionValue:
override def labelWithDescription(printer: MetalsPrinter)(using
Context
): String = label

override def description(printer: MetalsPrinter)(using Context): String =
printer.completionSymbol(symbol)

end CaseKeyword

case class Document(label: String, doc: String, description: String)
Expand Down
67 changes: 66 additions & 1 deletion tests/cross/src/test/scala/tests/pc/CompletionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2341,11 +2341,47 @@ class CompletionSuite extends BaseCompletionSuite {
|""".stripMargin,
compat = Map(
"3" -> """|x: String
|x: Int
|x - a.O: Int
|""".stripMargin
)
)

check(
"conflict-2",
"""|package a
|object A {
| val foo = 1
|}
|object B {
| val foo = 1
|}
|object O {
| val x: Int = foo@@
|}
|""".stripMargin,
"""|foo - a.A: Int
|foo - a.B: Int
|""".stripMargin
)

check(
"conflict-3",
"""|package a
|object A {
| var foo = 1
|}
|object B {
| var foo = 1
|}
|object O {
| val x: Int = foo@@
|}
|""".stripMargin,
"""|foo - a.A: Int
|foo - a.B: Int
|""".stripMargin
)

checkEdit(
"conflict-edit",
"""|object O {
Expand Down Expand Up @@ -2381,4 +2417,33 @@ class CompletionSuite extends BaseCompletionSuite {
""
)

checkEdit(
"conflict-edit-2",
"""|package a
|object A {
| val foo = 1
|}
|object B {
| val foo = 1
|}
|object O {
| val x: Int = foo@@
|}
|""".stripMargin,
"""|package a
|
|import a.A.foo
|object A {
| val foo = 1
|}
|object B {
| val foo = 1
|}
|object O {
| val x: Int = foo
|}
|""".stripMargin,
assertSingleItem = false
)

}
16 changes: 3 additions & 13 deletions tests/cross/src/test/scala/tests/pc/CompletionWorkspaceSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ class CompletionWorkspaceSuite extends BaseCompletionSuite {
|package b:
| def main: Unit = incre@@
|""".stripMargin,
"""|increment3: Int
"""|increment3 - d: Int
|increment - a: Int
|increment2 - a.c: Int
|""".stripMargin
Expand Down Expand Up @@ -939,18 +939,8 @@ class CompletionWorkspaceSuite extends BaseCompletionSuite {
|}
|""".stripMargin,
"""|fooBar: String
|fooBar: List[Int]
|""".stripMargin,
compat = Map(
"2" -> """|fooBar: String
|fooBar - case_class_param.A: List[Int]
|""".stripMargin,
">=3.4.1-RC1-bin-20240208-hash-NIGHTLY" ->
"""|fooBar: String
|fooBar: List[Int]
|fooBar(n: Int): Int
|""".stripMargin
)
|fooBar - case_class_param.A: List[Int]
|""".stripMargin
)

check(
Expand Down
Loading