src/Pure/PIDE/text.scala
author wenzelm
Sun Aug 22 18:46:16 2010 +0200 (2010-08-22 ago)
changeset 38577 4e4d3ea3725a
parent 38570 3fa11fb01f86
child 38578 1ebc6b76e5ff
permissions -rw-r--r--
renamed Markup_Tree.Node to Text.Info;
Markup_Tree.select: body may depend on full Text.Info, including range;
tuned;
wenzelm@38425
     1
/*  Title:      Pure/PIDE/text.scala
wenzelm@34276
     2
    Author:     Fabian Immler, TU Munich
wenzelm@34276
     3
    Author:     Makarius
wenzelm@34276
     4
wenzelm@38425
     5
Basic operations on plain text.
wenzelm@34276
     6
*/
wenzelm@34276
     7
wenzelm@34276
     8
package isabelle
wenzelm@34276
     9
wenzelm@34276
    10
wenzelm@38425
    11
object Text
wenzelm@34276
    12
{
wenzelm@38477
    13
  /* offset */
wenzelm@38426
    14
wenzelm@38426
    15
  type Offset = Int
wenzelm@38426
    16
wenzelm@38477
    17
wenzelm@38565
    18
  /* range -- with total quasi-ordering */
wenzelm@38477
    19
wenzelm@38568
    20
  object Range
wenzelm@38568
    21
  {
wenzelm@38568
    22
    def apply(start: Offset): Range = Range(start, start)
wenzelm@38568
    23
  }
wenzelm@38568
    24
wenzelm@38426
    25
  sealed case class Range(val start: Offset, val stop: Offset)
wenzelm@38427
    26
  {
wenzelm@38565
    27
    // denotation: {start} Un {i. start < i & i < stop}
wenzelm@38477
    28
    require(start <= stop)
wenzelm@38477
    29
wenzelm@38563
    30
    override def toString = "[" + start.toString + ":" + stop.toString + "]"
wenzelm@38563
    31
wenzelm@38427
    32
    def map(f: Offset => Offset): Range = Range(f(start), f(stop))
wenzelm@38427
    33
    def +(i: Offset): Range = map(_ + i)
wenzelm@38570
    34
    def -(i: Offset): Range = map(_ - i)
wenzelm@38565
    35
    def contains(i: Offset): Boolean = start == i || start < i && i < stop
wenzelm@38565
    36
    def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
wenzelm@38565
    37
    def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
wenzelm@38565
    38
    def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
wenzelm@38485
    39
wenzelm@38564
    40
    def restrict(that: Range): Range =
wenzelm@38485
    41
      Range(this.start max that.start, this.stop min that.stop)
wenzelm@38427
    42
  }
wenzelm@38426
    43
wenzelm@38426
    44
wenzelm@38577
    45
  /* information associated with text range */
wenzelm@38577
    46
wenzelm@38577
    47
  case class Info[A](val range: Text.Range, val info: A)
wenzelm@38577
    48
  {
wenzelm@38577
    49
    def contains[B](that: Info[B]): Boolean = this.range contains that.range
wenzelm@38577
    50
    def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
wenzelm@38577
    51
  }
wenzelm@38577
    52
wenzelm@38577
    53
wenzelm@38426
    54
  /* editing */
wenzelm@34286
    55
wenzelm@38425
    56
  object Edit
wenzelm@38425
    57
  {
wenzelm@38426
    58
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
    59
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
    60
  }
wenzelm@34286
    61
wenzelm@38426
    62
  class Edit(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
    63
  {
wenzelm@38425
    64
    override def toString =
wenzelm@38425
    65
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
    66
wenzelm@34286
    67
wenzelm@38425
    68
    /* transform offsets */
wenzelm@34286
    69
wenzelm@38426
    70
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
    71
      if (i < start) i
wenzelm@38426
    72
      else if (is_insert == do_insert) i + text.length
wenzelm@38426
    73
      else (i - text.length) max start
wenzelm@34286
    74
wenzelm@38426
    75
    def convert(i: Offset): Offset = transform(true, i)
wenzelm@38426
    76
    def revert(i: Offset): Offset = transform(false, i)
wenzelm@38425
    77
wenzelm@34286
    78
wenzelm@38425
    79
    /* edit strings */
wenzelm@38425
    80
wenzelm@38426
    81
    private def insert(i: Offset, string: String): String =
wenzelm@38426
    82
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
    83
wenzelm@38426
    84
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
    85
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
    86
wenzelm@38425
    87
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
    88
      shift <= start && start < shift + string.length
wenzelm@38425
    89
wenzelm@38425
    90
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
    91
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
    92
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
    93
      else {
wenzelm@38426
    94
        val i = start - shift
wenzelm@38426
    95
        val count = text.length min (string.length - i)
wenzelm@38425
    96
        val rest =
wenzelm@38425
    97
          if (count == text.length) None
wenzelm@38425
    98
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
    99
        (rest, remove(i, count, string))
wenzelm@38425
   100
      }
wenzelm@38425
   101
  }
wenzelm@34276
   102
}