author | wenzelm |
Thu, 12 Aug 2010 15:19:11 +0200 | |
changeset 38363 | af7f41a8a0a8 |
parent 38154 | 343cb5d4034a |
permissions | -rw-r--r-- |
36676 | 1 |
/* Title: Pure/PIDE/text_edit.scala |
34276 | 2 |
Author: Fabian Immler, TU Munich |
3 |
Author: Makarius |
|
4 |
||
5 |
Basic edits on plain text. |
|
6 |
*/ |
|
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
34312
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
11 |
class Text_Edit(val is_insert: Boolean, val start: Int, val text: String) |
34276 | 12 |
{ |
34315 | 13 |
override def toString = |
14 |
(if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")" |
|
15 |
||
16 |
||
34286 | 17 |
/* transform offsets */ |
18 |
||
34312
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
19 |
private def transform(do_insert: Boolean, offset: Int): Int = |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
20 |
if (offset < start) offset |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
21 |
else if (is_insert == do_insert) offset + text.length |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
22 |
else (offset - text.length) max start |
34286 | 23 |
|
38154 | 24 |
def convert(offset: Int): Int = transform(true, offset) |
25 |
def revert(offset: Int): Int = transform(false, offset) |
|
34286 | 26 |
|
27 |
||
28 |
/* edit strings */ |
|
29 |
||
34312
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
30 |
private def insert(index: Int, string: String): String = |
34286 | 31 |
string.substring(0, index) + text + string.substring(index) |
32 |
||
33 |
private def remove(index: Int, count: Int, string: String): String = |
|
34 |
string.substring(0, index) + string.substring(index + count) |
|
35 |
||
34312
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
36 |
def can_edit(string: String, shift: Int): Boolean = |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
37 |
shift <= start && start < shift + string.length |
34276 | 38 |
|
34312
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
39 |
def edit(string: String, shift: Int): (Option[Text_Edit], String) = |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
40 |
if (!can_edit(string, shift)) (Some(this), string) |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
41 |
else if (is_insert) (None, insert(start - shift, string)) |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
42 |
else { |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
43 |
val index = start - shift |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
44 |
val count = text.length min (string.length - index) |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
45 |
val rest = |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
46 |
if (count == text.length) None |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
47 |
else Some(new Text_Edit(false, start, text.substring(count))) |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
48 |
(rest, remove(index, count, string)) |
265075989f01
simplified Text_Edit -- deflated class hierarchy;
wenzelm
parents:
34302
diff
changeset
|
49 |
} |
34276 | 50 |
} |
51 |