src/Pure/PIDE/text.scala
author wenzelm
Tue, 29 Nov 2011 21:50:00 +0100
changeset 45674 eb65c9d17e2f
parent 45673 cd41e3903fbf
child 46207 e76b77356ed6
permissions -rw-r--r--
clarified Time vs. Timing;
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
45673
cd41e3903fbf separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents: 45667
diff changeset
     2
    Module:     PIDE
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     3
    Author:     Fabian Immler, TU Munich
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     4
    Author:     Makarius
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     5
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
     6
Basic operations on plain text.
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     7
*/
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     8
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     9
package isabelle
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    10
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    11
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    12
import scala.collection.mutable
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    13
import scala.math.Ordering
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    14
import scala.util.Sorting
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    15
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    16
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    17
object Text
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    18
{
38477
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    19
  /* offset */
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    20
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    21
  type Offset = Int
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    22
38477
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    23
38565
32b924a832c4 further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents: 38564
diff changeset
    24
  /* range -- with total quasi-ordering */
38477
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    25
38568
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    26
  object Range
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    27
  {
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    28
    def apply(start: Offset): Range = Range(start, start)
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    29
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    30
    object Ordering extends scala.math.Ordering[Text.Range]
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    31
    {
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    32
      def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    33
    }
38568
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    34
  }
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    35
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    36
  sealed case class Range(val start: Offset, val stop: Offset)
38427
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    37
  {
38565
32b924a832c4 further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents: 38564
diff changeset
    38
    // denotation: {start} Un {i. start < i & i < stop}
43425
0a5612040a8b more explicit error message;
wenzelm
parents: 38725
diff changeset
    39
    if (start > stop)
0a5612040a8b more explicit error message;
wenzelm
parents: 38725
diff changeset
    40
      error("Bad range: [" + start.toString + ":" + stop.toString + "]")
38477
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    41
38563
f6c9a4f9f66f added toString methods;
wenzelm
parents: 38562
diff changeset
    42
    override def toString = "[" + start.toString + ":" + stop.toString + "]"
f6c9a4f9f66f added toString methods;
wenzelm
parents: 38562
diff changeset
    43
38427
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    44
    def map(f: Offset => Offset): Range = Range(f(start), f(stop))
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    45
    def +(i: Offset): Range = map(_ + i)
38570
3fa11fb01f86 added Text.Range.- convenience;
wenzelm
parents: 38568
diff changeset
    46
    def -(i: Offset): Range = map(_ - i)
38662
4d4553e09337 Text.Range.is_singleton;
wenzelm
parents: 38578
diff changeset
    47
38725
3d9d5ff80f6f tuned signature;
wenzelm
parents: 38662
diff changeset
    48
    def is_singularity: Boolean = start == stop
38662
4d4553e09337 Text.Range.is_singleton;
wenzelm
parents: 38578
diff changeset
    49
38565
32b924a832c4 further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents: 38564
diff changeset
    50
    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
    51
    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
    52
    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
    53
    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
    54
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
    55
    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
    56
      (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
    57
38564
a6e2715fac5f parameterized type Markup_Tree.Node;
wenzelm
parents: 38563
diff changeset
    58
    def restrict(that: Range): Range =
38485
c5eae9fc1fa4 Text.Range: improved handling of singularities;
wenzelm
parents: 38477
diff changeset
    59
      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
    60
b41dea5772c6 more robust treatment of partial range restriction;
wenzelm
parents: 43425
diff changeset
    61
    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
    62
      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
    63
      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
    64
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
    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
    66
      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
    67
      else Some(Range(this.start min that.start, this.stop max that.stop))
38427
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    68
  }
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    69
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    70
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    71
  /* perspective */
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    72
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
    73
  object Perspective
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    74
  {
44474
681447a9ffe5 slightly more abstract Command.Perspective;
wenzelm
parents: 44473
diff changeset
    75
    val empty: Perspective = Perspective(Nil)
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    76
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
    77
    def apply(ranges: Seq[Range]): Perspective =
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    78
    {
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
    79
      val result = new mutable.ListBuffer[Text.Range]
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
    80
      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
    81
      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
    82
9d97bd3c086a proper normal form for Perspective.ranges (overlapping ranges could be joined in wrong order, crashing multiple editor views);
wenzelm
parents: 44474
diff changeset
    83
      for (range <- ranges.sortBy(_.start))
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
    84
      {
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
    85
        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
    86
          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
    87
          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
    88
            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
    89
              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
    90
              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
    91
            }
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
    92
        }
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    93
      }
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
    94
      ship(None)
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
    95
      new Perspective(result.toList)
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
    96
    }
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
    97
  }
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
    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
  class Perspective private(val ranges: List[Range]) // visible text partitioning in canonical order
44473
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
   100
  {
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
   101
    def is_empty: Boolean = ranges.isEmpty
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
   102
    def range: Range =
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
   103
      if (is_empty) Range(0)
4f264fdf8d0e slightly more abstract Text.Perspective;
wenzelm
parents: 44384
diff changeset
   104
      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
   105
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
   106
    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
   107
    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
   108
      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
   109
        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
   110
        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
   111
      }
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
   112
    override def toString = ranges.toString
44379
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
   113
  }
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
   114
1079ab6b342b added official Text.Range.Ordering;
wenzelm
parents: 43714
diff changeset
   115
38577
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
   116
  /* information associated with text range */
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
   117
43714
3749d1e6dde9 tuned signature;
wenzelm
parents: 43650
diff changeset
   118
  sealed case class Info[A](val range: Text.Range, val info: A)
38577
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
   119
  {
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
   120
    def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
43428
b41dea5772c6 more robust treatment of partial range restriction;
wenzelm
parents: 43425
diff changeset
   121
    def try_restrict(r: Text.Range): Option[Info[A]] =
b41dea5772c6 more robust treatment of partial range restriction;
wenzelm
parents: 43425
diff changeset
   122
      try { Some(Info(range.restrict(r), info)) }
43650
f00da558b78e imitate exception ERROR of Isabelle/ML;
wenzelm
parents: 43428
diff changeset
   123
      catch { case ERROR(_) => None }
38577
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
   124
  }
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
   125
45470
81b85d4ed269 more precise type;
wenzelm
parents: 45455
diff changeset
   126
  type Markup = Info[XML.Elem]
45455
4f974c0c5c2f prefer statically typed Text.Markup;
wenzelm
parents: 45250
diff changeset
   127
38577
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
   128
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   129
  /* editing */
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   130
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   131
  object Edit
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   132
  {
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   133
    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
   134
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   135
  }
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   136
45250
feef63bcd787 class Text.Edit as abstract datatype;
wenzelm
parents: 45240
diff changeset
   137
  class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   138
  {
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   139
    override def toString =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   140
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   141
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   142
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   143
    /* transform offsets */
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   144
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   145
    private def transform(do_insert: Boolean, i: Offset): Offset =
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   146
      if (i < start) i
43425
0a5612040a8b more explicit error message;
wenzelm
parents: 38725
diff changeset
   147
      else if (do_insert) i + text.length
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   148
      else (i - text.length) max start
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   149
43425
0a5612040a8b more explicit error message;
wenzelm
parents: 38725
diff changeset
   150
    def convert(i: Offset): Offset = transform(is_insert, i)
0a5612040a8b more explicit error message;
wenzelm
parents: 38725
diff changeset
   151
    def revert(i: Offset): Offset = transform(!is_insert, i)
0a5612040a8b more explicit error message;
wenzelm
parents: 38725
diff changeset
   152
    def convert(range: Range): Range = range.map(convert)
0a5612040a8b more explicit error message;
wenzelm
parents: 38725
diff changeset
   153
    def revert(range: Range): Range = range.map(revert)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   154
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
   155
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   156
    /* edit strings */
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   157
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   158
    private def insert(i: Offset, string: String): String =
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   159
      string.substring(0, i) + text + string.substring(i)
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
   160
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   161
    private def remove(i: Offset, count: Int, string: String): String =
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   162
      string.substring(0, i) + string.substring(i + count)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   163
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   164
    def can_edit(string: String, shift: Int): Boolean =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   165
      shift <= start && start < shift + string.length
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   166
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   167
    def edit(string: String, shift: Int): (Option[Edit], String) =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   168
      if (!can_edit(string, shift)) (Some(this), string)
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   169
      else if (is_insert) (None, insert(start - shift, string))
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   170
      else {
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   171
        val i = start - shift
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   172
        val count = text.length min (string.length - i)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   173
        val rest =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   174
          if (count == text.length) None
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   175
          else Some(Edit.remove(start, text.substring(count)))
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   176
        (rest, remove(i, count, string))
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   177
      }
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   178
  }
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
   179
}