aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Mulder <felix.mulder@gmail.com>2016-09-19 20:00:44 +0200
committerFelix Mulder <felix.mulder@gmail.com>2016-10-10 13:25:34 +0200
commit1532c82788ab2366df04cac2e418d3db98390ef4 (patch)
treecf77d386016cc442c161d7fce347b7fd4e89e3a6
parente24289ac19a21c6b3794d02e8fe42766224f173c (diff)
downloaddotty-1532c82788ab2366df04cac2e418d3db98390ef4.tar.gz
dotty-1532c82788ab2366df04cac2e418d3db98390ef4.tar.bz2
dotty-1532c82788ab2366df04cac2e418d3db98390ef4.zip
Remove duplication of console reporters
-rw-r--r--src/dotty/tools/dotc/Compiler.scala2
-rw-r--r--src/dotty/tools/dotc/core/Contexts.scala2
-rw-r--r--src/dotty/tools/dotc/repl/CompilingInterpreter.scala40
-rw-r--r--src/dotty/tools/dotc/reporting/ConsoleReporter.scala86
-rw-r--r--src/dotty/tools/dotc/reporting/FancyConsoleReporter.scala93
-rw-r--r--test/test/TestREPL.scala4
-rw-r--r--tests/repl/errmsgs.check97
-rw-r--r--tests/repl/imports.check22
8 files changed, 148 insertions, 198 deletions
diff --git a/src/dotty/tools/dotc/Compiler.scala b/src/dotty/tools/dotc/Compiler.scala
index ea6254f5b..178cba7c4 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, ConsoleReporter, FancyConsoleReporter}
+import reporting.{Reporter, ConsoleReporter}
import Phases.Phase
import transform._
import transform.TreeTransforms.{TreeTransform, TreeTransformer}
diff --git a/src/dotty/tools/dotc/core/Contexts.scala b/src/dotty/tools/dotc/core/Contexts.scala
index d1447a98f..5c9fdaf88 100644
--- a/src/dotty/tools/dotc/core/Contexts.scala
+++ b/src/dotty/tools/dotc/core/Contexts.scala
@@ -501,7 +501,7 @@ object Contexts {
outer = NoContext
period = InitialPeriod
mode = Mode.None
- typerState = new TyperState(new FancyConsoleReporter())
+ typerState = new TyperState(new ConsoleReporter())
printerFn = new RefinedPrinter(_)
owner = NoSymbol
sstate = settings.defaultState
diff --git a/src/dotty/tools/dotc/repl/CompilingInterpreter.scala b/src/dotty/tools/dotc/repl/CompilingInterpreter.scala
index 70b32a16a..0964de303 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.{ConsoleReporter, FancyConsoleReporter, Reporter}
+import reporting.{ConsoleReporter, Reporter}
import core.Flags
import util.{SourceFile, NameTransformer}
import io.ClassPath
@@ -117,30 +117,22 @@ class CompilingInterpreter(
}
}
- 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 =
+ new ConsoleReporter(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 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
index 92eea9fa9..a5979bd5b 100644
--- a/src/dotty/tools/dotc/reporting/ConsoleReporter.scala
+++ b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala
@@ -8,13 +8,14 @@ import core.Contexts._
import Reporter._
import java.io.{ BufferedReader, IOException, PrintWriter }
import scala.reflect.internal.util._
-import diagnostic.Message
-import diagnostic.MessageContainer
+import printing.SyntaxHighlighting._
+import printing.Highlighting._
+import diagnostic.{ Message, MessageContainer }
import diagnostic.messages._
/**
- * 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 ConsoleReporter(
reader: BufferedReader = Console.in,
@@ -26,32 +27,72 @@ class ConsoleReporter(
/** 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() }
+ def stripColor(str: String): String =
+ str.replaceAll("\u001B\\[[;\\d]*m", "")
+
+ def sourceLine(pos: SourcePosition)(implicit ctx: Context): (String, Int) = {
+ val lineNum = s"${pos.line}:"
+ (lineNum + hl"${pos.lineContent.stripLineEnd}", lineNum.length)
+ }
+
+ def columnMarker(pos: SourcePosition, offset: Int)(implicit ctx: Context) =
+ if (pos.startLine == pos.endLine) {
+ val whitespace = " " * (pos.column + offset)
+ val carets =
+ Red("^" * math.max(1, pos.endColumn - pos.startColumn))
+
+ whitespace + carets.show
+ } else {
+ Red(" " * (pos.column + offset) + "^").show
+ }
+
+ def errorMsg(pos: SourcePosition, msg: String, offset: Int)(implicit ctx: Context) = {
+ var hasLongLines = false
+ val leastWhitespace = msg.lines.foldLeft(Int.MaxValue) { (minPad, line) =>
+ val lineLength = stripColor(line).length
+ val padding =
+ math.min(math.max(0, ctx.settings.pageWidth.value - offset - lineLength), offset + pos.startColumn)
+
+ if (padding < minPad) padding
+ else minPad
+ }
+
+ msg
+ .lines
+ .map { line => " " * leastWhitespace + line }
+ .mkString(sys.props("line.separator"))
+ }
+
+ def posStr(pos: SourcePosition, kind: String, errorId: String)(implicit ctx: Context) =
+ if (pos.exists) Blue({
+ val file = pos.source.file.toString
+ val errId = if (errorId != "") s"[$errorId] " else ""
+ val prefix = s"-- ${errId}${kind}: $file "
+ prefix +
+ ("-" * math.max(ctx.settings.pageWidth.value - stripColor(prefix).length, 0))
+ }).show else ""
+
/** Prints the message with the given position indication. */
def printMessageAndPos(msg: Message, pos: SourcePosition, kind: String)(implicit ctx: Context): Unit = {
- val posStr = if (pos.exists) s"$pos: " else ""
- printMessage(s"${posStr}${kind}: ${msg.msg}")
+ printMessage(posStr(pos, kind, msg.errorId))
if (pos.exists) {
- printSourceLine(pos)
- printColumnMarker(pos)
- }
+ val (src, offset) = sourceLine(pos)
+ val marker = columnMarker(pos, offset)
+ val err = errorMsg(pos, msg.msg, offset)
+
+ printMessage(List(src, marker, err).mkString("\n"))
+ } else printMessage(msg.msg)
}
- def printExplanation(m: Message)(implicit ctx: Context): Unit =
- printMessage(
- s"""|
- |Explanation
- |===========
- |${m.explanation}""".stripMargin
- )
+ def printExplanation(m: Message)(implicit ctx: Context): Unit = {
+ printMessage(hl"""|
+ |${Blue("Explanation")}
+ |${Blue("===========")}""".stripMargin)
+ printMessage(m.explanation)
+ }
override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = {
m match {
@@ -87,3 +128,4 @@ class ConsoleReporter(
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
deleted file mode 100644
index 725e69ff8..000000000
--- a/src/dotty/tools/dotc/reporting/FancyConsoleReporter.scala
+++ /dev/null
@@ -1,93 +0,0 @@
-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._
-import printing.SyntaxHighlighting._
-import printing.Highlighting._
-import diagnostic.Message
-
-/**
- * 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 ConsoleReporter(reader, writer) {
-
- def stripColor(str: String): String =
- str.replaceAll("\u001B\\[[;\\d]*m", "")
-
- def sourceLine(pos: SourcePosition)(implicit ctx: Context): (String, Int) = {
- val lineNum = s"${pos.line}:"
- (lineNum + hl"${pos.lineContent.stripLineEnd}", lineNum.length)
- }
-
- def columnMarker(pos: SourcePosition, offset: Int)(implicit ctx: Context) =
- if (pos.startLine == pos.endLine) {
- val whitespace = " " * (pos.column + offset)
- val carets =
- Red("^" * math.max(1, pos.endColumn - pos.startColumn))
-
- whitespace + carets.show
- } else {
- Red(" " * (pos.column + offset) + "^").show
- }
-
- def errorMsg(pos: SourcePosition, msg: String, offset: Int)(implicit ctx: Context) = {
- var hasLongLines = false
- val leastWhitespace = msg.lines.foldLeft(Int.MaxValue) { (minPad, line) =>
- val lineLength = stripColor(line).length
- val padding =
- math.min(math.max(0, ctx.settings.pageWidth.value - offset - lineLength), offset + pos.startColumn)
-
- if (padding < minPad) padding
- else minPad
- }
-
- msg
- .lines
- .map { line => " " * leastWhitespace + line }
- .mkString(sys.props("line.separator"))
- }
-
- def posStr(pos: SourcePosition, kind: String)(implicit ctx: Context) =
- if (pos.exists) Blue({
- val file = pos.source.file.toString
-
- val outer = if (pos.outer.exists) {
- s"This location is in code that was inlined at ${pos.outer}:\n" +
- printStr(pos.outer) + "\n" + "-" * ctx.settings.pageWidth.value
- } else ""
-
- val prefix = s"-- $kind: $file "
- prefix +
- ("-" * math.max(ctx.settings.pageWidth.value - stripColor(prefix).length, 0)) +
- "\n" + outer
- }).show else ""
-
- /** Prints the message with the given position indication. */
- override def printMessageAndPos(msg: Message, pos: SourcePosition, kind: String)(implicit ctx: Context): Unit = {
- printMessage(posStr(pos, kind))
- if (pos.exists) {
- val (src, offset) = sourceLine(pos)
- val marker = columnMarker(pos, offset)
- val err = errorMsg(pos, msg.msg, offset)
-
- printMessage(List(src, marker, err).mkString("\n"))
- } else printMessage(msg.msg)
- }
-
- override def printExplanation(m: Message)(implicit ctx: Context): Unit = {
- printMessage(hl"""|
- |${Blue("Explanation")}
- |${Blue("===========")}""".stripMargin)
- printMessage(m.explanation)
- }
-}
diff --git a/test/test/TestREPL.scala b/test/test/TestREPL.scala
index 9867cb4ec..090c774bb 100644
--- a/test/test/TestREPL.scala
+++ b/test/test/TestREPL.scala
@@ -21,7 +21,9 @@ class TestREPL(script: String) extends REPL {
override val output = new NewLinePrintWriter(out)
override def context(ctx: Context) =
- ctx.fresh.setSetting(ctx.settings.color, "never")
+ ctx.fresh
+ .setSetting(ctx.settings.color, "never")
+ .setSetting(ctx.settings.pageWidth, 80)
override def input(in: Interpreter)(implicit ctx: Context) = new InteractiveReader {
val lines = script.lines.buffered
diff --git a/tests/repl/errmsgs.check b/tests/repl/errmsgs.check
index e32e72d1f..d8c1b9215 100644
--- a/tests/repl/errmsgs.check
+++ b/tests/repl/errmsgs.check
@@ -1,43 +1,48 @@
scala> class Inv[T](x: T)
defined class Inv
scala> val x: List[String] = List(1)
-<console>:4: Error: type mismatch:
-found: Int(1)
-required: String
-val x: List[String] = List(1)
- ^
+-- Error: <console> ------------------------------------------------------------
+3:val x: List[String] = List(1)
+ ^
+ type mismatch:
+ found: Int(1)
+ required: String
scala> val y: List[List[String]] = List(List(1))
-<console>:4: Error: type mismatch:
-found: Int(1)
-required: String
-val y: List[List[String]] = List(List(1))
- ^
+-- Error: <console> ------------------------------------------------------------
+3:val y: List[List[String]] = List(List(1))
+ ^
+ type mismatch:
+ found: Int(1)
+ required: String
scala> val z: (List[String], List[Int]) = (List(1), List("a"))
-<console>:4: Error: type mismatch:
-found: Int(1)
-required: String
-val z: (List[String], List[Int]) = (List(1), List("a"))
- ^
-<console>:4: Error: type mismatch:
-found: String("a")
-required: Int
-val z: (List[String], List[Int]) = (List(1), List("a"))
- ^
+-- Error: <console> ------------------------------------------------------------
+3:val z: (List[String], List[Int]) = (List(1), List("a"))
+ ^
+ type mismatch:
+ found: Int(1)
+ required: String
+-- Error: <console> ------------------------------------------------------------
+3:val z: (List[String], List[Int]) = (List(1), List("a"))
+ ^
+ type mismatch:
+ found: String("a")
+ required: Int
scala> val a: Inv[String] = new Inv(new Inv(1))
-<console>:5: Error: type mismatch:
-found: Inv[T]
-required: String
-
-where T is a type variable with constraint >: Int(1)
-
-val a: Inv[String] = new Inv(new Inv(1))
- ^
+-- Error: <console> ------------------------------------------------------------
+4:val a: Inv[String] = new Inv(new Inv(1))
+ ^^^^
+ type mismatch:
+ found: Inv[T]
+ required: String
+
+ where T is a type variable with constraint >: Int(1)
scala> val b: Inv[String] = new Inv(1)
-<console>:5: Error: type mismatch:
-found: Int(1)
-required: String
-val b: Inv[String] = new Inv(1)
- ^
+-- Error: <console> ------------------------------------------------------------
+4:val b: Inv[String] = new Inv(1)
+ ^
+ type mismatch:
+ found: Int(1)
+ required: String
scala> abstract class C {
type T
val x: T
@@ -53,22 +58,22 @@ scala> abstract class C {
}
}
}
-<console>:9: Error: type mismatch:
-found: C.this.T(C.this.x)
-required: T'
-
-where T is a type in class C
- T' is a type in the initalizer of value s which is an alias of String
-
- var y: T = x
- ^
-<console>:13: Error: type mismatch:
+-- Error: <console> ------------------------------------------------------------
+8: var y: T = x
+ ^
+ type mismatch:
+ found: C.this.T(C.this.x)
+ required: T'
+
+ where T is a type in class C
+ T' is a type in the initalizer of value s which is an alias of String
+-- Error: <console> ------------------------------------------------------------
+12: val z: T = y
+ ^
+type mismatch:
found: T(y)
required: T'
where T is a type in the initalizer of value s which is an alias of String
T' is a type in method f which is an alias of Int
-
- val z: T = y
- ^
scala> :quit
diff --git a/tests/repl/imports.check b/tests/repl/imports.check
index 50b7a93d7..f9e48c011 100644
--- a/tests/repl/imports.check
+++ b/tests/repl/imports.check
@@ -7,16 +7,18 @@ defined module o
scala> import o._
import o._
scala> buf += xs
-<console>:11: Error: type mismatch:
-found: scala.collection.immutable.List[Int](o.xs)
-required: String
-buf += xs
- ^
-<console>:11: Error: type mismatch:
-found: String
-required: scala.collection.mutable.ListBuffer[Int]
-buf += xs
-^
+-- Error: <console> ------------------------------------------------------------
+10:buf += xs
+ ^^
+ type mismatch:
+ found: scala.collection.immutable.List[Int](o.xs)
+ required: String
+-- Error: <console> ------------------------------------------------------------
+10:buf += xs
+ ^^^^^^^^^
+ type mismatch:
+ found: String
+ required: scala.collection.mutable.ListBuffer[Int]
scala> buf ++= xs
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
scala> :quit