Skip to content

Fix #3655: Remove hl interpolator #6471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 12, 2019
Merged
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
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import collection.mutable.ListBuffer
import reporting.diagnostic.messages._
import reporting.trace
import annotation.constructorOnly
import printing.Formatting.hl

import scala.annotation.internal.sharable

Expand Down Expand Up @@ -762,12 +763,12 @@ object desugar {
def flagSourcePos(flag: FlagSet) = mods.mods.find(_.flags == flag).fold(mdef.sourcePos)(_.sourcePos)

if (mods.is(Abstract))
ctx.error(hl"""$Abstract modifier cannot be used for objects""", flagSourcePos(Abstract))
ctx.error(em"${hl("abstract")} modifier cannot be used for objects", flagSourcePos(Abstract))
if (mods.is(Sealed))
ctx.error(hl"""$Sealed modifier is redundant for objects""", flagSourcePos(Sealed))
ctx.error(em"${hl("sealed")} modifier is redundant for objects", flagSourcePos(Sealed))
// Maybe this should be an error; see https://github.com/scala/bug/issues/11094.
if (mods.is(Final) && !mods.is(Synthetic))
ctx.warning(hl"""$Final modifier is redundant for objects""", flagSourcePos(Final))
ctx.warning(em"${hl("final")} modifier is redundant for objects", flagSourcePos(Final))

if (mods is Package)
PackageDef(Ident(moduleName), cpy.ModuleDef(mdef)(nme.PACKAGE, impl).withMods(mods &~ Package) :: Nil)
Expand Down
5 changes: 0 additions & 5 deletions compiler/src/dotty/tools/dotc/core/Decorators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ object Decorators {
}

implicit class StringInterpolators(val sc: StringContext) extends AnyVal {

/** General purpose string formatting */
def i(args: Any*)(implicit ctx: Context): String =
new StringFormatter(sc).assemble(args)
Expand All @@ -200,10 +199,6 @@ object Decorators {
*/
def ex(args: Any*)(implicit ctx: Context): String =
explained(implicit ctx => em(args: _*))

/** Formatter that adds syntax highlighting to all interpolated values */
def hl(args: Any*)(implicit ctx: Context): String =
new SyntaxFormatter(sc).assemble(args).stripMargin
}

implicit class ArrayInterpolator[T <: AnyRef](val arr: Array[T]) extends AnyVal {
Expand Down
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/printing/Formatting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ object Formatting {
* against accidentally treating an interpolated value as a margin.
*/
class StringFormatter(protected val sc: StringContext) {

protected def showArg(arg: Any)(implicit ctx: Context): String = arg match {
case arg: Showable =>
try arg.show
Expand Down Expand Up @@ -207,7 +206,7 @@ object Formatting {
lazy val maxLen = parts.map(_._1.length).max
parts.map {
case (leader, trailer) =>
val variable = hl"$leader"
val variable = hl(leader)
s"""$variable${" " * (maxLen - leader.length)} $trailer"""
}
}
Expand Down Expand Up @@ -277,4 +276,8 @@ object Formatting {
case _ => (fnd, exp)
}
}

/** Explicit syntax highlighting */
def hl(s: String)(implicit ctx: Context): String =
SyntaxHighlighting.highlight(s)
}
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
val declsText =
if (trueDecls.isEmpty || !ctx.settings.Ydebug.value) Text()
else dclsText(trueDecls)
tparamsText ~ " extends " ~ toTextParents(tp.parents) ~ "{" ~ selfText ~ declsText ~
tparamsText ~ " extends " ~ toTextParents(tp.parents) ~~ "{" ~ selfText ~ declsText ~
"} at " ~ preText
case mt: MethodType =>
toTextGlobal(mt)
Expand Down Expand Up @@ -424,7 +424,7 @@ class PlainPrinter(_ctx: Context) extends Printer {

def toText(sym: Symbol): Text =
(kindString(sym) ~~ {
if (sym.isAnonymousClass) toText(sym.info.parents, " with ") ~ "{...}"
if (sym.isAnonymousClass) toTextParents(sym.info.parents) ~~ "{...}"
else if (hasMeaninglessName(sym)) simpleNameString(sym.owner) + idString(sym)
else nameString(sym)
}).close
Expand Down
28 changes: 19 additions & 9 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
super.toTextPrefix(tp)
}

override protected def toTextParents(parents: List[Type]): Text =
Text(parents.map(toTextLocal).map(typeText), keywordStr(" with "))

override protected def refinementNameString(tp: RefinedType): String =
if (tp.parent.isInstanceOf[WildcardType] || tp.refinedName == nme.WILDCARD)
super.refinementNameString(tp)
Expand Down Expand Up @@ -212,7 +215,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
case ErasedValueType(tycon, underlying) =>
return "ErasedValueType(" ~ toText(tycon) ~ ", " ~ toText(underlying) ~ ")"
case tp: ClassInfo =>
return toTextParents(tp.parents) ~ "{...}"
return toTextParents(tp.parents) ~~ "{...}"
case JavaArrayType(elemtp) =>
return toText(elemtp) ~ "[]"
case tp: AnnotatedType if homogenizedView =>
Expand Down Expand Up @@ -825,14 +828,21 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
case info: ImportType => return s"import $info.expr.show"
case _ =>
}
if (sym.is(ModuleClass)) {
val name =
if (sym.isPackageObject && sym.name.stripModuleClassSuffix == tpnme.PACKAGE) sym.owner.name
else sym.name.stripModuleClassSuffix
kindString(sym) ~~ (nameString(name) + idString(sym))
}
else
super.toText(sym)
def name =
if (sym.is(ModuleClass) && sym.isPackageObject && sym.name.stripModuleClassSuffix == tpnme.PACKAGE)
nameString(sym.owner.name)
else if (sym.is(ModuleClass))
nameString(sym.name.stripModuleClassSuffix)
else if (hasMeaninglessName(sym))
simpleNameString(sym.owner)
else
nameString(sym)
(keywordText(kindString(sym)) ~~ {
if (sym.isAnonymousClass)
toTextParents(sym.info.parents) ~~ "{...}"
else
typeText(name)
}).close
}

/** String representation of symbol's kind. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ trait MessageRendering {
/** Explanation rendered under "Explanation" header */
def explanation(m: Message)(implicit ctx: Context): String = {
val sb = new StringBuilder(
hl"""|
|${Blue("Explanation")}
|${Blue("===========")}"""
s"""|
|${Blue("Explanation").show}
|${Blue("===========").show}""".stripMargin
)
sb.append(EOL).append(m.explanation)
if (m.explanation.lastOption != Some(EOL)) sb.append(EOL)
Expand Down
Loading