src/Pure/PIDE/text.scala
author wenzelm
Tue Aug 24 23:49:07 2010 +0200 (2010-08-24)
changeset 38662 4d4553e09337
parent 38578 1ebc6b76e5ff
child 38725 3d9d5ff80f6f
permissions -rw-r--r--
Text.Range.is_singleton;
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@38662
    35
wenzelm@38662
    36
    def is_singleton: Boolean = start == stop
wenzelm@38662
    37
wenzelm@38565
    38
    def contains(i: Offset): Boolean = start == i || start < i && i < stop
wenzelm@38565
    39
    def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
wenzelm@38565
    40
    def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
wenzelm@38565
    41
    def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
wenzelm@38485
    42
wenzelm@38564
    43
    def restrict(that: Range): Range =
wenzelm@38485
    44
      Range(this.start max that.start, this.stop min that.stop)
wenzelm@38427
    45
  }
wenzelm@38426
    46
wenzelm@38426
    47
wenzelm@38577
    48
  /* information associated with text range */
wenzelm@38577
    49
wenzelm@38577
    50
  case class Info[A](val range: Text.Range, val info: A)
wenzelm@38577
    51
  {
wenzelm@38577
    52
    def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
wenzelm@38577
    53
  }
wenzelm@38577
    54
wenzelm@38577
    55
wenzelm@38426
    56
  /* editing */
wenzelm@34286
    57
wenzelm@38425
    58
  object Edit
wenzelm@38425
    59
  {
wenzelm@38426
    60
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
    61
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
    62
  }
wenzelm@34286
    63
wenzelm@38426
    64
  class Edit(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
    65
  {
wenzelm@38425
    66
    override def toString =
wenzelm@38425
    67
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
    68
wenzelm@34286
    69
wenzelm@38425
    70
    /* transform offsets */
wenzelm@34286
    71
wenzelm@38426
    72
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
    73
      if (i < start) i
wenzelm@38426
    74
      else if (is_insert == do_insert) i + text.length
wenzelm@38426
    75
      else (i - text.length) max start
wenzelm@34286
    76
wenzelm@38426
    77
    def convert(i: Offset): Offset = transform(true, i)
wenzelm@38426
    78
    def revert(i: Offset): Offset = transform(false, i)
wenzelm@38425
    79
wenzelm@34286
    80
wenzelm@38425
    81
    /* edit strings */
wenzelm@38425
    82
wenzelm@38426
    83
    private def insert(i: Offset, string: String): String =
wenzelm@38426
    84
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
    85
wenzelm@38426
    86
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
    87
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
    88
wenzelm@38425
    89
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
    90
      shift <= start && start < shift + string.length
wenzelm@38425
    91
wenzelm@38425
    92
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
    93
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
    94
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
    95
      else {
wenzelm@38426
    96
        val i = start - shift
wenzelm@38426
    97
        val count = text.length min (string.length - i)
wenzelm@38425
    98
        val rest =
wenzelm@38425
    99
          if (count == text.length) None
wenzelm@38425
   100
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
   101
        (rest, remove(i, count, string))
wenzelm@38425
   102
      }
wenzelm@38425
   103
  }
wenzelm@34276
   104
}