src/Pure/PIDE/text.scala
author wenzelm
Sun Aug 15 23:07:22 2010 +0200 (2010-08-15)
changeset 38427 7066fbd315ae
parent 38426 2858ec7b6dd8
child 38477 f01f4ab2a0af
permissions -rw-r--r--
some derived operations on 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@38426
    13
  /* offset and range */
wenzelm@38426
    14
wenzelm@38426
    15
  type Offset = Int
wenzelm@38426
    16
wenzelm@38426
    17
  sealed case class Range(val start: Offset, val stop: Offset)
wenzelm@38427
    18
  {
wenzelm@38427
    19
    def contains(i: Offset): Boolean = start <= i && i < stop
wenzelm@38427
    20
    def map(f: Offset => Offset): Range = Range(f(start), f(stop))
wenzelm@38427
    21
    def +(i: Offset): Range = map(_ + i)
wenzelm@38427
    22
  }
wenzelm@38426
    23
wenzelm@38426
    24
wenzelm@38426
    25
  /* editing */
wenzelm@34286
    26
wenzelm@38425
    27
  object Edit
wenzelm@38425
    28
  {
wenzelm@38426
    29
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
    30
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
    31
  }
wenzelm@34286
    32
wenzelm@38426
    33
  class Edit(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
    34
  {
wenzelm@38425
    35
    override def toString =
wenzelm@38425
    36
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
    37
wenzelm@34286
    38
wenzelm@38425
    39
    /* transform offsets */
wenzelm@34286
    40
wenzelm@38426
    41
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
    42
      if (i < start) i
wenzelm@38426
    43
      else if (is_insert == do_insert) i + text.length
wenzelm@38426
    44
      else (i - text.length) max start
wenzelm@34286
    45
wenzelm@38426
    46
    def convert(i: Offset): Offset = transform(true, i)
wenzelm@38426
    47
    def revert(i: Offset): Offset = transform(false, i)
wenzelm@38425
    48
wenzelm@34286
    49
wenzelm@38425
    50
    /* edit strings */
wenzelm@38425
    51
wenzelm@38426
    52
    private def insert(i: Offset, string: String): String =
wenzelm@38426
    53
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
    54
wenzelm@38426
    55
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
    56
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
    57
wenzelm@38425
    58
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
    59
      shift <= start && start < shift + string.length
wenzelm@38425
    60
wenzelm@38425
    61
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
    62
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
    63
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
    64
      else {
wenzelm@38426
    65
        val i = start - shift
wenzelm@38426
    66
        val count = text.length min (string.length - i)
wenzelm@38425
    67
        val rest =
wenzelm@38425
    68
          if (count == text.length) None
wenzelm@38425
    69
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
    70
        (rest, remove(i, count, string))
wenzelm@38425
    71
      }
wenzelm@38425
    72
  }
wenzelm@34276
    73
}