src/Pure/PIDE/text.scala
author wenzelm
Wed Aug 18 14:02:32 2010 +0200 (2010-08-18)
changeset 38477 f01f4ab2a0af
parent 38427 7066fbd315ae
child 38485 c5eae9fc1fa4
permissions -rw-r--r--
refined notion of Text.Range;
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@38477
    35
    def contains(that: Range): Boolean = start <= that.start && that.stop <= stop
wenzelm@38477
    36
    def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
wenzelm@38477
    37
    def compare(that: Range): Int =
wenzelm@38477
    38
      if (overlaps(that)) 0
wenzelm@38477
    39
      else if (this.start < that.start) -1
wenzelm@38477
    40
      else 1
wenzelm@38427
    41
  }
wenzelm@38426
    42
wenzelm@38426
    43
wenzelm@38426
    44
  /* editing */
wenzelm@34286
    45
wenzelm@38425
    46
  object Edit
wenzelm@38425
    47
  {
wenzelm@38426
    48
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
    49
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
    50
  }
wenzelm@34286
    51
wenzelm@38426
    52
  class Edit(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
    53
  {
wenzelm@38425
    54
    override def toString =
wenzelm@38425
    55
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
    56
wenzelm@34286
    57
wenzelm@38425
    58
    /* transform offsets */
wenzelm@34286
    59
wenzelm@38426
    60
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
    61
      if (i < start) i
wenzelm@38426
    62
      else if (is_insert == do_insert) i + text.length
wenzelm@38426
    63
      else (i - text.length) max start
wenzelm@34286
    64
wenzelm@38426
    65
    def convert(i: Offset): Offset = transform(true, i)
wenzelm@38426
    66
    def revert(i: Offset): Offset = transform(false, i)
wenzelm@38425
    67
wenzelm@34286
    68
wenzelm@38425
    69
    /* edit strings */
wenzelm@38425
    70
wenzelm@38426
    71
    private def insert(i: Offset, string: String): String =
wenzelm@38426
    72
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
    73
wenzelm@38426
    74
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
    75
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
    76
wenzelm@38425
    77
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
    78
      shift <= start && start < shift + string.length
wenzelm@38425
    79
wenzelm@38425
    80
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
    81
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
    82
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
    83
      else {
wenzelm@38426
    84
        val i = start - shift
wenzelm@38426
    85
        val count = text.length min (string.length - i)
wenzelm@38425
    86
        val rest =
wenzelm@38425
    87
          if (count == text.length) None
wenzelm@38425
    88
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
    89
        (rest, remove(i, count, string))
wenzelm@38425
    90
      }
wenzelm@38425
    91
  }
wenzelm@34276
    92
}