Skip to content

Commit a28bc0f

Browse files
committed
fix: disambiguate workspace completions for vals
1 parent e0e3695 commit a28bc0f

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
@@ -914,7 +914,7 @@ class Completions(
914914
completionItemPriority
915915
.workspaceMemberPriority(
916916
SemanticdbSymbols.symbolName(symbol),
917-
)
917+
).nn
918918

919919
def compareFrequency(o1: CompletionValue, o2: CompletionValue): Int =
920920
(o1, o2) match

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

+81
Original file line numberDiff line numberDiff line change
@@ -2055,3 +2055,84 @@ class CompletionSuite extends BaseCompletionSuite:
20552055
|""".stripMargin,
20562056
""
20572057
)
2058+
2059+
@Test def conflict =
2060+
check(
2061+
"""|package a
2062+
|object O {
2063+
| val foofoo: Int = 123
2064+
| def method = {
2065+
| val foofoo: String = "abc"
2066+
| foofoo@@
2067+
| }
2068+
|}
2069+
|""".stripMargin,
2070+
"""|foofoo: String
2071+
|foofoo - a.O: Int
2072+
|""".stripMargin
2073+
)
2074+
2075+
@Test def `conflict-2` =
2076+
check(
2077+
"""|package a
2078+
|object A {
2079+
| val foo = 1
2080+
|}
2081+
|object B {
2082+
| val foo = 1
2083+
|}
2084+
|object O {
2085+
| val x: Int = foo@@
2086+
|}
2087+
|""".stripMargin,
2088+
"""|foo - a.A: Int
2089+
|foo - a.B: Int
2090+
|""".stripMargin
2091+
)
2092+
2093+
@Test def `conflict-3` =
2094+
check(
2095+
"""|package a
2096+
|object A {
2097+
| var foo = 1
2098+
|}
2099+
|object B {
2100+
| var foo = 1
2101+
|}
2102+
|object O {
2103+
| val x: Int = foo@@
2104+
|}
2105+
|""".stripMargin,
2106+
"""|foo - a.A: Int
2107+
|foo - a.B: Int
2108+
|""".stripMargin
2109+
)
2110+
2111+
@Test def `conflict-edit-2` =
2112+
checkEdit(
2113+
"""|package a
2114+
|object A {
2115+
| val foo = 1
2116+
|}
2117+
|object B {
2118+
| val foo = 1
2119+
|}
2120+
|object O {
2121+
| val x: Int = foo@@
2122+
|}
2123+
|""".stripMargin,
2124+
"""|package a
2125+
|
2126+
|import a.A.foo
2127+
|object A {
2128+
| val foo = 1
2129+
|}
2130+
|object B {
2131+
| val foo = 1
2132+
|}
2133+
|object O {
2134+
| val x: Int = foo
2135+
|}
2136+
|""".stripMargin,
2137+
assertSingleItem = false
2138+
)

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)