aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Mulder <felix.mulder@gmail.com>2016-09-16 11:49:41 +0200
committerFelix Mulder <felix.mulder@gmail.com>2016-10-10 13:25:33 +0200
commit12ac3054bf4288403babb172c125cdc98cfff012 (patch)
tree978ffdb3a6699edd6a47ca0f86a3055e8baa6d54
parentfb4f8ce66c406bfb6376396ea0521df063b883e9 (diff)
downloaddotty-12ac3054bf4288403babb172c125cdc98cfff012.tar.gz
dotty-12ac3054bf4288403babb172c125cdc98cfff012.tar.bz2
dotty-12ac3054bf4288403babb172c125cdc98cfff012.zip
Add ability to choose between fancy and non-fancy output
-rw-r--r--src/dotty/tools/dotc/Compiler.scala6
-rw-r--r--src/dotty/tools/dotc/printing/SyntaxHighlighting.scala5
-rw-r--r--src/dotty/tools/dotc/repl/CompilingInterpreter.scala38
-rw-r--r--src/dotty/tools/dotc/reporting/ConsoleReporter.scala72
-rw-r--r--src/dotty/tools/dotc/reporting/FancyConsoleReporter.scala50
5 files changed, 110 insertions, 61 deletions
diff --git a/src/dotty/tools/dotc/Compiler.scala b/src/dotty/tools/dotc/Compiler.scala
index fb9ce83fc..d6e3cc89b 100644
--- a/src/dotty/tools/dotc/Compiler.scala
+++ b/src/dotty/tools/dotc/Compiler.scala
@@ -8,7 +8,7 @@ import Symbols._
import Types._
import Scopes._
import typer.{FrontEnd, Typer, ImportInfo, RefChecks}
-import reporting.{Reporter, FancyConsoleReporter}
+import reporting.{Reporter, ConsoleReporter, FancyConsoleReporter}
import Phases.Phase
import transform._
import transform.TreeTransforms.{TreeTransform, TreeTransformer}
@@ -140,6 +140,10 @@ class Compiler {
.setTyper(new Typer)
.setMode(Mode.ImplicitsEnabled)
.setTyperState(new MutableTyperState(ctx.typerState, ctx.typerState.reporter, isCommittable = true))
+ .setReporter(
+ if (ctx.settings.color.value == "never") new ConsoleReporter()
+ else new FancyConsoleReporter()
+ )
ctx.initialize()(start) // re-initialize the base context with start
def addImport(ctx: Context, refFn: () => TermRef) =
ctx.fresh.setImportInfo(ImportInfo.rootImport(refFn)(ctx))
diff --git a/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala
index 6c7e65a0b..958e71086 100644
--- a/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala
+++ b/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala
@@ -11,8 +11,9 @@ import core.Contexts.Context
object SyntaxHighlighting {
implicit class SyntaxFormatting(val sc: StringContext) extends AnyVal {
- def hl(args: Any*): String =
- sc.s(args.map(x => new String(apply(x.toString).toArray)): _*)
+ def hl(args: Any*)(implicit ctx: Context): String =
+ if (ctx.settings.color.value == "never") sc.s(args)
+ else sc.s(args.map(x => new String(apply(x.toString).toArray)): _*)
}
val NoColor = Console.RESET
diff --git a/src/dotty/tools/dotc/repl/CompilingInterpreter.scala b/src/dotty/tools/dotc/repl/CompilingInterpreter.scala
index ec2c167ce..70b32a16a 100644
--- a/src/dotty/tools/dotc/repl/CompilingInterpreter.scala
+++ b/src/dotty/tools/dotc/repl/CompilingInterpreter.scala
@@ -16,7 +16,7 @@ import scala.collection.mutable.{ListBuffer, HashSet, ArrayBuffer}
//import ast.parser.SyntaxAnalyzer
import io.{PlainFile, VirtualDirectory}
import scala.reflect.io.{PlainDirectory, Directory}
-import reporting.{FancyConsoleReporter, Reporter}
+import reporting.{ConsoleReporter, FancyConsoleReporter, Reporter}
import core.Flags
import util.{SourceFile, NameTransformer}
import io.ClassPath
@@ -117,23 +117,31 @@ class CompilingInterpreter(
}
}
- private def newReporter = new FancyConsoleReporter(Console.in, out) {
- override def printMessage(msg: String) = {
- if (!delayOutput) {
- out.print(/*clean*/(msg) + "\n")
- // Suppress clean for now for compiler messages
- // Otherwise we will completely delete all references to
- // line$object$ module classes. The previous interpreter did not
- // have the project because the module class was written without the final `$'
- // and therefore escaped the purge. We can turn this back on once
- // we drop the final `$' from module classes.
- out.flush()
- } else {
- previousOutput += (/*clean*/(msg) + "\n")
- }
+ private def customPrintMessage(msg: String) = {
+ if (!delayOutput) {
+ out.print(/*clean*/(msg) + "\n")
+ // Suppress clean for now for compiler messages
+ // Otherwise we will completely delete all references to
+ // line$object$ module classes. The previous interpreter did not
+ // have the project because the module class was written without the final `$'
+ // and therefore escaped the purge. We can turn this back on once
+ // we drop the final `$' from module classes.
+ out.flush()
+ } else {
+ previousOutput += (/*clean*/(msg) + "\n")
}
}
+ private def newReporter(implicit ctx: Context) =
+ if (ctx.settings.color.value == "never")
+ new ConsoleReporter(Console.in, out) {
+ override def printMessage(msg: String) = customPrintMessage(msg)
+ }
+ else
+ new FancyConsoleReporter(Console.in, out) {
+ override def printMessage(msg: String) = customPrintMessage(msg)
+ }
+
/** the previous requests this interpreter has processed */
private val prevRequests = new ArrayBuffer[Request]()
diff --git a/src/dotty/tools/dotc/reporting/ConsoleReporter.scala b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala
new file mode 100644
index 000000000..1d9423b70
--- /dev/null
+++ b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala
@@ -0,0 +1,72 @@
+package dotty.tools
+package dotc
+package reporting
+
+import scala.collection.mutable
+import util.SourcePosition
+import core.Contexts._
+import Reporter._
+import java.io.{ BufferedReader, IOException, PrintWriter }
+import scala.reflect.internal.util._
+
+/**
+ * This class implements a Reporter that displays messages on a text
+ * console.
+ */
+class ConsoleReporter(
+ reader: BufferedReader = Console.in,
+ writer: PrintWriter = new PrintWriter(Console.err, true))
+ extends Reporter with UniqueMessagePositions with HideNonSensicalMessages {
+
+ /** maximal number of error messages to be printed */
+ protected def ErrorLimit = 100
+
+ def printSourceLine(pos: SourcePosition) =
+ printMessage(pos.lineContent.stripLineEnd)
+
+ def printColumnMarker(pos: SourcePosition) =
+ if (pos.exists) { printMessage(" " * pos.column + "^") }
+
+ /** Prints the message. */
+ def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
+
+ /** Prints the message with the given position indication. */
+ def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
+ val posStr = if (pos.exists) s"$pos: " else ""
+ printMessage(posStr + kind + msg)
+ if (pos.exists) {
+ printSourceLine(pos)
+ printColumnMarker(pos)
+ }
+ }
+
+ override def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match {
+ case d: Error =>
+ printMessageAndPos(d.message, d.pos, d.kind)
+ if (ctx.settings.prompt.value) displayPrompt()
+ case d: ConditionalWarning if !d.enablingOption.value =>
+ case d: MigrationWarning =>
+ printMessageAndPos(d.message, d.pos, d.kind)
+ case d: Warning =>
+ printMessageAndPos(d.message, d.pos, d.kind)
+ case _ =>
+ printMessageAndPos(d.message, d.pos, d.kind)
+ }
+
+ def displayPrompt(): Unit = {
+ writer.print("\na)bort, s)tack, r)esume: ")
+ writer.flush()
+ if (reader != null) {
+ val response = reader.read().asInstanceOf[Char].toLower
+ if (response == 'a' || response == 's') {
+ Thread.dumpStack()
+ if (response == 'a')
+ sys.exit(1)
+ }
+ writer.print("\n")
+ writer.flush()
+ }
+ }
+
+ override def flush()(implicit ctx: Context): Unit = { writer.flush() }
+}
diff --git a/src/dotty/tools/dotc/reporting/FancyConsoleReporter.scala b/src/dotty/tools/dotc/reporting/FancyConsoleReporter.scala
index 57f9998a2..80f15a4b0 100644
--- a/src/dotty/tools/dotc/reporting/FancyConsoleReporter.scala
+++ b/src/dotty/tools/dotc/reporting/FancyConsoleReporter.scala
@@ -11,18 +11,15 @@ import scala.reflect.internal.util._
import printing.SyntaxHighlighting._
/**
- * This class implements a Reporter that displays messages on a text
- * console.
+ * This class implements a more Fancy version (with colors!) of the regular
+ * `ConsoleReporter`
*/
class FancyConsoleReporter(
- reader: BufferedReader = Console.in,
- writer: PrintWriter = new PrintWriter(Console.err, true))
- extends Reporter with UniqueMessagePositions with HideNonSensicalMessages {
+ reader: BufferedReader = Console.in,
+ writer: PrintWriter = new PrintWriter(Console.err, true)
+) extends ConsoleReporter(reader, writer) {
- /** maximal number of error messages to be printed */
- protected def ErrorLimit = 100
-
- def sourceLine(pos: SourcePosition): (String, Int) = {
+ def sourceLine(pos: SourcePosition)(implicit ctx: Context): (String, Int) = {
val lineNum = s"${pos.line}:"
(lineNum + hl"${pos.lineContent.stripLineEnd}", lineNum.length)
}
@@ -74,11 +71,8 @@ class FancyConsoleReporter(
}
else ""
- /** Prints the message. */
- def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
-
/** Prints the message with the given position indication. */
- def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
+ override def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
printMessage(posStr(pos, kind))
if (pos.exists) {
val (src, offset) = sourceLine(pos)
@@ -88,34 +82,4 @@ class FancyConsoleReporter(
printMessage(List(src, marker, err).mkString("\n"))
} else printMessage(msg)
}
-
- override def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match {
- case d: Error =>
- printMessageAndPos(d.message, d.pos, d.kind)
- if (ctx.settings.prompt.value) displayPrompt()
- case d: ConditionalWarning if !d.enablingOption.value =>
- case d: MigrationWarning =>
- printMessageAndPos(d.message, d.pos, d.kind)
- case d: Warning =>
- printMessageAndPos(d.message, d.pos, d.kind)
- case _ =>
- printMessageAndPos(d.message, d.pos)
- }
-
- def displayPrompt(): Unit = {
- writer.print("\na)bort, s)tack, r)esume: ")
- writer.flush()
- if (reader != null) {
- val response = reader.read().asInstanceOf[Char].toLower
- if (response == 'a' || response == 's') {
- Thread.dumpStack()
- if (response == 'a')
- sys.exit(1)
- }
- writer.print("\n")
- writer.flush()
- }
- }
-
- override def flush()(implicit ctx: Context): Unit = { writer.flush() }
}