Skip to content

Commit e5d59ba

Browse files
committed
fix: don't use color codes for pattern match code action
1 parent 2d0e373 commit e5d59ba

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import printing.Formatting
1515
import ErrorMessageID.*
1616
import ast.Trees
1717
import config.{Feature, ScalaVersion}
18+
import transform.patmat.Space
19+
import transform.patmat.SpaceEngine
1820
import typer.ErrorReporting.{err, matchReductionAddendum, substitutableTypeSymbolsInScope}
1921
import typer.ProtoTypes.{ViewProto, SelectionProto, FunProto}
2022
import typer.Implicits.*
@@ -856,12 +858,13 @@ extends Message(LossyWideningConstantConversionID):
856858
|Write `.to$targetType` instead."""
857859
def explain(using Context) = ""
858860

859-
class PatternMatchExhaustivity(uncoveredCases: Seq[String], tree: untpd.Match)(using Context)
861+
class PatternMatchExhaustivity(uncoveredCases: Seq[Space], tree: untpd.Match)(using Context)
860862
extends Message(PatternMatchExhaustivityID) {
861863
def kind = MessageKind.PatternMatchExhaustivity
862864

863865
private val hasMore = uncoveredCases.lengthCompare(6) > 0
864-
val uncovered = uncoveredCases.take(6).mkString(", ")
866+
val uncovered = uncoveredCases.take(6).map(SpaceEngine.display(_)).mkString(", ")
867+
private val casesWithoutColor = uncoveredCases.map(SpaceEngine.display(_, withoutColors = true))
865868

866869
def msg(using Context) =
867870
val addendum = if hasMore then "(More unmatched cases are elided)" else ""
@@ -889,12 +892,12 @@ extends Message(PatternMatchExhaustivityID) {
889892
val pathes = List(
890893
ActionPatch(
891894
srcPos = endPos,
892-
replacement = uncoveredCases.map(c => indent(s"case $c => ???", startColumn))
895+
replacement = casesWithoutColor.map(c => indent(s"case $c => ???", startColumn))
893896
.mkString("\n", "\n", "")
894897
),
895898
)
896899
List(
897-
CodeAction(title = s"Insert missing cases (${uncoveredCases.size})",
900+
CodeAction(title = s"Insert missing cases (${casesWithoutColor.size})",
898901
description = None,
899902
patches = pathes
900903
)

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

+7-3
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,11 @@ object SpaceEngine {
721721
}
722722

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

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

841845
if uncovered.nonEmpty then
842846
val deduped = dedup(uncovered)
843-
report.warning(PatternMatchExhaustivity(deduped.map(display), m), m.selector)
847+
report.warning(PatternMatchExhaustivity(deduped, m), m.selector)
844848
}
845849

846850
private def reachabilityCheckable(sel: Tree)(using Context): Boolean =
@@ -903,7 +907,7 @@ object SpaceEngine {
903907
def checkMatch(m: Match)(using Context): Unit =
904908
checkMatchExhaustivityOnly(m)
905909
if reachabilityCheckable(m.selector) then checkReachability(m)
906-
910+
907911
def checkMatchExhaustivityOnly(m: Match)(using Context): Unit =
908912
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
909913
}

0 commit comments

Comments
 (0)