Skip to content

Commit

Permalink
Add support for Scala 3.0.1-RC1
Browse files Browse the repository at this point in the history
This also includes a bunch of fixes that were kind of a chain reaction due to some tests failing for 3.0.1-RC1
- print outer type instead of singleton type for objects
- make sure to take method signature into account when filtering or sorting symbols
  • Loading branch information
tgodzik committed Jun 4, 2021
1 parent e675993 commit 0560fa4
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 80 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Expand Up @@ -198,6 +198,7 @@ lazy val V = new {
val bsp = "2.0.0-M13"
val bloop = "1.4.8-19-4d9f966b"
val scala3 = "3.0.0"
val nextScala3RC = "3.0.1-RC1"
val bloopNightly = bloop
val sbtBloop = bloop
val gradleBloop = bloop
Expand Down Expand Up @@ -239,7 +240,7 @@ lazy val V = new {
// Scala 3
def scala3RCVersions = Seq("3.0.0-RC3", "3.0.0-RC2", "3.0.0-RC1")
def nonDeprecatedScala3Versions =
scala3 +: scala3RCVersions
scala3 +: scala3RCVersions :+ nextScala3RC
def deprecatedScala3Versions = Seq()
def scala3Versions = nonDeprecatedScala3Versions ++ deprecatedScala3Versions

Expand Down
Expand Up @@ -34,13 +34,13 @@ class CompletionProvider(
def completions(): (List[CompletionValue], SymbolSearch.Result) = {
val (_, compilerCompletions) = Completion.completions(pos)

val (completions, result) =
compilerCompletions.map(CompletionValue.Compiler(_)).filterInteresting()
val completions = compilerCompletions.map(CompletionValue.Compiler(_))
val args = Completions.namedArgCompletions(pos, path)

val values =
(completions ++ args).sorted(completionOrdering)
(values, result)
val (values, result) =
(completions ::: args).filterInteresting()

(values.sorted(completionOrdering), result)
}

private def description(sym: Symbol): String = {
Expand Down Expand Up @@ -89,13 +89,34 @@ class CompletionProvider(
s.sourcePos.exists && s1.sourcePos.exists && s.sourcePos.point > s1.sourcePos.point
}

extension (sym: Symbol) {
def detailString: String = {
if (sym.is(Method)) {
val sig = sym.signature
val sigString =
if (sig.paramsSig.isEmpty) "()"
else
sig.paramsSig
.map(p => p.toString)
.mkString("(", ",", ")")
sym.showFullName + sigString
} else {
sym.showFullName
}
}
}
extension (l: List[CompletionValue]) {
def filterInteresting(): (List[CompletionValue], SymbolSearch.Result) = {
val isSeen = mutable.Set.empty[String]
val buf = List.newBuilder[CompletionValue]
def visit(head: CompletionValue): Boolean = {
val sym = head.value.sym
val id = sym.showFullName
val id = head match {
case _: CompletionValue.NamedArg =>
sym.detailString + "="
case _ =>
sym.detailString
}
def isNotLocalForwardReference: Boolean =
!sym.isLocalToBlock ||
!sym.srcPos.isAfter(pos) ||
Expand Down Expand Up @@ -151,7 +172,9 @@ class CompletionProvider(

def hasGetter = try {
// isField returns true for some classes
sym.isField || sym.getter != NoSymbol
(sym.isField && !sym.is(JavaDefined)
&& !sym.is(Module) && !sym.isClass) ||
sym.getter != NoSymbol
} catch {
case _ => false
}
Expand Down Expand Up @@ -259,7 +282,7 @@ class CompletionProvider(
)
if (byParamCount != 0) byParamCount
else {
s1.show.compareTo(s2.show)
s1.detailString.compareTo(s2.detailString)
}
}
}
Expand Down
Expand Up @@ -293,34 +293,33 @@ case class ScalaPresentationCompiler(
val sourceFile = CompilerInterfaces.toSource(params.uri, params.text)
driver.run(uri, sourceFile)

val ctx = driver.currentCtx
given ctx: Context = driver.currentCtx
val pos = sourcePosition(driver, params, uri)
val trees = driver.openedTrees(uri)
val path = Interactive.pathTo(trees, pos)(using ctx)
val tp = Interactive.enclosingType(trees, pos)(using ctx)
val tpw = tp.widenTermRefExpr(using ctx)
val path = Interactive.pathTo(trees, pos)
val tp = Interactive.enclosingType(trees, pos)
val tpw = tp.widenTermRefExpr

if (tp.isError(using ctx) || tpw == NoType || tpw.isError(using ctx))
if (tp.isError || tpw == NoType || tpw.isError)
ju.Optional.empty()
else {
Interactive.enclosingSourceSymbols(path, pos)(using ctx) match {
Interactive.enclosingSourceSymbols(path, pos) match {
case Nil =>
ju.Optional.empty()
case symbols =>
val printer = SymbolPrinter()(using ctx)
val printer = SymbolPrinter()
val docComments =
symbols.flatMap(ParsedComment.docOf(_)(using ctx))
val keywordName = symbols.headOption.map { symbol =>
printer.fullDefinition(
symbol,
tpw
)
}
val typeString = symbols.headOption.map { symbol =>
symbols.flatMap(ParsedComment.docOf(_))
val hoverString = symbols.headOption.map { symbol =>
tpw match {
// https://github.com/lampepfl/dotty/issues/8891
case _: ImportType =>
printer.typeString(symbol.paramRef(using ctx))
case tpw: ImportType =>
val history = new ShortenedNames(ctx)
printer.hoverDetails(
symbol,
history,
symbol.paramRef
)
case _ =>
driver.compilationUnits.get(uri) match {
case Some(unit) =>
Expand All @@ -333,16 +332,14 @@ case class ScalaPresentationCompiler(
val context =
Interactive.contextOfPath(tpdPath)(using newctx)
val history = new ShortenedNames(context)
printer.infoString(symbol, history, tpw)(using context)
case None => printer.typeString(tpw)
printer.hoverDetails(symbol, history, tpw)(using context)
case None =>
val history = new ShortenedNames(ctx)
printer.hoverDetails(symbol, history, tpw)
}
}
}
val content = hoverContent(
keywordName,
typeString,
docComments
)(using ctx)
val content = hoverContent(hoverString, docComments)
ju.Optional.of(new Hover(content))
}
}
Expand Down Expand Up @@ -488,7 +485,12 @@ case class ScalaPresentationCompiler(
val printer = SymbolPrinter()
s" ${printer.fullNameString(sym.owner)}"
} else {
printer.infoString(sym, history, sym.info.widenTermRefExpr)(using ctx)
printer.hoverDetails(
sym,
history,
sym.info.widenTermRefExpr,
addFullDef = false
)(using ctx)
}
}

Expand Down Expand Up @@ -554,7 +556,7 @@ case class ScalaPresentationCompiler(

val documentation = ParsedComment.docOf(sym)
if (documentation.nonEmpty) {
item.setDocumentation(hoverContent(None, None, documentation.toList))
item.setDocumentation(hoverContent(None, documentation.toList))
}

if (sym.isDeprecated) {
Expand Down Expand Up @@ -629,15 +631,13 @@ case class ScalaPresentationCompiler(
}

private def hoverContent(
keywordName: Option[String],
typeInfo: Option[String],
comments: List[ParsedComment]
)(using ctx: Context): MarkupContent = {
val buf = new StringBuilder
typeInfo.foreach { info =>
val keyName = keywordName.getOrElse("")
buf.append(s"""```scala
|$keyName$info
|$info
|```
|""".stripMargin)
}
Expand Down
16 changes: 11 additions & 5 deletions mtags/src/main/scala-3/scala/meta/internal/pc/SymbolPrinter.scala
Expand Up @@ -61,18 +61,24 @@ class SymbolPrinter(using ctx: Context) extends RefinedPrinter(ctx) {
* - otherwise: its shortened type
* - e.g. ` java.lang.String` ` Symbols.Symbol`
*/
def infoString(
def hoverDetails(
sym: Symbol,
history: ShortenedNames,
info: Type
info: Type,
addFullDef: Boolean = true
)(using Context): String = {
val fullDef = if (addFullDef) fullDefinition(sym, info) else ""
// info is dealiased, while sym is not
val typeSymbol = info.typeSymbol
sym match {
case p if p.is(Flags.Package) => fullDef
case p if typeSymbol.is(Flags.Module) =>
fullDef.trim + " " + typeSymbol.owner.fullName.stripModuleClassSuffix.toString
case m if m.is(Flags.Method) =>
defaultMethodSignature(sym, history, info)
case p if p.is(Flags.Package) => ""
fullDef + defaultMethodSignature(m, history, info)
case _ =>
val short = shortType(info, history)
s"${typeString(short)}"
fullDef + s"${typeString(short)}"
}
}

Expand Down
Expand Up @@ -147,7 +147,7 @@ class HoverDefnSuite extends BaseHoverSuite {
|""".stripMargin,
"",
compat = Map(
"3.0" -> "object MyObject: MyObject".hover
"3.0" -> "object MyObject: object".hover
)
)

Expand Down
Expand Up @@ -52,7 +52,7 @@ class HoverScala3TypeSuite extends BaseHoverSuite {
| case <<Re@@d>>, Green, Blue
|
|""".stripMargin,
"""|case Red: enums.SimpleEnum.Color.Red
"""|case Red: Red
|""".stripMargin.hover
)

Expand Down
12 changes: 6 additions & 6 deletions tests/cross/src/test/scala/tests/hover/HoverTermSuite.scala
Expand Up @@ -56,7 +56,7 @@ class HoverTermSuite extends BaseHoverSuite {
"""|def apply(name: String): Person
|""".stripMargin.hover,
compat = Map(
"3.0" -> "case class Person: Person".hover
"3.0" -> "case class Person: case-apply".hover
)
)

Expand Down Expand Up @@ -147,7 +147,7 @@ class HoverTermSuite extends BaseHoverSuite {
|""".stripMargin.hover,
compat = Map(
// https://github.com/lampepfl/dotty/issues/8835
"3.0" -> "object num: Xtension#num".hover
"3.0" -> "object num: interpolator-unapply.a$.Xtension".hover
)
)

Expand Down Expand Up @@ -335,7 +335,7 @@ class HoverTermSuite extends BaseHoverSuite {
|```
|""".stripMargin,
compat = Map(
"3.0" -> "enum FileVisitResult: FileVisitResult".hover
"3.0" -> "enum FileVisitResult: java.nio.file".hover
)
)

Expand All @@ -358,7 +358,7 @@ class HoverTermSuite extends BaseHoverSuite {
|""".stripMargin,
automaticPackage = false,
compat = Map(
"3.0" -> "object Foo: Foo".hover
"3.0" -> "object Foo: app.Outer".hover
)
)

Expand Down Expand Up @@ -403,7 +403,7 @@ class HoverTermSuite extends BaseHoverSuite {
"""|class java.nio.file.Files
|""".stripMargin.hover,
compat = Map(
"3.0" -> "object Files: java.nio.file.Files".hover
"3.0" -> "object Files: java.nio.file".hover
)
)

Expand All @@ -415,7 +415,7 @@ class HoverTermSuite extends BaseHoverSuite {
"""|class java.nio.file.Paths
|""".stripMargin.hover,
compat = Map(
"3.0" -> "object Paths: java.nio.file.Paths".hover
"3.0" -> "object Paths: java.nio.file".hover
)
)

Expand Down

0 comments on commit 0560fa4

Please sign in to comment.