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

Weekly metals backport #21343

Merged
merged 3 commits into from
Aug 12, 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 @@ -48,7 +48,8 @@ case class ScalaPresentationCompiler(
sh: Option[ScheduledExecutorService] = None,
config: PresentationCompilerConfig = PresentationCompilerConfigImpl(),
folderPath: Option[Path] = None,
reportsLevel: ReportLevel = ReportLevel.Info
reportsLevel: ReportLevel = ReportLevel.Info,
completionItemPriority: CompletionItemPriority = (_: String) => 0,
) extends PresentationCompiler:

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

override def withCompletionItemPriority(
priority: CompletionItemPriority
): PresentationCompiler =
copy(completionItemPriority = priority)

override def withBuildTargetName(buildTargetName: String) =
copy(buildTargetName = Some(buildTargetName))

Expand Down Expand Up @@ -142,7 +148,8 @@ case class ScalaPresentationCompiler(
params,
config,
buildTargetIdentifier,
folderPath
folderPath,
completionItemPriority
).completions()

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ import org.eclipse.lsp4j.InsertTextFormat
import org.eclipse.lsp4j.InsertTextMode
import org.eclipse.lsp4j.Range as LspRange
import org.eclipse.lsp4j.TextEdit
import scala.meta.pc.CompletionItemPriority

class CompletionProvider(
search: SymbolSearch,
driver: InteractiveDriver,
params: OffsetParams,
config: PresentationCompilerConfig,
buildTargetIdentifier: String,
folderPath: Option[Path]
folderPath: Option[Path],
referenceCounter: CompletionItemPriority
)(using reports: ReportContext):
def completions(): CompletionList =
val uri = params.uri().nn
Expand Down Expand Up @@ -86,7 +88,8 @@ class CompletionProvider(
folderPath,
autoImportsGen,
unit.comments,
driver.settings
driver.settings,
referenceCounter
).completions()

val items = completions.zipWithIndex.map { case (item, idx) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ object CompletionValue:
)(using Context): String =
if symbol.isConstructor then s"${snippetAffix.toPrefix}${label}${description(printer)}"
else if symbol.is(Method) then s"${label}${description(printer)}"
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
s"${labelWithSuffix(printer)}${description(printer)}"
else if symbol.isType then labelWithSuffix(printer)
else if symbol.isTerm && symbol.info.typeSymbol.is(Module) then
s"${label}${description(printer)}"
else s"$label: ${description(printer)}"
else s"$label${description(printer)}"

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

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

end Symbolic

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

override def labelWithDescription(printer: ShortenedTypePrinter)(using Context): String =
def isMethodOrValue = !(symbol.isType || symbol.is(Module))
if symbol.isConstructor || symbol.name == nme.apply then
s"${snippetAffix.toPrefix}${label}${description(printer)} - ${printer.fullNameString(importSymbol.effectiveOwner)}"
else if symbol.is(Method) then
else if isMethodOrValue then
s"${labelWithSuffix(printer)} - ${printer.fullNameString(symbol.effectiveOwner)}"
else if symbol.is(Package) || symbol.is(Module) || symbol.isClass then
s"${labelWithSuffix(printer)} -${description(printer)}"
Expand All @@ -199,7 +203,7 @@ object CompletionValue:
CompletionItemKind.Method
override def completionItemDataKind: Integer = CompletionSource.ImplicitClassKind.ordinal
override def description(printer: ShortenedTypePrinter)(using Context): String =
s"${printer.completionSymbol(denotation)} (implicit)"
s"${super.description(printer)} (implicit)"

/**
* CompletionValue for extension methods via SymbolSearch
Expand Down Expand Up @@ -339,6 +343,9 @@ object CompletionValue:

override def labelWithDescription(printer: ShortenedTypePrinter)(using Context): String =
label

override def description(printer: ShortenedTypePrinter)(using Context): String =
printer.completionSymbol(denotation)
end CaseKeyword

case class Document(label: String, doc: String, description: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class Completions(
workspace: Option[Path],
autoImports: AutoImportsGenerator,
comments: List[Comment],
options: List[String]
options: List[String],
completionItemPriority: CompletionItemPriority
)(using ReportContext):

given context: Context = ctx
Expand Down Expand Up @@ -909,6 +910,20 @@ class Completions(
else 0
end compareLocalSymbols

private def workspaceMemberPriority(symbol: Symbol): Int =
completionItemPriority
.workspaceMemberPriority(
SemanticdbSymbols.symbolName(symbol),
).nn

def compareFrequency(o1: CompletionValue, o2: CompletionValue): Int =
(o1, o2) match
case (w1: CompletionValue.Workspace, w2: CompletionValue.Workspace) =>
workspaceMemberPriority(w1.symbol)
.compareTo(workspaceMemberPriority(w2.symbol))
case _ => 0
end compareFrequency

def compareByRelevance(o1: CompletionValue, o2: CompletionValue): Int =
Integer.compare(
computeRelevancePenalty(o1, application),
Expand Down Expand Up @@ -1018,17 +1033,20 @@ class Completions(
)
if byIdentifier != 0 then byIdentifier
else
val byOwner =
s1.owner.fullName.toString
.compareTo(s2.owner.fullName.toString)
if byOwner != 0 then byOwner
val byFrequency = compareFrequency(o1, o2)
if byFrequency != 0 then byFrequency
else
val byParamCount = Integer.compare(
s1.paramSymss.flatten.size,
s2.paramSymss.flatten.size
)
if byParamCount != 0 then byParamCount
else s1.detailString.compareTo(s2.detailString)
val byOwner =
s1.owner.fullName.toString
.compareTo(s2.owner.fullName.toString)
if byOwner != 0 then byOwner
else
val byParamCount = Integer.compare(
s1.paramSymss.flatten.size,
s2.paramSymss.flatten.size
)
if byParamCount != 0 then byParamCount
else s1.detailString.compareTo(s2.detailString)
end if
end if
end if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import dotty.tools.pc.utils._
import org.eclipse.lsp4j.MarkupContent
import org.eclipse.lsp4j.jsonrpc.messages.Either as JEither
import org.junit.runner.RunWith
import scala.meta.pc.CompletionItemPriority

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

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

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

protected def config: PresentationCompilerConfig =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dotty.tools.pc.tests.completion

import dotty.tools.pc.base.BaseCompletionSuite
import scala.meta.pc.CompletionItemPriority
import org.junit.Test

class CompletionContextSuite extends BaseCompletionSuite:
override val completionItemPriority: CompletionItemPriority = {
case "scala/concurrent/Future." => -1
case _ => 0
}
// scala.concurrent.Future should be ranked higher than java.util.concurrent.Future
val futureCompletionResult: List[String] =
List("Future - scala.concurrent", "Future - java.util.concurrent")

@Test
def `context` =
check(
"""package fut
|object A {
| Futur@@
|}""".stripMargin,
"""Future - scala.concurrent
|Future - java.util.concurrent
|""".stripMargin,
filter = futureCompletionResult.contains
)
Original file line number Diff line number Diff line change
Expand Up @@ -2042,3 +2042,97 @@ class CompletionSuite extends BaseCompletionSuite:
|""".stripMargin,
includeCompletionKind = true
)

@Test def `def-arg` =
check(
"""|package a
|object W {
| val aaaaaa = 1
|}
|object O {
| def foo(aa@@)
|}
|""".stripMargin,
""
)

@Test def conflict =
check(
"""|package a
|object O {
| val foofoo: Int = 123
| def method = {
| val foofoo: String = "abc"
| foofoo@@
| }
|}
|""".stripMargin,
"""|foofoo: String
|foofoo - a.O: Int
|""".stripMargin
)

@Test def `conflict-2` =
check(
"""|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
)

@Test def `conflict-3` =
check(
"""|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
)

@Test def `conflict-edit-2` =
checkEdit(
"""|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
)
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,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 @@ -810,7 +810,7 @@ class CompletionWorkspaceSuite extends BaseCompletionSuite:
|}
|""".stripMargin,
"""|fooBar: String
|fooBar: List[Int]
|fooBar - test.A: List[Int]
|""".stripMargin,
)

Expand Down
Loading