@@ -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
}
@@ -123,7 +123,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
123
123
atPrec(InfixPrec ) { argText(args.head) }
124
124
else
125
125
toTextTuple(args.init)
126
- (" implicit " provided isImplicit) ~ argStr ~ " => " ~ argText(args.last)
126
+ (keywordStr( " implicit " ) provided isImplicit) ~ argStr ~ " => " ~ argText(args.last)
127
127
}
128
128
129
129
def isInfixType (tp : Type ): Boolean = tp match {
@@ -236,18 +236,18 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
236
236
237
237
def enumText (tree : untpd.Tree ) = tree match { // DD
238
238
case _ : untpd.GenFrom | _ : untpd.GenAlias => toText(tree)
239
- case _ => " if " ~ toText(tree)
239
+ case _ => keywordStr( " if " ) ~ toText(tree)
240
240
}
241
241
242
242
def forText (enums : List [untpd.Tree ], expr : untpd.Tree , sep : String ): Text = // DD
243
- changePrec(GlobalPrec ) { " for " ~ Text (enums map enumText, " ; " ) ~ sep ~ toText(expr) }
243
+ changePrec(GlobalPrec ) { keywordStr( " for " ) ~ Text (enums map enumText, " ; " ) ~ sep ~ toText(expr) }
244
244
245
245
def cxBoundToText (bound : untpd.Tree ): Text = bound match { // DD
246
246
case AppliedTypeTree (tpt, _) => " : " ~ toText(tpt)
247
247
case untpd.Function (_, tpt) => " <% " ~ toText(tpt)
248
248
}
249
249
250
- def constrText (tree : untpd.Tree ): Text = toTextLocal(tree).stripPrefix(" new " ) // DD
250
+ def constrText (tree : untpd.Tree ): Text = toTextLocal(tree).stripPrefix(keywordStr( " new " ) ) // DD
251
251
252
252
def annotText (tree : untpd.Tree ): Text = " @" ~ constrText(tree) // DD
253
253
@@ -314,9 +314,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
314
314
if (constr.mods.hasAnnotations && ! constr.mods.hasFlags) modsText = modsText ~~ " this"
315
315
withEnclosingDef(constr) { addVparamssText(tparamsTxt ~~ modsText, vparamss) }
316
316
}
317
- val parentsText = Text (parents map constrText, " with " )
317
+ val parentsText = Text (parents map constrText, keywordStr( " with " ) )
318
318
val selfText = {
319
- val selfName = if (self.name == nme.WILDCARD ) " this" else self.name.toString
319
+ val selfName = if (self.name == nme.WILDCARD ) keywordStr( " this" ) else self.name.toString
320
320
(selfName ~ optText(self.tpt)(" : " ~ _) ~ " =>" ).close
321
321
} provided ! self.isEmpty
322
322
@@ -333,7 +333,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
333
333
334
334
val bodyText = " {" ~~ selfText ~~ toTextGlobal(primaryConstrs ::: body, " \n " ) ~ " }"
335
335
336
- prefix ~ (" extends" provided ! ofNew) ~~ parentsText ~~ bodyText
336
+ prefix ~ (keywordStr( " extends" ) provided ! ofNew) ~~ parentsText ~~ bodyText
337
337
}
338
338
339
339
def toTextPackageId (pid : Tree ): Text =
@@ -346,29 +346,31 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
346
346
case id : Trees .SearchFailureIdent [_] =>
347
347
tree.typeOpt match {
348
348
case reason : Implicits .SearchFailureType =>
349
- toText(id.name) ~ " implicitly[ " ~ toText(reason.expectedType) ~ " ]"
349
+ toText(id.name) ~ " implicitly" ~ toText(reason.expectedType) ~ " ]"
350
350
case _ =>
351
351
toText(id.name)
352
352
}
353
353
case Ident (name) =>
354
- tree.typeOpt match {
354
+ val txt = tree.typeOpt match {
355
355
case tp : NamedType if name != nme.WILDCARD =>
356
356
val pre = if (tp.symbol is JavaStatic ) tp.prefix.widen else tp.prefix
357
357
toTextPrefix(pre) ~ withPos(selectionString(tp), tree.pos)
358
358
case _ =>
359
359
toText(name)
360
360
}
361
+ if (name.isType) typeText(txt)
362
+ else txt
361
363
case tree @ Select (qual, name) =>
362
- if (qual.isType) toTextLocal(qual) ~ " #" ~ toText(name)
364
+ if (qual.isType) toTextLocal(qual) ~ " #" ~ typeText( toText(name) )
363
365
else toTextLocal(qual) ~ (" ." ~ nameIdText(tree) provided name != nme.CONSTRUCTOR )
364
366
case tree : This =>
365
- optDotPrefix(tree) ~ " this" ~ idText(tree)
367
+ optDotPrefix(tree) ~ keywordStr( " this" ) ~ idText(tree)
366
368
case Super (qual : This , mix) =>
367
- optDotPrefix(qual) ~ " super" ~ optText(mix)(" [" ~ _ ~ " ]" )
369
+ optDotPrefix(qual) ~ keywordStr( " super" ) ~ optText(mix)(" [" ~ _ ~ " ]" )
368
370
case Apply (fun, args) =>
369
371
if (fun.hasType && fun.symbol == defn.throwMethod)
370
372
changePrec (GlobalPrec ) {
371
- " throw " ~ toText(args.head)
373
+ keywordStr( " throw " ) ~ toText(args.head)
372
374
}
373
375
else
374
376
toTextLocal(fun) ~ " (" ~ toTextGlobal(args, " , " ) ~ " )"
@@ -380,7 +382,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
380
382
case _ => withPos(toText(c), tree.pos)
381
383
}
382
384
case New (tpt) =>
383
- " new " ~ {
385
+ keywordStr( " new " ) ~ {
384
386
tpt match {
385
387
case tpt : Template => toTextTemplate(tpt, ofNew = true )
386
388
case _ =>
@@ -400,25 +402,25 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
400
402
blockText(stats :+ expr)
401
403
case If (cond, thenp, elsep) =>
402
404
changePrec(GlobalPrec ) {
403
- " if " ~ toText(cond) ~ (" then" provided ! cond.isInstanceOf [Parens ]) ~~ toText(thenp) ~ optText(elsep)(" else " ~ _)
405
+ keywordStr( " if " ) ~ toText(cond) ~ (keywordStr( " then" ) provided ! cond.isInstanceOf [Parens ]) ~~ toText(thenp) ~ optText(elsep)(keywordStr( " else " ) ~ _)
404
406
}
405
407
case Closure (env, ref, target) =>
406
408
" closure(" ~ (toTextGlobal(env, " , " ) ~ " | " provided env.nonEmpty) ~
407
409
toTextGlobal(ref) ~ (" :" ~ toText(target) provided ! target.isEmpty) ~ " )"
408
410
case Match (sel, cases) =>
409
411
if (sel.isEmpty) blockText(cases)
410
- else changePrec(GlobalPrec ) { toText(sel) ~ " match " ~ blockText(cases) }
412
+ else changePrec(GlobalPrec ) { toText(sel) ~ keywordStr( " match " ) ~ blockText(cases) }
411
413
case CaseDef (pat, guard, body) =>
412
- " case " ~ inPattern(toText(pat)) ~ optText(guard)(" if " ~ _) ~ " => " ~ caseBlockText(body)
414
+ keywordStr( " case " ) ~ inPattern(toText(pat)) ~ optText(guard)(" if " ~ _) ~ " => " ~ caseBlockText(body)
413
415
case Return (expr, from) =>
414
- changePrec(GlobalPrec ) { " return" ~ optText(expr)(" " ~ _) }
416
+ changePrec(GlobalPrec ) { keywordStr( " return" ) ~ optText(expr)(" " ~ _) }
415
417
case Try (expr, cases, finalizer) =>
416
418
changePrec(GlobalPrec ) {
417
- " try " ~ toText(expr) ~ optText(cases)(" catch " ~ _) ~ optText(finalizer)(" finally " ~ _)
419
+ keywordStr( " try " ) ~ toText(expr) ~ optText(cases)(keywordStr( " catch " ) ~ _) ~ optText(finalizer)(keywordStr( " finally " ) ~ _)
418
420
}
419
421
case Throw (expr) =>
420
422
changePrec(GlobalPrec ) {
421
- " throw " ~ toText(expr)
423
+ keywordStr( " throw " ) ~ toText(expr)
422
424
}
423
425
case SeqLiteral (elems, elemtpt) =>
424
426
" [" ~ toTextGlobal(elems, " ," ) ~ " : " ~ toText(elemtpt) ~ " ]"
@@ -428,9 +430,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
428
430
case tpt : untpd.DerivedTypeTree =>
429
431
" <derived typetree watching " ~ summarized(toText(tpt.watched)) ~ " >"
430
432
case TypeTree () =>
431
- toText(tree.typeOpt)
433
+ typeText( toText(tree.typeOpt) )
432
434
case SingletonTypeTree (ref) =>
433
- toTextLocal(ref) ~ " .type"
435
+ toTextLocal(ref) ~ " ." ~ keywordStr( " type" )
434
436
case AndTypeTree (l, r) =>
435
437
changePrec(AndPrec ) { toText(l) ~ " & " ~ toText(r) }
436
438
case OrTypeTree (l, r) =>
@@ -461,13 +463,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
461
463
(" (" ~ toTextGlobal(implicits, " , " ) ~ " )" provided implicits.nonEmpty)
462
464
case tree @ ValDef (name, tpt, _) =>
463
465
dclTextOr {
464
- modText(tree.mods, if (tree.mods is Mutable ) " var" else " val" ) ~~
466
+ modText(tree.mods, keywordStr( if (tree.mods is Mutable ) " var" else " val" ) ) ~~
465
467
nameIdText(tree) ~ optAscription(tpt) ~
466
468
withEnclosingDef(tree) { optText(tree.rhs)(" = " ~ _) }
467
469
}
468
470
case tree @ DefDef (name, tparams, vparamss, tpt, _) =>
469
471
dclTextOr {
470
- val prefix = modText(tree.mods, " def" ) ~~ nameIdText(tree)
472
+ val prefix = modText(tree.mods, keywordStr( " def" ) ) ~~ nameIdText(tree)
471
473
withEnclosingDef(tree) {
472
474
addVparamssText(prefix ~ tparamsText(tparams), vparamss) ~ optAscription(tpt) ~
473
475
optText(tree.rhs)(" = " ~ _)
@@ -476,16 +478,16 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
476
478
case tree @ TypeDef (name, rhs) =>
477
479
def typeDefText (tparamsText : => Text , rhsText : => Text ) =
478
480
dclTextOr {
479
- modText(tree.mods, " type" ) ~~ (varianceText(tree.mods) ~ nameIdText(tree)) ~
481
+ modText(tree.mods, keywordStr( " type" )) ~~ (varianceText(tree.mods) ~ typeText( nameIdText(tree) )) ~
480
482
withEnclosingDef(tree) {
481
483
if (tree.hasType) toText(tree.symbol.info) // TODO: always print RHS, once we pickle/unpickle type trees
482
484
else tparamsText ~ rhsText
483
485
}
484
486
}
485
487
def recur (rhs : Tree , tparamsTxt : => Text ): Text = rhs match {
486
488
case impl : Template =>
487
- modText(tree.mods, if ((tree).mods is Trait ) " trait" else " class" ) ~~
488
- nameIdText(tree) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~
489
+ modText(tree.mods, keywordStr( if ((tree).mods is Trait ) " trait" else " class" ) ) ~~
490
+ typeText( nameIdText(tree) ) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~
489
491
(if (tree.hasType && ctx.settings.verbose.value) i " [decls = ${tree.symbol.info.decls}] " else " " )
490
492
case rhs : TypeBoundsTree =>
491
493
typeDefText(tparamsTxt, toText(rhs))
@@ -504,15 +506,15 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
504
506
case id :: Nil => toText(id)
505
507
case _ => " {" ~ Text (selectors map selectorText, " , " ) ~ " }"
506
508
}
507
- " import " ~ toTextLocal(expr) ~ " ." ~ selectorsText
509
+ keywordStr( " import " ) ~ toTextLocal(expr) ~ " ." ~ selectorsText
508
510
case PackageDef (pid, stats) =>
509
511
val statsText = stats match {
510
512
case (pdef : PackageDef ) :: Nil => toText(pdef)
511
513
case _ => toTextGlobal(stats, " \n " )
512
514
}
513
515
val bodyText =
514
516
if (currentPrecedence == TopLevelPrec ) " \n " ~ statsText else " {" ~ statsText ~ " }"
515
- " package " ~ toTextPackageId(pid) ~ bodyText
517
+ keywordStr( " package " ) ~ toTextPackageId(pid) ~ bodyText
516
518
case tree : Template =>
517
519
toTextTemplate(tree)
518
520
case Annotated (arg, annot) =>
@@ -523,7 +525,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
523
525
toText(t)
524
526
case tree @ ModuleDef (name, impl) =>
525
527
withEnclosingDef(tree) {
526
- modText(tree.mods, " object" ) ~~ nameIdText(tree) ~ toTextTemplate(impl)
528
+ modText(tree.mods, keywordStr( " object" ) ) ~~ nameIdText(tree) ~ toTextTemplate(impl)
527
529
}
528
530
case SymbolLit (str) =>
529
531
" '" + str
@@ -539,7 +541,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
539
541
def argToText (arg : Tree ) = arg match {
540
542
case arg @ ValDef (name, tpt, _) =>
541
543
val implicitText =
542
- if ((arg.mods is Implicit ) && ! implicitSeen) { implicitSeen = true ; " implicit " }
544
+ if ((arg.mods is Implicit ) && ! implicitSeen) { implicitSeen = true ; keywordStr( " implicit " ) }
543
545
else " "
544
546
implicitText ~ toText(name) ~ optAscription(tpt)
545
547
case _ =>
@@ -564,13 +566,13 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
564
566
case Tuple (ts) =>
565
567
" (" ~ toTextGlobal(ts, " , " ) ~ " )"
566
568
case WhileDo (cond, body) =>
567
- changePrec(GlobalPrec ) { " while " ~ toText(cond) ~ " do " ~ toText(body) }
569
+ changePrec(GlobalPrec ) { keywordStr( " while " ) ~ toText(cond) ~ keywordStr( " do " ) ~ toText(body) }
568
570
case DoWhile (cond, body) =>
569
- changePrec(GlobalPrec ) { " do " ~ toText(body) ~ " while " ~ toText(cond) }
571
+ changePrec(GlobalPrec ) { keywordStr( " do " ) ~ toText(body) ~ keywordStr( " while " ) ~ toText(cond) }
570
572
case ForYield (enums, expr) =>
571
- forText(enums, expr, " yield " )
573
+ forText(enums, expr, keywordStr( " yield " ) )
572
574
case ForDo (enums, expr) =>
573
- forText(enums, expr, " do " )
575
+ forText(enums, expr, keywordStr( " do " ) )
574
576
case GenFrom (pat, expr) =>
575
577
toText(pat) ~ " <- " ~ toText(expr)
576
578
case GenAlias (pat, expr) =>
@@ -580,11 +582,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
580
582
t ~ cxBoundToText(cxb)
581
583
}
582
584
case PatDef (mods, pats, tpt, rhs) =>
583
- modText(mods, " val" ) ~~ toText(pats, " , " ) ~ optAscription(tpt) ~
585
+ modText(mods, keywordStr( " val" ) ) ~~ toText(pats, " , " ) ~ optAscription(tpt) ~
584
586
optText(rhs)(" = " ~ _)
585
587
case ParsedTry (expr, handler, finalizer) =>
586
588
changePrec(GlobalPrec ) {
587
- " try " ~ toText(expr) ~ " catch {" ~ toText(handler) ~ " }" ~ optText(finalizer)(" finally " ~ _)
589
+ keywordStr( " try " ) ~ toText(expr) ~ " " ~ keywordStr( " catch" ) ~ " {" ~ toText(handler) ~ " }" ~ optText(finalizer)(keywordStr( " finally " ) ~ _)
588
590
}
589
591
case Thicket (trees) =>
590
592
" Thicket {" ~~ toTextGlobal(trees, " \n " ) ~~ " }"
0 commit comments