src/Pure/PIDE/text.scala
author wenzelm
Mon Nov 28 22:18:19 2011 +0100 (2011-11-28 ago)
changeset 45667 546d78f0d81f
parent 45631 6bdf8b926f50
child 45673 cd41e3903fbf
permissions -rw-r--r--
explicit indication of modules for independent Scala library;
wenzelm@38425
     1
/*  Title:      Pure/PIDE/text.scala
wenzelm@45667
     2
    Module:     Library
wenzelm@34276
     3
    Author:     Fabian Immler, TU Munich
wenzelm@34276
     4
    Author:     Makarius
wenzelm@34276
     5
wenzelm@38425
     6
Basic operations on plain text.
wenzelm@34276
     7
*/
wenzelm@34276
     8
wenzelm@34276
     9
package isabelle
wenzelm@34276
    10
wenzelm@34276
    11
wenzelm@44379
    12
import scala.collection.mutable
wenzelm@44379
    13
import scala.math.Ordering
wenzelm@44379
    14
import scala.util.Sorting
wenzelm@44379
    15
wenzelm@44379
    16
wenzelm@38425
    17
object Text
wenzelm@34276
    18
{
wenzelm@38477
    19
  /* offset */
wenzelm@38426
    20
wenzelm@38426
    21
  type Offset = Int
wenzelm@38426
    22
wenzelm@38477
    23
wenzelm@38565
    24
  /* range -- with total quasi-ordering */
wenzelm@38477
    25
wenzelm@38568
    26
  object Range
wenzelm@38568
    27
  {
wenzelm@38568
    28
    def apply(start: Offset): Range = Range(start, start)
wenzelm@44379
    29
wenzelm@44379
    30
    object Ordering extends scala.math.Ordering[Text.Range]
wenzelm@44379
    31
    {
wenzelm@44379
    32
      def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
wenzelm@44379
    33
    }
wenzelm@38568
    34
  }
wenzelm@38568
    35
wenzelm@38426
    36
  sealed case class Range(val start: Offset, val stop: Offset)
wenzelm@38427
    37
  {
wenzelm@38565
    38
    // denotation: {start} Un {i. start < i & i < stop}
wenzelm@43425
    39
    if (start > stop)
wenzelm@43425
    40
      error("Bad range: [" + start.toString + ":" + stop.toString + "]")
wenzelm@38477
    41
wenzelm@38563
    42
    override def toString = "[" + start.toString + ":" + stop.toString + "]"
wenzelm@38563
    43
wenzelm@38427
    44
    def map(f: Offset => Offset): Range = Range(f(start), f(stop))
wenzelm@38427
    45
    def +(i: Offset): Range = map(_ + i)
wenzelm@38570
    46
    def -(i: Offset): Range = map(_ - i)
wenzelm@38662
    47
wenzelm@38725
    48
    def is_singularity: Boolean = start == stop
wenzelm@38662
    49
wenzelm@38565
    50
    def contains(i: Offset): Boolean = start == i || start < i && i < stop
wenzelm@38565
    51
    def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
wenzelm@38565
    52
    def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
wenzelm@38565
    53
    def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
wenzelm@38485
    54
wenzelm@45240
    55
    def apart(that: Range): Boolean =
wenzelm@45240
    56
      (this.start max that.start) > (this.stop min that.stop)
wenzelm@45240
    57
wenzelm@38564
    58
    def restrict(that: Range): Range =
wenzelm@38485
    59
      Range(this.start max that.start, this.stop min that.stop)
wenzelm@43428
    60
wenzelm@43428
    61
    def try_restrict(that: Range): Option[Range] =
wenzelm@45240
    62
      if (this apart that) None
wenzelm@45240
    63
      else Some(restrict(that))
wenzelm@45240
    64
wenzelm@45240
    65
    def try_join(that: Range): Option[Range] =
wenzelm@45240
    66
      if (this apart that) None
wenzelm@45240
    67
      else Some(Range(this.start min that.start, this.stop max that.stop))
wenzelm@38427
    68
  }
wenzelm@38426
    69
wenzelm@38426
    70
wenzelm@44379
    71
  /* perspective */
wenzelm@44379
    72
wenzelm@44473
    73
  object Perspective
wenzelm@44379
    74
  {
wenzelm@44474
    75
    val empty: Perspective = Perspective(Nil)
wenzelm@44379
    76
wenzelm@44473
    77
    def apply(ranges: Seq[Range]): Perspective =
wenzelm@44379
    78
    {
wenzelm@44473
    79
      val result = new mutable.ListBuffer[Text.Range]
wenzelm@44473
    80
      var last: Option[Text.Range] = None
wenzelm@45240
    81
      def ship(next: Option[Range]) { result ++= last; last = next }
wenzelm@45240
    82
wenzelm@45240
    83
      for (range <- ranges.sortBy(_.start))
wenzelm@44473
    84
      {
wenzelm@44473
    85
        last match {
wenzelm@45240
    86
          case None => ship(Some(range))
wenzelm@45240
    87
          case Some(last_range) =>
wenzelm@45240
    88
            last_range.try_join(range) match {
wenzelm@45240
    89
              case None => ship(Some(range))
wenzelm@45240
    90
              case joined => last = joined
wenzelm@45240
    91
            }
wenzelm@44473
    92
        }
wenzelm@44379
    93
      }
wenzelm@45240
    94
      ship(None)
wenzelm@44473
    95
      new Perspective(result.toList)
wenzelm@44379
    96
    }
wenzelm@44473
    97
  }
wenzelm@44473
    98
wenzelm@45240
    99
  class Perspective private(val ranges: List[Range]) // visible text partitioning in canonical order
wenzelm@44473
   100
  {
wenzelm@44473
   101
    def is_empty: Boolean = ranges.isEmpty
wenzelm@44473
   102
    def range: Range =
wenzelm@44473
   103
      if (is_empty) Range(0)
wenzelm@44473
   104
      else Range(ranges.head.start, ranges.last.stop)
wenzelm@45631
   105
wenzelm@45631
   106
    override def hashCode: Int = ranges.hashCode
wenzelm@45631
   107
    override def equals(that: Any): Boolean =
wenzelm@45631
   108
      that match {
wenzelm@45631
   109
        case other: Perspective => ranges == other.ranges
wenzelm@45631
   110
        case _ => false
wenzelm@45631
   111
      }
wenzelm@45240
   112
    override def toString = ranges.toString
wenzelm@44379
   113
  }
wenzelm@44379
   114
wenzelm@44379
   115
wenzelm@38577
   116
  /* information associated with text range */
wenzelm@38577
   117
wenzelm@43714
   118
  sealed case class Info[A](val range: Text.Range, val info: A)
wenzelm@38577
   119
  {
wenzelm@38577
   120
    def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
wenzelm@43428
   121
    def try_restrict(r: Text.Range): Option[Info[A]] =
wenzelm@43428
   122
      try { Some(Info(range.restrict(r), info)) }
wenzelm@43650
   123
      catch { case ERROR(_) => None }
wenzelm@38577
   124
  }
wenzelm@38577
   125
wenzelm@45470
   126
  type Markup = Info[XML.Elem]
wenzelm@45455
   127
wenzelm@38577
   128
wenzelm@38426
   129
  /* editing */
wenzelm@34286
   130
wenzelm@38425
   131
  object Edit
wenzelm@38425
   132
  {
wenzelm@38426
   133
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
   134
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
   135
  }
wenzelm@34286
   136
wenzelm@45250
   137
  class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
   138
  {
wenzelm@38425
   139
    override def toString =
wenzelm@38425
   140
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
   141
wenzelm@34286
   142
wenzelm@38425
   143
    /* transform offsets */
wenzelm@34286
   144
wenzelm@38426
   145
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
   146
      if (i < start) i
wenzelm@43425
   147
      else if (do_insert) i + text.length
wenzelm@38426
   148
      else (i - text.length) max start
wenzelm@34286
   149
wenzelm@43425
   150
    def convert(i: Offset): Offset = transform(is_insert, i)
wenzelm@43425
   151
    def revert(i: Offset): Offset = transform(!is_insert, i)
wenzelm@43425
   152
    def convert(range: Range): Range = range.map(convert)
wenzelm@43425
   153
    def revert(range: Range): Range = range.map(revert)
wenzelm@38425
   154
wenzelm@34286
   155
wenzelm@38425
   156
    /* edit strings */
wenzelm@38425
   157
wenzelm@38426
   158
    private def insert(i: Offset, string: String): String =
wenzelm@38426
   159
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
   160
wenzelm@38426
   161
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
   162
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
   163
wenzelm@38425
   164
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
   165
      shift <= start && start < shift + string.length
wenzelm@38425
   166
wenzelm@38425
   167
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
   168
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
   169
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
   170
      else {
wenzelm@38426
   171
        val i = start - shift
wenzelm@38426
   172
        val count = text.length min (string.length - i)
wenzelm@38425
   173
        val rest =
wenzelm@38425
   174
          if (count == text.length) None
wenzelm@38425
   175
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
   176
        (rest, remove(i, count, string))
wenzelm@38425
   177
      }
wenzelm@38425
   178
  }
wenzelm@34276
   179
}