Skip to content

Commit 974c64e

Browse files
committed
Add ability to choose between fancy and non-fancy output
1 parent 676d9ee commit 974c64e

File tree

5 files changed

+110
-61
lines changed

5 files changed

+110
-61
lines changed

src/dotty/tools/dotc/Compiler.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Symbols._
88
import Types._
99
import Scopes._
1010
import typer.{FrontEnd, Typer, ImportInfo, RefChecks}
11-
import reporting.{Reporter, FancyConsoleReporter}
11+
import reporting.{Reporter, ConsoleReporter, FancyConsoleReporter}
1212
import Phases.Phase
1313
import transform._
1414
import transform.TreeTransforms.{TreeTransform, TreeTransformer}
@@ -139,6 +139,10 @@ class Compiler {
139139
.setTyper(new Typer)
140140
.setMode(Mode.ImplicitsEnabled)
141141
.setTyperState(new MutableTyperState(ctx.typerState, ctx.typerState.reporter, isCommittable = true))
142+
.setReporter(
143+
if (ctx.settings.color.value == "never") new ConsoleReporter()
144+
else new FancyConsoleReporter()
145+
)
142146
ctx.initialize()(start) // re-initialize the base context with start
143147
def addImport(ctx: Context, refFn: () => TermRef) =
144148
ctx.fresh.setImportInfo(ImportInfo.rootImport(refFn)(ctx))

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import core.Contexts.Context
1111
object SyntaxHighlighting {
1212

1313
implicit class SyntaxFormatting(val sc: StringContext) extends AnyVal {
14-
def hl(args: Any*): String =
15-
sc.s(args.map(x => new String(apply(x.toString).toArray)): _*)
14+
def hl(args: Any*)(implicit ctx: Context): String =
15+
if (ctx.settings.color.value == "never") sc.s(args)
16+
else sc.s(args.map(x => new String(apply(x.toString).toArray)): _*)
1617
}
1718

1819
val NoColor = Console.RESET

src/dotty/tools/dotc/repl/CompilingInterpreter.scala

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import scala.collection.mutable.{ListBuffer, HashSet, ArrayBuffer}
1616
//import ast.parser.SyntaxAnalyzer
1717
import io.{PlainFile, VirtualDirectory}
1818
import scala.reflect.io.{PlainDirectory, Directory}
19-
import reporting.{FancyConsoleReporter, Reporter}
19+
import reporting.{ConsoleReporter, FancyConsoleReporter, Reporter}
2020
import core.Flags
2121
import util.{SourceFile, NameTransformer}
2222
import io.ClassPath
@@ -117,23 +117,31 @@ class CompilingInterpreter(
117117
}
118118
}
119119

120-
private def newReporter = new FancyConsoleReporter(Console.in, out) {
121-
override def printMessage(msg: String) = {
122-
if (!delayOutput) {
123-
out.print(/*clean*/(msg) + "\n")
124-
// Suppress clean for now for compiler messages
125-
// Otherwise we will completely delete all references to
126-
// line$object$ module classes. The previous interpreter did not
127-
// have the project because the module class was written without the final `$'
128-
// and therefore escaped the purge. We can turn this back on once
129-
// we drop the final `$' from module classes.
130-
out.flush()
131-
} else {
132-
previousOutput += (/*clean*/(msg) + "\n")
133-
}
120+
private def customPrintMessage(msg: String) = {
121+
if (!delayOutput) {
122+
out.print(/*clean*/(msg) + "\n")
123+
// Suppress clean for now for compiler messages
124+
// Otherwise we will completely delete all references to
125+
// line$object$ module classes. The previous interpreter did not
126+
// have the project because the module class was written without the final `$'
127+
// and therefore escaped the purge. We can turn this back on once
128+
// we drop the final `$' from module classes.
129+
out.flush()
130+
} else {
131+
previousOutput += (/*clean*/(msg) + "\n")
134132
}
135133
}
136134

135+
private def newReporter(implicit ctx: Context) =
136+
if (ctx.settings.color.value == "never")
137+
new ConsoleReporter(Console.in, out) {
138+
override def printMessage(msg: String) = customPrintMessage(msg)
139+
}
140+
else
141+
new FancyConsoleReporter(Console.in, out) {
142+
override def printMessage(msg: String) = customPrintMessage(msg)
143+
}
144+
137145
/** the previous requests this interpreter has processed */
138146
private val prevRequests = new ArrayBuffer[Request]()
139147

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package dotty.tools
2+
package dotc
3+
package reporting
4+
5+
import scala.collection.mutable
6+
import util.SourcePosition
7+
import core.Contexts._
8+
import Reporter._
9+
import java.io.{ BufferedReader, IOException, PrintWriter }
10+
import scala.reflect.internal.util._
11+
12+
/**
13+
* This class implements a Reporter that displays messages on a text
14+
* console.
15+
*/
16+
class ConsoleReporter(
17+
reader: BufferedReader = Console.in,
18+
writer: PrintWriter = new PrintWriter(Console.err, true))
19+
extends Reporter with UniqueMessagePositions with HideNonSensicalMessages {
20+
21+
/** maximal number of error messages to be printed */
22+
protected def ErrorLimit = 100
23+
24+
def printSourceLine(pos: SourcePosition) =
25+
printMessage(pos.lineContent.stripLineEnd)
26+
27+
def printColumnMarker(pos: SourcePosition) =
28+
if (pos.exists) { printMessage(" " * pos.column + "^") }
29+
30+
/** Prints the message. */
31+
def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
32+
33+
/** Prints the message with the given position indication. */
34+
def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
35+
val posStr = if (pos.exists) s"$pos: " else ""
36+
printMessage(posStr + kind + msg)
37+
if (pos.exists) {
38+
printSourceLine(pos)
39+
printColumnMarker(pos)
40+
}
41+
}
42+
43+
override def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match {
44+
case d: Error =>
45+
printMessageAndPos(d.message, d.pos, d.kind)
46+
if (ctx.settings.prompt.value) displayPrompt()
47+
case d: ConditionalWarning if !d.enablingOption.value =>
48+
case d: MigrationWarning =>
49+
printMessageAndPos(d.message, d.pos, d.kind)
50+
case d: Warning =>
51+
printMessageAndPos(d.message, d.pos, d.kind)
52+
case _ =>
53+
printMessageAndPos(d.message, d.pos, d.kind)
54+
}
55+
56+
def displayPrompt(): Unit = {
57+
writer.print("\na)bort, s)tack, r)esume: ")
58+
writer.flush()
59+
if (reader != null) {
60+
val response = reader.read().asInstanceOf[Char].toLower
61+
if (response == 'a' || response == 's') {
62+
Thread.dumpStack()
63+
if (response == 'a')
64+
sys.exit(1)
65+
}
66+
writer.print("\n")
67+
writer.flush()
68+
}
69+
}
70+
71+
override def flush()(implicit ctx: Context): Unit = { writer.flush() }
72+
}

src/dotty/tools/dotc/reporting/FancyConsoleReporter.scala

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@ import scala.reflect.internal.util._
1111
import printing.SyntaxHighlighting._
1212

1313
/**
14-
* This class implements a Reporter that displays messages on a text
15-
* console.
14+
* This class implements a more Fancy version (with colors!) of the regular
15+
* `ConsoleReporter`
1616
*/
1717
class FancyConsoleReporter(
18-
reader: BufferedReader = Console.in,
19-
writer: PrintWriter = new PrintWriter(Console.err, true))
20-
extends Reporter with UniqueMessagePositions with HideNonSensicalMessages {
18+
reader: BufferedReader = Console.in,
19+
writer: PrintWriter = new PrintWriter(Console.err, true)
20+
) extends ConsoleReporter(reader, writer) {
2121

22-
/** maximal number of error messages to be printed */
23-
protected def ErrorLimit = 100
24-
25-
def sourceLine(pos: SourcePosition): (String, Int) = {
22+
def sourceLine(pos: SourcePosition)(implicit ctx: Context): (String, Int) = {
2623
val lineNum = s"${pos.line}:"
2724
(lineNum + hl"${pos.lineContent.stripLineEnd}", lineNum.length)
2825
}
@@ -68,11 +65,8 @@ class FancyConsoleReporter(
6865
NoColor
6966
} else ""
7067

71-
/** Prints the message. */
72-
def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
73-
7468
/** Prints the message with the given position indication. */
75-
def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
69+
override def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
7670
printMessage(posStr(pos, kind))
7771
if (pos.exists) {
7872
val (src, offset) = sourceLine(pos)
@@ -82,34 +76,4 @@ class FancyConsoleReporter(
8276
printMessage(List(src, marker, err).mkString("\n"))
8377
} else printMessage(msg)
8478
}
85-
86-
override def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match {
87-
case d: Error =>
88-
printMessageAndPos(d.message, d.pos, d.kind)
89-
if (ctx.settings.prompt.value) displayPrompt()
90-
case d: ConditionalWarning if !d.enablingOption.value =>
91-
case d: MigrationWarning =>
92-
printMessageAndPos(d.message, d.pos, d.kind)
93-
case d: Warning =>
94-
printMessageAndPos(d.message, d.pos, d.kind)
95-
case _ =>
96-
printMessageAndPos(d.message, d.pos)
97-
}
98-
99-
def displayPrompt(): Unit = {
100-
writer.print("\na)bort, s)tack, r)esume: ")
101-
writer.flush()
102-
if (reader != null) {
103-
val response = reader.read().asInstanceOf[Char].toLower
104-
if (response == 'a' || response == 's') {
105-
Thread.dumpStack()
106-
if (response == 'a')
107-
sys.exit(1)
108-
}
109-
writer.print("\n")
110-
writer.flush()
111-
}
112-
}
113-
114-
override def flush()(implicit ctx: Context): Unit = { writer.flush() }
11579
}

0 commit comments

Comments
 (0)