@@ -11,7 +11,7 @@ object Texts {
11
11
def relems : List [Text ]
12
12
13
13
def isEmpty : Boolean = this match {
14
- case Str (s, _, _ ) => s.isEmpty
14
+ case Str (s, _) => s.isEmpty
15
15
case Fluid (relems) => relems forall (_.isEmpty)
16
16
case Vertical (relems) => relems.isEmpty
17
17
}
@@ -24,7 +24,7 @@ object Texts {
24
24
def close = new Closed (relems)
25
25
26
26
def remaining (width : Int ): Int = this match {
27
- case Str (s, _, _ ) =>
27
+ case Str (s, _) =>
28
28
width - s.length
29
29
case Fluid (Nil ) =>
30
30
width
@@ -36,15 +36,15 @@ object Texts {
36
36
}
37
37
38
38
def lastLine : String = this match {
39
- case Str (s, _, _ ) => s
39
+ case Str (s, _) => s
40
40
case _ => relems.head.lastLine
41
41
}
42
42
43
43
def appendToLastLine (that : Text ): Text = that match {
44
- case Str (s2, start1, end1 ) =>
44
+ case Str (s2, lines1 ) =>
45
45
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)
48
48
case Fluid (relems) => Fluid (that :: relems)
49
49
}
50
50
case Fluid (relems) =>
@@ -65,7 +65,7 @@ object Texts {
65
65
}
66
66
67
67
def layout (width : Int ): Text = this match {
68
- case Str (s, _, _ ) =>
68
+ case Str (s, _) =>
69
69
this
70
70
case Fluid (relems) =>
71
71
((Str (" " ): Text ) /: relems.reverse)(_.append(width)(_))
@@ -74,13 +74,13 @@ object Texts {
74
74
}
75
75
76
76
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 )
78
78
case Fluid (relems) => Fluid (relems map (_ map f))
79
79
case Vertical (relems) => Vertical (relems map (_ map f))
80
80
}
81
81
82
82
def stripPrefix (pre : String ): Text = this match {
83
- case Str (s, _, _ ) =>
83
+ case Str (s, _) =>
84
84
if (s.startsWith(pre)) s drop pre.length else s
85
85
case Fluid (relems) =>
86
86
val elems = relems.reverse
@@ -93,24 +93,21 @@ object Texts {
93
93
}
94
94
95
95
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 )
97
97
case Fluid (relems) => Fluid (relems map (_.indented))
98
98
case Vertical (relems) => Vertical (relems map (_.indented))
99
99
}
100
100
101
101
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
104
105
val pad = (numberWidth - ln.length - 1 )
105
106
assert(pad >= 0 )
106
107
sb.append(" " * pad)
107
108
sb.append(ln)
108
109
sb.append(" |" )
109
110
}
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(" " )
114
111
sb.append(s)
115
112
case _ =>
116
113
var follow = false
@@ -122,7 +119,7 @@ object Texts {
122
119
}
123
120
124
121
def maxLine : Int = this match {
125
- case Str (_, _, end ) => end
122
+ case Str (_, lines ) => lines. end
126
123
case _ => (- 1 /: relems)((acc, relem) => acc max relem.maxLine)
127
124
}
128
125
@@ -171,7 +168,7 @@ object Texts {
171
168
def lines (xs : Traversable [Text ]) = Vertical (xs.toList.reverse)
172
169
}
173
170
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 {
175
172
override def relems : List [Text ] = List (this )
176
173
}
177
174
@@ -181,4 +178,15 @@ object Texts {
181
178
class Closed (relems : List [Text ]) extends Fluid (relems)
182
179
183
180
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 )
184
192
}
0 commit comments