src/Pure/PIDE/text.scala
author wenzelm
Thu Aug 19 17:37:13 2010 +0200 (2010-08-19)
changeset 38485 c5eae9fc1fa4
parent 38477 f01f4ab2a0af
child 38562 3de5f0424caa
permissions -rw-r--r--
Text.Range: improved handling of singularities;
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@38477
    18
  /* range: interval with total quasi-ordering */
wenzelm@38477
    19
wenzelm@38477
    20
  object Range
wenzelm@38477
    21
  {
wenzelm@38477
    22
    object Ordering extends scala.math.Ordering[Range]
wenzelm@38477
    23
    {
wenzelm@38477
    24
      override def compare(r1: Range, r2: Range): Int = r1 compare r2
wenzelm@38477
    25
    }
wenzelm@38477
    26
  }
wenzelm@38477
    27
wenzelm@38426
    28
  sealed case class Range(val start: Offset, val stop: Offset)
wenzelm@38427
    29
  {
wenzelm@38477
    30
    require(start <= stop)
wenzelm@38477
    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@38477
    34
    def contains(i: Offset): Boolean = start <= i && i < stop
wenzelm@38485
    35
    def contains(that: Range): Boolean =
wenzelm@38485
    36
      this == that || this.contains(that.start) && that.stop <= this.stop
wenzelm@38485
    37
    def overlaps(that: Range): Boolean =
wenzelm@38485
    38
      this == that || this.contains(that.start) || that.contains(this.start)
wenzelm@38477
    39
    def compare(that: Range): Int =
wenzelm@38477
    40
      if (overlaps(that)) 0
wenzelm@38477
    41
      else if (this.start < that.start) -1
wenzelm@38477
    42
      else 1
wenzelm@38485
    43
wenzelm@38485
    44
    def start_range: Range = Range(start, start)
wenzelm@38485
    45
    def stop_range: Range = Range(stop, stop)
wenzelm@38485
    46
wenzelm@38485
    47
    def intersect(that: Range): Range =
wenzelm@38485
    48
      Range(this.start max that.start, this.stop min that.stop)
wenzelm@38427
    49
  }
wenzelm@38426
    50
wenzelm@38426
    51
wenzelm@38426
    52
  /* editing */
wenzelm@34286
    53
wenzelm@38425
    54
  object Edit
wenzelm@38425
    55
  {
wenzelm@38426
    56
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
    57
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
    58
  }
wenzelm@34286
    59
wenzelm@38426
    60
  class Edit(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
    61
  {
wenzelm@38425
    62
    override def toString =
wenzelm@38425
    63
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
    64
wenzelm@34286
    65
wenzelm@38425
    66
    /* transform offsets */
wenzelm@34286
    67
wenzelm@38426
    68
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
    69
      if (i < start) i
wenzelm@38426
    70
      else if (is_insert == do_insert) i + text.length
wenzelm@38426
    71
      else (i - text.length) max start
wenzelm@34286
    72
wenzelm@38426
    73
    def convert(i: Offset): Offset = transform(true, i)
wenzelm@38426
    74
    def revert(i: Offset): Offset = transform(false, i)
wenzelm@38425
    75
wenzelm@34286
    76
wenzelm@38425
    77
    /* edit strings */
wenzelm@38425
    78
wenzelm@38426
    79
    private def insert(i: Offset, string: String): String =
wenzelm@38426
    80
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
    81
wenzelm@38426
    82
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
    83
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
    84
wenzelm@38425
    85
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
    86
      shift <= start && start < shift + string.length
wenzelm@38425
    87
wenzelm@38425
    88
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
    89
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
    90
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
    91
      else {
wenzelm@38426
    92
        val i = start - shift
wenzelm@38426
    93
        val count = text.length min (string.length - i)
wenzelm@38425
    94
        val rest =
wenzelm@38425
    95
          if (count == text.length) None
wenzelm@38425
    96
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
    97
        (rest, remove(i, count, string))
wenzelm@38425
    98
      }
wenzelm@38425
    99
  }
wenzelm@34276
   100
}