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