@@ -5,6 +5,7 @@ package init
5
5
6
6
import ast .tpd ._
7
7
import core ._
8
+ import util .SourcePosition
8
9
import Decorators ._ , printing .SyntaxHighlighting
9
10
import Types ._ , Symbols ._ , Contexts ._
10
11
@@ -26,28 +27,44 @@ object Errors {
26
27
def toErrors : Errors = this :: Nil
27
28
28
29
def stacktrace (using Context ): String = if (trace.isEmpty) " " else " Calling trace:\n " + {
29
- var indentCount = 0
30
30
var last : String = " "
31
31
val sb = new StringBuilder
32
32
trace.foreach { tree =>
33
- indentCount += 1
34
33
val pos = tree.sourcePos
35
- val prefix = s " ${ " " * indentCount } -> "
34
+ val prefix = " -> "
36
35
val line =
37
36
if pos.source.exists then
38
37
val loc = " [ " + pos.source.file.name + " :" + (pos.line + 1 ) + " ]"
39
38
val code = SyntaxHighlighting .highlight(pos.lineContent.trim.nn)
40
39
i " $code\t $loc"
41
40
else
42
41
tree.show
42
+ val positionMarkerLine =
43
+ if pos.exists && pos.source.exists then
44
+ positionMarker(pos)
45
+ else " "
43
46
44
- if (last != line) sb.append(prefix + line + " \n " )
47
+ if (last != line) sb.append(prefix + line + " \n " + positionMarkerLine )
45
48
46
49
last = line
47
50
}
48
51
sb.toString
49
52
}
50
53
54
+ /** Used to underline source positions in the stack trace
55
+ * pos.source must exist
56
+ */
57
+ private def positionMarker (pos : SourcePosition ): String = {
58
+ val trimmed = pos.lineContent.takeWhile(c => c.isWhitespace).length
59
+ val padding = pos.startColumnPadding.substring(trimmed).nn + " "
60
+ val carets =
61
+ if (pos.startLine == pos.endLine)
62
+ " ^" * math.max(1 , pos.endColumn - pos.startColumn)
63
+ else " ^"
64
+
65
+ s " $padding$carets\n "
66
+ }
67
+
51
68
/** Flatten UnsafePromotion errors
52
69
*/
53
70
def flatten : Errors = this match {
0 commit comments