Skip to content

Commit 894270f

Browse files
kasiaMarekWojciechMazur
authored andcommitted
fix: disambiguate workspace completions for vals
[Cherry-picked a28bc0f]
1 parent c48ca3f commit 894270f

File tree

4 files changed

+96
-8
lines changed

4 files changed

+96
-8
lines changed

Diff for: 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)

Diff for: 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

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

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

Diff for: 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)