@@ -370,83 +370,84 @@ object SyntaxHighlighting {
370
370
override def doReport (m : MessageContainer )(implicit ctx : Context ): Unit = ()
371
371
}
372
372
373
- private val ignoredKwds = Seq (nme.ARROWkw , nme.EQ , nme.EQL , nme.COLONkw )
373
+ private val ignoredKwds = Set (nme.ARROWkw , nme.EQ , nme.EQL , nme.COLONkw )
374
374
375
375
def highlight (in : String )(ctx0 : Context ): String = {
376
376
import dotty .tools .dotc .ast .untpd ._
377
377
378
378
implicit val ctx : Context = ctx0.fresh.setReporter(new NoReporter )
379
379
380
- val sf = new SourceFile (" <highlighting>" , in.toCharArray)
381
- val p = new Parser (sf)
382
- val s = new Scanner (sf)
383
- val trees = p.blockStatSeq()
380
+ val source = new SourceFile (" <highlighting>" , in.toCharArray)
381
+ val parser = new Parser (source)
382
+ val trees = parser.blockStatSeq()
384
383
385
- val outputH = Array .fill(in.length)(NoColor )
384
+ val colorAt = Array .fill(in.length)(NoColor )
386
385
387
- def highlightRange (p : Position , color : String ): Unit = {
388
- if (p.exists) {
389
- for {
390
- i <- p.start until math.min(p.end, outputH.length)
391
- } outputH(i) = color
386
+ def highlightRange (from : Int , to : Int , color : String ) = {
387
+ try {
388
+ for (i <- from until to)
389
+ colorAt(i) = color
390
+ } catch {
391
+ case _ : IndexOutOfBoundsException =>
392
+ ctx.error(" Encountered tree with invalid position, please open an issue with the code snippet that caused the error" )
392
393
}
393
394
}
395
+ def highlightPosition (pos : Position , color : String ) =
396
+ if (pos.exists) highlightRange(pos.start, pos.end, color)
394
397
395
- val treeTraverser = new UntypedTreeTraverser {
398
+ val treeHighlighter = new UntypedTreeTraverser {
396
399
def traverse (tree : Tree )(implicit ctx : Context ): Unit = {
397
400
tree match {
398
401
case tpe : TypeDef =>
399
- highlightRange (tpe.namePos, TypeColor )
402
+ highlightPosition (tpe.namePos, TypeColor )
400
403
case _ : TypTree =>
401
- highlightRange (tree.pos, TypeColor )
404
+ highlightPosition (tree.pos, TypeColor )
402
405
case mod : ModuleDef =>
403
- highlightRange (mod.namePos, TypeColor )
406
+ highlightPosition (mod.namePos, TypeColor )
404
407
case v : ValOrDefDef =>
405
- highlightRange (v.namePos, ValDefColor )
406
- highlightRange (v.tpt.pos, TypeColor )
408
+ highlightPosition (v.namePos, ValDefColor )
409
+ highlightPosition (v.tpt.pos, TypeColor )
407
410
case _ : Literal =>
408
- highlightRange (tree.pos, LiteralColor )
411
+ highlightPosition (tree.pos, LiteralColor )
409
412
case _ =>
410
413
}
411
414
traverseChildren(tree)
412
415
}
413
416
}
414
417
415
- for {
416
- t <- trees
417
- } {
418
- treeTraverser.traverse(t)
419
- }
418
+ for (tree <- trees)
419
+ treeHighlighter.traverse(tree)
420
420
421
- val sb = new mutable. StringBuilder ( )
421
+ val scanner = new Scanner (source )
422
422
423
- while (s .token != EOF ) {
424
- val isKwd = isKeyword(s .token) && ! ignoredKwds.contains(s .name)
425
- val offsetStart = s .offset
423
+ while (scanner .token != EOF ) {
424
+ val isKwd = isKeyword(scanner .token) && ! ignoredKwds.contains(scanner .name)
425
+ val offsetStart = scanner .offset
426
426
427
- if (s .token == IDENTIFIER && s .name == nme.??? ) {
428
- highlightRange(Position (s .offset, s .offset + s .name.length) , Console .RED_B )
427
+ if (scanner .token == IDENTIFIER && scanner .name == nme.??? ) {
428
+ highlightRange(scanner .offset, scanner .offset + scanner .name.length, Console .RED_B )
429
429
}
430
- s .nextToken()
430
+ scanner .nextToken()
431
431
432
- if (isKwd) {
433
- val offsetEnd = s .lastOffset
434
- highlightRange (Position (offsetStart, offsetEnd), KeywordColor )
432
+ if (isKwd) {
433
+ val offsetEnd = scanner .lastOffset
434
+ highlightPosition (Position (offsetStart, offsetEnd), KeywordColor )
435
435
}
436
436
}
437
437
438
- for {
439
- idx <- outputH.indices
440
- } {
441
- if (idx == 0 || outputH(idx- 1 ) != outputH(idx)){
442
- sb.append(outputH(idx))
438
+ val sb = new mutable.StringBuilder ()
439
+
440
+ for (idx <- colorAt.indices) {
441
+ if ( (idx == 0 && colorAt(idx) != NoColor )
442
+ || (idx > 0 && colorAt(idx- 1 ) != colorAt(idx))) {
443
+ sb.append(colorAt(idx))
443
444
}
444
445
sb.append(in(idx))
445
446
}
446
- if (outputH .last != NoColor ) {
447
+ if (colorAt.nonEmpty && colorAt .last != NoColor ) {
447
448
sb.append(NoColor )
448
449
}
449
450
450
- sb.mkString
451
+ sb.toString
451
452
}
452
453
}
0 commit comments