src/Pure/PIDE/text.scala
author wenzelm
Sun, 22 Aug 2010 19:33:01 +0200
changeset 38578 1ebc6b76e5ff
parent 38577 4e4d3ea3725a
child 38662 4d4553e09337
permissions -rw-r--r--
misc tuning and simplification;
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
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    11
object Text
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    12
{
38477
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    13
  /* offset */
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    14
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    15
  type Offset = Int
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    16
38477
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    17
38565
32b924a832c4 further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents: 38564
diff changeset
    18
  /* range -- with total quasi-ordering */
38477
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    19
38568
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    20
  object Range
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    21
  {
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    22
    def apply(start: Offset): Range = Range(start, start)
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    23
  }
f117ba49a59c alternative constructor for Range singularities;
wenzelm
parents: 38565
diff changeset
    24
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    25
  sealed case class Range(val start: Offset, val stop: Offset)
38427
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    26
  {
38565
32b924a832c4 further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents: 38564
diff changeset
    27
    // denotation: {start} Un {i. start < i & i < stop}
38477
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    28
    require(start <= stop)
f01f4ab2a0af refined notion of Text.Range;
wenzelm
parents: 38427
diff changeset
    29
38563
f6c9a4f9f66f added toString methods;
wenzelm
parents: 38562
diff changeset
    30
    override def toString = "[" + start.toString + ":" + stop.toString + "]"
f6c9a4f9f66f added toString methods;
wenzelm
parents: 38562
diff changeset
    31
38427
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    32
    def map(f: Offset => Offset): Range = Range(f(start), f(stop))
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    33
    def +(i: Offset): Range = map(_ + i)
38570
3fa11fb01f86 added Text.Range.- convenience;
wenzelm
parents: 38568
diff changeset
    34
    def -(i: Offset): Range = map(_ - i)
38565
32b924a832c4 further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents: 38564
diff changeset
    35
    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
    36
    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
    37
    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
    38
    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
    39
38564
a6e2715fac5f parameterized type Markup_Tree.Node;
wenzelm
parents: 38563
diff changeset
    40
    def restrict(that: Range): Range =
38485
c5eae9fc1fa4 Text.Range: improved handling of singularities;
wenzelm
parents: 38477
diff changeset
    41
      Range(this.start max that.start, this.stop min that.stop)
38427
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    42
  }
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    43
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    44
38577
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
    45
  /* information associated with text range */
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
    46
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
    47
  case class Info[A](val range: Text.Range, val info: A)
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
    48
  {
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
    49
    def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
    50
  }
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
    51
4e4d3ea3725a renamed Markup_Tree.Node to Text.Info;
wenzelm
parents: 38570
diff changeset
    52
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    53
  /* editing */
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    54
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    55
  object Edit
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    56
  {
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    57
    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
    58
    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
    59
  }
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    60
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    61
  class Edit(val is_insert: Boolean, val start: Offset, val text: String)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    62
  {
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    63
    override def toString =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    64
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    65
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    66
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    67
    /* transform offsets */
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    68
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    69
    private def transform(do_insert: Boolean, i: Offset): Offset =
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    70
      if (i < start) i
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    71
      else if (is_insert == do_insert) i + text.length
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    72
      else (i - text.length) max start
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    73
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    74
    def convert(i: Offset): Offset = transform(true, i)
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    75
    def revert(i: Offset): Offset = transform(false, i)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    76
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    77
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    78
    /* edit strings */
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    79
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    80
    private def insert(i: Offset, string: String): String =
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    81
      string.substring(0, i) + text + string.substring(i)
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    82
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    83
    private def remove(i: Offset, count: Int, string: String): String =
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    84
      string.substring(0, i) + string.substring(i + count)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    85
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    86
    def can_edit(string: String, shift: Int): Boolean =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    87
      shift <= start && start < shift + string.length
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    88
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    89
    def edit(string: String, shift: Int): (Option[Edit], String) =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    90
      if (!can_edit(string, shift)) (Some(this), string)
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    91
      else if (is_insert) (None, insert(start - shift, string))
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    92
      else {
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    93
        val i = start - shift
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    94
        val count = text.length min (string.length - i)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    95
        val rest =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    96
          if (count == text.length) None
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    97
          else Some(Edit.remove(start, text.substring(count)))
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    98
        (rest, remove(i, count, string))
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    99
      }
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
   100
  }
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
   101
}