@@ -369,17 +369,12 @@ object SyntaxHighlighting {
369
369
override def doReport (m : MessageContainer )(implicit ctx : Context ): Unit = ()
370
370
}
371
371
372
- private val ignoredKwds = Set (nme.ARROWkw , nme.EQ , nme.EQL , nme.COLONkw )
373
-
374
372
def highlight (in : String )(ctx0 : Context ): String = {
375
373
import dotty .tools .dotc .ast .untpd ._
376
374
377
375
implicit val ctx : Context = ctx0.fresh.setReporter(new NoReporter )
378
376
379
377
val source = new SourceFile (" <highlighting>" , in.toCharArray)
380
- val parser = new Parser (source)
381
- val trees = parser.blockStatSeq()
382
-
383
378
val colorAt = Array .fill(in.length)(NoColor )
384
379
385
380
def highlightRange (from : Int , to : Int , color : String ) = {
@@ -394,18 +389,39 @@ object SyntaxHighlighting {
394
389
def highlightPosition (pos : Position , color : String ) =
395
390
if (pos.exists) highlightRange(pos.start, pos.end, color)
396
391
392
+ val scanner = new Scanner (source)
393
+
394
+ while (scanner.token != EOF ) {
395
+ val isKwd = alphaKeywords.contains(scanner.token)
396
+ val offsetStart = scanner.offset
397
+
398
+ if (scanner.token == IDENTIFIER && scanner.name == nme.??? ) {
399
+ highlightRange(scanner.offset, scanner.offset + scanner.name.length, Console .RED_B )
400
+ }
401
+ scanner.nextToken()
402
+
403
+ if (isKwd) {
404
+ val offsetEnd = scanner.lastOffset
405
+ highlightPosition(Position (offsetStart, offsetEnd), KeywordColor )
406
+ }
407
+ }
408
+
397
409
val treeHighlighter = new UntypedTreeTraverser {
398
410
def traverse (tree : Tree )(implicit ctx : Context ): Unit = {
399
411
tree match {
400
- case id : Ident if id.isType =>
412
+ case id : Ident if id.isType =>
401
413
highlightPosition(id.pos, TypeColor )
402
414
case tpe : TypeDef =>
415
+ for (annotation <- tpe.rawMods.annotations)
416
+ highlightPosition(annotation.pos, AnnotationColor )
403
417
highlightPosition(tpe.namePos, TypeColor )
404
418
case _ : TypTree =>
405
419
highlightPosition(tree.pos, TypeColor )
406
420
case mod : ModuleDef =>
407
421
highlightPosition(mod.namePos, TypeColor )
408
422
case v : ValOrDefDef =>
423
+ for (annotation <- v.rawMods.annotations)
424
+ highlightPosition(annotation.pos, AnnotationColor )
409
425
highlightPosition(v.namePos, ValDefColor )
410
426
highlightPosition(v.tpt.pos, TypeColor )
411
427
case _ : Literal =>
@@ -416,26 +432,12 @@ object SyntaxHighlighting {
416
432
}
417
433
}
418
434
435
+ val parser = new Parser (source)
436
+ val trees = parser.blockStatSeq()
437
+
419
438
for (tree <- trees)
420
439
treeHighlighter.traverse(tree)
421
440
422
- val scanner = new Scanner (source)
423
-
424
- while (scanner.token != EOF ) {
425
- val isKwd = isKeyword(scanner.token) && ! ignoredKwds.contains(scanner.name)
426
- val offsetStart = scanner.offset
427
-
428
- if (scanner.token == IDENTIFIER && scanner.name == nme.??? ) {
429
- highlightRange(scanner.offset, scanner.offset + scanner.name.length, Console .RED_B )
430
- }
431
- scanner.nextToken()
432
-
433
- if (isKwd) {
434
- val offsetEnd = scanner.lastOffset
435
- highlightPosition(Position (offsetStart, offsetEnd), KeywordColor )
436
- }
437
- }
438
-
439
441
val sb = new mutable.StringBuilder ()
440
442
441
443
for (idx <- colorAt.indices) {
0 commit comments