Skip to content

Commit

Permalink
improvement:don't show types for match case for showInferredType (#5284)
Browse files Browse the repository at this point in the history
* improvement:don't show types for match case when showInferredType

* improvement: more granularity for showInferredType option

* improvement:don't show types for match case when showInferredType

* improvement: more granularity for showInferredType option

* improvement: fix test for showInferredType option

* improvement: finally fix test for showInferredType and backward compatiable

* Update metals/src/main/scala/scala/meta/internal/decorations/SyntheticsDecorationProvider.scala

Co-authored-by: Tomasz Godzik <[email protected]>

* improvement: add test for showInferredType

---------

Co-authored-by: Tomasz Godzik <[email protected]>
  • Loading branch information
doofin and tgodzik authored Jul 21, 2023
1 parent 0a0cede commit f759de6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ class SemanticdbTreePrinter(
* hello<<[String]>>("")
*/
case tree @ s.TypeApplyTree(_: s.OriginalTree | _: s.SelectTree, _)
if !ignoreTypesTrees && userConfig.showInferredType =>
if !ignoreTypesTrees && userConfig.showInferredType
.contains("true") =>
gatherSynthetics(tree)
/**
* implicit def implicitFun(object: T): R = ???
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ final class SyntheticsDecorationProvider(
}

private def areSyntheticsEnabled: Boolean = {
userConfig().showImplicitArguments || userConfig().showInferredType || userConfig().showImplicitConversionsAndClasses
val showInferredType = !userConfig().showInferredType.contains(
"false"
) && userConfig().showInferredType.nonEmpty
userConfig().showImplicitArguments || showInferredType || userConfig().showImplicitConversionsAndClasses
}

private def createHoverAtPoint(
Expand Down Expand Up @@ -445,7 +448,10 @@ final class SyntheticsDecorationProvider(
} yield decorationOptions(lspRange, decoration)

val typDecorations =
if (userConfig().showInferredType)
if (
userConfig().showInferredType.contains("true") |
userConfig().showInferredType.contains("minimal")
)
typeDecorations(path, textDocument, decorationPrinter)
else Nil
decorations ++ typDecorations
Expand Down Expand Up @@ -488,7 +494,10 @@ final class SyntheticsDecorationProvider(
case param: m.Term.Param =>
if (param.decltpe.isEmpty) List(param.name.pos.toSemanticdb) else Nil
case cs: m.Case =>
explorePatterns(List(cs.pat)) ++ visit(cs.body)
// if the case is too long then it'll be too messy
if (userConfig().showInferredType.contains("minimal"))
visit(cs.body) // don't show type hint for cases inside match
else explorePatterns(List(cs.pat)) ++ visit(cs.body)
case vl: m.Defn.Val =>
val values =
if (vl.decltpe.isEmpty) explorePatterns(vl.pats) else Nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ case class UserConfiguration(
bloopJvmProperties: Option[List[String]] = None,
ammoniteJvmProperties: Option[List[String]] = None,
superMethodLensesEnabled: Boolean = false,
showInferredType: Boolean = false,
showInferredType: Option[String] = None,
showImplicitArguments: Boolean = false,
showImplicitConversionsAndClasses: Boolean = false,
remoteLanguageServer: Option[String] = None,
Expand Down Expand Up @@ -484,7 +484,7 @@ object UserConfiguration {
val superMethodLensesEnabled =
getBooleanKey("super-method-lenses-enabled").getOrElse(false)
val showInferredType =
getBooleanKey("show-inferred-type").getOrElse(false)
getStringKey("show-inferred-type")
val showImplicitArguments =
getBooleanKey("show-implicit-arguments").getOrElse(false)
val showImplicitConversionsAndClasses =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,44 @@ class SyntheticDecorationsLspSuite extends BaseLspSuite("implicits") {
|}
|""".stripMargin
)
// minimal style for show-inferred-type : don't show types for match case
_ <- server.didChangeConfiguration(
"""{
| "show-implicit-arguments": true,
| "show-implicit-conversions-and-classes": true,
| "show-inferred-type": minimal
|}
|""".stripMargin
)
_ <- server.didOpen("a/src/main/scala/Main.scala")
_ <- server.didSave("a/src/main/scala/Main.scala")(identity)
_ = assertNoDiagnostics()
_ = assertNoDiff( // foo[t]() to foo()
client.workspaceDecorations,
"""|import scala.concurrent.Future
|case class Location(city: String)
|object Main{
| def hello()(implicit name: String, from: Location): Unit = {
| println(s"Hello $name from ${from.city}")
| }
| implicit val andy : String = "Andy"
|
| def greeting(): Unit = {
| implicit val boston: Location = Location("Boston")
| hello()(andy, boston)
| hello()(andy, boston); hello()(andy, boston)
| }
|
| val ordered: String = augmentString("acb").sorted(Char)
| augmentString("foo").map(c: Char => c.toInt)
| implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global
| Future{
| println("")
| }(ec)
|}
|""".stripMargin,
)
// full style for show-inferred-type
_ <- server.didChangeConfiguration(
"""{
| "show-implicit-arguments": true,
Expand Down

0 comments on commit f759de6

Please sign in to comment.