@@ -17,7 +17,7 @@ import transform.SymUtils._
17
17
import scala .annotation .switch
18
18
import language .implicitConversions
19
19
import dotty .tools .dotc .util .SourcePosition
20
-
20
+ import Highlighting . _
21
21
22
22
class RefinedPrinter (_ctx : Context ) extends PlainPrinter (_ctx) {
23
23
@@ -80,7 +80,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
80
80
override def toTextRef (tp : SingletonType ): Text = controlled {
81
81
tp match {
82
82
case tp : ThisType =>
83
- if (tp.cls.isAnonymousClass) return " this"
83
+ if (tp.cls.isAnonymousClass) return keywordStr( " this" )
84
84
if (tp.cls is ModuleClass ) return fullNameString(tp.cls.sourceModule)
85
85
case _ =>
86
86
}
@@ -124,7 +124,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
124
124
atPrec(InfixPrec ) { argText(args.head) }
125
125
else
126
126
toTextTuple(args.init)
127
- (" implicit " provided isImplicit) ~ argStr ~ " => " ~ argText(args.last)
127
+ (keywordStr( " implicit " ) provided isImplicit) ~ argStr ~ " => " ~ argText(args.last)
128
128
}
129
129
130
130
def toTextDependentFunction (appType : MethodType ): Text = {
@@ -244,18 +244,18 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
244
244
245
245
def enumText (tree : untpd.Tree ) = tree match { // DD
246
246
case _ : untpd.GenFrom | _ : untpd.GenAlias => toText(tree)
247
- case _ => " if " ~ toText(tree)
247
+ case _ => keywordStr( " if " ) ~ toText(tree)
248
248
}
249
249
250
250
def forText (enums : List [untpd.Tree ], expr : untpd.Tree , sep : String ): Text = // DD
251
- changePrec(GlobalPrec ) { " for " ~ Text (enums map enumText, " ; " ) ~ sep ~ toText(expr) }
251
+ changePrec(GlobalPrec ) { keywordStr( " for " ) ~ Text (enums map enumText, " ; " ) ~ sep ~ toText(expr) }
252
252
253
253
def cxBoundToText (bound : untpd.Tree ): Text = bound match { // DD
254
254
case AppliedTypeTree (tpt, _) => " : " ~ toText(tpt)
255
255
case untpd.Function (_, tpt) => " <% " ~ toText(tpt)
256
256
}
257
257
258
- def constrText (tree : untpd.Tree ): Text = toTextLocal(tree).stripPrefix(" new " ) // DD
258
+ def constrText (tree : untpd.Tree ): Text = toTextLocal(tree).stripPrefix(keywordStr( " new " ) ) // DD
259
259
260
260
def annotText (tree : untpd.Tree ): Text = " @" ~ constrText(tree) // DD
261
261
@@ -322,9 +322,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
322
322
if (constr.mods.hasAnnotations && ! constr.mods.hasFlags) modsText = modsText ~~ " this"
323
323
withEnclosingDef(constr) { addVparamssText(tparamsTxt ~~ modsText, vparamss) }
324
324
}
325
- val parentsText = Text (parents map constrText, " with " )
325
+ val parentsText = Text (parents map constrText, keywordStr( " with " ) )
326
326
val selfText = {
327
- val selfName = if (self.name == nme.WILDCARD ) " this" else self.name.toString
327
+ val selfName = if (self.name == nme.WILDCARD ) keywordStr( " this" ) else self.name.toString
328
328
(selfName ~ optText(self.tpt)(" : " ~ _) ~ " =>" ).close
329
329
} provided ! self.isEmpty
330
330
@@ -341,7 +341,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
341
341
342
342
val bodyText = " {" ~~ selfText ~~ toTextGlobal(primaryConstrs ::: body, " \n " ) ~ " }"
343
343
344
- prefix ~ (" extends" provided ! ofNew) ~~ parentsText ~~ bodyText
344
+ prefix ~ (keywordStr( " extends" ) provided ! ofNew) ~~ parentsText ~~ bodyText
345
345
}
346
346
347
347
def toTextPackageId (pid : Tree ): Text =
@@ -359,24 +359,26 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
359
359
toText(id.name)
360
360
}
361
361
case Ident (name) =>
362
- tree.typeOpt match {
362
+ val txt = tree.typeOpt match {
363
363
case tp : NamedType if name != nme.WILDCARD =>
364
364
val pre = if (tp.symbol is JavaStatic ) tp.prefix.widen else tp.prefix
365
365
toTextPrefix(pre) ~ withPos(selectionString(tp), tree.pos)
366
366
case _ =>
367
367
toText(name)
368
368
}
369
+ if (name.isType) typeText(txt)
370
+ else txt
369
371
case tree @ Select (qual, name) =>
370
- if (qual.isType) toTextLocal(qual) ~ " #" ~ toText(name)
372
+ if (qual.isType) toTextLocal(qual) ~ " #" ~ typeText( toText(name) )
371
373
else toTextLocal(qual) ~ (" ." ~ nameIdText(tree) provided name != nme.CONSTRUCTOR )
372
374
case tree : This =>
373
- optDotPrefix(tree) ~ " this" ~ idText(tree)
375
+ optDotPrefix(tree) ~ keywordStr( " this" ) ~ idText(tree)
374
376
case Super (qual : This , mix) =>
375
- optDotPrefix(qual) ~ " super" ~ optText(mix)(" [" ~ _ ~ " ]" )
377
+ optDotPrefix(qual) ~ keywordStr( " super" ) ~ optText(mix)(" [" ~ _ ~ " ]" )
376
378
case Apply (fun, args) =>
377
379
if (fun.hasType && fun.symbol == defn.throwMethod)
378
380
changePrec (GlobalPrec ) {
379
- " throw " ~ toText(args.head)
381
+ keywordStr( " throw " ) ~ toText(args.head)
380
382
}
381
383
else
382
384
toTextLocal(fun) ~ " (" ~ toTextGlobal(args, " , " ) ~ " )"
@@ -388,7 +390,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
388
390
case _ => withPos(toText(c), tree.pos)
389
391
}
390
392
case New (tpt) =>
391
- " new " ~ {
393
+ keywordStr( " new " ) ~ {
392
394
tpt match {
393
395
case tpt : Template => toTextTemplate(tpt, ofNew = true )
394
396
case _ =>
@@ -408,25 +410,25 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
408
410
blockText(stats :+ expr)
409
411
case If (cond, thenp, elsep) =>
410
412
changePrec(GlobalPrec ) {
411
- " if " ~ toText(cond) ~ (" then" provided ! cond.isInstanceOf [Parens ]) ~~ toText(thenp) ~ optText(elsep)(" else " ~ _)
413
+ keywordStr( " if " ) ~ toText(cond) ~ (keywordStr( " then" ) provided ! cond.isInstanceOf [Parens ]) ~~ toText(thenp) ~ optText(elsep)(keywordStr( " else " ) ~ _)
412
414
}
413
415
case Closure (env, ref, target) =>
414
416
" closure(" ~ (toTextGlobal(env, " , " ) ~ " | " provided env.nonEmpty) ~
415
417
toTextGlobal(ref) ~ (" :" ~ toText(target) provided ! target.isEmpty) ~ " )"
416
418
case Match (sel, cases) =>
417
419
if (sel.isEmpty) blockText(cases)
418
- else changePrec(GlobalPrec ) { toText(sel) ~ " match " ~ blockText(cases) }
420
+ else changePrec(GlobalPrec ) { toText(sel) ~ keywordStr( " match " ) ~ blockText(cases) }
419
421
case CaseDef (pat, guard, body) =>
420
- " case " ~ inPattern(toText(pat)) ~ optText(guard)(" if " ~ _) ~ " => " ~ caseBlockText(body)
422
+ keywordStr( " case " ) ~ inPattern(toText(pat)) ~ optText(guard)(keywordStr( " if " ) ~ _) ~ " => " ~ caseBlockText(body)
421
423
case Return (expr, from) =>
422
- changePrec(GlobalPrec ) { " return" ~ optText(expr)(" " ~ _) }
424
+ changePrec(GlobalPrec ) { keywordStr( " return" ) ~ optText(expr)(" " ~ _) }
423
425
case Try (expr, cases, finalizer) =>
424
426
changePrec(GlobalPrec ) {
425
- " try " ~ toText(expr) ~ optText(cases)(" catch " ~ _) ~ optText(finalizer)(" finally " ~ _)
427
+ keywordStr( " try " ) ~ toText(expr) ~ optText(cases)(keywordStr( " catch " ) ~ _) ~ optText(finalizer)(keywordStr( " finally " ) ~ _)
426
428
}
427
429
case Throw (expr) =>
428
430
changePrec(GlobalPrec ) {
429
- " throw " ~ toText(expr)
431
+ keywordStr( " throw " ) ~ toText(expr)
430
432
}
431
433
case SeqLiteral (elems, elemtpt) =>
432
434
" [" ~ toTextGlobal(elems, " ," ) ~ " : " ~ toText(elemtpt) ~ " ]"
@@ -436,9 +438,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
436
438
case tpt : untpd.DerivedTypeTree =>
437
439
" <derived typetree watching " ~ summarized(toText(tpt.watched)) ~ " >"
438
440
case TypeTree () =>
439
- toText(tree.typeOpt)
441
+ typeText( toText(tree.typeOpt) )
440
442
case SingletonTypeTree (ref) =>
441
- toTextLocal(ref) ~ " .type"
443
+ toTextLocal(ref) ~ " ." ~ keywordStr( " type" )
442
444
case AndTypeTree (l, r) =>
443
445
changePrec(AndPrec ) { toText(l) ~ " & " ~ toText(r) }
444
446
case OrTypeTree (l, r) =>
@@ -469,13 +471,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
469
471
(" (" ~ toTextGlobal(implicits, " , " ) ~ " )" provided implicits.nonEmpty)
470
472
case tree @ ValDef (name, tpt, _) =>
471
473
dclTextOr {
472
- modText(tree.mods, if (tree.mods is Mutable ) " var" else " val" ) ~~
474
+ modText(tree.mods, keywordStr( if (tree.mods is Mutable ) " var" else " val" ) ) ~~
473
475
nameIdText(tree) ~ optAscription(tpt) ~
474
476
withEnclosingDef(tree) { optText(tree.rhs)(" = " ~ _) }
475
477
}
476
478
case tree @ DefDef (name, tparams, vparamss, tpt, _) =>
477
479
dclTextOr {
478
- val prefix = modText(tree.mods, " def" ) ~~ nameIdText(tree)
480
+ val prefix = modText(tree.mods, keywordStr( " def" ) ) ~~ nameIdText(tree)
479
481
withEnclosingDef(tree) {
480
482
addVparamssText(prefix ~ tparamsText(tparams), vparamss) ~ optAscription(tpt) ~
481
483
optText(tree.rhs)(" = " ~ _)
@@ -484,16 +486,16 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
484
486
case tree @ TypeDef (name, rhs) =>
485
487
def typeDefText (tparamsText : => Text , rhsText : => Text ) =
486
488
dclTextOr {
487
- modText(tree.mods, " type" ) ~~ (varianceText(tree.mods) ~ nameIdText(tree)) ~
489
+ modText(tree.mods, keywordStr( " type" )) ~~ (varianceText(tree.mods) ~ typeText( nameIdText(tree) )) ~
488
490
withEnclosingDef(tree) {
489
491
if (tree.hasType) toText(tree.symbol.info) // TODO: always print RHS, once we pickle/unpickle type trees
490
492
else tparamsText ~ rhsText
491
493
}
492
494
}
493
495
def recur (rhs : Tree , tparamsTxt : => Text ): Text = rhs match {
494
496
case impl : Template =>
495
- modText(tree.mods, if ((tree).mods is Trait ) " trait" else " class" ) ~~
496
- nameIdText(tree) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~
497
+ modText(tree.mods, keywordStr( if ((tree).mods is Trait ) " trait" else " class" ) ) ~~
498
+ typeText( nameIdText(tree) ) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~
497
499
(if (tree.hasType && ctx.settings.verbose.value) i " [decls = ${tree.symbol.info.decls}] " else " " )
498
500
case rhs : TypeBoundsTree =>
499
501
typeDefText(tparamsTxt, toText(rhs))
@@ -512,15 +514,15 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
512
514
case id :: Nil => toText(id)
513
515
case _ => " {" ~ Text (selectors map selectorText, " , " ) ~ " }"
514
516
}
515
- " import " ~ toTextLocal(expr) ~ " ." ~ selectorsText
517
+ keywordStr( " import " ) ~ toTextLocal(expr) ~ " ." ~ selectorsText
516
518
case PackageDef (pid, stats) =>
517
519
val statsText = stats match {
518
520
case (pdef : PackageDef ) :: Nil => toText(pdef)
519
521
case _ => toTextGlobal(stats, " \n " )
520
522
}
521
523
val bodyText =
522
524
if (currentPrecedence == TopLevelPrec ) " \n " ~ statsText else " {" ~ statsText ~ " }"
523
- " package " ~ toTextPackageId(pid) ~ bodyText
525
+ keywordStr( " package " ) ~ toTextPackageId(pid) ~ bodyText
524
526
case tree : Template =>
525
527
toTextTemplate(tree)
526
528
case Annotated (arg, annot) =>
@@ -531,7 +533,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
531
533
toText(t)
532
534
case tree @ ModuleDef (name, impl) =>
533
535
withEnclosingDef(tree) {
534
- modText(tree.mods, " object" ) ~~ nameIdText(tree) ~ toTextTemplate(impl)
536
+ modText(tree.mods, keywordStr( " object" ) ) ~~ nameIdText(tree) ~ toTextTemplate(impl)
535
537
}
536
538
case SymbolLit (str) =>
537
539
" '" + str
@@ -547,7 +549,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
547
549
def argToText (arg : Tree ) = arg match {
548
550
case arg @ ValDef (name, tpt, _) =>
549
551
val implicitText =
550
- if ((arg.mods is Implicit ) && ! implicitSeen) { implicitSeen = true ; " implicit " }
552
+ if ((arg.mods is Implicit ) && ! implicitSeen) { implicitSeen = true ; keywordStr( " implicit " ) }
551
553
else " "
552
554
implicitText ~ toText(name) ~ optAscription(tpt)
553
555
case _ =>
@@ -572,13 +574,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
572
574
case Tuple (ts) =>
573
575
" (" ~ toTextGlobal(ts, " , " ) ~ " )"
574
576
case WhileDo (cond, body) =>
575
- changePrec(GlobalPrec ) { " while " ~ toText(cond) ~ " do " ~ toText(body) }
577
+ changePrec(GlobalPrec ) { keywordStr( " while " ) ~ toText(cond) ~ keywordStr( " do " ) ~ toText(body) }
576
578
case DoWhile (cond, body) =>
577
- changePrec(GlobalPrec ) { " do " ~ toText(body) ~ " while " ~ toText(cond) }
579
+ changePrec(GlobalPrec ) { keywordStr( " do " ) ~ toText(body) ~ keywordStr( " while " ) ~ toText(cond) }
578
580
case ForYield (enums, expr) =>
579
- forText(enums, expr, " yield " )
581
+ forText(enums, expr, keywordStr( " yield " ) )
580
582
case ForDo (enums, expr) =>
581
- forText(enums, expr, " do " )
583
+ forText(enums, expr, keywordStr( " do " ) )
582
584
case GenFrom (pat, expr) =>
583
585
toText(pat) ~ " <- " ~ toText(expr)
584
586
case GenAlias (pat, expr) =>
@@ -588,11 +590,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
588
590
t ~ cxBoundToText(cxb)
589
591
}
590
592
case PatDef (mods, pats, tpt, rhs) =>
591
- modText(mods, " val" ) ~~ toText(pats, " , " ) ~ optAscription(tpt) ~
593
+ modText(mods, keywordStr( " val" ) ) ~~ toText(pats, " , " ) ~ optAscription(tpt) ~
592
594
optText(rhs)(" = " ~ _)
593
595
case ParsedTry (expr, handler, finalizer) =>
594
596
changePrec(GlobalPrec ) {
595
- " try " ~ toText(expr) ~ " catch {" ~ toText(handler) ~ " }" ~ optText(finalizer)(" finally " ~ _)
597
+ keywordStr( " try " ) ~ toText(expr) ~ " " ~ keywordStr( " catch" ) ~ " {" ~ toText(handler) ~ " }" ~ optText(finalizer)(keywordStr( " finally " ) ~ _)
596
598
}
597
599
case Thicket (trees) =>
598
600
" Thicket {" ~~ toTextGlobal(trees, " \n " ) ~~ " }"
0 commit comments