src/Pure/PIDE/text.scala
author wenzelm
Thu Aug 25 11:27:37 2011 +0200 (2011-08-25)
changeset 44473 4f264fdf8d0e
parent 44384 8f6054a63f96
child 44474 681447a9ffe5
permissions -rw-r--r--
slightly more abstract Text.Perspective;
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@44379
    11
import scala.collection.mutable
wenzelm@44379
    12
import scala.math.Ordering
wenzelm@44379
    13
import scala.util.Sorting
wenzelm@44379
    14
wenzelm@44379
    15
wenzelm@38425
    16
object Text
wenzelm@34276
    17
{
wenzelm@38477
    18
  /* offset */
wenzelm@38426
    19
wenzelm@38426
    20
  type Offset = Int
wenzelm@38426
    21
wenzelm@38477
    22
wenzelm@38565
    23
  /* range -- with total quasi-ordering */
wenzelm@38477
    24
wenzelm@38568
    25
  object Range
wenzelm@38568
    26
  {
wenzelm@38568
    27
    def apply(start: Offset): Range = Range(start, start)
wenzelm@44379
    28
wenzelm@44379
    29
    object Ordering extends scala.math.Ordering[Text.Range]
wenzelm@44379
    30
    {
wenzelm@44379
    31
      def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
wenzelm@44379
    32
    }
wenzelm@38568
    33
  }
wenzelm@38568
    34
wenzelm@38426
    35
  sealed case class Range(val start: Offset, val stop: Offset)
wenzelm@38427
    36
  {
wenzelm@38565
    37
    // denotation: {start} Un {i. start < i & i < stop}
wenzelm@43425
    38
    if (start > stop)
wenzelm@43425
    39
      error("Bad range: [" + start.toString + ":" + stop.toString + "]")
wenzelm@38477
    40
wenzelm@38563
    41
    override def toString = "[" + start.toString + ":" + stop.toString + "]"
wenzelm@38563
    42
wenzelm@38427
    43
    def map(f: Offset => Offset): Range = Range(f(start), f(stop))
wenzelm@38427
    44
    def +(i: Offset): Range = map(_ + i)
wenzelm@38570
    45
    def -(i: Offset): Range = map(_ - i)
wenzelm@38662
    46
wenzelm@38725
    47
    def is_singularity: Boolean = start == stop
wenzelm@38662
    48
wenzelm@38565
    49
    def contains(i: Offset): Boolean = start == i || start < i && i < stop
wenzelm@38565
    50
    def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
wenzelm@38565
    51
    def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
wenzelm@38565
    52
    def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
wenzelm@38485
    53
wenzelm@38564
    54
    def restrict(that: Range): Range =
wenzelm@38485
    55
      Range(this.start max that.start, this.stop min that.stop)
wenzelm@43428
    56
wenzelm@43428
    57
    def try_restrict(that: Range): Option[Range] =
wenzelm@43428
    58
      try { Some (restrict(that)) }
wenzelm@43650
    59
      catch { case ERROR(_) => None }
wenzelm@38427
    60
  }
wenzelm@38426
    61
wenzelm@38426
    62
wenzelm@44379
    63
  /* perspective */
wenzelm@44379
    64
wenzelm@44473
    65
  object Perspective
wenzelm@44379
    66
  {
wenzelm@44473
    67
    val empty = Perspective(Nil)
wenzelm@44379
    68
wenzelm@44473
    69
    def apply(ranges: Seq[Range]): Perspective =
wenzelm@44379
    70
    {
wenzelm@44473
    71
      val sorted_ranges = ranges.toArray
wenzelm@44473
    72
      Sorting.quickSort(sorted_ranges)(Range.Ordering)
wenzelm@44473
    73
wenzelm@44473
    74
      val result = new mutable.ListBuffer[Text.Range]
wenzelm@44473
    75
      var last: Option[Text.Range] = None
wenzelm@44473
    76
      for (range <- sorted_ranges)
wenzelm@44473
    77
      {
wenzelm@44473
    78
        last match {
wenzelm@44473
    79
          case Some(last_range)
wenzelm@44473
    80
          if ((last_range overlaps range) || last_range.stop == range.start) =>
wenzelm@44473
    81
            last = Some(Text.Range(last_range.start, range.stop))
wenzelm@44473
    82
          case _ =>
wenzelm@44473
    83
            result ++= last
wenzelm@44473
    84
            last = Some(range)
wenzelm@44473
    85
        }
wenzelm@44379
    86
      }
wenzelm@44473
    87
      result ++= last
wenzelm@44473
    88
      new Perspective(result.toList)
wenzelm@44379
    89
    }
wenzelm@44473
    90
  }
wenzelm@44473
    91
wenzelm@44473
    92
  sealed case class Perspective(ranges: List[Range]) // visible text partitioning in canonical order
wenzelm@44473
    93
  {
wenzelm@44473
    94
    def is_empty: Boolean = ranges.isEmpty
wenzelm@44473
    95
    def range: Range =
wenzelm@44473
    96
      if (is_empty) Range(0)
wenzelm@44473
    97
      else Range(ranges.head.start, ranges.last.stop)
wenzelm@44379
    98
  }
wenzelm@44379
    99
wenzelm@44379
   100
wenzelm@38577
   101
  /* information associated with text range */
wenzelm@38577
   102
wenzelm@43714
   103
  sealed case class Info[A](val range: Text.Range, val info: A)
wenzelm@38577
   104
  {
wenzelm@38577
   105
    def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
wenzelm@43428
   106
    def try_restrict(r: Text.Range): Option[Info[A]] =
wenzelm@43428
   107
      try { Some(Info(range.restrict(r), info)) }
wenzelm@43650
   108
      catch { case ERROR(_) => None }
wenzelm@38577
   109
  }
wenzelm@38577
   110
wenzelm@38577
   111
wenzelm@38426
   112
  /* editing */
wenzelm@34286
   113
wenzelm@38425
   114
  object Edit
wenzelm@38425
   115
  {
wenzelm@38426
   116
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
   117
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
   118
  }
wenzelm@34286
   119
wenzelm@38426
   120
  class Edit(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
   121
  {
wenzelm@38425
   122
    override def toString =
wenzelm@38425
   123
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
   124
wenzelm@34286
   125
wenzelm@38425
   126
    /* transform offsets */
wenzelm@34286
   127
wenzelm@38426
   128
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
   129
      if (i < start) i
wenzelm@43425
   130
      else if (do_insert) i + text.length
wenzelm@38426
   131
      else (i - text.length) max start
wenzelm@34286
   132
wenzelm@43425
   133
    def convert(i: Offset): Offset = transform(is_insert, i)
wenzelm@43425
   134
    def revert(i: Offset): Offset = transform(!is_insert, i)
wenzelm@43425
   135
    def convert(range: Range): Range = range.map(convert)
wenzelm@43425
   136
    def revert(range: Range): Range = range.map(revert)
wenzelm@38425
   137
wenzelm@34286
   138
wenzelm@38425
   139
    /* edit strings */
wenzelm@38425
   140
wenzelm@38426
   141
    private def insert(i: Offset, string: String): String =
wenzelm@38426
   142
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
   143
wenzelm@38426
   144
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
   145
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
   146
wenzelm@38425
   147
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
   148
      shift <= start && start < shift + string.length
wenzelm@38425
   149
wenzelm@38425
   150
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
   151
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
   152
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
   153
      else {
wenzelm@38426
   154
        val i = start - shift
wenzelm@38426
   155
        val count = text.length min (string.length - i)
wenzelm@38425
   156
        val rest =
wenzelm@38425
   157
          if (count == text.length) None
wenzelm@38425
   158
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
   159
        (rest, remove(i, count, string))
wenzelm@38425
   160
      }
wenzelm@38425
   161
  }
wenzelm@34276
   162
}