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