@@ -16,6 +16,7 @@ import config.Config
16
16
import transform .SymUtils ._
17
17
import scala .annotation .switch
18
18
import language .implicitConversions
19
+ import Highlighting ._
19
20
20
21
class RefinedPrinter (_ctx : Context ) extends PlainPrinter (_ctx) {
21
22
@@ -77,7 +78,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
77
78
override def toTextRef (tp : SingletonType ): Text = controlled {
78
79
tp match {
79
80
case tp : ThisType =>
80
- if (tp.cls.isAnonymousClass) return " this"
81
+ if (tp.cls.isAnonymousClass) return keywordStr( " this" )
81
82
if (tp.cls is ModuleClass ) return fullNameString(tp.cls.sourceModule)
82
83
case _ =>
83
84
}
@@ -120,7 +121,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
120
121
atPrec(InfixPrec ) { argText(args.head) }
121
122
else
122
123
toTextTuple(args.init)
123
- (" implicit " provided isImplicit) ~ argStr ~ " => " ~ argText(args.last)
124
+ (keywordStr( " implicit " ) provided isImplicit) ~ argStr ~ " => " ~ argText(args.last)
124
125
}
125
126
126
127
def isInfixType (tp : Type ): Boolean = tp match {
@@ -233,18 +234,18 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
233
234
234
235
def enumText (tree : untpd.Tree ) = tree match { // DD
235
236
case _ : untpd.GenFrom | _ : untpd.GenAlias => toText(tree)
236
- case _ => " if " ~ toText(tree)
237
+ case _ => keywordStr( " if " ) ~ toText(tree)
237
238
}
238
239
239
240
def forText (enums : List [untpd.Tree ], expr : untpd.Tree , sep : String ): Text = // DD
240
- changePrec(GlobalPrec ) { " for " ~ Text (enums map enumText, " ; " ) ~ sep ~ toText(expr) }
241
+ changePrec(GlobalPrec ) { keywordStr( " for " ) ~ Text (enums map enumText, " ; " ) ~ sep ~ toText(expr) }
241
242
242
243
def cxBoundToText (bound : untpd.Tree ): Text = bound match { // DD
243
244
case AppliedTypeTree (tpt, _) => " : " ~ toText(tpt)
244
245
case untpd.Function (_, tpt) => " <% " ~ toText(tpt)
245
246
}
246
247
247
- def constrText (tree : untpd.Tree ): Text = toTextLocal(tree).stripPrefix(" new " ) // DD
248
+ def constrText (tree : untpd.Tree ): Text = toTextLocal(tree).stripPrefix(keywordStr( " new " ) ) // DD
248
249
249
250
def annotText (tree : untpd.Tree ): Text = " @" ~ constrText(tree) // DD
250
251
@@ -303,9 +304,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
303
304
if (constr.mods.hasAnnotations && ! constr.mods.hasFlags) modsText = modsText ~~ " this"
304
305
withEnclosingDef(constr) { addVparamssText(tparamsTxt ~~ modsText, vparamss) }
305
306
}
306
- val parentsText = Text (parents map constrText, " with " )
307
+ val parentsText = Text (parents map constrText, keywordStr( " with " ) )
307
308
val selfText = {
308
- val selfName = if (self.name == nme.WILDCARD ) " this" else self.name.toString
309
+ val selfName = if (self.name == nme.WILDCARD ) keywordStr( " this" ) else self.name.toString
309
310
(selfName ~ optText(self.tpt)(" : " ~ _) ~ " =>" ).close
310
311
} provided ! self.isEmpty
311
312
@@ -322,7 +323,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
322
323
323
324
val bodyText = " {" ~~ selfText ~~ toTextGlobal(primaryConstrs ::: body, " \n " ) ~ " }"
324
325
325
- prefix ~ (" extends" provided ! ofNew) ~~ parentsText ~~ bodyText
326
+ prefix ~ (keywordStr( " extends" ) provided ! ofNew) ~~ parentsText ~~ bodyText
326
327
}
327
328
328
329
def toTextPackageId (pid : Tree ): Text =
@@ -335,29 +336,31 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
335
336
case id : Trees .SearchFailureIdent [_] =>
336
337
tree.typeOpt match {
337
338
case reason : Implicits .SearchFailureType =>
338
- toText(id.name) ~ " implicitly[ " ~ toText(reason.expectedType) ~ " ]"
339
+ toText(id.name) ~ " implicitly" ~ toText(reason.expectedType) ~ " ]"
339
340
case _ =>
340
341
toText(id.name)
341
342
}
342
343
case Ident (name) =>
343
- tree.typeOpt match {
344
+ val txt = tree.typeOpt match {
344
345
case tp : NamedType if name != nme.WILDCARD =>
345
346
val pre = if (tp.symbol is JavaStatic ) tp.prefix.widen else tp.prefix
346
347
toTextPrefix(pre) ~ selectionString(tp)
347
348
case _ =>
348
349
toText(name)
349
350
}
351
+ if (name.isType) typeText(txt)
352
+ else txt
350
353
case tree @ Select (qual, name) =>
351
- if (qual.isType) toTextLocal(qual) ~ " #" ~ toText(name)
354
+ if (qual.isType) toTextLocal(qual) ~ " #" ~ typeText( toText(name) )
352
355
else toTextLocal(qual) ~ (" ." ~ nameIdText(tree) provided name != nme.CONSTRUCTOR )
353
356
case tree : This =>
354
- optDotPrefix(tree) ~ " this" ~ idText(tree)
357
+ optDotPrefix(tree) ~ keywordStr( " this" ) ~ idText(tree)
355
358
case Super (qual : This , mix) =>
356
- optDotPrefix(qual) ~ " super" ~ optText(mix)(" [" ~ _ ~ " ]" )
359
+ optDotPrefix(qual) ~ keywordStr( " super" ) ~ optText(mix)(" [" ~ _ ~ " ]" )
357
360
case Apply (fun, args) =>
358
361
if (fun.hasType && fun.symbol == defn.throwMethod)
359
362
changePrec (GlobalPrec ) {
360
- " throw " ~ toText(args.head)
363
+ keywordStr( " throw " ) ~ toText(args.head)
361
364
}
362
365
else
363
366
toTextLocal(fun) ~ " (" ~ toTextGlobal(args, " , " ) ~ " )"
@@ -369,7 +372,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
369
372
case _ => toText(c)
370
373
}
371
374
case New (tpt) =>
372
- " new " ~ {
375
+ keywordStr( " new " ) ~ {
373
376
tpt match {
374
377
case tpt : Template => toTextTemplate(tpt, ofNew = true )
375
378
case _ =>
@@ -389,25 +392,25 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
389
392
blockText(stats :+ expr)
390
393
case If (cond, thenp, elsep) =>
391
394
changePrec(GlobalPrec ) {
392
- " if " ~ toText(cond) ~ (" then" provided ! cond.isInstanceOf [Parens ]) ~~ toText(thenp) ~ optText(elsep)(" else " ~ _)
395
+ keywordStr( " if " ) ~ toText(cond) ~ (keywordStr( " then" ) provided ! cond.isInstanceOf [Parens ]) ~~ toText(thenp) ~ optText(elsep)(keywordStr( " else " ) ~ _)
393
396
}
394
397
case Closure (env, ref, target) =>
395
398
" closure(" ~ (toTextGlobal(env, " , " ) ~ " | " provided env.nonEmpty) ~
396
399
toTextGlobal(ref) ~ (" :" ~ toText(target) provided ! target.isEmpty) ~ " )"
397
400
case Match (sel, cases) =>
398
401
if (sel.isEmpty) blockText(cases)
399
- else changePrec(GlobalPrec ) { toText(sel) ~ " match " ~ blockText(cases) }
402
+ else changePrec(GlobalPrec ) { toText(sel) ~ keywordStr( " match " ) ~ blockText(cases) }
400
403
case CaseDef (pat, guard, body) =>
401
- " case " ~ inPattern(toText(pat)) ~ optText(guard)(" if " ~ _) ~ " => " ~ caseBlockText(body)
404
+ keywordStr( " case " ) ~ inPattern(toText(pat)) ~ optText(guard)(" if " ~ _) ~ " => " ~ caseBlockText(body)
402
405
case Return (expr, from) =>
403
- changePrec(GlobalPrec ) { " return" ~ optText(expr)(" " ~ _) }
406
+ changePrec(GlobalPrec ) { keywordStr( " return" ) ~ optText(expr)(" " ~ _) }
404
407
case Try (expr, cases, finalizer) =>
405
408
changePrec(GlobalPrec ) {
406
- " try " ~ toText(expr) ~ optText(cases)(" catch " ~ _) ~ optText(finalizer)(" finally " ~ _)
409
+ keywordStr( " try " ) ~ toText(expr) ~ optText(cases)(keywordStr( " catch " ) ~ _) ~ optText(finalizer)(keywordStr( " finally " ) ~ _)
407
410
}
408
411
case Throw (expr) =>
409
412
changePrec(GlobalPrec ) {
410
- " throw " ~ toText(expr)
413
+ keywordStr( " throw " ) ~ toText(expr)
411
414
}
412
415
case SeqLiteral (elems, elemtpt) =>
413
416
" [" ~ toTextGlobal(elems, " ," ) ~ " : " ~ toText(elemtpt) ~ " ]"
@@ -417,9 +420,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
417
420
case tpt : untpd.DerivedTypeTree =>
418
421
" <derived typetree watching " ~ summarized(toText(tpt.watched)) ~ " >"
419
422
case TypeTree () =>
420
- toText(tree.typeOpt)
423
+ typeText( toText(tree.typeOpt) )
421
424
case SingletonTypeTree (ref) =>
422
- toTextLocal(ref) ~ " .type"
425
+ toTextLocal(ref) ~ " ." ~ keywordStr( " type" )
423
426
case AndTypeTree (l, r) =>
424
427
changePrec(AndPrec ) { toText(l) ~ " & " ~ toText(r) }
425
428
case OrTypeTree (l, r) =>
@@ -450,13 +453,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
450
453
(" (" ~ toTextGlobal(implicits, " , " ) ~ " )" provided implicits.nonEmpty)
451
454
case tree @ ValDef (name, tpt, _) =>
452
455
dclTextOr {
453
- modText(tree.mods, if (tree.mods is Mutable ) " var" else " val" ) ~~
456
+ modText(tree.mods, keywordStr( if (tree.mods is Mutable ) " var" else " val" ) ) ~~
454
457
nameIdText(tree) ~ optAscription(tpt) ~
455
458
withEnclosingDef(tree) { optText(tree.rhs)(" = " ~ _) }
456
459
}
457
460
case tree @ DefDef (name, tparams, vparamss, tpt, _) =>
458
461
dclTextOr {
459
- val prefix = modText(tree.mods, " def" ) ~~ nameIdText(tree)
462
+ val prefix = modText(tree.mods, keywordStr( " def" ) ) ~~ nameIdText(tree)
460
463
withEnclosingDef(tree) {
461
464
addVparamssText(prefix ~ tparamsText(tparams), vparamss) ~ optAscription(tpt) ~
462
465
optText(tree.rhs)(" = " ~ _)
@@ -465,16 +468,16 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
465
468
case tree @ TypeDef (name, rhs) =>
466
469
def typeDefText (tparamsText : => Text , rhsText : => Text ) =
467
470
dclTextOr {
468
- modText(tree.mods, " type" ) ~~ (varianceText(tree.mods) ~ nameIdText(tree)) ~
471
+ modText(tree.mods, keywordStr( " type" )) ~~ (varianceText(tree.mods) ~ typeText( nameIdText(tree) )) ~
469
472
withEnclosingDef(tree) {
470
473
if (tree.hasType) toText(tree.symbol.info) // TODO: always print RHS, once we pickle/unpickle type trees
471
474
else tparamsText ~ rhsText
472
475
}
473
476
}
474
477
def recur (rhs : Tree , tparamsTxt : => Text ): Text = rhs match {
475
478
case impl : Template =>
476
- modText(tree.mods, if ((tree).mods is Trait ) " trait" else " class" ) ~~
477
- nameIdText(tree) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~
479
+ modText(tree.mods, keywordStr( if ((tree).mods is Trait ) " trait" else " class" ) ) ~~
480
+ typeText( nameIdText(tree) ) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~
478
481
(if (tree.hasType && ctx.settings.verbose.value) i " [decls = ${tree.symbol.info.decls}] " else " " )
479
482
case rhs : TypeBoundsTree =>
480
483
typeDefText(tparamsTxt, toText(rhs))
@@ -493,15 +496,15 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
493
496
case id :: Nil => toText(id)
494
497
case _ => " {" ~ Text (selectors map selectorText, " , " ) ~ " }"
495
498
}
496
- " import " ~ toTextLocal(expr) ~ " ." ~ selectorsText
499
+ keywordStr( " import " ) ~ toTextLocal(expr) ~ " ." ~ selectorsText
497
500
case PackageDef (pid, stats) =>
498
501
val statsText = stats match {
499
502
case (pdef : PackageDef ) :: Nil => toText(pdef)
500
503
case _ => toTextGlobal(stats, " \n " )
501
504
}
502
505
val bodyText =
503
506
if (currentPrecedence == TopLevelPrec ) " \n " ~ statsText else " {" ~ statsText ~ " }"
504
- " package " ~ toTextPackageId(pid) ~ bodyText
507
+ keywordStr( " package " ) ~ toTextPackageId(pid) ~ bodyText
505
508
case tree : Template =>
506
509
toTextTemplate(tree)
507
510
case Annotated (arg, annot) =>
@@ -512,7 +515,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
512
515
toText(t)
513
516
case tree @ ModuleDef (name, impl) =>
514
517
withEnclosingDef(tree) {
515
- modText(tree.mods, " object" ) ~~ nameIdText(tree) ~ toTextTemplate(impl)
518
+ modText(tree.mods, keywordStr( " object" ) ) ~~ nameIdText(tree) ~ toTextTemplate(impl)
516
519
}
517
520
case SymbolLit (str) =>
518
521
" '" + str
@@ -528,7 +531,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
528
531
def argToText (arg : Tree ) = arg match {
529
532
case arg @ ValDef (name, tpt, _) =>
530
533
val implicitText =
531
- if ((arg.mods is Implicit ) && ! implicitSeen) { implicitSeen = true ; " implicit " }
534
+ if ((arg.mods is Implicit ) && ! implicitSeen) { implicitSeen = true ; keywordStr( " implicit " ) }
532
535
else " "
533
536
implicitText ~ toText(name) ~ optAscription(tpt)
534
537
case _ =>
@@ -553,13 +556,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
553
556
case Tuple (ts) =>
554
557
" (" ~ toTextGlobal(ts, " , " ) ~ " )"
555
558
case WhileDo (cond, body) =>
556
- changePrec(GlobalPrec ) { " while " ~ toText(cond) ~ " do " ~ toText(body) }
559
+ changePrec(GlobalPrec ) { keywordStr( " while " ) ~ toText(cond) ~ keywordStr( " do " ) ~ toText(body) }
557
560
case DoWhile (cond, body) =>
558
- changePrec(GlobalPrec ) { " do " ~ toText(body) ~ " while " ~ toText(cond) }
561
+ changePrec(GlobalPrec ) { keywordStr( " do " ) ~ toText(body) ~ keywordStr( " while " ) ~ toText(cond) }
559
562
case ForYield (enums, expr) =>
560
- forText(enums, expr, " yield " )
563
+ forText(enums, expr, keywordStr( " yield " ) )
561
564
case ForDo (enums, expr) =>
562
- forText(enums, expr, " do " )
565
+ forText(enums, expr, keywordStr( " do " ) )
563
566
case GenFrom (pat, expr) =>
564
567
toText(pat) ~ " <- " ~ toText(expr)
565
568
case GenAlias (pat, expr) =>
@@ -569,11 +572,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
569
572
t ~ cxBoundToText(cxb)
570
573
}
571
574
case PatDef (mods, pats, tpt, rhs) =>
572
- modText(mods, " val" ) ~~ toText(pats, " , " ) ~ optAscription(tpt) ~
575
+ modText(mods, keywordStr( " val" ) ) ~~ toText(pats, " , " ) ~ optAscription(tpt) ~
573
576
optText(rhs)(" = " ~ _)
574
577
case ParsedTry (expr, handler, finalizer) =>
575
578
changePrec(GlobalPrec ) {
576
- " try " ~ toText(expr) ~ " catch {" ~ toText(handler) ~ " }" ~ optText(finalizer)(" finally " ~ _)
579
+ keywordStr( " try " ) ~ toText(expr) ~ " " ~ keywordStr( " catch" ) ~ " {" ~ toText(handler) ~ " }" ~ optText(finalizer)(keywordStr( " finally " ) ~ _)
577
580
}
578
581
case Thicket (trees) =>
579
582
" Thicket {" ~~ toTextGlobal(trees, " \n " ) ~~ " }"
0 commit comments