| author | wenzelm | 
| Tue, 07 Sep 2021 17:07:28 +0200 | |
| changeset 74258 | e942eedd9229 | 
| parent 73359 | d8a0e996614b | 
| child 75393 | 87ebf5a50283 | 
| permissions | -rw-r--r-- | 
| 64611 | 1  | 
/* Title: Pure/PIDE/line.scala  | 
| 64605 | 2  | 
Author: Makarius  | 
3  | 
||
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
4  | 
Line-oriented text documents, with some bias towards VSCode.  | 
| 64605 | 5  | 
*/  | 
6  | 
||
| 64611 | 7  | 
package isabelle  | 
| 64605 | 8  | 
|
9  | 
||
10  | 
import scala.annotation.tailrec  | 
|
11  | 
||
12  | 
||
13  | 
object Line  | 
|
14  | 
{
 | 
|
| 64806 | 15  | 
/* logical lines */  | 
16  | 
||
17  | 
def normalize(text: String): String =  | 
|
18  | 
    if (text.contains('\r')) text.replace("\r\n", "\n") else text
 | 
|
19  | 
||
20  | 
def logical_lines(text: String): List[String] =  | 
|
| 66923 | 21  | 
split_lines(normalize(text))  | 
| 64806 | 22  | 
|
23  | 
||
| 64605 | 24  | 
/* position */  | 
25  | 
||
26  | 
object Position  | 
|
27  | 
  {
 | 
|
| 64650 | 28  | 
val zero: Position = Position()  | 
| 
66605
 
261dcd52c5a0
less aggressive default position: prefer persistent defaults maintained by jEdit (amending 89c5bb2a2128);
 
wenzelm 
parents: 
65903 
diff
changeset
 | 
29  | 
val offside: Position = Position(line = -1)  | 
| 
65234
 
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
 
wenzelm 
parents: 
65203 
diff
changeset
 | 
30  | 
|
| 
 
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
 
wenzelm 
parents: 
65203 
diff
changeset
 | 
31  | 
object Ordering extends scala.math.Ordering[Position]  | 
| 
 
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
 
wenzelm 
parents: 
65203 
diff
changeset
 | 
32  | 
    {
 | 
| 
 
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
 
wenzelm 
parents: 
65203 
diff
changeset
 | 
33  | 
def compare(p1: Position, p2: Position): Int = p1 compare p2  | 
| 
 
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
 
wenzelm 
parents: 
65203 
diff
changeset
 | 
34  | 
}  | 
| 64605 | 35  | 
}  | 
36  | 
||
| 64650 | 37  | 
sealed case class Position(line: Int = 0, column: Int = 0)  | 
| 64605 | 38  | 
  {
 | 
39  | 
def line1: Int = line + 1  | 
|
40  | 
def column1: Int = column + 1  | 
|
41  | 
def print: String = line1.toString + ":" + column1.toString  | 
|
42  | 
||
43  | 
def compare(that: Position): Int =  | 
|
44  | 
      line compare that.line match {
 | 
|
45  | 
case 0 => column compare that.column  | 
|
46  | 
case i => i  | 
|
47  | 
}  | 
|
48  | 
||
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
49  | 
def advance(text: String): Position =  | 
| 64617 | 50  | 
if (text.isEmpty) this  | 
51  | 
      else {
 | 
|
| 64806 | 52  | 
val lines = logical_lines(text)  | 
| 64619 | 53  | 
val l = line + lines.length - 1  | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
54  | 
val c = (if (l == line) column else 0) + Library.trim_line(lines.last).length  | 
| 64619 | 55  | 
Position(l, c)  | 
| 64605 | 56  | 
}  | 
57  | 
}  | 
|
58  | 
||
59  | 
||
60  | 
/* range (right-open interval) */  | 
|
61  | 
||
| 64647 | 62  | 
object Range  | 
63  | 
  {
 | 
|
| 64666 | 64  | 
def apply(start: Position): Range = Range(start, start)  | 
65  | 
val zero: Range = Range(Position.zero)  | 
|
| 64647 | 66  | 
}  | 
67  | 
||
| 64605 | 68  | 
sealed case class Range(start: Position, stop: Position)  | 
69  | 
  {
 | 
|
70  | 
if (start.compare(stop) > 0)  | 
|
71  | 
      error("Bad line range: " + start.print + ".." + stop.print)
 | 
|
72  | 
||
| 64647 | 73  | 
def print: String =  | 
74  | 
if (start == stop) start.print  | 
|
75  | 
else start.print + ".." + stop.print  | 
|
| 64605 | 76  | 
}  | 
77  | 
||
78  | 
||
| 64649 | 79  | 
/* positions within document node */  | 
80  | 
||
| 
66605
 
261dcd52c5a0
less aggressive default position: prefer persistent defaults maintained by jEdit (amending 89c5bb2a2128);
 
wenzelm 
parents: 
65903 
diff
changeset
 | 
81  | 
object Node_Position  | 
| 
 
261dcd52c5a0
less aggressive default position: prefer persistent defaults maintained by jEdit (amending 89c5bb2a2128);
 
wenzelm 
parents: 
65903 
diff
changeset
 | 
82  | 
  {
 | 
| 
 
261dcd52c5a0
less aggressive default position: prefer persistent defaults maintained by jEdit (amending 89c5bb2a2128);
 
wenzelm 
parents: 
65903 
diff
changeset
 | 
83  | 
def offside(name: String): Node_Position = Node_Position(name, Position.offside)  | 
| 
 
261dcd52c5a0
less aggressive default position: prefer persistent defaults maintained by jEdit (amending 89c5bb2a2128);
 
wenzelm 
parents: 
65903 
diff
changeset
 | 
84  | 
}  | 
| 
 
261dcd52c5a0
less aggressive default position: prefer persistent defaults maintained by jEdit (amending 89c5bb2a2128);
 
wenzelm 
parents: 
65903 
diff
changeset
 | 
85  | 
|
| 64651 | 86  | 
sealed case class Node_Position(name: String, pos: Position = Position.zero)  | 
| 64650 | 87  | 
  {
 | 
88  | 
def line: Int = pos.line  | 
|
| 70792 | 89  | 
def line1: Int = pos.line1  | 
| 64650 | 90  | 
def column: Int = pos.column  | 
| 70792 | 91  | 
def column1: Int = pos.column1  | 
| 64650 | 92  | 
}  | 
93  | 
||
| 64651 | 94  | 
sealed case class Node_Range(name: String, range: Range = Range.zero)  | 
| 64650 | 95  | 
  {
 | 
96  | 
def start: Position = range.start  | 
|
97  | 
def stop: Position = range.stop  | 
|
98  | 
}  | 
|
| 64649 | 99  | 
|
100  | 
||
| 64605 | 101  | 
/* document with newline as separator (not terminator) */  | 
102  | 
||
103  | 
object Document  | 
|
104  | 
  {
 | 
|
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
105  | 
def apply(text: String): Document =  | 
| 65903 | 106  | 
Document(logical_lines(text).map(s => Line(Library.isolate_substring(s))))  | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
107  | 
|
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
108  | 
    val empty: Document = apply("")
 | 
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
109  | 
|
| 65159 | 110  | 
private def split(line_text: String): List[Line] =  | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
111  | 
if (line_text == "") List(Line.empty) else apply(line_text).lines  | 
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
112  | 
|
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
113  | 
private def chop(lines: List[Line]): (String, List[Line]) =  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
114  | 
      lines match {
 | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
115  | 
        case Nil => ("", Nil)
 | 
| 65159 | 116  | 
case line :: rest => (line.text, rest)  | 
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
117  | 
}  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
118  | 
|
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
119  | 
private def length(lines: List[Line]): Int =  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
120  | 
if (lines.isEmpty) 0  | 
| 73359 | 121  | 
      else (lines.foldLeft(0) { case (n, line) => n + line.text.length + 1 }) - 1
 | 
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
122  | 
|
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
123  | 
    def text(lines: List[Line]): String = lines.mkString("", "\n", "")
 | 
| 64605 | 124  | 
}  | 
125  | 
||
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
126  | 
sealed case class Document(lines: List[Line])  | 
| 64605 | 127  | 
  {
 | 
| 65197 | 128  | 
lazy val text_length: Text.Offset = Document.length(lines)  | 
129  | 
def text_range: Text.Range = Text.Range(0, text_length)  | 
|
130  | 
||
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
131  | 
lazy val text: String = Document.text(lines)  | 
| 64672 | 132  | 
|
| 67014 | 133  | 
def get_text(range: Text.Range): Option[String] =  | 
| 65522 | 134  | 
if (text_range.contains(range)) Some(range.substring(text)) else None  | 
| 64877 | 135  | 
|
| 64821 | 136  | 
override def toString: String = text  | 
| 64605 | 137  | 
|
138  | 
override def equals(that: Any): Boolean =  | 
|
139  | 
      that match {
 | 
|
140  | 
case other: Document => lines == other.lines  | 
|
141  | 
case _ => false  | 
|
142  | 
}  | 
|
143  | 
override def hashCode(): Int = lines.hashCode  | 
|
144  | 
||
| 
64683
 
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
 
wenzelm 
parents: 
64682 
diff
changeset
 | 
145  | 
def position(text_offset: Text.Offset): Position =  | 
| 64605 | 146  | 
    {
 | 
147  | 
@tailrec def move(i: Text.Offset, lines_count: Int, lines_rest: List[Line]): Position =  | 
|
148  | 
      {
 | 
|
149  | 
        lines_rest match {
 | 
|
| 
73120
 
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
 
wenzelm 
parents: 
70792 
diff
changeset
 | 
150  | 
case Nil => require(i == 0, "bad Line.position.move"); Position(lines_count)  | 
| 64605 | 151  | 
case line :: ls =>  | 
152  | 
val n = line.text.length  | 
|
| 64617 | 153  | 
if (ls.isEmpty || i <= n)  | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
154  | 
Position(lines_count).advance(line.text.substring(n - i))  | 
| 64605 | 155  | 
else move(i - (n + 1), lines_count + 1, ls)  | 
156  | 
}  | 
|
157  | 
}  | 
|
| 
64681
 
642b6105e6f4
clarified signature: explicit Length to avoid implicit mistakes;
 
wenzelm 
parents: 
64679 
diff
changeset
 | 
158  | 
move(text_offset, 0, lines)  | 
| 64605 | 159  | 
}  | 
160  | 
||
| 
64683
 
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
 
wenzelm 
parents: 
64682 
diff
changeset
 | 
161  | 
def range(text_range: Text.Range): Range =  | 
| 
 
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
 
wenzelm 
parents: 
64682 
diff
changeset
 | 
162  | 
Range(position(text_range.start), position(text_range.stop))  | 
| 
64679
 
b2bf280b7e13
more uniform treatment of input/output wrt. client;
 
wenzelm 
parents: 
64672 
diff
changeset
 | 
163  | 
|
| 
64683
 
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
 
wenzelm 
parents: 
64682 
diff
changeset
 | 
164  | 
def offset(pos: Position): Option[Text.Offset] =  | 
| 64605 | 165  | 
    {
 | 
166  | 
val l = pos.line  | 
|
167  | 
val c = pos.column  | 
|
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
168  | 
val n = lines.length  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
169  | 
      if (0 <= l && l < n) {
 | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
170  | 
        if (0 <= c && c <= lines(l).text.length) {
 | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
171  | 
val line_offset =  | 
| 73359 | 172  | 
            lines.iterator.take(l).foldLeft(0) { case (n, line) => n + line.text.length + 1 }
 | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
173  | 
Some(line_offset + c)  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
174  | 
}  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
175  | 
else None  | 
| 64605 | 176  | 
}  | 
| 65197 | 177  | 
else if (l == n && c == 0) Some(text_length)  | 
| 64605 | 178  | 
else None  | 
179  | 
}  | 
|
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
180  | 
|
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
181  | 
def change(remove: Range, insert: String): Option[(List[Text.Edit], Document)] =  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
182  | 
    {
 | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
183  | 
      for {
 | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
184  | 
edit_start <- offset(remove.start)  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
185  | 
if remove.stop == remove.start || offset(remove.stop).isDefined  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
186  | 
l1 = remove.start.line  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
187  | 
l2 = remove.stop.line  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
188  | 
if l1 <= l2  | 
| 65159 | 189  | 
(removed_text, new_lines) <-  | 
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
190  | 
        {
 | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
191  | 
val c1 = remove.start.column  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
192  | 
val c2 = remove.stop.column  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
193  | 
|
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
194  | 
val (prefix, lines1) = lines.splitAt(l1)  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
195  | 
val (s1, rest1) = Document.chop(lines1)  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
196  | 
|
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
197  | 
          if (l1 == l2) {
 | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
198  | 
            if (0 <= c1 && c1 <= c2 && c2 <= s1.length) {
 | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
199  | 
Some(  | 
| 65203 | 200  | 
                if (lines1.isEmpty) ("", prefix ::: Document.split(insert))
 | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
201  | 
                else {
 | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
202  | 
val removed_text = s1.substring(c1, c2)  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
203  | 
val changed_text = s1.substring(0, c1) + insert + s1.substring(c2)  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
204  | 
(removed_text, prefix ::: Document.split(changed_text) ::: rest1)  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
205  | 
})  | 
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
206  | 
}  | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
207  | 
else None  | 
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
208  | 
}  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
209  | 
          else {
 | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
210  | 
val (middle, lines2) = rest1.splitAt(l2 - l1 - 1)  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
211  | 
val (s2, rest2) = Document.chop(lines2)  | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
212  | 
            if (0 <= c1 && c1 <= s1.length && 0 <= c2 && c2 <= s2.length) {
 | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
213  | 
Some(  | 
| 65203 | 214  | 
                if (lines1.isEmpty) ("", prefix ::: Document.split(insert))
 | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
215  | 
                else {
 | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
216  | 
val r1 = s1.substring(c1)  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
217  | 
val r2 = s2.substring(0, c2)  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
218  | 
val removed_text =  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
219  | 
if (lines2.isEmpty) Document.text(Line(r1) :: middle)  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
220  | 
else Document.text(Line(r1) :: middle ::: List(Line(r2)))  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
221  | 
val changed_text = s1.substring(0, c1) + insert + s2.substring(c2)  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
222  | 
(removed_text, prefix ::: Document.split(changed_text) ::: rest2)  | 
| 
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
223  | 
})  | 
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
224  | 
}  | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
225  | 
else None  | 
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
226  | 
}  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
227  | 
}  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
228  | 
}  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
229  | 
yield  | 
| 
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
230  | 
(Text.Edit.removes(edit_start, removed_text) ::: Text.Edit.inserts(edit_start, insert),  | 
| 
65196
 
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
 
wenzelm 
parents: 
65159 
diff
changeset
 | 
231  | 
Document(new_lines))  | 
| 
65157
 
cd977a5bd928
clarified Document.offset: including final position;
 
wenzelm 
parents: 
64877 
diff
changeset
 | 
232  | 
}  | 
| 64605 | 233  | 
}  | 
234  | 
||
235  | 
||
236  | 
/* line text */  | 
|
237  | 
||
238  | 
  val empty: Line = new Line("")
 | 
|
239  | 
def apply(text: String): Line = if (text == "") empty else new Line(text)  | 
|
240  | 
}  | 
|
241  | 
||
242  | 
final class Line private(val text: String)  | 
|
243  | 
{
 | 
|
| 
73120
 
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
 
wenzelm 
parents: 
70792 
diff
changeset
 | 
244  | 
require(text.forall(c => c != '\r' && c != '\n'), "bad line text")  | 
| 64605 | 245  | 
|
246  | 
override def equals(that: Any): Boolean =  | 
|
247  | 
    that match {
 | 
|
248  | 
case other: Line => text == other.text  | 
|
249  | 
case _ => false  | 
|
250  | 
}  | 
|
251  | 
override def hashCode(): Int = text.hashCode  | 
|
252  | 
override def toString: String = text  | 
|
253  | 
}  |