Skip to content

Commit 685a0ed

Browse files
committed
Address review comments
1 parent f1cde6d commit 685a0ed

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -370,83 +370,84 @@ object SyntaxHighlighting {
370370
override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = ()
371371
}
372372

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)
374374

375375
def highlight(in: String)(ctx0: Context): String = {
376376
import dotty.tools.dotc.ast.untpd._
377377

378378
implicit val ctx: Context = ctx0.fresh.setReporter(new NoReporter)
379379

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()
384383

385-
val outputH = Array.fill(in.length)(NoColor)
384+
val colorAt = Array.fill(in.length)(NoColor)
386385

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")
392393
}
393394
}
395+
def highlightPosition(pos: Position, color: String) =
396+
if (pos.exists) highlightRange(pos.start, pos.end, color)
394397

395-
val treeTraverser = new UntypedTreeTraverser {
398+
val treeHighlighter = new UntypedTreeTraverser {
396399
def traverse(tree: Tree)(implicit ctx: Context): Unit = {
397400
tree match {
398401
case tpe : TypeDef =>
399-
highlightRange(tpe.namePos, TypeColor)
402+
highlightPosition(tpe.namePos, TypeColor)
400403
case _ : TypTree =>
401-
highlightRange(tree.pos, TypeColor)
404+
highlightPosition(tree.pos, TypeColor)
402405
case mod: ModuleDef =>
403-
highlightRange(mod.namePos, TypeColor)
406+
highlightPosition(mod.namePos, TypeColor)
404407
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)
407410
case _ : Literal =>
408-
highlightRange(tree.pos, LiteralColor)
411+
highlightPosition(tree.pos, LiteralColor)
409412
case _ =>
410413
}
411414
traverseChildren(tree)
412415
}
413416
}
414417

415-
for {
416-
t <- trees
417-
} {
418-
treeTraverser.traverse(t)
419-
}
418+
for (tree <- trees)
419+
treeHighlighter.traverse(tree)
420420

421-
val sb = new mutable.StringBuilder()
421+
val scanner = new Scanner(source)
422422

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
426426

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)
429429
}
430-
s.nextToken()
430+
scanner.nextToken()
431431

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)
435435
}
436436
}
437437

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))
443444
}
444445
sb.append(in(idx))
445446
}
446-
if(outputH.last != NoColor) {
447+
if (colorAt.nonEmpty && colorAt.last != NoColor) {
447448
sb.append(NoColor)
448449
}
449450

450-
sb.mkString
451+
sb.toString
451452
}
452453
}

0 commit comments

Comments
 (0)