Skip to content

Commit

Permalink
fix: don't use color codes for pattern match code action
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Jul 8, 2024
1 parent 2d0e373 commit e5d59ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 7 additions & 4 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import printing.Formatting
import ErrorMessageID.*
import ast.Trees
import config.{Feature, ScalaVersion}
import transform.patmat.Space
import transform.patmat.SpaceEngine
import typer.ErrorReporting.{err, matchReductionAddendum, substitutableTypeSymbolsInScope}
import typer.ProtoTypes.{ViewProto, SelectionProto, FunProto}
import typer.Implicits.*
Expand Down Expand Up @@ -856,12 +858,13 @@ extends Message(LossyWideningConstantConversionID):
|Write `.to$targetType` instead."""
def explain(using Context) = ""

class PatternMatchExhaustivity(uncoveredCases: Seq[String], tree: untpd.Match)(using Context)
class PatternMatchExhaustivity(uncoveredCases: Seq[Space], tree: untpd.Match)(using Context)
extends Message(PatternMatchExhaustivityID) {
def kind = MessageKind.PatternMatchExhaustivity

private val hasMore = uncoveredCases.lengthCompare(6) > 0
val uncovered = uncoveredCases.take(6).mkString(", ")
val uncovered = uncoveredCases.take(6).map(SpaceEngine.display(_)).mkString(", ")
private val casesWithoutColor = uncoveredCases.map(SpaceEngine.display(_, withoutColors = true))

def msg(using Context) =
val addendum = if hasMore then "(More unmatched cases are elided)" else ""
Expand Down Expand Up @@ -889,12 +892,12 @@ extends Message(PatternMatchExhaustivityID) {
val pathes = List(
ActionPatch(
srcPos = endPos,
replacement = uncoveredCases.map(c => indent(s"case $c => ???", startColumn))
replacement = casesWithoutColor.map(c => indent(s"case $c => ???", startColumn))
.mkString("\n", "\n", "")
),
)
List(
CodeAction(title = s"Insert missing cases (${uncoveredCases.size})",
CodeAction(title = s"Insert missing cases (${casesWithoutColor.size})",
description = None,
patches = pathes
)
Expand Down
10 changes: 7 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,11 @@ object SpaceEngine {
}

/** Display spaces. Used for printing uncovered spaces in the in-exhaustive error message. */
def display(s: Space)(using Context): String = inContext(ctx.fresh.setPrinterFn(LocalPrinter(_))) {
def display(s: Space, withoutColors: Boolean = false)(using Context): String = inContext{
val newCtx = ctx.fresh.setPrinterFn(LocalPrinter(_))
if withoutColors then newCtx.withoutColors
else newCtx
} {
def params(tp: Type): List[Type] = tp.classSymbol.primaryConstructor.info.firstParamTypes

/** does the companion object of the given symbol have custom unapply */
Expand Down Expand Up @@ -840,7 +844,7 @@ object SpaceEngine {

if uncovered.nonEmpty then
val deduped = dedup(uncovered)
report.warning(PatternMatchExhaustivity(deduped.map(display), m), m.selector)
report.warning(PatternMatchExhaustivity(deduped, m), m.selector)
}

private def reachabilityCheckable(sel: Tree)(using Context): Boolean =
Expand Down Expand Up @@ -903,7 +907,7 @@ object SpaceEngine {
def checkMatch(m: Match)(using Context): Unit =
checkMatchExhaustivityOnly(m)
if reachabilityCheckable(m.selector) then checkReachability(m)

def checkMatchExhaustivityOnly(m: Match)(using Context): Unit =
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
}

0 comments on commit e5d59ba

Please sign in to comment.