author | wenzelm |
Tue, 03 Jan 2017 17:21:37 +0100 | |
changeset 64763 | 20e498a28f5e |
parent 64701 | 931f51fb24ca |
child 64800 | 415dafeb9669 |
permissions | -rw-r--r-- |
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 |
||
64682 | 34 |
def advance(text: String, text_length: Text.Length): Position = |
64617 | 35 |
if (text.isEmpty) this |
36 |
else { |
|
37 |
val lines = Library.split_lines(text) |
|
64619 | 38 |
val l = line + lines.length - 1 |
64681
642b6105e6f4
clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents:
64679
diff
changeset
|
39 |
val c = (if (l == line) column else 0) + text_length(Library.trim_line(lines.last)) |
64619 | 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 |
{ |
|
64683
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents:
64682
diff
changeset
|
83 |
def apply(text: String, text_length: Text.Length): Document = |
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents:
64682
diff
changeset
|
84 |
if (text.contains('\r')) |
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents:
64682
diff
changeset
|
85 |
Document(Library.split_lines(text).map(s => Line(Library.trim_line(s))), text_length) |
64672 | 86 |
else |
64683
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents:
64682
diff
changeset
|
87 |
Document(Library.split_lines(text).map(s => Line(s)), text_length) |
64605 | 88 |
} |
89 |
||
64683
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents:
64682
diff
changeset
|
90 |
sealed case class Document(lines: List[Line], text_length: Text.Length) |
64605 | 91 |
{ |
64672 | 92 |
def make_text: String = lines.mkString("", "\n", "") |
93 |
||
94 |
override def toString: String = make_text |
|
64605 | 95 |
|
96 |
override def equals(that: Any): Boolean = |
|
97 |
that match { |
|
98 |
case other: Document => lines == other.lines |
|
99 |
case _ => false |
|
100 |
} |
|
101 |
override def hashCode(): Int = lines.hashCode |
|
102 |
||
64683
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents:
64682
diff
changeset
|
103 |
def position(text_offset: Text.Offset): Position = |
64605 | 104 |
{ |
105 |
@tailrec def move(i: Text.Offset, lines_count: Int, lines_rest: List[Line]): Position = |
|
106 |
{ |
|
107 |
lines_rest match { |
|
64650 | 108 |
case Nil => require(i == 0); Position(lines_count) |
64605 | 109 |
case line :: ls => |
110 |
val n = line.text.length |
|
64617 | 111 |
if (ls.isEmpty || i <= n) |
64681
642b6105e6f4
clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents:
64679
diff
changeset
|
112 |
Position(lines_count).advance(line.text.substring(n - i), text_length) |
64605 | 113 |
else move(i - (n + 1), lines_count + 1, ls) |
114 |
} |
|
115 |
} |
|
64681
642b6105e6f4
clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents:
64679
diff
changeset
|
116 |
move(text_offset, 0, lines) |
64605 | 117 |
} |
118 |
||
64683
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents:
64682
diff
changeset
|
119 |
def range(text_range: Text.Range): Range = |
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents:
64682
diff
changeset
|
120 |
Range(position(text_range.start), position(text_range.stop)) |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64672
diff
changeset
|
121 |
|
64683
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents:
64682
diff
changeset
|
122 |
def offset(pos: Position): Option[Text.Offset] = |
64605 | 123 |
{ |
124 |
val l = pos.line |
|
125 |
val c = pos.column |
|
126 |
if (0 <= l && l < lines.length) { |
|
127 |
val line_offset = |
|
64763 | 128 |
(0 /: lines.iterator.take(l)) { case (n, line) => n + text_length(line.text) + 1 } |
64681
642b6105e6f4
clarified signature: explicit Length to avoid implicit mistakes;
wenzelm
parents:
64679
diff
changeset
|
129 |
text_length.offset(lines(l).text, c).map(line_offset + _) |
64605 | 130 |
} |
131 |
else None |
|
132 |
} |
|
64688
d8f194556c70
precise full_range and thus proper try_restrict in Snapshot.cumulate;
wenzelm
parents:
64683
diff
changeset
|
133 |
|
64701
931f51fb24ca
clarified Document.length -- independent of text_length;
wenzelm
parents:
64689
diff
changeset
|
134 |
lazy val length: Int = |
64688
d8f194556c70
precise full_range and thus proper try_restrict in Snapshot.cumulate;
wenzelm
parents:
64683
diff
changeset
|
135 |
if (lines.isEmpty) 0 |
64701
931f51fb24ca
clarified Document.length -- independent of text_length;
wenzelm
parents:
64689
diff
changeset
|
136 |
else ((0 /: lines) { case (n, line) => n + line.text.length + 1 }) - 1 |
64688
d8f194556c70
precise full_range and thus proper try_restrict in Snapshot.cumulate;
wenzelm
parents:
64683
diff
changeset
|
137 |
|
64701
931f51fb24ca
clarified Document.length -- independent of text_length;
wenzelm
parents:
64689
diff
changeset
|
138 |
def full_range: Text.Range = Text.Range(0, length) |
64605 | 139 |
} |
140 |
||
141 |
||
142 |
/* line text */ |
|
143 |
||
144 |
val empty: Line = new Line("") |
|
145 |
def apply(text: String): Line = if (text == "") empty else new Line(text) |
|
146 |
} |
|
147 |
||
148 |
final class Line private(val text: String) |
|
149 |
{ |
|
150 |
require(text.forall(c => c != '\r' && c != '\n')) |
|
151 |
||
152 |
override def equals(that: Any): Boolean = |
|
153 |
that match { |
|
154 |
case other: Line => text == other.text |
|
155 |
case _ => false |
|
156 |
} |
|
157 |
override def hashCode(): Int = text.hashCode |
|
158 |
override def toString: String = text |
|
159 |
} |