Skip to content

Commit 5f26a26

Browse files
Backport "Weekly metals backport" to 3.5.2 (#21488)
Backports #21343 to the 3.5.2 branch. PR submitted by the release tooling. [skip ci]
2 parents f253d1c + 894270f commit 5f26a26

File tree

8 files changed

+181
-22
lines changed

8 files changed

+181
-22
lines changed

Diff for: presentation-compiler/src/main/dotty/tools/pc/ScalaPresentationCompiler.scala

+9-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ case class ScalaPresentationCompiler(
4848
sh: Option[ScheduledExecutorService] = None,
4949
config: PresentationCompilerConfig = PresentationCompilerConfigImpl(),
5050
folderPath: Option[Path] = None,
51-
reportsLevel: ReportLevel = ReportLevel.Info
51+
reportsLevel: ReportLevel = ReportLevel.Info,
52+
completionItemPriority: CompletionItemPriority = (_: String) => 0,
5253
) extends PresentationCompiler:
5354

5455
def this() = this("", None, Nil, Nil)
@@ -63,6 +64,11 @@ case class ScalaPresentationCompiler(
6364
.map(StdReportContext(_, _ => buildTargetName, reportsLevel))
6465
.getOrElse(EmptyReportContext)
6566

67+
override def withCompletionItemPriority(
68+
priority: CompletionItemPriority
69+
): PresentationCompiler =
70+
copy(completionItemPriority = priority)
71+
6672
override def withBuildTargetName(buildTargetName: String) =
6773
copy(buildTargetName = Some(buildTargetName))
6874

@@ -142,7 +148,8 @@ case class ScalaPresentationCompiler(
142148
params,
143149
config,
144150
buildTargetIdentifier,
145-
folderPath
151+
folderPath,
152+
completionItemPriority
146153
).completions()
147154

148155
}

Diff for: presentation-compiler/src/main/dotty/tools/pc/completions/CompletionProvider.scala

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ import org.eclipse.lsp4j.InsertTextFormat
3232
import org.eclipse.lsp4j.InsertTextMode
3333
import org.eclipse.lsp4j.Range as LspRange
3434
import org.eclipse.lsp4j.TextEdit
35+
import scala.meta.pc.CompletionItemPriority
3536

3637
class CompletionProvider(
3738
search: SymbolSearch,
3839
driver: InteractiveDriver,
3940
params: OffsetParams,
4041
config: PresentationCompilerConfig,
4142
buildTargetIdentifier: String,
42-
folderPath: Option[Path]
43+
folderPath: Option[Path],
44+
referenceCounter: CompletionItemPriority
4345
)(using reports: ReportContext):
4446
def completions(): CompletionList =
4547
val uri = params.uri().nn
@@ -86,7 +88,8 @@ class CompletionProvider(
8688
folderPath,
8789
autoImportsGen,
8890
unit.comments,
89-
driver.settings
91+
driver.settings,
92+
referenceCounter
9093
).completions()
9194

9295
val items = completions.zipWithIndex.map { case (item, idx) =>

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

+29-11
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class Completions(
5050
workspace: Option[Path],
5151
autoImports: AutoImportsGenerator,
5252
comments: List[Comment],
53-
options: List[String]
53+
options: List[String],
54+
completionItemPriority: CompletionItemPriority
5455
)(using ReportContext):
5556

5657
given context: Context = ctx
@@ -909,6 +910,20 @@ class Completions(
909910
else 0
910911
end compareLocalSymbols
911912

913+
private def workspaceMemberPriority(symbol: Symbol): Int =
914+
completionItemPriority
915+
.workspaceMemberPriority(
916+
SemanticdbSymbols.symbolName(symbol),
917+
).nn
918+
919+
def compareFrequency(o1: CompletionValue, o2: CompletionValue): Int =
920+
(o1, o2) match
921+
case (w1: CompletionValue.Workspace, w2: CompletionValue.Workspace) =>
922+
workspaceMemberPriority(w1.symbol)
923+
.compareTo(workspaceMemberPriority(w2.symbol))
924+
case _ => 0
925+
end compareFrequency
926+
912927
def compareByRelevance(o1: CompletionValue, o2: CompletionValue): Int =
913928
Integer.compare(
914929
computeRelevancePenalty(o1, application),
@@ -1018,17 +1033,20 @@ class Completions(
10181033
)
10191034
if byIdentifier != 0 then byIdentifier
10201035
else
1021-
val byOwner =
1022-
s1.owner.fullName.toString
1023-
.compareTo(s2.owner.fullName.toString)
1024-
if byOwner != 0 then byOwner
1036+
val byFrequency = compareFrequency(o1, o2)
1037+
if byFrequency != 0 then byFrequency
10251038
else
1026-
val byParamCount = Integer.compare(
1027-
s1.paramSymss.flatten.size,
1028-
s2.paramSymss.flatten.size
1029-
)
1030-
if byParamCount != 0 then byParamCount
1031-
else s1.detailString.compareTo(s2.detailString)
1039+
val byOwner =
1040+
s1.owner.fullName.toString
1041+
.compareTo(s2.owner.fullName.toString)
1042+
if byOwner != 0 then byOwner
1043+
else
1044+
val byParamCount = Integer.compare(
1045+
s1.paramSymss.flatten.size,
1046+
s2.paramSymss.flatten.size
1047+
)
1048+
if byParamCount != 0 then byParamCount
1049+
else s1.detailString.compareTo(s2.detailString)
10321050
end if
10331051
end if
10341052
end if

Diff for: presentation-compiler/test/dotty/tools/pc/base/BasePCSuite.scala

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import dotty.tools.pc.utils._
2222
import org.eclipse.lsp4j.MarkupContent
2323
import org.eclipse.lsp4j.jsonrpc.messages.Either as JEither
2424
import org.junit.runner.RunWith
25+
import scala.meta.pc.CompletionItemPriority
2526

2627
object TestResources:
2728
val scalaLibrary = BuildInfo.ideTestsDependencyClasspath.map(_.toPath).toSeq
@@ -30,6 +31,7 @@ object TestResources:
3031

3132
@RunWith(classOf[ReusableClassRunner])
3233
abstract class BasePCSuite extends PcAssertions:
34+
val completionItemPriority: CompletionItemPriority = (_: String) => 0
3335
private val isDebug = ManagementFactory.getRuntimeMXBean.getInputArguments.toString.contains("-agentlib:jdwp")
3436

3537
val tmp = Files.createTempDirectory("stable-pc-tests")
@@ -53,6 +55,7 @@ abstract class BasePCSuite extends PcAssertions:
5355
.withExecutorService(executorService)
5456
.withScheduledExecutorService(executorService)
5557
.withSearch(search)
58+
.withCompletionItemPriority(completionItemPriority)
5659
.newInstance("", myclasspath.asJava, scalacOpts.asJava)
5760

5861
protected def config: PresentationCompilerConfig =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dotty.tools.pc.tests.completion
2+
3+
import dotty.tools.pc.base.BaseCompletionSuite
4+
import scala.meta.pc.CompletionItemPriority
5+
import org.junit.Test
6+
7+
class CompletionContextSuite extends BaseCompletionSuite:
8+
override val completionItemPriority: CompletionItemPriority = {
9+
case "scala/concurrent/Future." => -1
10+
case _ => 0
11+
}
12+
// scala.concurrent.Future should be ranked higher than java.util.concurrent.Future
13+
val futureCompletionResult: List[String] =
14+
List("Future - scala.concurrent", "Future - java.util.concurrent")
15+
16+
@Test
17+
def `context` =
18+
check(
19+
"""package fut
20+
|object A {
21+
| Futur@@
22+
|}""".stripMargin,
23+
"""Future - scala.concurrent
24+
|Future - java.util.concurrent
25+
|""".stripMargin,
26+
filter = futureCompletionResult.contains
27+
)

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

+94
Original file line numberDiff line numberDiff line change
@@ -2041,3 +2041,97 @@ class CompletionSuite extends BaseCompletionSuite:
20412041
|""".stripMargin,
20422042
includeCompletionKind = true
20432043
)
2044+
2045+
@Test def `def-arg` =
2046+
check(
2047+
"""|package a
2048+
|object W {
2049+
| val aaaaaa = 1
2050+
|}
2051+
|object O {
2052+
| def foo(aa@@)
2053+
|}
2054+
|""".stripMargin,
2055+
""
2056+
)
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)