src/Pure/PIDE/text.scala
author wenzelm
Thu Aug 19 22:52:00 2010 +0200 (2010-08-19)
changeset 38564 a6e2715fac5f
parent 38563 f6c9a4f9f66f
child 38565 32b924a832c4
permissions -rw-r--r--
parameterized type Markup_Tree.Node;
Markup_Tree.select: allow arbitrary interpretations, not just filtering;
renamed Text.Range.intersect to Text.Range.restrict -- emphasize that it is not directly related to contains/overlaps;
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@38426
    20
  sealed case class Range(val start: Offset, val stop: Offset)
wenzelm@38427
    21
  {
wenzelm@38477
    22
    require(start <= stop)
wenzelm@38477
    23
wenzelm@38563
    24
    override def toString = "[" + start.toString + ":" + stop.toString + "]"
wenzelm@38563
    25
wenzelm@38427
    26
    def map(f: Offset => Offset): Range = Range(f(start), f(stop))
wenzelm@38427
    27
    def +(i: Offset): Range = map(_ + i)
wenzelm@38477
    28
    def contains(i: Offset): Boolean = start <= i && i < stop
wenzelm@38485
    29
    def contains(that: Range): Boolean =
wenzelm@38485
    30
      this == that || this.contains(that.start) && that.stop <= this.stop
wenzelm@38485
    31
    def overlaps(that: Range): Boolean =
wenzelm@38485
    32
      this == that || this.contains(that.start) || that.contains(this.start)
wenzelm@38477
    33
    def compare(that: Range): Int =
wenzelm@38477
    34
      if (overlaps(that)) 0
wenzelm@38477
    35
      else if (this.start < that.start) -1
wenzelm@38477
    36
      else 1
wenzelm@38485
    37
wenzelm@38485
    38
    def start_range: Range = Range(start, start)
wenzelm@38485
    39
    def stop_range: Range = Range(stop, stop)
wenzelm@38485
    40
wenzelm@38564
    41
    def restrict(that: Range): Range =
wenzelm@38485
    42
      Range(this.start max that.start, this.stop min that.stop)
wenzelm@38427
    43
  }
wenzelm@38426
    44
wenzelm@38426
    45
wenzelm@38426
    46
  /* editing */
wenzelm@34286
    47
wenzelm@38425
    48
  object Edit
wenzelm@38425
    49
  {
wenzelm@38426
    50
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
    51
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
    52
  }
wenzelm@34286
    53
wenzelm@38426
    54
  class Edit(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
    55
  {
wenzelm@38425
    56
    override def toString =
wenzelm@38425
    57
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
    58
wenzelm@34286
    59
wenzelm@38425
    60
    /* transform offsets */
wenzelm@34286
    61
wenzelm@38426
    62
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
    63
      if (i < start) i
wenzelm@38426
    64
      else if (is_insert == do_insert) i + text.length
wenzelm@38426
    65
      else (i - text.length) max start
wenzelm@34286
    66
wenzelm@38426
    67
    def convert(i: Offset): Offset = transform(true, i)
wenzelm@38426
    68
    def revert(i: Offset): Offset = transform(false, i)
wenzelm@38425
    69
wenzelm@34286
    70
wenzelm@38425
    71
    /* edit strings */
wenzelm@38425
    72
wenzelm@38426
    73
    private def insert(i: Offset, string: String): String =
wenzelm@38426
    74
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
    75
wenzelm@38426
    76
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
    77
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
    78
wenzelm@38425
    79
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
    80
      shift <= start && start < shift + string.length
wenzelm@38425
    81
wenzelm@38425
    82
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
    83
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
    84
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
    85
      else {
wenzelm@38426
    86
        val i = start - shift
wenzelm@38426
    87
        val count = text.length min (string.length - i)
wenzelm@38425
    88
        val rest =
wenzelm@38425
    89
          if (count == text.length) None
wenzelm@38425
    90
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
    91
        (rest, remove(i, count, string))
wenzelm@38425
    92
      }
wenzelm@38425
    93
  }
wenzelm@34276
    94
}