Skip to content

Commit a897319

Browse files
committed
Put lines at the start of the line and respect wageWidth
1 parent 24bfade commit a897319

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

compiler/src/dotty/tools/dotc/core/Decorators.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ object Decorators {
143143
}
144144

145145
implicit class TextToString(val text: Text) extends AnyVal {
146-
def show(implicit ctx: Context) = text.mkString(ctx.settings.pageWidth.value)
146+
def show(implicit ctx: Context) = text.mkString(ctx.settings.pageWidth.value, ctx.settings.printLines.value)
147147
}
148148

149149
/** Test whether a list of strings representing phases contains

compiler/src/dotty/tools/dotc/printing/Texts.scala

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,38 @@ object Texts {
9898
case Vertical(relems) => Vertical(relems map (_.indented))
9999
}
100100

101-
def print(sb: StringBuilder): Unit = this match {
101+
def print(sb: StringBuilder, numberWidth: Int): Unit = this match {
102102
case Str(s, start, end) =>
103+
def printLine(ln: String) = {
104+
val pad = (numberWidth - ln.length - 1)
105+
assert(pad >= 0)
106+
sb.append(" " * pad)
107+
sb.append(ln)
108+
sb.append("|")
109+
}
110+
if (numberWidth == 0) () // Do not print line numbers
111+
else if (start == end) printLine((start + 1).toString)
112+
else if (start != Int.MaxValue) printLine(s"${start + 1}-${end + 1}")
113+
else printLine("")
103114
sb.append(s)
104-
if (start == end)
105-
sb.append(s" // @line ${start + 1}")
106-
else if (start != Int.MaxValue)
107-
sb.append(s" // @lines ${start + 1} to ${end + 1}")
108115
case _ =>
109116
var follow = false
110117
for (elem <- relems.reverse) {
111118
if (follow) sb.append("\n")
112-
elem.print(sb)
119+
elem.print(sb, numberWidth)
113120
follow = true
114121
}
115122
}
116123

117-
def mkString(width: Int): String = {
124+
def maxLine: Int = this match {
125+
case Str(_, _, end) => end
126+
case _ => (-1 /: relems)((acc, relem) => acc max relem.maxLine)
127+
}
128+
129+
def mkString(width: Int, withLineNumbers: Boolean): String = {
118130
val sb = new StringBuilder
119-
layout(width).print(sb)
131+
val numberWidth = if (withLineNumbers) (2 * maxLine.toString.length) + 2 else 0
132+
layout(width - numberWidth).print(sb, numberWidth)
120133
sb.toString
121134
}
122135

compiler/src/dotty/tools/repl/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ package object repl {
2222
def showUser(implicit ctx: Context): String = {
2323
val printer = new UserFacingPrinter(ctx)
2424
val text = printer.dclText(s)
25-
text.mkString(ctx.settings.pageWidth.value)
25+
text.mkString(ctx.settings.pageWidth.value, ctx.settings.printLines.value)
2626
}
2727
}
2828

0 commit comments

Comments
 (0)