aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/reporting/ConsoleReporter.scala
blob: 1ed889683e77f58e440c91db79b9cd8efa506aa7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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 diagnostic.Message

/**
 * 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(m: Message)(implicit ctx: Context): Unit = m match {
    case m: Error =>
      printMessageAndPos(m.message, m.pos, m.kind)
      if (ctx.settings.prompt.value) displayPrompt()
    case m: ConditionalWarning if !m.enablingOption.value =>
    case m: MigrationWarning =>
      printMessageAndPos(m.message, m.pos, m.kind)
    case m: Warning =>
      printMessageAndPos(m.message, m.pos, m.kind)
    case _ =>
      printMessageAndPos(m.message, m.pos, m.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() }
}