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