Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
def toText(denot: Denotation): Text = toText(denot.symbol) ~ "/D"

def toText(const: Constant): Text = const.tag match {
case StringTag => stringText(Chars.escapedString(const.value.toString, quoted = true))
case StringTag => literalText(Chars.escapedString(const.value.toString, quoted = true))
case ClazzTag => "classOf[" ~ toText(const.typeValue) ~ "]"
case CharTag => literalText(Chars.escapedChar(const.charValue))
case LongTag => literalText(const.longValue.toString + "L")
Expand Down Expand Up @@ -841,10 +841,9 @@ class PlainPrinter(_ctx: Context) extends Printer {

protected def keywordStr(text: String): String = coloredStr(text, SyntaxHighlighting.KeywordColor)
protected def keywordText(text: String): Text = coloredStr(text, SyntaxHighlighting.KeywordColor)
protected def valDefText(text: Text): Text = coloredText(text, SyntaxHighlighting.ValDefColor)
protected def valDefText(text: Text): Text = coloredText(text, SyntaxHighlighting.DefinitionColor)
protected def typeText(text: Text): Text = coloredText(text, SyntaxHighlighting.TypeColor)
protected def literalText(text: Text): Text = coloredText(text, SyntaxHighlighting.LiteralColor)
protected def stringText(text: Text): Text = coloredText(text, SyntaxHighlighting.StringColor)

protected def coloredStr(text: String, color: String): String =
if (ctx.useColors) color + text + SyntaxHighlighting.NoColor else text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
case tree @ Inlined(call, bindings, body) =>
val bodyText = if bindings.isEmpty then toText(body) else blockText(bindings :+ body)
if homogenizedView || !ctx.settings.XprintInline.value then bodyText
else if tree.inlinedFromOuterScope then stringText("{{") ~ stringText("/* inlined from outside */") ~ bodyText ~ stringText("}}")
else if tree.inlinedFromOuterScope then literalText("{{") ~ literalText("/* inlined from outside */") ~ bodyText ~ literalText("}}")
else keywordText("{{") ~ keywordText("/* inlined from ") ~ toText(call) ~ keywordText(" */") ~ bodyText ~ keywordText("}}")
case tpt: untpd.DerivedTypeTree =>
"<derived typetree watching " ~ tpt.watched.showSummary() ~ ">"
Expand Down
18 changes: 9 additions & 9 deletions compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ object SyntaxHighlighting {
val NoColor: String = Console.RESET
val CommentColor: String = Console.BLUE
val KeywordColor: String = Console.YELLOW
val ValDefColor: String = Console.CYAN
val DefinitionColor: String = Console.CYAN
val LiteralColor: String = Console.GREEN
val StringColor: String = Console.GREEN
val TypeColor: String = Console.MAGENTA
val AnnotationColor: String = Console.MAGENTA
val TypeColor: String = Console.GREEN

def highlight(in: String)(using Context): String = {
def freshCtx = ctx.fresh.setReporter(Reporter.NoReporter)
Expand Down Expand Up @@ -101,7 +99,7 @@ object SyntaxHighlighting {

def highlightAnnotations(tree: MemberDef): Unit =
for (annotation <- tree.rawMods.annotations)
highlightPosition(annotation.span, AnnotationColor)
highlightPosition(annotation.span, TypeColor)

def highlight(trees: List[Tree])(using Context): Unit =
trees.foreach(traverse)
Expand All @@ -112,14 +110,16 @@ object SyntaxHighlighting {
()
case tree: ValOrDefDef =>
highlightAnnotations(tree)
highlightPosition(tree.nameSpan, ValDefColor)
highlightPosition(tree.endSpan, ValDefColor)
highlightPosition(tree.nameSpan, DefinitionColor)
highlightPosition(tree.endSpan, DefinitionColor)
case tree: MemberDef /* ModuleDef | TypeDef */ =>
highlightAnnotations(tree)
highlightPosition(tree.nameSpan, TypeColor)
highlightPosition(tree.endSpan, TypeColor)
highlightPosition(tree.nameSpan, DefinitionColor)
highlightPosition(tree.endSpan, DefinitionColor)
case tree: Ident if tree.isType =>
highlightPosition(tree.span, TypeColor)
case tree: Select if tree.isType =>
highlightPosition(tree.nameSpan, TypeColor)
case _: TypeTree =>
highlightPosition(tree.span, TypeColor)
case _ =>
Expand Down
96 changes: 48 additions & 48 deletions compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ object SourceCode {
if (flags.is(Flags.Case)) this += highlightKeyword("case ")

if (name == "package$") {
this += highlightKeyword("package object ") += highlightTypeDef(cdef.symbol.owner.name.stripSuffix("$"))
this += highlightKeyword("package object ") += highlightType(cdef.symbol.owner.name.stripSuffix("$"))
}
else if (flags.is(Flags.Module)) this += highlightKeyword("object ") += highlightTypeDef(name.stripSuffix("$"))
else if (flags.is(Flags.Trait)) this += highlightKeyword("trait ") += highlightTypeDef(name)
else if (flags.is(Flags.Abstract)) this += highlightKeyword("abstract class ") += highlightTypeDef(name)
else this += highlightKeyword("class ") += highlightTypeDef(name)
else if (flags.is(Flags.Module)) this += highlightKeyword("object ") += highlightType(name.stripSuffix("$"))
else if (flags.is(Flags.Trait)) this += highlightKeyword("trait ") += highlightType(name)
else if (flags.is(Flags.Abstract)) this += highlightKeyword("abstract class ") += highlightType(name)
else this += highlightKeyword("class ") += highlightType(name)

if (!flags.is(Flags.Module)) {
for paramClause <- paramss do
Expand Down Expand Up @@ -237,7 +237,7 @@ object SourceCode {
val Some(ValDef(name, tpt, _)) = self: @unchecked
indented {
val name1 = if (name == "_") "this" else name
this += " " += highlightValDef(name1) += ": "
this += " " += highlightDefinition(name1) += ": "
printTypeTree(tpt)(using Some(cdef.symbol))
this += " =>"
}
Expand Down Expand Up @@ -279,7 +279,7 @@ object SourceCode {
else this += highlightKeyword("val ")

val name1 = splicedName(vdef.symbol).getOrElse(name)
this += highlightValDef(name1) += ": "
this += highlightDefinition(name1) += ": "
printTypeTree(tpt)
rhs match {
case Some(tree) =>
Expand Down Expand Up @@ -315,7 +315,7 @@ object SourceCode {
printProtectedOrPrivate(ddef)

val name1: String = if (isConstructor) "this" else splicedName(ddef.symbol).getOrElse(name)
this += highlightKeyword("def ") += highlightValDef(name1)
this += highlightKeyword("def ") += highlightDefinition(name1)
for clause <- paramss do
clause match
case TermParamClause(params) => printMethdArgsDefs(params)
Expand All @@ -337,7 +337,7 @@ object SourceCode {

case tree: Ident =>
splicedName(tree.symbol) match {
case Some(name) => this += highlightTypeDef(name)
case Some(name) => this += highlightType(name)
case _ => printType(tree.tpe)
}

Expand Down Expand Up @@ -438,7 +438,7 @@ object SourceCode {
printTree(term)
term match {
case Repeated(_, _) | Inlined(None, Nil, Repeated(_, _)) => this
case _ => this += ": " += highlightTypeDef("_*")
case _ => this += ": " += highlightType("_*")
}
case _ =>
inParens {
Expand Down Expand Up @@ -764,9 +764,9 @@ object SourceCode {

if (isDef) {
if (argDef.symbol.flags.is(Flags.Covariant)) {
this += highlightValDef("+")
this += highlightDefinition("+")
} else if (argDef.symbol.flags.is(Flags.Contravariant)) {
this += highlightValDef("-")
this += highlightDefinition("-")
}
}

Expand Down Expand Up @@ -873,26 +873,26 @@ object SourceCode {
printedPrefix = true
}
printedPrefix |= printProtectedOrPrivate(vdef)
if (vdef.symbol.flags.is(Flags.Mutable)) this += highlightValDef("var ")
else if (printedPrefix || !vdef.symbol.flags.is(Flags.CaseAccessor)) this += highlightValDef("val ")
if (vdef.symbol.flags.is(Flags.Mutable)) this += highlightDefinition("var ")
else if (printedPrefix || !vdef.symbol.flags.is(Flags.CaseAccessor)) this += highlightDefinition("val ")
}
}
end if

this += highlightValDef(name) += ": "
this += highlightDefinition(name) += ": "
printTypeTree(arg.tpt)
}

private def printCaseDef(caseDef: CaseDef): this.type = {
this += highlightValDef("case ")
this += highlightDefinition("case ")
printPattern(caseDef.pattern)
caseDef.guard match {
case Some(t) =>
this += " if "
printTree(t)
case None =>
}
this += highlightValDef(" =>")
this += highlightDefinition(" =>")
indented {
caseDef.rhs match {
case Block(stats, expr) =>
Expand All @@ -906,9 +906,9 @@ object SourceCode {
}

private def printTypeCaseDef(caseDef: TypeCaseDef): this.type = {
this += highlightValDef("case ")
this += highlightDefinition("case ")
printTypeTree(caseDef.pattern)
this += highlightValDef(" => ")
this += highlightDefinition(" => ")
printTypeTree(caseDef.rhs)
this
}
Expand All @@ -921,7 +921,7 @@ object SourceCode {
this += name

case Bind(name, Typed(Wildcard(), tpt)) =>
this += highlightValDef(name) += ": "
this += highlightDefinition(name) += ": "
printTypeTree(tpt)

case Bind(name, pattern) =>
Expand Down Expand Up @@ -972,8 +972,8 @@ object SourceCode {
case LongConstant(v) => this += highlightLiteral(v.toString + "L")
case FloatConstant(v) => this += highlightLiteral(v.toString + "f")
case DoubleConstant(v) => this += highlightLiteral(v.toString)
case CharConstant(v) => this += highlightString(Chars.escapedChar(v))
case StringConstant(v) => this += highlightString(Chars.escapedString(v, quoted = true))
case CharConstant(v) => this += highlightLiteral(Chars.escapedChar(v))
case StringConstant(v) => this += highlightLiteral(Chars.escapedString(v, quoted = true))
case ClassOfConstant(v) =>
this += "classOf"
inSquare(printType(v))
Expand Down Expand Up @@ -1027,10 +1027,10 @@ object SourceCode {
printType(tree.tpe)

case TypeSelect(qual, name) =>
printTree(qual) += "." += highlightTypeDef(name)
printTree(qual) += "." += highlightType(name)

case TypeProjection(qual, name) =>
printTypeTree(qual) += "#" += highlightTypeDef(name)
printTypeTree(qual) += "#" += highlightType(name)

case Singleton(ref) =>
printTree(ref)
Expand All @@ -1053,7 +1053,7 @@ object SourceCode {
case tpe: TypeRef if tpe.typeSymbol == Symbol.requiredClass("scala.annotation.internal.Repeated") =>
val Types.Sequence(tp) = tpt.tpe: @unchecked
printType(tp)
this += highlightTypeDef("*")
this += highlightType("*")
case _ =>
printTypeTree(tpt)
this += " "
Expand All @@ -1066,16 +1066,16 @@ object SourceCode {
inBlock(printTypeCases(cases, lineBreak()))

case ByName(result) =>
this += highlightTypeDef("=> ")
this += highlightType("=> ")
printTypeTree(result)

case LambdaTypeTree(tparams, body) =>
printTargsDefs(tparams.zip(tparams), isDef = false)
this += highlightTypeDef(" =>> ")
this += highlightType(" =>> ")
printTypeOrBoundsTree(body)

case TypeBind(name, _) =>
this += highlightTypeDef(name)
this += highlightType(name)

case TypeBlock(_, tpt) =>
printTypeTree(tpt)
Expand Down Expand Up @@ -1121,23 +1121,23 @@ object SourceCode {
printType(prefix)
this += "."
}
this += highlightTypeDef(sym.name.stripSuffix("$"))
this += highlightType(sym.name.stripSuffix("$"))

case TermRef(prefix, name) =>
if fullNames then
prefix match {
case NoPrefix() =>
this += highlightTypeDef(name)
this += highlightType(name)
case ThisType(tp) if tp.typeSymbol == defn.RootClass || tp.typeSymbol == defn.EmptyPackageClass =>
this += highlightTypeDef(name)
this += highlightType(name)
case _ =>
printType(prefix)
if (name != "package")
this += "." += highlightTypeDef(name)
this += "." += highlightType(name)
this
}
else
this += highlightTypeDef(name)
this += highlightType(name)

case tpe @ Refinement(_, _, _) =>
printRefinement(tpe)
Expand Down Expand Up @@ -1175,12 +1175,12 @@ object SourceCode {

case AndType(left, right) =>
printType(left)
this += highlightTypeDef(" & ")
this += highlightType(" & ")
printType(right)

case OrType(left, right) =>
printType(left)
this += highlightTypeDef(" | ")
this += highlightType(" | ")
printType(right)

case MatchType(bound, scrutinee, cases) =>
Expand All @@ -1189,14 +1189,14 @@ object SourceCode {
inBlock(printTypes(cases, lineBreak()))

case ByNameType(tp) =>
this += highlightTypeDef(" => ")
this += highlightType(" => ")
printType(tp)

case ThisType(tp) =>
tp match {
case tp: TypeRef if !tp.typeSymbol.flags.is(Flags.Module) =>
printFullClassName(tp)
this += highlightTypeDef(".this")
this += highlightType(".this")
case TypeRef(prefix, name) if name.endsWith("$") =>
if (fullNames){
prefix match {
Expand All @@ -1207,18 +1207,18 @@ object SourceCode {
this += "."
}
}
this += highlightTypeDef(name.stripSuffix("$"))
this += highlightType(name.stripSuffix("$"))
case _ =>
printType(tp)
}

case SuperType(thistpe, supertpe) =>
printType(thistpe)
this += highlightTypeDef(".super")
this += highlightType(".super")

case TypeLambda(paramNames, tparams, body) =>
inSquare(printMethodicTypeParams(paramNames, tparams))
this += highlightTypeDef(" =>> ")
this += highlightType(" =>> ")
printType(body)

case ParamRef(lambda, idx) =>
Expand All @@ -1232,7 +1232,7 @@ object SourceCode {
printType(tpe)

case RecursiveThis(_) =>
this += highlightTypeDef("this")
this += highlightType("this")

case tpe: MethodType =>
this += "("
Expand Down Expand Up @@ -1283,10 +1283,10 @@ object SourceCode {
}

private def printDefinitionName(tree: Definition): this.type = tree match {
case ValDef(name, _, _) => this += highlightValDef(name)
case DefDef(name, _, _, _) => this += highlightValDef(name)
case ClassDef(name, _, _, _, _) => this += highlightTypeDef(name.stripSuffix("$"))
case TypeDef(name, _) => this += highlightTypeDef(name)
case ValDef(name, _, _) => this += highlightDefinition(name)
case DefDef(name, _, _, _) => this += highlightDefinition(name)
case ClassDef(name, _, _, _, _) => this += highlightType(name.stripSuffix("$"))
case TypeDef(name, _) => this += highlightType(name)
}

private def printAnnotation(annot: Term)(using elideThis: Option[Symbol]): this.type = {
Expand Down Expand Up @@ -1336,13 +1336,13 @@ object SourceCode {
this += lineBreak()
info match {
case info: TypeBounds =>
this += highlightKeyword("type ") += highlightTypeDef(name)
this += highlightKeyword("type ") += highlightType(name)
printBounds(info)
case ByNameType(_) | MethodType(_, _, _) | TypeLambda(_, _, _) =>
this += highlightKeyword("def ") += highlightTypeDef(name)
this += highlightKeyword("def ") += highlightType(name)
printMethodicType(info)
case info: TypeRepr =>
this += highlightKeyword("val ") += highlightValDef(name)
this += highlightKeyword("val ") += highlightDefinition(name)
printMethodicType(info)
}
}
Expand Down
Loading
Loading