Skip to content

Commit df23392

Browse files
slothspotallanrenucci
authored andcommitted
Address review comments
1 parent 91f9896 commit df23392

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
@@ -369,83 +369,84 @@ object SyntaxHighlighting {
369369
override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = ()
370370
}
371371

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

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

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

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

384-
val outputH = Array.fill(in.length)(NoColor)
383+
val colorAt = Array.fill(in.length)(NoColor)
385384

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

394-
val treeTraverser = new UntypedTreeTraverser {
397+
val treeHighlighter = new UntypedTreeTraverser {
395398
def traverse(tree: Tree)(implicit ctx: Context): Unit = {
396399
tree match {
397400
case tpe : TypeDef =>
398-
highlightRange(tpe.namePos, TypeColor)
401+
highlightPosition(tpe.namePos, TypeColor)
399402
case _ : TypTree =>
400-
highlightRange(tree.pos, TypeColor)
403+
highlightPosition(tree.pos, TypeColor)
401404
case mod: ModuleDef =>
402-
highlightRange(mod.namePos, TypeColor)
405+
highlightPosition(mod.namePos, TypeColor)
403406
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)
406409
case _ : Literal =>
407-
highlightRange(tree.pos, LiteralColor)
410+
highlightPosition(tree.pos, LiteralColor)
408411
case _ =>
409412
}
410413
traverseChildren(tree)
411414
}
412415
}
413416

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

420-
val sb = new mutable.StringBuilder()
420+
val scanner = new Scanner(source)
421421

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
425425

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)
428428
}
429-
s.nextToken()
429+
scanner.nextToken()
430430

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

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

449-
sb.mkString
450+
sb.toString
450451
}
451452
}

0 commit comments

Comments
 (0)