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