src/Pure/PIDE/text.scala
author wenzelm
Mon, 29 Aug 2022 19:26:27 +0200
changeset 76019 f3d8da992445
parent 75393 87ebf5a50283
child 76235 16c12979c132
permissions -rw-r--r--
provide cvc5-1.0.2 (inactive);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
     1
/*  Title:      Pure/PIDE/text.scala
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     2
    Author:     Fabian Immler, TU Munich
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     4
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
     5
Basic operations on plain text.
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     6
*/
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     7
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     8
package isabelle
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     9
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    10
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    11
import scala.collection.mutable
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    12
import scala.util.Sorting
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    13
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    14
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    15
object Text {
38477
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    16
  /* offset */
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    17
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    18
  type Offset = Int
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    19
38477
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    20
38565
32b924a832c4 further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents: 38564
diff changeset
    21
  /* range -- with total quasi-ordering */
38477
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    22
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    23
  object Range {
38568
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    24
    def apply(start: Offset): Range = Range(start, start)
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    25
64678
wenzelm
parents: 64546
diff changeset
    26
    val full: Range = apply(0, Integer.MAX_VALUE / 2)
56172
31289387fdf8 tuned signature;
wenzelm
parents: 48677
diff changeset
    27
    val offside: Range = apply(-1)
31289387fdf8 tuned signature;
wenzelm
parents: 48677
diff changeset
    28
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    29
    object Ordering extends scala.math.Ordering[Range] {
65154
wenzelm
parents: 65132
diff changeset
    30
      def compare(r1: Range, r2: Range): Int = r1 compare r2
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    31
    }
38568
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    32
  }
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    33
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    34
  sealed case class Range(start: Offset, stop: Offset) {
38565
32b924a832c4 further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents: 38564
diff changeset
    35
    // denotation: {start} Un {i. start < i & i < stop}
43425
0a5612040a8b more explicit error message;
wenzelm
parents: 38725
diff changeset
    36
    if (start > stop)
64546
134ae7da2ccf clarified output: avoid confusion with line:column notation;
wenzelm
parents: 64370
diff changeset
    37
      error("Bad range: [" + start.toString + ".." + stop.toString + "]")
38477
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    38
64546
134ae7da2ccf clarified output: avoid confusion with line:column notation;
wenzelm
parents: 64370
diff changeset
    39
    override def toString: String = "[" + start.toString + ".." + stop.toString + "]"
38563
f6c9a4f9f66f added toString methods;
wenzelm
parents: 38562
diff changeset
    40
65155
25bccf5bf33e clarified native Text.Offset versus Text.Length index Int;
wenzelm
parents: 65154
diff changeset
    41
    def length: Offset = stop - start
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
    42
38427
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    43
    def map(f: Offset => Offset): Range = Range(f(start), f(stop))
56308
wenzelm
parents: 56172
diff changeset
    44
    def +(i: Offset): Range = if (i == 0) this else map(_ + i)
wenzelm
parents: 56172
diff changeset
    45
    def -(i: Offset): Range = if (i == 0) this else map(_ - i)
38662
4d4553e09337 Text.Range.is_singleton;
wenzelm
parents: 38578
diff changeset
    46
38725
3d9d5ff80f6f tuned signature;
wenzelm
parents: 38662
diff changeset
    47
    def is_singularity: Boolean = start == stop
56590
d01d183e84ea clarified treatment of markup ranges wrt. revert/convert: inflate_singularity allows to retrieve information like language_context more reliably during editing;
wenzelm
parents: 56473
diff changeset
    48
    def inflate_singularity: Range = if (is_singularity) Range(start, start + 1) else this
38662
4d4553e09337 Text.Range.is_singleton;
wenzelm
parents: 38578
diff changeset
    49
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 57912
diff changeset
    50
    def touches(i: Offset): Boolean = start <= i && i <= stop
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 57912
diff changeset
    51
38565
32b924a832c4 further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents: 38564
diff changeset
    52
    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
    53
    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
    54
    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
    55
    def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
38485
c5eae9fc1fa4 Text.Range: improved handling of singularities;
wenzelm
parents: 38477
diff changeset
    56
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
    57
    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
    58
      (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
    59
38564
a6e2715fac5f parameterized type Markup_Tree.Node;
wenzelm
parents: 38563
diff changeset
    60
    def restrict(that: Range): Range =
38485
c5eae9fc1fa4 Text.Range: improved handling of singularities;
wenzelm
parents: 38477
diff changeset
    61
      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
    62
b41dea5772c6 more robust treatment of partial range restriction;
wenzelm
parents: 43425
diff changeset
    63
    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
    64
      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
    65
      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
    66
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
    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
    68
      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
    69
      else Some(Range(this.start min that.start, this.stop max that.stop))
65522
4d7c5df70a14 tuned signature;
wenzelm
parents: 65371
diff changeset
    70
4d7c5df70a14 tuned signature;
wenzelm
parents: 65371
diff changeset
    71
    def substring(text: String): String = text.substring(start, stop)
66114
c137a9f038a6 clarified signature;
wenzelm
parents: 65522
diff changeset
    72
c137a9f038a6 clarified signature;
wenzelm
parents: 65522
diff changeset
    73
    def try_substring(text: String): Option[String] =
c137a9f038a6 clarified signature;
wenzelm
parents: 65522
diff changeset
    74
      try { Some(substring(text)) }
c137a9f038a6 clarified signature;
wenzelm
parents: 65522
diff changeset
    75
      catch { case _: IndexOutOfBoundsException => None }
38427
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    76
  }
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    77
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    78
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    79
  /* perspective */
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    80
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    81
  object Perspective {
44474
681447a9ffe5 slightly more abstract Command.Perspective;
wenzelm
parents: 44473
diff changeset
    82
    val empty: Perspective = Perspective(Nil)
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    83
64678
wenzelm
parents: 64546
diff changeset
    84
    def full: Perspective = Perspective(List(Range.full))
46576
ae9286f64574 approximate Perspective.full within the bounds of the JVM;
wenzelm
parents: 46207
diff changeset
    85
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    86
    def apply(ranges: List[Range]): Perspective = {
65154
wenzelm
parents: 65132
diff changeset
    87
      val result = new mutable.ListBuffer[Range]
wenzelm
parents: 65132
diff changeset
    88
      var last: Option[Range] = None
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 66114
diff changeset
    89
      def ship(next: Option[Range]): Unit = { result ++= last; last = next }
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
    90
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    91
      for (range <- ranges.sortBy(_.start)) {
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
    92
        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
    93
          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
    94
          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
    95
            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
    96
              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
    97
              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
    98
            }
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
    99
        }
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
   100
      }
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
   101
      ship(None)
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
   102
      new Perspective(result.toList)
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
   103
    }
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
   104
  }
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
   105
46712
8650d9a95736 prefer final ADTs -- prevent ooddities;
wenzelm
parents: 46576
diff changeset
   106
  final class Perspective private(
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   107
    val ranges: List[Range]  // visible text partitioning in canonical order
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   108
  ) {
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
   109
    def is_empty: Boolean = ranges.isEmpty
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
   110
    def range: Range =
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
   111
      if (is_empty) Range(0)
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
   112
      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
   113
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
    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
   115
    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
   116
      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
   117
        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
   118
        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
   119
      }
57912
wenzelm
parents: 57620
diff changeset
   120
    override def toString: String = ranges.toString
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
   121
  }
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
   122
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
   123
38577
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
   124
  /* information associated with text range */
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
   125
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   126
  sealed case class Info[A](range: Range, info: A) {
65154
wenzelm
parents: 65132
diff changeset
   127
    def restrict(r: Range): Info[A] = Info(range.restrict(r), info)
wenzelm
parents: 65132
diff changeset
   128
    def try_restrict(r: Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
65132
wenzelm
parents: 64816
diff changeset
   129
wenzelm
parents: 64816
diff changeset
   130
    def map[B](f: A => B): Info[B] = Info(range, f(info))
38577
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
   131
  }
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
   132
45470
81b85d4ed269 more precise type;
wenzelm
parents: 45455
diff changeset
   133
  type Markup = Info[XML.Elem]
45455
4f974c0c5c2f prefer statically typed Text.Markup;
wenzelm
parents: 45250
diff changeset
   134
38577
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
   135
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   136
  /* editing */
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   137
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   138
  object Edit {
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   139
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   140
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
65156
35fefc249311 suppress vacuous edits;
wenzelm
parents: 65155
diff changeset
   141
    def inserts(start: Offset, text: String): List[Edit] =
35fefc249311 suppress vacuous edits;
wenzelm
parents: 65155
diff changeset
   142
      if (text == "") Nil else List(insert(start, text))
35fefc249311 suppress vacuous edits;
wenzelm
parents: 65155
diff changeset
   143
    def removes(start: Offset, text: String): List[Edit] =
35fefc249311 suppress vacuous edits;
wenzelm
parents: 65155
diff changeset
   144
      if (text == "") Nil else List(remove(start, text))
64816
wenzelm
parents: 64682
diff changeset
   145
    def replace(start: Offset, old_text: String, new_text: String): List[Edit] =
wenzelm
parents: 64682
diff changeset
   146
      if (old_text == new_text) Nil
65156
35fefc249311 suppress vacuous edits;
wenzelm
parents: 65155
diff changeset
   147
      else removes(start, old_text) ::: inserts(start, new_text)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   148
  }
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   149
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   150
  final class Edit private(val is_insert: Boolean, val start: Offset, val text: String) {
57912
wenzelm
parents: 57620
diff changeset
   151
    override def toString: String =
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   152
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   153
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   154
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   155
    /* transform offsets */
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   156
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   157
    private def transform(do_insert: Boolean, i: Offset): Offset =
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   158
      if (i < start) i
43425
0a5612040a8b more explicit error message;
wenzelm
parents: 38725
diff changeset
   159
      else if (do_insert) i + text.length
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   160
      else (i - text.length) max start
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   161
43425
0a5612040a8b more explicit error message;
wenzelm
parents: 38725
diff changeset
   162
    def convert(i: Offset): Offset = transform(is_insert, i)
0a5612040a8b more explicit error message;
wenzelm
parents: 38725
diff changeset
   163
    def revert(i: Offset): Offset = transform(!is_insert, i)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   164
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   165
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   166
    /* edit strings */
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   167
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   168
    private def insert(i: Offset, string: String): String =
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   169
      string.substring(0, i) + text + string.substring(i)
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
   170
65155
25bccf5bf33e clarified native Text.Offset versus Text.Length index Int;
wenzelm
parents: 65154
diff changeset
   171
    private def remove(i: Offset, count: Offset, string: String): String =
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   172
      string.substring(0, i) + string.substring(i + count)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   173
65155
25bccf5bf33e clarified native Text.Offset versus Text.Length index Int;
wenzelm
parents: 65154
diff changeset
   174
    def can_edit(string: String, shift: Offset): Boolean =
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   175
      shift <= start && start < shift + string.length
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   176
65155
25bccf5bf33e clarified native Text.Offset versus Text.Length index Int;
wenzelm
parents: 65154
diff changeset
   177
    def edit(string: String, shift: Offset): (Option[Edit], String) =
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   178
      if (!can_edit(string, shift)) (Some(this), string)
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   179
      else if (is_insert) (None, insert(start - shift, string))
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   180
      else {
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   181
        val i = start - shift
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   182
        val count = text.length min (string.length - i)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   183
        val rest =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   184
          if (count == text.length) None
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   185
          else Some(Edit.remove(start, text.substring(count)))
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   186
        (rest, remove(i, count, string))
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   187
      }
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   188
  }
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
   189
}