Skip to content

Commit 7bdc7b7

Browse files
committed
Fix #2063: Print tabs in message padding
1 parent 8a9aedc commit 7bdc7b7

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,13 @@ trait MessageRendering {
7777
/** The column markers aligned under the error */
7878
def columnMarker(pos: SourcePosition, offset: Int)(implicit ctx: Context): String = {
7979
val prefix = " " * (offset - 1)
80-
val whitespace = " " * pos.startColumn
80+
val padding = pos.startColumnPadding
8181
val carets = Red {
8282
if (pos.startLine == pos.endLine)
8383
"^" * math.max(1, pos.endColumn - pos.startColumn)
8484
else "^"
8585
}
86-
87-
s"$prefix|$whitespace${carets.show}"
86+
s"$prefix|$padding${carets.show}"
8887
}
8988

9089
/** The error message (`msg`) aligned under `pos`
@@ -101,9 +100,12 @@ trait MessageRendering {
101100

102101
math.min(currPad, minPad)
103102
}
103+
val padding =
104+
if (leastWhitespace - offset == pos.startColumn) pos.startColumnPadding
105+
else " " * (leastWhitespace - offset)
104106

105107
msg.lines
106-
.map { line => " " * (offset - 1) + "|" + (" " * (leastWhitespace - offset)) + line}
108+
.map { line => " " * (offset - 1) + "|" + padding + line}
107109
.mkString(sys.props("line.separator"))
108110
}
109111

compiler/src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) extends interfac
136136
col
137137
}
138138

139+
/** The padding of the column corresponding to `offset`, includes tabs */
140+
def startColumnPadding(offset: Int): String = {
141+
var idx = startOfLine(offset)
142+
val pad = new StringBuffer("")
143+
while (idx != offset) {
144+
pad.append(if (idx < length && content(idx) == '\t') '\t' else ' ')
145+
idx += 1
146+
}
147+
pad.toString
148+
}
149+
139150
override def toString = file.toString
140151
}
141152

compiler/src/dotty/tools/dotc/util/SourcePosition.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extends interfaces.SourcePosition {
3939
def start: Int = pos.start
4040
def startLine: Int = source.offsetToLine(start)
4141
def startColumn: Int = source.column(start)
42+
def startColumnPadding: String = source.startColumnPadding(start)
4243

4344
def end: Int = pos.end
4445
def endLine: Int = source.offsetToLine(end)

tests/repl/i2063.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
scala> class Foo extends Bar
2+
-- [E006] Unbound Identifier Error: <console>:4:18 -----------------------------
3+
4 | class Foo extends Bar
4+
| ^^^
5+
| not found: type Bar
6+
7+
longer explanation available when compiling with `-explain`
8+
scala> :quit

0 commit comments

Comments
 (0)