64611
|
1 |
/* Title: Pure/PIDE/line.scala
|
64605
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Line-oriented text.
|
|
5 |
*/
|
|
6 |
|
64611
|
7 |
package isabelle
|
64605
|
8 |
|
|
9 |
|
|
10 |
import scala.annotation.tailrec
|
|
11 |
|
|
12 |
|
|
13 |
object Line
|
|
14 |
{
|
|
15 |
/* position */
|
|
16 |
|
|
17 |
object Position
|
|
18 |
{
|
64650
|
19 |
val zero: Position = Position()
|
64605
|
20 |
}
|
|
21 |
|
64650
|
22 |
sealed case class Position(line: Int = 0, column: Int = 0)
|
64605
|
23 |
{
|
|
24 |
def line1: Int = line + 1
|
|
25 |
def column1: Int = column + 1
|
|
26 |
def print: String = line1.toString + ":" + column1.toString
|
|
27 |
|
|
28 |
def compare(that: Position): Int =
|
|
29 |
line compare that.line match {
|
|
30 |
case 0 => column compare that.column
|
|
31 |
case i => i
|
|
32 |
}
|
|
33 |
|
64617
|
34 |
def advance(text: String, length: Length = Length): Position =
|
|
35 |
if (text.isEmpty) this
|
|
36 |
else {
|
|
37 |
val lines = Library.split_lines(text)
|
64619
|
38 |
val l = line + lines.length - 1
|
|
39 |
val c = (if (l == line) column else 0) + length(Library.trim_line(lines.last))
|
|
40 |
Position(l, c)
|
64605
|
41 |
}
|
|
42 |
}
|
|
43 |
|
|
44 |
|
|
45 |
/* range (right-open interval) */
|
|
46 |
|
64647
|
47 |
object Range
|
|
48 |
{
|
64666
|
49 |
def apply(start: Position): Range = Range(start, start)
|
|
50 |
val zero: Range = Range(Position.zero)
|
64647
|
51 |
}
|
|
52 |
|
64605
|
53 |
sealed case class Range(start: Position, stop: Position)
|
|
54 |
{
|
|
55 |
if (start.compare(stop) > 0)
|
|
56 |
error("Bad line range: " + start.print + ".." + stop.print)
|
|
57 |
|
64647
|
58 |
def print: String =
|
|
59 |
if (start == stop) start.print
|
|
60 |
else start.print + ".." + stop.print
|
64605
|
61 |
}
|
|
62 |
|
|
63 |
|
64649
|
64 |
/* positions within document node */
|
|
65 |
|
64651
|
66 |
sealed case class Node_Position(name: String, pos: Position = Position.zero)
|
64650
|
67 |
{
|
|
68 |
def line: Int = pos.line
|
|
69 |
def column: Int = pos.column
|
|
70 |
}
|
|
71 |
|
64651
|
72 |
sealed case class Node_Range(name: String, range: Range = Range.zero)
|
64650
|
73 |
{
|
|
74 |
def start: Position = range.start
|
|
75 |
def stop: Position = range.stop
|
|
76 |
}
|
64649
|
77 |
|
|
78 |
|
64605
|
79 |
/* document with newline as separator (not terminator) */
|
|
80 |
|
|
81 |
object Document
|
|
82 |
{
|
|
83 |
val empty: Document = new Document("", Nil)
|
|
84 |
|
|
85 |
def apply(lines: List[Line]): Document =
|
|
86 |
if (lines.isEmpty) empty
|
|
87 |
else new Document(lines.mkString("", "\n", ""), lines)
|
|
88 |
|
|
89 |
def apply(text: String): Document =
|
|
90 |
if (text.contains('\r'))
|
|
91 |
apply(Library.cat_lines(Library.split_lines(text).map(Library.trim_line(_))))
|
|
92 |
else if (text == "") Document.empty
|
|
93 |
else new Document(text, Library.split_lines(text).map(Line(_)))
|
|
94 |
}
|
|
95 |
|
|
96 |
final class Document private(val text: String, val lines: List[Line])
|
|
97 |
{
|
|
98 |
override def toString: String = text
|
|
99 |
|
|
100 |
override def equals(that: Any): Boolean =
|
|
101 |
that match {
|
|
102 |
case other: Document => lines == other.lines
|
|
103 |
case _ => false
|
|
104 |
}
|
|
105 |
override def hashCode(): Int = lines.hashCode
|
|
106 |
|
64617
|
107 |
def position(offset: Text.Offset, length: Length = Length): Position =
|
64605
|
108 |
{
|
|
109 |
@tailrec def move(i: Text.Offset, lines_count: Int, lines_rest: List[Line]): Position =
|
|
110 |
{
|
|
111 |
lines_rest match {
|
64650
|
112 |
case Nil => require(i == 0); Position(lines_count)
|
64605
|
113 |
case line :: ls =>
|
|
114 |
val n = line.text.length
|
64617
|
115 |
if (ls.isEmpty || i <= n)
|
64650
|
116 |
Position(lines_count).advance(line.text.substring(n - i), length)
|
64605
|
117 |
else move(i - (n + 1), lines_count + 1, ls)
|
|
118 |
}
|
|
119 |
}
|
|
120 |
move(offset, 0, lines)
|
|
121 |
}
|
|
122 |
|
64617
|
123 |
def offset(pos: Position, length: Length = Length): Option[Text.Offset] =
|
64605
|
124 |
{
|
|
125 |
val l = pos.line
|
|
126 |
val c = pos.column
|
|
127 |
if (0 <= l && l < lines.length) {
|
|
128 |
val line_offset =
|
|
129 |
if (l == 0) 0
|
64617
|
130 |
else (0 /: lines.iterator.take(l - 1))({ case (n, line) => n + length(line.text) + 1 })
|
|
131 |
length.offset(lines(l).text, c).map(line_offset + _)
|
64605
|
132 |
}
|
|
133 |
else None
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
|
|
138 |
/* line text */
|
|
139 |
|
|
140 |
val empty: Line = new Line("")
|
|
141 |
def apply(text: String): Line = if (text == "") empty else new Line(text)
|
|
142 |
}
|
|
143 |
|
|
144 |
final class Line private(val text: String)
|
|
145 |
{
|
|
146 |
require(text.forall(c => c != '\r' && c != '\n'))
|
|
147 |
|
64610
|
148 |
lazy val length_codepoints: Int = Codepoint.iterator(text).length
|
64605
|
149 |
|
|
150 |
override def equals(that: Any): Boolean =
|
|
151 |
that match {
|
|
152 |
case other: Line => text == other.text
|
|
153 |
case _ => false
|
|
154 |
}
|
|
155 |
override def hashCode(): Int = text.hashCode
|
|
156 |
override def toString: String = text
|
|
157 |
}
|