Skip to content

Commit 0f8c950

Browse files
authored
Merge pull request #6471 from dotty-staging/fix-3655
Fix #3655: Remove hl interpolator
2 parents 9df4dfb + b8110a6 commit 0f8c950

File tree

12 files changed

+362
-332
lines changed

12 files changed

+362
-332
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import collection.mutable.ListBuffer
1414
import reporting.diagnostic.messages._
1515
import reporting.trace
1616
import annotation.constructorOnly
17+
import printing.Formatting.hl
1718

1819
import scala.annotation.internal.sharable
1920

@@ -765,12 +766,12 @@ object desugar {
765766
def flagSourcePos(flag: FlagSet) = mods.mods.find(_.flags == flag).fold(mdef.sourcePos)(_.sourcePos)
766767

767768
if (mods.is(Abstract))
768-
ctx.error(hl"""$Abstract modifier cannot be used for objects""", flagSourcePos(Abstract))
769+
ctx.error(em"${hl("abstract")} modifier cannot be used for objects", flagSourcePos(Abstract))
769770
if (mods.is(Sealed))
770-
ctx.error(hl"""$Sealed modifier is redundant for objects""", flagSourcePos(Sealed))
771+
ctx.error(em"${hl("sealed")} modifier is redundant for objects", flagSourcePos(Sealed))
771772
// Maybe this should be an error; see https://github.com/scala/bug/issues/11094.
772773
if (mods.is(Final) && !mods.is(Synthetic))
773-
ctx.warning(hl"""$Final modifier is redundant for objects""", flagSourcePos(Final))
774+
ctx.warning(em"${hl("final")} modifier is redundant for objects", flagSourcePos(Final))
774775

775776
if (mods is Package)
776777
PackageDef(Ident(moduleName), cpy.ModuleDef(mdef)(nme.PACKAGE, impl).withMods(mods &~ Package) :: Nil)

compiler/src/dotty/tools/dotc/core/Decorators.scala

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ object Decorators {
184184
}
185185

186186
implicit class StringInterpolators(val sc: StringContext) extends AnyVal {
187-
188187
/** General purpose string formatting */
189188
def i(args: Any*)(implicit ctx: Context): String =
190189
new StringFormatter(sc).assemble(args)
@@ -200,10 +199,6 @@ object Decorators {
200199
*/
201200
def ex(args: Any*)(implicit ctx: Context): String =
202201
explained(implicit ctx => em(args: _*))
203-
204-
/** Formatter that adds syntax highlighting to all interpolated values */
205-
def hl(args: Any*)(implicit ctx: Context): String =
206-
new SyntaxFormatter(sc).assemble(args).stripMargin
207202
}
208203

209204
implicit class ArrayInterpolator[T <: AnyRef](val arr: Array[T]) extends AnyVal {

compiler/src/dotty/tools/dotc/printing/Formatting.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ object Formatting {
2323
* against accidentally treating an interpolated value as a margin.
2424
*/
2525
class StringFormatter(protected val sc: StringContext) {
26-
2726
protected def showArg(arg: Any)(implicit ctx: Context): String = arg match {
2827
case arg: Showable =>
2928
try arg.show
@@ -230,7 +229,7 @@ object Formatting {
230229
lazy val maxLen = parts.map(_._1.length).max
231230
parts.map {
232231
case (leader, trailer) =>
233-
val variable = hl"$leader"
232+
val variable = hl(leader)
234233
s"""$variable${" " * (maxLen - leader.length)} $trailer"""
235234
}
236235
}
@@ -300,4 +299,8 @@ object Formatting {
300299
case _ => (fnd, exp)
301300
}
302301
}
302+
303+
/** Explicit syntax highlighting */
304+
def hl(s: String)(implicit ctx: Context): String =
305+
SyntaxHighlighting.highlight(s)
303306
}

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
343343
val declsText =
344344
if (trueDecls.isEmpty || !ctx.settings.Ydebug.value) Text()
345345
else dclsText(trueDecls)
346-
tparamsText ~ " extends " ~ toTextParents(tp.parents) ~ "{" ~ selfText ~ declsText ~
346+
tparamsText ~ " extends " ~ toTextParents(tp.parents) ~~ "{" ~ selfText ~ declsText ~
347347
"} at " ~ preText
348348
case mt: MethodType =>
349349
toTextGlobal(mt)
@@ -424,7 +424,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
424424

425425
def toText(sym: Symbol): Text =
426426
(kindString(sym) ~~ {
427-
if (sym.isAnonymousClass) toText(sym.info.parents, " with ") ~ "{...}"
427+
if (sym.isAnonymousClass) toTextParents(sym.info.parents) ~~ "{...}"
428428
else if (hasMeaninglessName(sym)) simpleNameString(sym.owner) + idString(sym)
429429
else nameString(sym)
430430
}).close

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
116116
super.toTextPrefix(tp)
117117
}
118118

119+
override protected def toTextParents(parents: List[Type]): Text =
120+
Text(parents.map(toTextLocal).map(typeText), keywordStr(" with "))
121+
119122
override protected def refinementNameString(tp: RefinedType): String =
120123
if (tp.parent.isInstanceOf[WildcardType] || tp.refinedName == nme.WILDCARD)
121124
super.refinementNameString(tp)
@@ -212,7 +215,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
212215
case ErasedValueType(tycon, underlying) =>
213216
return "ErasedValueType(" ~ toText(tycon) ~ ", " ~ toText(underlying) ~ ")"
214217
case tp: ClassInfo =>
215-
return toTextParents(tp.parents) ~ "{...}"
218+
return toTextParents(tp.parents) ~~ "{...}"
216219
case JavaArrayType(elemtp) =>
217220
return toText(elemtp) ~ "[]"
218221
case tp: AnnotatedType if homogenizedView =>
@@ -825,14 +828,21 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
825828
case info: ImportType => return s"import $info.expr.show"
826829
case _ =>
827830
}
828-
if (sym.is(ModuleClass)) {
829-
val name =
830-
if (sym.isPackageObject && sym.name.stripModuleClassSuffix == tpnme.PACKAGE) sym.owner.name
831-
else sym.name.stripModuleClassSuffix
832-
kindString(sym) ~~ (nameString(name) + idString(sym))
833-
}
834-
else
835-
super.toText(sym)
831+
def name =
832+
if (sym.is(ModuleClass) && sym.isPackageObject && sym.name.stripModuleClassSuffix == tpnme.PACKAGE)
833+
nameString(sym.owner.name)
834+
else if (sym.is(ModuleClass))
835+
nameString(sym.name.stripModuleClassSuffix)
836+
else if (hasMeaninglessName(sym))
837+
simpleNameString(sym.owner)
838+
else
839+
nameString(sym)
840+
(keywordText(kindString(sym)) ~~ {
841+
if (sym.isAnonymousClass)
842+
toTextParents(sym.info.parents) ~~ "{...}"
843+
else
844+
typeText(name)
845+
}).close
836846
}
837847

838848
/** String representation of symbol's kind. */

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ trait MessageRendering {
134134
/** Explanation rendered under "Explanation" header */
135135
def explanation(m: Message)(implicit ctx: Context): String = {
136136
val sb = new StringBuilder(
137-
hl"""|
138-
|${Blue("Explanation")}
139-
|${Blue("===========")}"""
137+
s"""|
138+
|${Blue("Explanation").show}
139+
|${Blue("===========").show}""".stripMargin
140140
)
141141
sb.append(EOL).append(m.explanation)
142142
if (m.explanation.lastOption != Some(EOL)) sb.append(EOL)

0 commit comments

Comments
 (0)