src/Pure/PIDE/text.scala
author wenzelm
Mon Jul 04 13:43:10 2011 +0200 (2011-07-04)
changeset 43650 f00da558b78e
parent 43428 b41dea5772c6
child 43714 3749d1e6dde9
permissions -rw-r--r--
imitate exception ERROR of Isabelle/ML;
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@43425
    28
    if (start > stop)
wenzelm@43425
    29
      error("Bad range: [" + start.toString + ":" + stop.toString + "]")
wenzelm@38477
    30
wenzelm@38563
    31
    override def toString = "[" + start.toString + ":" + stop.toString + "]"
wenzelm@38563
    32
wenzelm@38427
    33
    def map(f: Offset => Offset): Range = Range(f(start), f(stop))
wenzelm@38427
    34
    def +(i: Offset): Range = map(_ + i)
wenzelm@38570
    35
    def -(i: Offset): Range = map(_ - i)
wenzelm@38662
    36
wenzelm@38725
    37
    def is_singularity: Boolean = start == stop
wenzelm@38662
    38
wenzelm@38565
    39
    def contains(i: Offset): Boolean = start == i || start < i && i < stop
wenzelm@38565
    40
    def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
wenzelm@38565
    41
    def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
wenzelm@38565
    42
    def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
wenzelm@38485
    43
wenzelm@38564
    44
    def restrict(that: Range): Range =
wenzelm@38485
    45
      Range(this.start max that.start, this.stop min that.stop)
wenzelm@43428
    46
wenzelm@43428
    47
    def try_restrict(that: Range): Option[Range] =
wenzelm@43428
    48
      try { Some (restrict(that)) }
wenzelm@43650
    49
      catch { case ERROR(_) => None }
wenzelm@38427
    50
  }
wenzelm@38426
    51
wenzelm@38426
    52
wenzelm@38577
    53
  /* information associated with text range */
wenzelm@38577
    54
wenzelm@38577
    55
  case class Info[A](val range: Text.Range, val info: A)
wenzelm@38577
    56
  {
wenzelm@38577
    57
    def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
wenzelm@43428
    58
    def try_restrict(r: Text.Range): Option[Info[A]] =
wenzelm@43428
    59
      try { Some(Info(range.restrict(r), info)) }
wenzelm@43650
    60
      catch { case ERROR(_) => None }
wenzelm@38577
    61
  }
wenzelm@38577
    62
wenzelm@38577
    63
wenzelm@38426
    64
  /* editing */
wenzelm@34286
    65
wenzelm@38425
    66
  object Edit
wenzelm@38425
    67
  {
wenzelm@38426
    68
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
    69
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
    70
  }
wenzelm@34286
    71
wenzelm@38426
    72
  class Edit(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
    73
  {
wenzelm@38425
    74
    override def toString =
wenzelm@38425
    75
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
    76
wenzelm@34286
    77
wenzelm@38425
    78
    /* transform offsets */
wenzelm@34286
    79
wenzelm@38426
    80
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
    81
      if (i < start) i
wenzelm@43425
    82
      else if (do_insert) i + text.length
wenzelm@38426
    83
      else (i - text.length) max start
wenzelm@34286
    84
wenzelm@43425
    85
    def convert(i: Offset): Offset = transform(is_insert, i)
wenzelm@43425
    86
    def revert(i: Offset): Offset = transform(!is_insert, i)
wenzelm@43425
    87
    def convert(range: Range): Range = range.map(convert)
wenzelm@43425
    88
    def revert(range: Range): Range = range.map(revert)
wenzelm@38425
    89
wenzelm@34286
    90
wenzelm@38425
    91
    /* edit strings */
wenzelm@38425
    92
wenzelm@38426
    93
    private def insert(i: Offset, string: String): String =
wenzelm@38426
    94
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
    95
wenzelm@38426
    96
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
    97
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
    98
wenzelm@38425
    99
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
   100
      shift <= start && start < shift + string.length
wenzelm@38425
   101
wenzelm@38425
   102
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
   103
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
   104
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
   105
      else {
wenzelm@38426
   106
        val i = start - shift
wenzelm@38426
   107
        val count = text.length min (string.length - i)
wenzelm@38425
   108
        val rest =
wenzelm@38425
   109
          if (count == text.length) None
wenzelm@38425
   110
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
   111
        (rest, remove(i, count, string))
wenzelm@38425
   112
      }
wenzelm@38425
   113
  }
wenzelm@34276
   114
}