diff --git a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala index 6f65320d2c8e..ce925e336b53 100644 --- a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala +++ b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala @@ -124,24 +124,30 @@ object SyntaxHighlighting { } } - val parser = new Parser(source) - val trees = parser.blockStatSeq() - TreeHighlighter.highlight(trees) - - val highlighted = new StringBuilder() - - for (idx <- colorAt.indices) { - val prev = if (idx == 0) NoColor else colorAt(idx - 1) - val curr = colorAt(idx) - if (curr != prev) - highlighted.append(curr) - highlighted.append(in(idx)) - } + try + val parser = new Parser(source) + val trees = parser.blockStatSeq() + TreeHighlighter.highlight(trees) + + + val highlighted = new StringBuilder() - if (colorAt.last != NoColor) - highlighted.append(NoColor) + for (idx <- colorAt.indices) { + val prev = if (idx == 0) NoColor else colorAt(idx - 1) + val curr = colorAt(idx) + if (curr != prev) + highlighted.append(curr) + highlighted.append(in(idx)) + } + + if (colorAt.last != NoColor) + highlighted.append(NoColor) - highlighted.toString + highlighted.toString + catch + case e: StackOverflowError => + in } } + } diff --git a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala index 26092b73f107..24645f7c5e47 100644 --- a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala +++ b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala @@ -458,3 +458,16 @@ class ReplVerboseTests extends ReplTest(ReplTest.defaultOptions :+ "-verbose"): } end ReplVerboseTests + +class ReplHighlightTests extends ReplTest(ReplTest.defaultOptions.filterNot(_.startsWith("-color")) :+ "-color:always"): + @Test def i18596: Unit = initially: + run("""(1 to 500).foldRight("x") { case (_, n) => s"$n" }""") + + @Test def i16904: Unit = initially: + run(""""works not fine"* 10000""") + + run(""" + case class Tree(left: Tree, right: Tree) + def deepTree(depth: Int): Tree + deepTree(300)""") +