@@ -8,6 +8,7 @@ import core.Contexts._
8
8
import Reporter ._
9
9
import java .io .{ BufferedReader , IOException , PrintWriter }
10
10
import scala .reflect .internal .util ._
11
+ import printing .SyntaxHighlighting ._
11
12
12
13
/**
13
14
* This class implements a Reporter that displays messages on a text
@@ -21,34 +22,76 @@ class ConsoleReporter(
21
22
/** maximal number of error messages to be printed */
22
23
protected def ErrorLimit = 100
23
24
24
- def printSourceLine (pos : SourcePosition ) =
25
- printMessage(pos.lineContent.stripLineEnd)
25
+ def sourceLine (pos : SourcePosition ): (String , Int ) = {
26
+ val lineNum = s " ${pos.line}: "
27
+ (lineNum + hl " ${pos.lineContent.stripLineEnd}" , lineNum.length)
28
+ }
29
+
30
+ def columnMarker (pos : SourcePosition , offset : Int ) =
31
+ if (pos.startLine == pos.endLine) {
32
+ val whitespace = " " * (pos.column + offset)
33
+ val carets =
34
+ AnnotationColor +
35
+ (" ^" * math.max(1 , pos.endColumn - pos.startColumn)) +
36
+ NoColor
37
+
38
+ whitespace + carets
39
+ } else {
40
+ " " * (pos.column + offset) + AnnotationColor + " ^" + NoColor
41
+ }
42
+
43
+ def errorMsg (pos : SourcePosition , msg : String , offset : Int )(implicit ctx : Context ) = {
44
+ var hasLongLines = false
45
+ val leastWhitespace = msg.lines.foldLeft(Int .MaxValue ) { (minPad, line) =>
46
+ val lineLength =
47
+ line.replaceAll(" \u001B\\ [[;\\ d]*m" , " " ).length
48
+ val padding =
49
+ math.min(math.max(0 , ctx.settings.pageWidth.value - offset - lineLength), offset + pos.startColumn)
50
+
51
+ if (padding < minPad) padding
52
+ else minPad
53
+ }
26
54
27
- def printColumnMarker (pos : SourcePosition ) =
28
- if (pos.exists) { printMessage(" " * pos.column + " ^" ) }
55
+ msg
56
+ .lines
57
+ .map { line => " " * leastWhitespace + line }
58
+ .mkString(sys.props(" line.separator" ))
59
+ }
60
+
61
+ def posStr (pos : SourcePosition , kind : String )(implicit ctx : Context ) =
62
+ if (pos.exists) {
63
+ val file = pos.source.file.toString
64
+
65
+ s " ${Console .CYAN }$kind: $file " +
66
+ " -" * math.max(ctx.settings.pageWidth.value - file.length - kind.length - 1 , 0 ) +
67
+ NoColor
68
+ }
69
+ else " "
29
70
30
71
/** Prints the message. */
31
72
def printMessage (msg : String ): Unit = { writer.print(msg + " \n " ); writer.flush() }
32
73
33
74
/** Prints the message with the given position indication. */
34
- def printMessageAndPos (msg : String , pos : SourcePosition )(implicit ctx : Context ): Unit = {
35
- val posStr = if (pos.exists) s " $pos: " else " "
36
- printMessage(posStr + msg)
75
+ def printMessageAndPos (msg : String , pos : SourcePosition , kind : String = " " )(implicit ctx : Context ): Unit = {
76
+ printMessage(posStr(pos, kind))
37
77
if (pos.exists) {
38
- printSourceLine(pos)
39
- printColumnMarker(pos)
40
- }
78
+ val (src, offset) = sourceLine(pos)
79
+ val marker = columnMarker(pos, offset)
80
+ val err = errorMsg(pos, msg, offset)
81
+
82
+ printMessage(List (src, marker, err).mkString(" \n " ))
83
+ } else printMessage(msg)
41
84
}
42
85
43
86
override def doReport (d : Diagnostic )(implicit ctx : Context ): Unit = d match {
44
87
case d : Error =>
45
- printMessageAndPos(s " error: ${ d.message} " , d.pos)
88
+ printMessageAndPos(d.message, d.pos, " Error in " )
46
89
if (ctx.settings.prompt.value) displayPrompt()
47
90
case d : ConditionalWarning if ! d.enablingOption.value =>
48
91
case d : MigrationWarning =>
49
- printMessageAndPos(s " migration warning: ${ d.message} " , d.pos)
92
+ printMessageAndPos(d.message, d.pos, " Migration Warning in " )
50
93
case d : Warning =>
51
- printMessageAndPos(s " warning: ${ d.message} " , d.pos)
94
+ printMessageAndPos(d.message, d.pos, " Warning in " )
52
95
case _ =>
53
96
printMessageAndPos(d.message, d.pos)
54
97
}
0 commit comments