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 |
{
|
|
19 |
val zero: Position = Position(0, 0)
|
|
20 |
}
|
|
21 |
|
|
22 |
sealed case class Position(line: Int, column: Int)
|
|
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)
|
|
38 |
Position(line + lines.length - 1, column + length(Library.trim_line(lines.last)))
|
64605
|
39 |
}
|
|
40 |
}
|
|
41 |
|
|
42 |
|
|
43 |
/* range (right-open interval) */
|
|
44 |
|
|
45 |
sealed case class Range(start: Position, stop: Position)
|
|
46 |
{
|
|
47 |
if (start.compare(stop) > 0)
|
|
48 |
error("Bad line range: " + start.print + ".." + stop.print)
|
|
49 |
|
|
50 |
def print: String = start.print + ".." + stop.print
|
|
51 |
}
|
|
52 |
|
|
53 |
|
|
54 |
/* document with newline as separator (not terminator) */
|
|
55 |
|
|
56 |
object Document
|
|
57 |
{
|
|
58 |
val empty: Document = new Document("", Nil)
|
|
59 |
|
|
60 |
def apply(lines: List[Line]): Document =
|
|
61 |
if (lines.isEmpty) empty
|
|
62 |
else new Document(lines.mkString("", "\n", ""), lines)
|
|
63 |
|
|
64 |
def apply(text: String): Document =
|
|
65 |
if (text.contains('\r'))
|
|
66 |
apply(Library.cat_lines(Library.split_lines(text).map(Library.trim_line(_))))
|
|
67 |
else if (text == "") Document.empty
|
|
68 |
else new Document(text, Library.split_lines(text).map(Line(_)))
|
|
69 |
}
|
|
70 |
|
|
71 |
final class Document private(val text: String, val lines: List[Line])
|
|
72 |
{
|
|
73 |
override def toString: String = text
|
|
74 |
|
|
75 |
override def equals(that: Any): Boolean =
|
|
76 |
that match {
|
|
77 |
case other: Document => lines == other.lines
|
|
78 |
case _ => false
|
|
79 |
}
|
|
80 |
override def hashCode(): Int = lines.hashCode
|
|
81 |
|
64617
|
82 |
def position(offset: Text.Offset, length: Length = Length): Position =
|
64605
|
83 |
{
|
|
84 |
@tailrec def move(i: Text.Offset, lines_count: Int, lines_rest: List[Line]): Position =
|
|
85 |
{
|
|
86 |
lines_rest match {
|
|
87 |
case Nil => require(i == 0); Position(lines_count, 0)
|
|
88 |
case line :: ls =>
|
|
89 |
val n = line.text.length
|
64617
|
90 |
if (ls.isEmpty || i <= n)
|
|
91 |
Position(lines_count, 0).advance(line.text.substring(n - i), length)
|
64605
|
92 |
else move(i - (n + 1), lines_count + 1, ls)
|
|
93 |
}
|
|
94 |
}
|
|
95 |
move(offset, 0, lines)
|
|
96 |
}
|
|
97 |
|
64617
|
98 |
def offset(pos: Position, length: Length = Length): Option[Text.Offset] =
|
64605
|
99 |
{
|
|
100 |
val l = pos.line
|
|
101 |
val c = pos.column
|
|
102 |
if (0 <= l && l < lines.length) {
|
|
103 |
val line_offset =
|
|
104 |
if (l == 0) 0
|
64617
|
105 |
else (0 /: lines.iterator.take(l - 1))({ case (n, line) => n + length(line.text) + 1 })
|
|
106 |
length.offset(lines(l).text, c).map(line_offset + _)
|
64605
|
107 |
}
|
|
108 |
else None
|
|
109 |
}
|
|
110 |
}
|
|
111 |
|
|
112 |
|
|
113 |
/* line text */
|
|
114 |
|
|
115 |
val empty: Line = new Line("")
|
|
116 |
def apply(text: String): Line = if (text == "") empty else new Line(text)
|
|
117 |
}
|
|
118 |
|
|
119 |
final class Line private(val text: String)
|
|
120 |
{
|
|
121 |
require(text.forall(c => c != '\r' && c != '\n'))
|
|
122 |
|
64610
|
123 |
lazy val length_codepoints: Int = Codepoint.iterator(text).length
|
64605
|
124 |
|
|
125 |
override def equals(that: Any): Boolean =
|
|
126 |
that match {
|
|
127 |
case other: Line => text == other.text
|
|
128 |
case _ => false
|
|
129 |
}
|
|
130 |
override def hashCode(): Int = text.hashCode
|
|
131 |
override def toString: String = text
|
|
132 |
}
|