Skip to content

Fix #2063: Print tabs in message padding #2486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,30 @@ trait MessageRendering {
/** The column markers aligned under the error */
def columnMarker(pos: SourcePosition, offset: Int)(implicit ctx: Context): String = {
val prefix = " " * (offset - 1)
val whitespace = " " * pos.startColumn
val padding = pos.startColumnPadding
val carets = Red {
if (pos.startLine == pos.endLine)
"^" * math.max(1, pos.endColumn - pos.startColumn)
else "^"
}

s"$prefix|$whitespace${carets.show}"
s"$prefix|$padding${carets.show}"
}

/** The error message (`msg`) aligned under `pos`
*
* @return aligned error message
*/
def errorMsg(pos: SourcePosition, msg: String, offset: Int)(implicit ctx: Context): String = {
val leastWhitespace = msg.lines.foldLeft(Int.MaxValue) { (minPad, line) =>
val padding = msg.lines.foldLeft(pos.startColumnPadding) { (pad, line) =>
val lineLength = stripColor(line).length
val currPad = math.min(
math.max(0, ctx.settings.pageWidth.value - offset - lineLength),
offset + pos.startColumn
)
val maxPad = math.max(0, ctx.settings.pageWidth.value - offset - lineLength) - offset

math.min(currPad, minPad)
if (maxPad < pad.length) " " * maxPad
else pad
}

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

Expand Down
11 changes: 11 additions & 0 deletions compiler/src/dotty/tools/dotc/util/SourceFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) extends interfac
col
}

/** The padding of the column corresponding to `offset`, includes tabs */
def startColumnPadding(offset: Int): String = {
var idx = startOfLine(offset)
val pad = new StringBuilder
while (idx != offset) {
pad.append(if (idx < length && content(idx) == '\t') '\t' else ' ')
idx += 1
}
pad.result()
}

override def toString = file.toString
}

Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/util/SourcePosition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extends interfaces.SourcePosition {
def start: Int = pos.start
def startLine: Int = source.offsetToLine(start)
def startColumn: Int = source.column(start)
def startColumnPadding: String = source.startColumnPadding(start)

def end: Int = pos.end
def endLine: Int = source.offsetToLine(end)
Expand Down
22 changes: 22 additions & 0 deletions tests/repl/i2063.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
scala> class Foo extends Bar // with one tab
-- [E006] Unbound Identifier Error: <console>:4:18 -----------------------------
4 | class Foo extends Bar // with one tab
| ^^^
| not found: type Bar

longer explanation available when compiling with `-explain`
scala> class Foo extends Bar // with spaces
-- [E006] Unbound Identifier Error: <console>:4:96 -----------------------------
4 | class Foo extends Bar // with spaces
| ^^^
| not found: type Bar

longer explanation available when compiling with `-explain`
scala> class Foo extends Bar // with tabs
-- [E006] Unbound Identifier Error: <console>:4:99 -----------------------------
4 | class Foo extends Bar // with tabs
| ^^^
| not found: type Bar

longer explanation available when compiling with `-explain`
scala> :quit