Skip to content

Commit

Permalink
aggregated settings into typeDetail and typeDiffsDetail
Browse files Browse the repository at this point in the history
fixed all test cases
  • Loading branch information
tribbloid committed Sep 25, 2023
1 parent 62283e2 commit 5d09a6f
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 217 deletions.
17 changes: 17 additions & 0 deletions core/src/main/scala-2.13.7+/latest/splain/SplainAnalyzer.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package splain

import scala.collection.mutable
import scala.tools.nsc._

class SplainAnalyzer(val global: Global, val pluginSettings: PluginSettings)
Expand All @@ -17,4 +18,20 @@ class SplainAnalyzer(val global: Global, val pluginSettings: PluginSettings)
// RefinedFormatter,
ByNameFormatter
)

override def splainFoundReqMsg(found: global.Type, req: global.Type): String = {
val original = super.splainFoundReqMsg(found, req)

val extra = mutable.Buffer.empty[String]

if (pluginSettings.debug) {

extra += "===[ ORIGINAL ERROR ]===" +
builtinFoundReqMsg(found, req) +
"\n"
}

val result = (Seq(original) ++ extra.toSeq).mkString("\n")
result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -380,28 +380,6 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with
result
}

override def splainFoundReqMsg(found: Type, req: Type): String = {
val body =
if (settings.VtypeDiffs.value) {
val formatted = formatDiff(found, req, top = true)
val show = showFormattedL(formatted, break = true)

";\n" + show.indent.joinLines
} else ""

val extra = mutable.Buffer.empty[String]

if (pluginSettings.debug) {

extra += "===[ ORIGINAL ERROR ]===" +
builtinFoundReqMsg(found, req) +
"\n"
}

val result = (Seq(body) ++ extra.toSeq).mkString("\n")
result
}

override def extractArgs(tpe: Type): List[global.Type] = TypeView(tpe).extractArgs

override def stripType(tt: Type): (List[String], String) = {
Expand Down Expand Up @@ -441,7 +419,7 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with

def element: Formatted

def annotations(break: Boolean): Seq[String]
def info(break: Boolean): Seq[String]
}

object Based {
Expand All @@ -451,18 +429,18 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with

case class Reduction(
element: Formatted,
basedOn: Seq[(String, Formatted)]
from: Seq[(String, Formatted)]
) extends Based {

def index(): Unit = {

if (pluginSettings.typeReduction)
if (pluginSettings.showTypeReduction)
Based.lookup += FormattedIndex(element) -> this
}

override def annotations(break: Boolean): Seq[String] = {
override def info(break: Boolean): Seq[String] = {

val extra = basedOn.flatMap {
val extra = from.flatMap {
case (clause, ft) =>
Seq(s".. ($clause)") ++
showFormattedLImpl(ft, break).lines.map { line =>
Expand All @@ -474,36 +452,75 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with
}
}

case class ExplainDiff(
case class BuiltInDiffMsg(
element: Formatted,
basedOn: String,
infix: String = "|"
msg: String,
infixOpt: Option[Formatted] = None
) extends Based {

def index(): Unit = {

Based.lookup += FormattedIndex(element) -> this
if (pluginSettings.TypeDiffsDetail.builtInMsg)
Based.lookup += FormattedIndex(element) -> this
}

override def annotations(break: Boolean): Seq[String] = {
override def info(break: Boolean): Seq[String] = {

val indented = basedOn
lazy val infixText = infixOpt match {
case None => "|"
case Some(ii) => showFormattedLImpl(ii, break).flat
}

val indented = msg
.split("\n")
.filter(_ != ";")
.map(v => s" $v")

Seq(s".. (type arguments in <found>$infix<required> are different)") ++ indented
Seq(s".. (comparing <found>$infixText<required>)") ++ indented
}
}

def formatTypeRaw(tpe: Type, top: Boolean): Formatted = {
formatWithInfix(tpe, extractArgs(tpe), top)(formatType)
}

val _EQ = Qualified(List(), InfixName("=:="))
val _SUB = Qualified(List(), InfixName("<:<"))

override def formatTypeImpl(tpe: Type, top: Boolean): Formatted = {

if (pluginSettings.TypeDiffsDetail.disambiguation) {

tpe.typeArgs match {
case List(t1, t2) =>
val result = withDisambiguation(Nil, t1, t2) {
formatTypeImplNoDisambiguation(tpe, top)
}

result match {
case Infix(ii, left, right, _) =>
val noApparentDiff = (left == right) && (t1 != t2)

if (noApparentDiff || pluginSettings.TypeDiffsDetail.builtInMsgAlways) {

BuiltInDiffMsg(
result,
TypeDiffView(t1, t2).builtInDiffMsg,
Some(ii)
).index()
}
case _ =>
}

result
case _ =>
formatTypeImplNoDisambiguation(tpe, top)
}

} else {

formatTypeImplNoDisambiguation(tpe, top)
}
}

protected def formatTypeImplNoDisambiguation(tpe: Type, top: Boolean): Formatted = {
val dtpe = dealias(tpe)

val results = Seq(tpe, dtpe).distinct.map { t =>
Expand All @@ -518,35 +535,40 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with

val result = results.last

result match {
case Infix(ii, left, right, _) =>
val noApparentDiff = left == right

val diffInfix = if (ii == _EQ) {
Some(_EQ.tpe.name)
} else if (ii == _SUB) {
Some(_SUB.tpe.name)
} else None

if (noApparentDiff && diffInfix.nonEmpty) {

tpe.typeArgs match {
case List(t1, t2) =>
ExplainDiff(
result,
TypeDiffView(t1, t2).builtInDiffMsg,
s" ${diffInfix.get.name} "
).index()
case _ =>
}
}
case _ =>
}

result
}

override def formatDiffImpl(found: Type, req: Type, top: Boolean): Formatted = {

if (pluginSettings.TypeDiffsDetail.disambiguation) {

val result = withDisambiguation(Nil, found, req) {
formatDiffImplNoDisambiguation(found, req, top)
}

result match {
case diff: Diff =>
val noApparentDiff = (diff.left == diff.right) && (found != req)

if (noApparentDiff || pluginSettings.TypeDiffsDetail.builtInMsgAlways) {

BuiltInDiffMsg(
diff,
TypeDiffView(found, req).builtInDiffMsg
).index()
}
case _ =>
}

result
} else {

formatDiffImplNoDisambiguation(found, req, top)
}
}

protected def formatDiffImplNoDisambiguation(found: Type, req: Type, top: Boolean): Formatted = {

val (left, right) = dealias(found) -> dealias(req)

val normalized = Seq(left, right).map(_.normalize).distinct
Expand Down Expand Up @@ -582,21 +604,6 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with
formatDiffSpecial(left, right, top).getOrElse {

val result = formatDiffSimple(left, right)

result match {
case diff: Diff =>
val noApparentDiff = diff.left == diff.right

if (noApparentDiff) {

ExplainDiff(
diff,
TypeDiffView(left, right).builtInDiffMsg
).index()
}
case _ =>
}

result
}
}
Expand Down Expand Up @@ -698,10 +705,10 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with
raw match {
case t: FlatType =>
BrokenType(
List(t.flat) ++ based.annotations(break)
List(t.flat) ++ based.info(break)
)
case t: BrokenType =>
t.copy(t.lines ++ based.annotations(break))
t.copy(t.lines ++ based.info(break))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ trait SplainPluginLike extends plugins.Plugin {
val description = "better types and implicit errors"
val components: List[PluginComponent] = Nil

val opts: mutable.Map[String, String] = PluginSettings.defaults.to(mutable.Map)
val opts: mutable.Map[String, String] = PluginSettings.inits.to(mutable.Map)
}
57 changes: 27 additions & 30 deletions core/src/main/scala-2.13.7+/latest/splain/TyperCompatViews.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,39 @@ trait TyperCompatViews {
self.prefix.typeSymbol.fullNameString
}

lazy val prefixContext: String = { s"in $prefixFullName" }
// probably not useful, withDisambiguation + longString should cover most cases
lazy val prefixContextIfNeeded: Option[String] = {

lazy val typeToString: String = {
prefixFullName.toLowerCase match {
case "<root>" | "<empty>" | "<none>" => None
case _ =>
if (self.toLongString.startsWith(prefixFullName)) None
else {
Some(s"(in $prefixFullName)")
}
}
}

val details = pluginSettings.typeDetails.getOrElse(1)
def typeToString: String = {

lazy val short = self.safeToString
lazy val long = scala.util.Try(self.toLongString).getOrElse(short)
val detailLvl = pluginSettings.typeDetail

lazy val ec = existentialContext(self)
def short = self.safeToString
def long = scala.util.Try(self.toLongString).getOrElse(short)

lazy val pc =
if (long.startsWith(prefixFullName)) ""
else {
s" {$prefixContext}"
}
def maybeContext = scala.util.Try(existentialContext(self)).toOption

lazy val withEc = scala.util
.Try(
long + ec
)
.getOrElse(long)
def maybeAlias = scala.util.Try(explainAlias(self)).toOption

lazy val withPcEc = scala.util
.Try(
long + pc + ec
)
.getOrElse(withEc)
detailLvl match {
case i if i <= 1 => short
case 2 => long
case 3 =>
(Seq(long) ++ maybeContext).mkString("")

case i if i >= 4 =>
(Seq(long) ++ maybeContext ++ maybeAlias).mkString("")

if (details <= 1) {
short
} else if (details == 2) {
long
} else if (details == 3) {
withEc
} else {
withPcEc
}
}
}
Expand Down Expand Up @@ -123,7 +119,8 @@ trait TyperCompatViews {
// }

lazy val builtInDiffMsg: String = {
builtinFoundReqMsg(found, req)
val result = builtinFoundReqMsg(found, req)
result
}
}

Expand Down
Loading

0 comments on commit 5d09a6f

Please sign in to comment.