Skip to content

Commit 350ce61

Browse files
kasiaMarekWojciechMazur
authored andcommitted
fix: disambiguate workspace completions for vals
[Cherry-picked a28bc0f]
1 parent c9d6c5f commit 350ce61

File tree

4 files changed

+96
-8
lines changed

4 files changed

+96
-8
lines changed

presentation-compiler/src/main/dotty/tools/pc/completions/CompletionValue.scala

+12-5
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ object CompletionValue:
101101
)(using Context): String =
102102
if symbol.isConstructor then s"${snippetAffix.toPrefix}${label}${description(printer)}"
103103
else if symbol.is(Method) then s"${label}${description(printer)}"
104-
else if symbol.is(Mutable) then s"$label: ${description(printer)}"
104+
else if symbol.is(Mutable) then s"$label${description(printer)}"
105105
else if symbol.is(Package) || symbol.is(Module) || symbol.isClass then
106106
s"${labelWithSuffix(printer)}${description(printer)}"
107107
else if symbol.isType then labelWithSuffix(printer)
108108
else if symbol.isTerm && symbol.info.typeSymbol.is(Module) then
109109
s"${label}${description(printer)}"
110-
else s"$label: ${description(printer)}"
110+
else s"$label${description(printer)}"
111111

112112
protected def labelWithSuffix(printer: ShortenedTypePrinter)(using Context): String =
113113
if snippetAffix.addLabelSnippet
@@ -119,7 +119,10 @@ object CompletionValue:
119119
else label
120120

121121
override def description(printer: ShortenedTypePrinter)(using Context): String =
122-
printer.completionSymbol(denotation)
122+
def info = denotation.info.widenTermRefExpr
123+
val isVal = !(symbol.is(Module) || symbol.is(Method) || symbol.isType || info.typeSymbol.is(Module))
124+
val prefix = if isVal then ": " else ""
125+
prefix ++ printer.completionSymbol(denotation)
123126

124127
end Symbolic
125128

@@ -178,9 +181,10 @@ object CompletionValue:
178181
override def completionItemDataKind: Integer = CompletionSource.WorkspaceKind.ordinal
179182

180183
override def labelWithDescription(printer: ShortenedTypePrinter)(using Context): String =
184+
def isMethodOrValue = !(symbol.isType || symbol.is(Module))
181185
if symbol.isConstructor || symbol.name == nme.apply then
182186
s"${snippetAffix.toPrefix}${label}${description(printer)} - ${printer.fullNameString(importSymbol.effectiveOwner)}"
183-
else if symbol.is(Method) then
187+
else if isMethodOrValue then
184188
s"${labelWithSuffix(printer)} - ${printer.fullNameString(symbol.effectiveOwner)}"
185189
else if symbol.is(Package) || symbol.is(Module) || symbol.isClass then
186190
s"${labelWithSuffix(printer)} -${description(printer)}"
@@ -199,7 +203,7 @@ object CompletionValue:
199203
CompletionItemKind.Method
200204
override def completionItemDataKind: Integer = CompletionSource.ImplicitClassKind.ordinal
201205
override def description(printer: ShortenedTypePrinter)(using Context): String =
202-
s"${printer.completionSymbol(denotation)} (implicit)"
206+
s"${super.description(printer)} (implicit)"
203207

204208
/**
205209
* CompletionValue for extension methods via SymbolSearch
@@ -339,6 +343,9 @@ object CompletionValue:
339343

340344
override def labelWithDescription(printer: ShortenedTypePrinter)(using Context): String =
341345
label
346+
347+
override def description(printer: ShortenedTypePrinter)(using Context): String =
348+
printer.completionSymbol(denotation)
342349
end CaseKeyword
343350

344351
case class Document(label: String, doc: String, description: String)

presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ class Completions(
916916
completionItemPriority
917917
.workspaceMemberPriority(
918918
SemanticdbSymbols.symbolName(symbol),
919-
)
919+
).nn
920920

921921
def compareFrequency(o1: CompletionValue, o2: CompletionValue): Int =
922922
(o1, o2) match

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionSuite.scala

+81
Original file line numberDiff line numberDiff line change
@@ -2021,3 +2021,84 @@ class CompletionSuite extends BaseCompletionSuite:
20212021
|""".stripMargin,
20222022
""
20232023
)
2024+
2025+
@Test def conflict =
2026+
check(
2027+
"""|package a
2028+
|object O {
2029+
| val foofoo: Int = 123
2030+
| def method = {
2031+
| val foofoo: String = "abc"
2032+
| foofoo@@
2033+
| }
2034+
|}
2035+
|""".stripMargin,
2036+
"""|foofoo: String
2037+
|foofoo - a.O: Int
2038+
|""".stripMargin
2039+
)
2040+
2041+
@Test def `conflict-2` =
2042+
check(
2043+
"""|package a
2044+
|object A {
2045+
| val foo = 1
2046+
|}
2047+
|object B {
2048+
| val foo = 1
2049+
|}
2050+
|object O {
2051+
| val x: Int = foo@@
2052+
|}
2053+
|""".stripMargin,
2054+
"""|foo - a.A: Int
2055+
|foo - a.B: Int
2056+
|""".stripMargin
2057+
)
2058+
2059+
@Test def `conflict-3` =
2060+
check(
2061+
"""|package a
2062+
|object A {
2063+
| var foo = 1
2064+
|}
2065+
|object B {
2066+
| var foo = 1
2067+
|}
2068+
|object O {
2069+
| val x: Int = foo@@
2070+
|}
2071+
|""".stripMargin,
2072+
"""|foo - a.A: Int
2073+
|foo - a.B: Int
2074+
|""".stripMargin
2075+
)
2076+
2077+
@Test def `conflict-edit-2` =
2078+
checkEdit(
2079+
"""|package a
2080+
|object A {
2081+
| val foo = 1
2082+
|}
2083+
|object B {
2084+
| val foo = 1
2085+
|}
2086+
|object O {
2087+
| val x: Int = foo@@
2088+
|}
2089+
|""".stripMargin,
2090+
"""|package a
2091+
|
2092+
|import a.A.foo
2093+
|object A {
2094+
| val foo = 1
2095+
|}
2096+
|object B {
2097+
| val foo = 1
2098+
|}
2099+
|object O {
2100+
| val x: Int = foo
2101+
|}
2102+
|""".stripMargin,
2103+
assertSingleItem = false
2104+
)

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionWorkspaceSuite.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ class CompletionWorkspaceSuite extends BaseCompletionSuite:
767767
|package b:
768768
| def main: Unit = incre@@
769769
|""".stripMargin,
770-
"""|increment3: Int
770+
"""|increment3 - d: Int
771771
|increment - a: Int
772772
|increment2 - a.c: Int
773773
|""".stripMargin
@@ -810,7 +810,7 @@ class CompletionWorkspaceSuite extends BaseCompletionSuite:
810810
|}
811811
|""".stripMargin,
812812
"""|fooBar: String
813-
|fooBar: List[Int]
813+
|fooBar - test.A: List[Int]
814814
|""".stripMargin,
815815
)
816816

0 commit comments

Comments
 (0)