Skip to content

Commit

Permalink
Merge pull request #2821 from tgodzik/shorten-importable-types
Browse files Browse the repository at this point in the history
Print better types on hover
  • Loading branch information
tgodzik authored May 26, 2021
2 parents 7ef52f2 + b972860 commit 4e10f57
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 36 deletions.
12 changes: 11 additions & 1 deletion mtags/src/main/scala-3/scala/meta/internal/pc/Signatures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ class ShortenedNames(context: Context) {
val foundTpe = lookupSymbol(short)
val syms = List(foundTpe.termSymbol, foundTpe.typeSymbol)
val isOk = syms.filter(_ != NoSymbol) match {
case Nil => false
case Nil =>
if (
short.symbol.isStatic || // Java static
short.symbol.owner.ownersIterator.forall { s =>
// ensure the symbol can be referenced in a static manner, without any instance
s.is(Package) || s.is(Module)
}
) {
history(short.name) = short
true
} else false
case founds => founds.exists(_ == short.symbol)
}
if (isOk) {
Expand Down
27 changes: 23 additions & 4 deletions mtags/src/main/scala-3/scala/meta/internal/pc/SymbolPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,34 @@ class SymbolPrinter(using ctx: Context) extends RefinedPrinter(ctx) {
}

def fullDefinition(sym: Symbol, tpe: Type): String = {
val isImplicit = sym.is(Flags.Implicit)
val name = nameString(sym)
val implicitKeyword = if (isImplicit) "implicit " else ""
def name = nameString(sym)
keyString(sym) match {
case "" =>
val isImplicit = sym.is(Flags.Implicit)
val implicitKeyword = if (isImplicit) "implicit " else ""
s"$implicitKeyword$name: "
case key if sym.is(Flags.Method) =>
s"$key $name"
// package
case key if sym.is(Flags.Package) =>
val owners = for {
sym <- sym.ownersIterator
if !(sym.isRoot || sym.isEmptyPackage)
name = super.nameString(sym.name)
} yield name
"package " + owners.toList.reverse.mkString(".")
// enum
case _ if sym.companionClass.is(Flags.Enum) =>
s"enum $name: "
// enum case
case _ if sym.is(Flags.EnumVal) =>
s"case $name: "
// default
case key =>
s"$key $name: "
// no need to add final on object, since they are all final
val finalKeyword =
if (sym.is(Flags.Final) && !sym.is(Flags.Module)) "final " else ""
s"$finalKeyword$key $name: "
}
}

Expand All @@ -51,6 +69,7 @@ class SymbolPrinter(using ctx: Context) extends RefinedPrinter(ctx) {
sym match {
case m if m.is(Flags.Method) =>
defaultMethodSignature(sym, history, info)
case p if p.is(Flags.Package) => ""
case _ =>
val short = shortType(info, history)
s"${typeString(short)}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class HoverScala3TypeSuite extends BaseHoverSuite {
| case <<Re@@d>>, Green, Blue
|
|""".stripMargin,
"""|val Red: enums.SimpleEnum.Color.Red
"""|case Red: enums.SimpleEnum.Color.Red
|""".stripMargin.hover
)

Expand All @@ -67,7 +67,7 @@ class HoverScala3TypeSuite extends BaseHoverSuite {
|
|
|""".stripMargin,
"""|val Green: Color
"""|case Green: Color
|""".stripMargin.hover
)

Expand Down
28 changes: 8 additions & 20 deletions tests/cross/src/test/scala/tests/hover/HoverTermSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ class HoverTermSuite extends BaseHoverSuite {
|def f[A >: Any](args: A*): String = macro
|""".stripMargin.hover,
compat = Map(
"3.0" -> "def f[A >: Any](args: A*): String".hover,
"0." -> ("def f: String".stripMargin.hover +
"\nImplementation of scala.StringContext.f used in Dotty")
"3.0" -> "def f[A >: Any](args: A*): String".hover
)
)

Expand Down Expand Up @@ -212,7 +210,7 @@ class HoverTermSuite extends BaseHoverSuite {
|""".stripMargin,
"",
compat = Map(
"3.0" -> "class Foo: new-anon.Foo".hover
"3.0" -> "class Foo: Foo".hover
)
)

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

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

Expand All @@ -372,10 +370,7 @@ class HoverTermSuite extends BaseHoverSuite {
"""|```scala
|package java.nio
|```
|""".stripMargin,
compat = Map(
"3.0" -> "object nio: java.nio".hover
)
|""".stripMargin
)

check(
Expand All @@ -386,10 +381,7 @@ class HoverTermSuite extends BaseHoverSuite {
"""|```scala
|package java
|```
|""".stripMargin,
compat = Map(
"3.0" -> "object java: java".hover
)
|""".stripMargin
)

check(
Expand All @@ -400,10 +392,7 @@ class HoverTermSuite extends BaseHoverSuite {
"""|```scala
|package java.nio.file
|```
|""".stripMargin,
compat = Map(
"3.0" -> "object file: java.nio.file".hover
)
|""".stripMargin
)

check(
Expand Down Expand Up @@ -467,8 +456,7 @@ class HoverTermSuite extends BaseHoverSuite {
|""".stripMargin,
"""final val CONTINUE: FileVisitResult""".hover,
compat = Map(
"3.0" -> "val CONTINUE: java.nio.file.FileVisitResult".hover,
"0." -> "val CONTINUE: (CONTINUE : java.nio.file.FileVisitResult)".hover
"3.0" -> "case CONTINUE: FileVisitResult".hover
)
)
}
18 changes: 9 additions & 9 deletions tests/cross/src/test/scala/tests/pc/CompletionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ class CompletionSuite extends BaseCompletionSuite {
"""|concat[T: ClassTag](xss: Array[T]*): Array[T]
|""".stripMargin,
compat = Map(
"3.0" -> "concat[T: scala.reflect.ClassTag](xss: Array[T]*): Array[T]"
"3.0" -> "concat[T: ClassTag](xss: Array[T]*): Array[T]"
)
)

Expand Down Expand Up @@ -615,11 +615,11 @@ class CompletionSuite extends BaseCompletionSuite {
|""".stripMargin,
compat = Map(
"3.0.0-RC1" ->
"""|readAttributes(x$0: java.nio.file.Path, x$1: String, x$2: java.nio.file.LinkOption*): java.util.Map[String, Object]
"""|readAttributes(x$0: Path, x$1: String, x$2: LinkOption*): java.util.Map[String, Object]
|""".stripMargin,
"3.0" ->
"""|readAttributes(x$0: java.nio.file.Path, x$1: String, x$2: java.nio.file.LinkOption*): java.util.Map[String, Object]
|readAttributes[A <: java.nio.file.attribute.BasicFileAttributes](x$0: java.nio.file.Path, x$1: Class[A], x$2: java.nio.file.LinkOption*): A
"""|readAttributes(x$0: Path, x$1: String, x$2: LinkOption*): java.util.Map[String, Object]
|readAttributes[A <: BasicFileAttributes](x$0: Path, x$1: Class[A], x$2: LinkOption*): A
|""".stripMargin
)
)
Expand Down Expand Up @@ -869,8 +869,8 @@ class CompletionSuite extends BaseCompletionSuite {
compat = Map(
"3.0" ->
"""|Some scala
|SomeToExpr[T: quoted.Type: quoted.ToExpr]: scala.quoted.ToExpr.SomeToExpr[T]
|SomeFromExpr[T](using quoted.Type[T], quoted.FromExpr[T]): scala.quoted.FromExpr.SomeFromExpr[T]
|SomeToExpr[T: Type: ToExpr]: SomeToExpr[T]
|SomeFromExpr[T](using Type[T], FromExpr[T]): SomeFromExpr[T]
|""".stripMargin
)
)
Expand All @@ -887,8 +887,8 @@ class CompletionSuite extends BaseCompletionSuite {
compat = Map(
"3.0" ->
"""|Some scala
|SomeToExpr[T: quoted.Type: quoted.ToExpr]: scala.quoted.ToExpr.SomeToExpr[T]
|SomeFromExpr[T](using quoted.Type[T], quoted.FromExpr[T]): scala.quoted.FromExpr.SomeFromExpr[T]
|SomeToExpr[T: Type: ToExpr]: SomeToExpr[T]
|SomeFromExpr[T](using Type[T], FromExpr[T]): SomeFromExpr[T]
|""".stripMargin
)
)
Expand Down Expand Up @@ -991,7 +991,7 @@ class CompletionSuite extends BaseCompletionSuite {
compat = Map(
"3.0.0-RC1" ->
"""|NotString: Int
|Number: scala.util.matching.Regex
|Number: Regex
|NegativeArraySizeException java.lang
|""".stripMargin,
"3.0" ->
Expand Down

0 comments on commit 4e10f57

Please sign in to comment.