author | wenzelm |
Mon, 17 Mar 2014 11:39:46 +0100 | |
changeset 56172 | 31289387fdf8 |
parent 48677 | bd4d132e32cf |
child 56308 | ebd3bf053969 |
permissions | -rw-r--r-- |
38425 | 1 |
/* Title: Pure/PIDE/text.scala |
45673
cd41e3903fbf
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents:
45667
diff
changeset
|
2 |
Module: PIDE |
34276 | 3 |
Author: Fabian Immler, TU Munich |
4 |
Author: Makarius |
|
5 |
||
38425 | 6 |
Basic operations on plain text. |
34276 | 7 |
*/ |
8 |
||
9 |
package isabelle |
|
10 |
||
11 |
||
44379 | 12 |
import scala.collection.mutable |
13 |
import scala.util.Sorting |
|
14 |
||
15 |
||
38425 | 16 |
object Text |
34276 | 17 |
{ |
38477 | 18 |
/* offset */ |
38426 | 19 |
|
20 |
type Offset = Int |
|
21 |
||
38477 | 22 |
|
38565
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
23 |
/* range -- with total quasi-ordering */ |
38477 | 24 |
|
38568 | 25 |
object Range |
26 |
{ |
|
27 |
def apply(start: Offset): Range = Range(start, start) |
|
44379 | 28 |
|
56172 | 29 |
val offside: Range = apply(-1) |
30 |
||
44379 | 31 |
object Ordering extends scala.math.Ordering[Text.Range] |
32 |
{ |
|
33 |
def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2 |
|
34 |
} |
|
38568 | 35 |
} |
36 |
||
38426 | 37 |
sealed case class Range(val start: Offset, val stop: Offset) |
38427 | 38 |
{ |
38565
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
39 |
// denotation: {start} Un {i. start < i & i < stop} |
43425 | 40 |
if (start > stop) |
41 |
error("Bad range: [" + start.toString + ":" + stop.toString + "]") |
|
38477 | 42 |
|
38563 | 43 |
override def toString = "[" + start.toString + ":" + stop.toString + "]" |
44 |
||
47542
26d0a76fef0a
more robust Sendback handling: JVM/jEdit paranoia for case matching, treat Pretty body not just XML.Text, replace proper_range only (without trailing whitespace);
wenzelm
parents:
46712
diff
changeset
|
45 |
def length: Int = stop - start |
26d0a76fef0a
more robust Sendback handling: JVM/jEdit paranoia for case matching, treat Pretty body not just XML.Text, replace proper_range only (without trailing whitespace);
wenzelm
parents:
46712
diff
changeset
|
46 |
|
38427 | 47 |
def map(f: Offset => Offset): Range = Range(f(start), f(stop)) |
48 |
def +(i: Offset): Range = map(_ + i) |
|
38570 | 49 |
def -(i: Offset): Range = map(_ - i) |
38662 | 50 |
|
38725 | 51 |
def is_singularity: Boolean = start == stop |
38662 | 52 |
|
38565
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
53 |
def contains(i: Offset): Boolean = start == i || start < i && i < stop |
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
54 |
def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop |
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
55 |
def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start) |
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
56 |
def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start |
38485 | 57 |
|
45240
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
58 |
def apart(that: Range): Boolean = |
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
59 |
(this.start max that.start) > (this.stop min that.stop) |
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
60 |
|
38564 | 61 |
def restrict(that: Range): Range = |
38485 | 62 |
Range(this.start max that.start, this.stop min that.stop) |
43428
b41dea5772c6
more robust treatment of partial range restriction;
wenzelm
parents:
43425
diff
changeset
|
63 |
|
b41dea5772c6
more robust treatment of partial range restriction;
wenzelm
parents:
43425
diff
changeset
|
64 |
def try_restrict(that: Range): Option[Range] = |
45240
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
65 |
if (this apart that) None |
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
66 |
else Some(restrict(that)) |
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
67 |
|
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
68 |
def try_join(that: Range): Option[Range] = |
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
69 |
if (this apart that) None |
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
70 |
else Some(Range(this.start min that.start, this.stop max that.stop)) |
38427 | 71 |
} |
38426 | 72 |
|
73 |
||
44379 | 74 |
/* perspective */ |
75 |
||
44473 | 76 |
object Perspective |
44379 | 77 |
{ |
44474 | 78 |
val empty: Perspective = Perspective(Nil) |
44379 | 79 |
|
46576
ae9286f64574
approximate Perspective.full within the bounds of the JVM;
wenzelm
parents:
46207
diff
changeset
|
80 |
def full: Perspective = Perspective(List(Range(0, Integer.MAX_VALUE / 2))) |
ae9286f64574
approximate Perspective.full within the bounds of the JVM;
wenzelm
parents:
46207
diff
changeset
|
81 |
|
44473 | 82 |
def apply(ranges: Seq[Range]): Perspective = |
44379 | 83 |
{ |
44473 | 84 |
val result = new mutable.ListBuffer[Text.Range] |
85 |
var last: Option[Text.Range] = None |
|
45240
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
86 |
def ship(next: Option[Range]) { result ++= last; last = next } |
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
87 |
|
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
88 |
for (range <- ranges.sortBy(_.start)) |
44473 | 89 |
{ |
90 |
last match { |
|
45240
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
91 |
case None => ship(Some(range)) |
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
92 |
case Some(last_range) => |
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
93 |
last_range.try_join(range) match { |
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
94 |
case None => ship(Some(range)) |
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
95 |
case joined => last = joined |
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
96 |
} |
44473 | 97 |
} |
44379 | 98 |
} |
45240
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
99 |
ship(None) |
44473 | 100 |
new Perspective(result.toList) |
44379 | 101 |
} |
44473 | 102 |
} |
103 |
||
46712 | 104 |
final class Perspective private( |
105 |
val ranges: List[Range]) // visible text partitioning in canonical order |
|
44473 | 106 |
{ |
107 |
def is_empty: Boolean = ranges.isEmpty |
|
108 |
def range: Range = |
|
109 |
if (is_empty) Range(0) |
|
110 |
else Range(ranges.head.start, ranges.last.stop) |
|
45631
6bdf8b926f50
recovered structural equality from 9d97bd3c086a, otherwise update_perspective might be issued over and over again, canceling a pending/slow execution;
wenzelm
parents:
45470
diff
changeset
|
111 |
|
6bdf8b926f50
recovered structural equality from 9d97bd3c086a, otherwise update_perspective might be issued over and over again, canceling a pending/slow execution;
wenzelm
parents:
45470
diff
changeset
|
112 |
override def hashCode: Int = ranges.hashCode |
6bdf8b926f50
recovered structural equality from 9d97bd3c086a, otherwise update_perspective might be issued over and over again, canceling a pending/slow execution;
wenzelm
parents:
45470
diff
changeset
|
113 |
override def equals(that: Any): Boolean = |
6bdf8b926f50
recovered structural equality from 9d97bd3c086a, otherwise update_perspective might be issued over and over again, canceling a pending/slow execution;
wenzelm
parents:
45470
diff
changeset
|
114 |
that match { |
6bdf8b926f50
recovered structural equality from 9d97bd3c086a, otherwise update_perspective might be issued over and over again, canceling a pending/slow execution;
wenzelm
parents:
45470
diff
changeset
|
115 |
case other: Perspective => ranges == other.ranges |
6bdf8b926f50
recovered structural equality from 9d97bd3c086a, otherwise update_perspective might be issued over and over again, canceling a pending/slow execution;
wenzelm
parents:
45470
diff
changeset
|
116 |
case _ => false |
6bdf8b926f50
recovered structural equality from 9d97bd3c086a, otherwise update_perspective might be issued over and over again, canceling a pending/slow execution;
wenzelm
parents:
45470
diff
changeset
|
117 |
} |
45240
9d97bd3c086a
proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents:
44474
diff
changeset
|
118 |
override def toString = ranges.toString |
44379 | 119 |
} |
120 |
||
121 |
||
38577 | 122 |
/* information associated with text range */ |
123 |
||
43714 | 124 |
sealed case class Info[A](val range: Text.Range, val info: A) |
38577 | 125 |
{ |
126 |
def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info) |
|
46207 | 127 |
def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info)) |
38577 | 128 |
} |
129 |
||
45470 | 130 |
type Markup = Info[XML.Elem] |
45455 | 131 |
|
38577 | 132 |
|
38426 | 133 |
/* editing */ |
34286 | 134 |
|
38425 | 135 |
object Edit |
136 |
{ |
|
38426 | 137 |
def insert(start: Offset, text: String): Edit = new Edit(true, start, text) |
138 |
def remove(start: Offset, text: String): Edit = new Edit(false, start, text) |
|
38425 | 139 |
} |
34286 | 140 |
|
46712 | 141 |
final class Edit private(val is_insert: Boolean, val start: Offset, val text: String) |
38425 | 142 |
{ |
143 |
override def toString = |
|
144 |
(if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")" |
|
34286 | 145 |
|
146 |
||
38425 | 147 |
/* transform offsets */ |
34286 | 148 |
|
38426 | 149 |
private def transform(do_insert: Boolean, i: Offset): Offset = |
150 |
if (i < start) i |
|
43425 | 151 |
else if (do_insert) i + text.length |
38426 | 152 |
else (i - text.length) max start |
34286 | 153 |
|
43425 | 154 |
def convert(i: Offset): Offset = transform(is_insert, i) |
155 |
def revert(i: Offset): Offset = transform(!is_insert, i) |
|
156 |
def convert(range: Range): Range = range.map(convert) |
|
157 |
def revert(range: Range): Range = range.map(revert) |
|
38425 | 158 |
|
34286 | 159 |
|
38425 | 160 |
/* edit strings */ |
161 |
||
38426 | 162 |
private def insert(i: Offset, string: String): String = |
163 |
string.substring(0, i) + text + string.substring(i) |
|
34276 | 164 |
|
38426 | 165 |
private def remove(i: Offset, count: Int, string: String): String = |
166 |
string.substring(0, i) + string.substring(i + count) |
|
38425 | 167 |
|
168 |
def can_edit(string: String, shift: Int): Boolean = |
|
169 |
shift <= start && start < shift + string.length |
|
170 |
||
171 |
def edit(string: String, shift: Int): (Option[Edit], String) = |
|
172 |
if (!can_edit(string, shift)) (Some(this), string) |
|
173 |
else if (is_insert) (None, insert(start - shift, string)) |
|
174 |
else { |
|
38426 | 175 |
val i = start - shift |
176 |
val count = text.length min (string.length - i) |
|
38425 | 177 |
val rest = |
178 |
if (count == text.length) None |
|
179 |
else Some(Edit.remove(start, text.substring(count))) |
|
38426 | 180 |
(rest, remove(i, count, string)) |
38425 | 181 |
} |
182 |
} |
|
34276 | 183 |
} |