Skip to content

Commit 83745f3

Browse files
authored
Merge pull request #2486 from dotty-staging/fix-#2063
Fix #2063: Print tabs in message padding
2 parents 7d34049 + 5f2d046 commit 83745f3

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,33 +77,30 @@ 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`
9190
*
9291
* @return aligned error message
9392
*/
9493
def errorMsg(pos: SourcePosition, msg: String, offset: Int)(implicit ctx: Context): String = {
95-
val leastWhitespace = msg.lines.foldLeft(Int.MaxValue) { (minPad, line) =>
94+
val padding = msg.lines.foldLeft(pos.startColumnPadding) { (pad, line) =>
9695
val lineLength = stripColor(line).length
97-
val currPad = math.min(
98-
math.max(0, ctx.settings.pageWidth.value - offset - lineLength),
99-
offset + pos.startColumn
100-
)
96+
val maxPad = math.max(0, ctx.settings.pageWidth.value - offset - lineLength) - offset
10197

102-
math.min(currPad, minPad)
98+
if (maxPad < pad.length) " " * maxPad
99+
else pad
103100
}
104101

105102
msg.lines
106-
.map { line => " " * (offset - 1) + "|" + (" " * (leastWhitespace - offset)) + line}
103+
.map { line => " " * (offset - 1) + "|" + padding + line}
107104
.mkString(sys.props("line.separator"))
108105
}
109106

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 StringBuilder
143+
while (idx != offset) {
144+
pad.append(if (idx < length && content(idx) == '\t') '\t' else ' ')
145+
idx += 1
146+
}
147+
pad.result()
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
scala> class Foo extends Bar // with one tab
2+
-- [E006] Unbound Identifier Error: <console>:4:18 -----------------------------
3+
4 | class Foo extends Bar // with one tab
4+
| ^^^
5+
| not found: type Bar
6+
7+
longer explanation available when compiling with `-explain`
8+
scala> class Foo extends Bar // with spaces
9+
-- [E006] Unbound Identifier Error: <console>:4:96 -----------------------------
10+
4 | class Foo extends Bar // with spaces
11+
| ^^^
12+
| not found: type Bar
13+
14+
longer explanation available when compiling with `-explain`
15+
scala> class Foo extends Bar // with tabs
16+
-- [E006] Unbound Identifier Error: <console>:4:99 -----------------------------
17+
4 | class Foo extends Bar // with tabs
18+
| ^^^
19+
| not found: type Bar
20+
21+
longer explanation available when compiling with `-explain`
22+
scala> :quit

0 commit comments

Comments
 (0)