Skip to content

Commit 48da50e

Browse files
committed
Create Texts.LineRange and use it in Texts.Str
1 parent d82d4ad commit 48da50e

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
709709
def withPos(txt: Text, pos: SourcePosition): Text = {
710710
if (!printLines || !pos.exists) txt
711711
else txt match {
712-
case Str(s, _, _) => Str(s, pos.line, pos.endLine)
712+
case Str(s, _) => Str(s, LineRange(pos.line, pos.endLine))
713713
case _ => txt
714714
}
715715
}

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object Texts {
1111
def relems: List[Text]
1212

1313
def isEmpty: Boolean = this match {
14-
case Str(s, _, _) => s.isEmpty
14+
case Str(s, _) => s.isEmpty
1515
case Fluid(relems) => relems forall (_.isEmpty)
1616
case Vertical(relems) => relems.isEmpty
1717
}
@@ -24,7 +24,7 @@ object Texts {
2424
def close = new Closed(relems)
2525

2626
def remaining(width: Int): Int = this match {
27-
case Str(s, _, _) =>
27+
case Str(s, _) =>
2828
width - s.length
2929
case Fluid(Nil) =>
3030
width
@@ -36,15 +36,15 @@ object Texts {
3636
}
3737

3838
def lastLine: String = this match {
39-
case Str(s, _, _) => s
39+
case Str(s, _) => s
4040
case _ => relems.head.lastLine
4141
}
4242

4343
def appendToLastLine(that: Text): Text = that match {
44-
case Str(s2, start1, end1) =>
44+
case Str(s2, lines1) =>
4545
this match {
46-
case Str(s1, start2, end2) => Str(s1 + s2, start1 min start2, end1 max end2)
47-
case Fluid(Str(s1, start2, end2) :: prev) => Fluid(Str(s1 + s2, start1 min start2, end1 max end2) :: prev)
46+
case Str(s1, lines2) => Str(s1 + s2, lines1 union lines2)
47+
case Fluid(Str(s1, lines2) :: prev) => Fluid(Str(s1 + s2, lines1 union lines2) :: prev)
4848
case Fluid(relems) => Fluid(that :: relems)
4949
}
5050
case Fluid(relems) =>
@@ -65,7 +65,7 @@ object Texts {
6565
}
6666

6767
def layout(width: Int): Text = this match {
68-
case Str(s, _, _) =>
68+
case Str(s, _) =>
6969
this
7070
case Fluid(relems) =>
7171
((Str(""): Text) /: relems.reverse)(_.append(width)(_))
@@ -74,13 +74,13 @@ object Texts {
7474
}
7575

7676
def map(f: String => String): Text = this match {
77-
case Str(s, start, end) => Str(f(s), start, end)
77+
case Str(s, lines) => Str(f(s), lines)
7878
case Fluid(relems) => Fluid(relems map (_ map f))
7979
case Vertical(relems) => Vertical(relems map (_ map f))
8080
}
8181

8282
def stripPrefix(pre: String): Text = this match {
83-
case Str(s, _, _) =>
83+
case Str(s, _) =>
8484
if (s.startsWith(pre)) s drop pre.length else s
8585
case Fluid(relems) =>
8686
val elems = relems.reverse
@@ -93,24 +93,21 @@ object Texts {
9393
}
9494

9595
private def indented: Text = this match {
96-
case Str(s, start, end) => Str((" " * indentMargin) + s, start, end)
96+
case Str(s, lines) => Str((" " * indentMargin) + s, lines)
9797
case Fluid(relems) => Fluid(relems map (_.indented))
9898
case Vertical(relems) => Vertical(relems map (_.indented))
9999
}
100100

101101
def print(sb: StringBuilder, numberWidth: Int): Unit = this match {
102-
case Str(s, start, end) =>
103-
def printLine(ln: String) = {
102+
case Str(s, lines) =>
103+
if (numberWidth != 0) {
104+
val ln = lines.show
104105
val pad = (numberWidth - ln.length - 1)
105106
assert(pad >= 0)
106107
sb.append(" " * pad)
107108
sb.append(ln)
108109
sb.append("|")
109110
}
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("")
114111
sb.append(s)
115112
case _ =>
116113
var follow = false
@@ -122,7 +119,7 @@ object Texts {
122119
}
123120

124121
def maxLine: Int = this match {
125-
case Str(_, _, end) => end
122+
case Str(_, lines) => lines.end
126123
case _ => (-1 /: relems)((acc, relem) => acc max relem.maxLine)
127124
}
128125

@@ -171,7 +168,7 @@ object Texts {
171168
def lines(xs: Traversable[Text]) = Vertical(xs.toList.reverse)
172169
}
173170

174-
case class Str(s: String, startLine: Int = Int.MaxValue, endLine: Int = -1) extends Text {
171+
case class Str(s: String, lineRange: LineRange = NoLineRange) extends Text {
175172
override def relems: List[Text] = List(this)
176173
}
177174

@@ -181,4 +178,15 @@ object Texts {
181178
class Closed(relems: List[Text]) extends Fluid(relems)
182179

183180
implicit def stringToText(s: String): Text = Str(s)
181+
182+
/** Inclusive line range */
183+
case class LineRange(start: Int, end: Int) {
184+
def union(that: LineRange): LineRange = LineRange(start min that.start, end max that.end)
185+
def show: String =
186+
if (start == end) (start + 1).toString
187+
else if (start < end) s"${start + 1}-${end + 1}"
188+
else "" // empty range
189+
}
190+
191+
def NoLineRange = LineRange(Int.MaxValue, Int.MinValue)
184192
}

0 commit comments

Comments
 (0)