@@ -2,15 +2,10 @@ package dotty.tools
2
2
package dotc
3
3
package reporting
4
4
5
- import scala .collection .mutable
6
5
import util .SourcePosition
7
- import core .Contexts ._ , core .Decorators ._
8
- import Reporter ._
9
- import java .io .{ BufferedReader , IOException , PrintWriter }
10
- import scala .reflect .internal .util ._
11
- import printing .SyntaxHighlighting ._
12
- import printing .Highlighting ._
13
- import diagnostic .{ Message , MessageContainer , NoExplanation }
6
+ import core .Contexts ._
7
+ import java .io .{ BufferedReader , PrintWriter }
8
+ import diagnostic .{ Message , MessageContainer }
14
9
import diagnostic .messages ._
15
10
16
11
/**
@@ -19,7 +14,7 @@ import diagnostic.messages._
19
14
class ConsoleReporter (
20
15
reader : BufferedReader = Console .in,
21
16
writer : PrintWriter = new PrintWriter (Console .err, true )
22
- ) extends Reporter with UniqueMessagePositions with HideNonSensicalMessages {
17
+ ) extends Reporter with UniqueMessagePositions with HideNonSensicalMessages with MessageRendering {
23
18
24
19
import MessageContainer ._
25
20
@@ -29,111 +24,26 @@ class ConsoleReporter(
29
24
/** Prints the message. */
30
25
def printMessage (msg : String ): Unit = { writer.print(msg + " \n " ); writer.flush() }
31
26
32
- def stripColor (str : String ): String =
33
- str.replaceAll(" \u001B\\ [[;\\ d]*m" , " " )
34
-
35
- def sourceLines (pos : SourcePosition )(implicit ctx : Context ): (List [String ], List [String ], Int ) = {
36
- var maxLen = Int .MinValue
37
- def render (xs : List [Int ]) =
38
- xs.map(pos.source.offsetToLine(_))
39
- .map { lineNbr =>
40
- val prefix = s " ${lineNbr + 1 } | "
41
- maxLen = math.max(maxLen, prefix.length)
42
- (prefix, pos.lineContent(lineNbr).stripLineEnd)
43
- }
44
- .map { case (prefix, line) =>
45
- val lnum = Red (" " * math.max(0 , maxLen - prefix.length) + prefix)
46
- hl " $lnum$line"
47
- }
48
-
49
- val (before, after) = pos.beforeAndAfterPoint
50
- (render(before), render(after), maxLen)
51
- }
52
-
53
- def columnMarker (pos : SourcePosition , offset : Int )(implicit ctx : Context ) = {
54
- val prefix = " " * (offset - 1 )
55
- val whitespace = " " * pos.startColumn
56
- val carets = Red {
57
- if (pos.startLine == pos.endLine)
58
- " ^" * math.max(1 , pos.endColumn - pos.startColumn)
59
- else " ^"
60
- }
61
-
62
- s " $prefix| $whitespace${carets.show}"
63
- }
64
-
65
- def errorMsg (pos : SourcePosition , msg : String , offset : Int )(implicit ctx : Context ) = {
66
- val leastWhitespace = msg.lines.foldLeft(Int .MaxValue ) { (minPad, line) =>
67
- val lineLength = stripColor(line).length
68
- val padding =
69
- math.min(math.max(0 , ctx.settings.pageWidth.value - offset - lineLength), offset + pos.startColumn)
70
-
71
- if (padding < minPad) padding
72
- else minPad
73
- }
74
-
75
- msg.lines
76
- .map { line => " " * (offset - 1 ) + " |" + (" " * (leastWhitespace - offset)) + line }
77
- .mkString(sys.props(" line.separator" ))
78
- }
79
-
80
- def posStr (pos : SourcePosition , diagnosticLevel : String , message : Message )(implicit ctx : Context ) =
81
- if (pos.exists) Blue ({
82
- val file = pos.source.file.toString
83
- val errId =
84
- if (message.errorId != NoExplanation .ID )
85
- s " [E ${" 0" * (3 - message.errorId.toString.length) + message.errorId}] "
86
- else " "
87
- val kind =
88
- if (message.kind == " " ) diagnosticLevel
89
- else s " ${message.kind} $diagnosticLevel"
90
- val prefix = s " -- ${errId}${kind}: $file "
91
-
92
- prefix +
93
- (" -" * math.max(ctx.settings.pageWidth.value - stripColor(prefix).length, 0 ))
94
- }).show else " "
95
-
96
27
/** Prints the message with the given position indication. */
97
28
def printMessageAndPos (msg : Message , pos : SourcePosition , diagnosticLevel : String )(implicit ctx : Context ): Boolean = {
98
- printMessage(posStr(pos, diagnosticLevel, msg))
99
- if (pos.exists) {
100
- val (srcBefore, srcAfter, offset) = sourceLines(pos)
101
- val marker = columnMarker(pos, offset)
102
- val err = errorMsg(pos, msg.msg, offset)
103
-
104
- printMessage((srcBefore ::: marker :: err :: srcAfter).mkString(" \n " ))
105
- } else printMessage(msg.msg)
29
+ printMessage(messageAndPos(msg, pos, diagnosticLevel))
106
30
true
107
31
}
108
32
109
33
def printExplanation (m : Message )(implicit ctx : Context ): Unit = {
110
- printMessage(hl """ |
111
- | ${Blue (" Explanation" )}
112
- | ${Blue (" ===========" )}""" .stripMargin)
113
- printMessage(m.explanation)
114
- if (m.explanation.lastOption != Some ('\n ' )) printMessage(" " )
34
+ printMessage(explanation(m))
115
35
}
116
36
117
37
override def doReport (m : MessageContainer )(implicit ctx : Context ): Unit = {
118
38
val didPrint = m match {
119
39
case m : Error =>
120
- val didPrint = printMessageAndPos(m.contained, m.pos, " Error " )
40
+ val didPrint = printMessageAndPos(m.contained, m.pos, diagnosticLevel(m) )
121
41
if (ctx.settings.prompt.value) displayPrompt()
122
42
didPrint
123
43
case m : ConditionalWarning if ! m.enablingOption.value =>
124
44
false
125
- case m : FeatureWarning =>
126
- printMessageAndPos(m.contained, m.pos, " Feature Warning" )
127
- case m : DeprecationWarning =>
128
- printMessageAndPos(m.contained, m.pos, " Deprecation Warning" )
129
- case m : UncheckedWarning =>
130
- printMessageAndPos(m.contained, m.pos, " Unchecked Warning" )
131
- case m : MigrationWarning =>
132
- printMessageAndPos(m.contained, m.pos, " Migration Warning" )
133
- case m : Warning =>
134
- printMessageAndPos(m.contained, m.pos, " Warning" )
135
- case m : Info =>
136
- printMessageAndPos(m.contained, m.pos, " Info" )
45
+ case m =>
46
+ printMessageAndPos(m.contained, m.pos, diagnosticLevel(m))
137
47
}
138
48
139
49
if (didPrint && ctx.shouldExplain(m))
0 commit comments