38425
|
1 |
/* Title: Pure/PIDE/text.scala
|
34276
|
2 |
Author: Fabian Immler, TU Munich
|
|
3 |
Author: Makarius
|
|
4 |
|
38425
|
5 |
Basic operations on plain text.
|
34276
|
6 |
*/
|
|
7 |
|
|
8 |
package isabelle
|
|
9 |
|
|
10 |
|
38425
|
11 |
object Text
|
34276
|
12 |
{
|
38477
|
13 |
/* offset */
|
38426
|
14 |
|
|
15 |
type Offset = Int
|
|
16 |
|
38477
|
17 |
|
|
18 |
/* range: interval with total quasi-ordering */
|
|
19 |
|
|
20 |
object Range
|
|
21 |
{
|
|
22 |
object Ordering extends scala.math.Ordering[Range]
|
|
23 |
{
|
|
24 |
override def compare(r1: Range, r2: Range): Int = r1 compare r2
|
|
25 |
}
|
|
26 |
}
|
|
27 |
|
38426
|
28 |
sealed case class Range(val start: Offset, val stop: Offset)
|
38427
|
29 |
{
|
38477
|
30 |
require(start <= stop)
|
|
31 |
|
38427
|
32 |
def map(f: Offset => Offset): Range = Range(f(start), f(stop))
|
|
33 |
def +(i: Offset): Range = map(_ + i)
|
38477
|
34 |
def contains(i: Offset): Boolean = start <= i && i < stop
|
|
35 |
def contains(that: Range): Boolean = start <= that.start && that.stop <= stop
|
|
36 |
def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
|
|
37 |
def compare(that: Range): Int =
|
|
38 |
if (overlaps(that)) 0
|
|
39 |
else if (this.start < that.start) -1
|
|
40 |
else 1
|
38427
|
41 |
}
|
38426
|
42 |
|
|
43 |
|
|
44 |
/* editing */
|
34286
|
45 |
|
38425
|
46 |
object Edit
|
|
47 |
{
|
38426
|
48 |
def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
|
|
49 |
def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
|
38425
|
50 |
}
|
34286
|
51 |
|
38426
|
52 |
class Edit(val is_insert: Boolean, val start: Offset, val text: String)
|
38425
|
53 |
{
|
|
54 |
override def toString =
|
|
55 |
(if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
|
34286
|
56 |
|
|
57 |
|
38425
|
58 |
/* transform offsets */
|
34286
|
59 |
|
38426
|
60 |
private def transform(do_insert: Boolean, i: Offset): Offset =
|
|
61 |
if (i < start) i
|
|
62 |
else if (is_insert == do_insert) i + text.length
|
|
63 |
else (i - text.length) max start
|
34286
|
64 |
|
38426
|
65 |
def convert(i: Offset): Offset = transform(true, i)
|
|
66 |
def revert(i: Offset): Offset = transform(false, i)
|
38425
|
67 |
|
34286
|
68 |
|
38425
|
69 |
/* edit strings */
|
|
70 |
|
38426
|
71 |
private def insert(i: Offset, string: String): String =
|
|
72 |
string.substring(0, i) + text + string.substring(i)
|
34276
|
73 |
|
38426
|
74 |
private def remove(i: Offset, count: Int, string: String): String =
|
|
75 |
string.substring(0, i) + string.substring(i + count)
|
38425
|
76 |
|
|
77 |
def can_edit(string: String, shift: Int): Boolean =
|
|
78 |
shift <= start && start < shift + string.length
|
|
79 |
|
|
80 |
def edit(string: String, shift: Int): (Option[Edit], String) =
|
|
81 |
if (!can_edit(string, shift)) (Some(this), string)
|
|
82 |
else if (is_insert) (None, insert(start - shift, string))
|
|
83 |
else {
|
38426
|
84 |
val i = start - shift
|
|
85 |
val count = text.length min (string.length - i)
|
38425
|
86 |
val rest =
|
|
87 |
if (count == text.length) None
|
|
88 |
else Some(Edit.remove(start, text.substring(count)))
|
38426
|
89 |
(rest, remove(i, count, string))
|
38425
|
90 |
}
|
|
91 |
}
|
34276
|
92 |
}
|