Skip to content

Commit a592293

Browse files
committed
Respect -color:never in the REPL
If `-color:never` is set, the REPL will not do any syntax highlighting.
1 parent 0c88214 commit a592293

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package dotty.tools
22
package dotc
33
package printing
44

5+
import dotty.tools.dotc.core.Contexts.Context
6+
57
import parsing.Tokens._
68
import scala.annotation.switch
79
import scala.collection.mutable.StringBuilder
@@ -45,7 +47,12 @@ object SyntaxHighlighting {
4547
'{' :: '}' :: ')' :: '(' :: '[' :: ']' :: '=' :: ' ' :: ',' :: '.' :: '|' ::
4648
'&' :: '\n' :: Nil
4749

48-
def apply(chars: Iterable[Char]): Iterable[Char] = {
50+
def apply(chars: Iterable[Char])(implicit ctx: Context): Iterable[Char] = {
51+
if (ctx.settings.color.value != "never") highlight(chars)
52+
else chars
53+
}
54+
55+
def highlight(chars: Iterable[Char]): Iterable[Char] = {
4956
var prev: Char = 0
5057
var remaining = chars.toStream
5158
val newBuf = new StringBuilder

compiler/src/dotty/tools/repl/JLineTerminal.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ final class JLineTerminal extends java.io.Closeable {
2222
.build()
2323
private val history = new DefaultHistory
2424

25-
private def blue(str: String) = Console.BLUE + str + Console.RESET
26-
private val prompt = blue("scala> ")
27-
private val newLinePrompt = blue(" | ")
25+
private def blue(str: String)(implicit ctx: Context) =
26+
if (ctx.settings.color.value != "never") Console.BLUE + str + Console.RESET
27+
else str
28+
private def prompt(implicit ctx: Context) = blue("scala> ")
29+
private def newLinePrompt(implicit ctx: Context) = blue(" | ")
2830

2931
/** Blockingly read line from `System.in`
3032
*
@@ -64,7 +66,7 @@ final class JLineTerminal extends java.io.Closeable {
6466
def close() = terminal.close()
6567

6668
/** Provide syntax highlighting */
67-
private class Highlighter extends reader.Highlighter {
69+
private class Highlighter(implicit ctx: Context) extends reader.Highlighter {
6870
def highlight(reader: LineReader, buffer: String): AttributedString = {
6971
val highlighted = SyntaxHighlighting(buffer).mkString
7072
AttributedString.fromAnsi(highlighted)

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,14 @@ class ReplDriver(settings: Array[String],
339339
case TypeOf(expr) =>
340340
compiler.typeOf(expr)(newRun(state)).fold(
341341
displayErrors,
342-
res => out.println(SyntaxHighlighting(res))
342+
res => out.println(SyntaxHighlighting(res)(state.context))
343343
)
344344
state
345345

346346
case DocOf(expr) =>
347347
compiler.docOf(expr)(newRun(state)).fold(
348348
displayErrors,
349-
res => out.println(SyntaxHighlighting(res))
349+
res => out.println(SyntaxHighlighting(res)(state.context))
350350
)
351351
state
352352

compiler/test/dotty/tools/dotc/printing/SyntaxHighlightingTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class SyntaxHighlightingTests {
99
import SyntaxHighlighting._
1010

1111
private def test(source: String, expected: String): Unit = {
12-
val highlighted = SyntaxHighlighting.apply(source)
12+
val highlighted = SyntaxHighlighting.highlight(source)
1313
.mkString
1414
.replace(NoColor, ">")
1515
.replace(CommentColor, "<C|")

0 commit comments

Comments
 (0)