src/Pure/PIDE/text.scala
author wenzelm
Tue Aug 12 15:46:20 2014 +0200 (2014-08-12 ago)
changeset 57912 dd9550f84106
parent 57620 c30ab960875e
child 58749 83b0f633190e
permissions -rw-r--r--
tuned;
wenzelm@38425
     1
/*  Title:      Pure/PIDE/text.scala
wenzelm@45673
     2
    Module:     PIDE
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.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@56172
    29
    val offside: Range = apply(-1)
wenzelm@56172
    30
wenzelm@44379
    31
    object Ordering extends scala.math.Ordering[Text.Range]
wenzelm@44379
    32
    {
wenzelm@44379
    33
      def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
wenzelm@44379
    34
    }
wenzelm@38568
    35
  }
wenzelm@38568
    36
wenzelm@38426
    37
  sealed case class Range(val start: Offset, val stop: Offset)
wenzelm@38427
    38
  {
wenzelm@38565
    39
    // denotation: {start} Un {i. start < i & i < stop}
wenzelm@43425
    40
    if (start > stop)
wenzelm@43425
    41
      error("Bad range: [" + start.toString + ":" + stop.toString + "]")
wenzelm@38477
    42
wenzelm@57912
    43
    override def toString: String = "[" + start.toString + ":" + stop.toString + "]"
wenzelm@38563
    44
wenzelm@47542
    45
    def length: Int = stop - start
wenzelm@47542
    46
wenzelm@38427
    47
    def map(f: Offset => Offset): Range = Range(f(start), f(stop))
wenzelm@56308
    48
    def +(i: Offset): Range = if (i == 0) this else map(_ + i)
wenzelm@56308
    49
    def -(i: Offset): Range = if (i == 0) this else map(_ - i)
wenzelm@38662
    50
wenzelm@38725
    51
    def is_singularity: Boolean = start == stop
wenzelm@56590
    52
    def inflate_singularity: Range = if (is_singularity) Range(start, start + 1) else this
wenzelm@38662
    53
wenzelm@38565
    54
    def contains(i: Offset): Boolean = start == i || start < i && i < stop
wenzelm@38565
    55
    def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
wenzelm@38565
    56
    def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
wenzelm@38565
    57
    def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
wenzelm@38485
    58
wenzelm@45240
    59
    def apart(that: Range): Boolean =
wenzelm@45240
    60
      (this.start max that.start) > (this.stop min that.stop)
wenzelm@45240
    61
wenzelm@38564
    62
    def restrict(that: Range): Range =
wenzelm@38485
    63
      Range(this.start max that.start, this.stop min that.stop)
wenzelm@43428
    64
wenzelm@43428
    65
    def try_restrict(that: Range): Option[Range] =
wenzelm@45240
    66
      if (this apart that) None
wenzelm@45240
    67
      else Some(restrict(that))
wenzelm@45240
    68
wenzelm@45240
    69
    def try_join(that: Range): Option[Range] =
wenzelm@45240
    70
      if (this apart that) None
wenzelm@45240
    71
      else Some(Range(this.start min that.start, this.stop max that.stop))
wenzelm@38427
    72
  }
wenzelm@38426
    73
wenzelm@38426
    74
wenzelm@44379
    75
  /* perspective */
wenzelm@44379
    76
wenzelm@44473
    77
  object Perspective
wenzelm@44379
    78
  {
wenzelm@44474
    79
    val empty: Perspective = Perspective(Nil)
wenzelm@44379
    80
wenzelm@46576
    81
    def full: Perspective = Perspective(List(Range(0, Integer.MAX_VALUE / 2)))
wenzelm@46576
    82
wenzelm@44473
    83
    def apply(ranges: Seq[Range]): Perspective =
wenzelm@44379
    84
    {
wenzelm@44473
    85
      val result = new mutable.ListBuffer[Text.Range]
wenzelm@44473
    86
      var last: Option[Text.Range] = None
wenzelm@45240
    87
      def ship(next: Option[Range]) { result ++= last; last = next }
wenzelm@45240
    88
wenzelm@45240
    89
      for (range <- ranges.sortBy(_.start))
wenzelm@44473
    90
      {
wenzelm@44473
    91
        last match {
wenzelm@45240
    92
          case None => ship(Some(range))
wenzelm@45240
    93
          case Some(last_range) =>
wenzelm@45240
    94
            last_range.try_join(range) match {
wenzelm@45240
    95
              case None => ship(Some(range))
wenzelm@45240
    96
              case joined => last = joined
wenzelm@45240
    97
            }
wenzelm@44473
    98
        }
wenzelm@44379
    99
      }
wenzelm@45240
   100
      ship(None)
wenzelm@44473
   101
      new Perspective(result.toList)
wenzelm@44379
   102
    }
wenzelm@44473
   103
  }
wenzelm@44473
   104
wenzelm@46712
   105
  final class Perspective private(
wenzelm@46712
   106
    val ranges: List[Range]) // visible text partitioning in canonical order
wenzelm@44473
   107
  {
wenzelm@44473
   108
    def is_empty: Boolean = ranges.isEmpty
wenzelm@44473
   109
    def range: Range =
wenzelm@44473
   110
      if (is_empty) Range(0)
wenzelm@44473
   111
      else Range(ranges.head.start, ranges.last.stop)
wenzelm@45631
   112
wenzelm@45631
   113
    override def hashCode: Int = ranges.hashCode
wenzelm@45631
   114
    override def equals(that: Any): Boolean =
wenzelm@45631
   115
      that match {
wenzelm@45631
   116
        case other: Perspective => ranges == other.ranges
wenzelm@45631
   117
        case _ => false
wenzelm@45631
   118
      }
wenzelm@57912
   119
    override def toString: String = ranges.toString
wenzelm@44379
   120
  }
wenzelm@44379
   121
wenzelm@44379
   122
wenzelm@38577
   123
  /* information associated with text range */
wenzelm@38577
   124
wenzelm@43714
   125
  sealed case class Info[A](val range: Text.Range, val info: A)
wenzelm@38577
   126
  {
wenzelm@38577
   127
    def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
wenzelm@46207
   128
    def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
wenzelm@38577
   129
  }
wenzelm@38577
   130
wenzelm@45470
   131
  type Markup = Info[XML.Elem]
wenzelm@45455
   132
wenzelm@38577
   133
wenzelm@38426
   134
  /* editing */
wenzelm@34286
   135
wenzelm@38425
   136
  object Edit
wenzelm@38425
   137
  {
wenzelm@38426
   138
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
   139
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
   140
  }
wenzelm@34286
   141
wenzelm@46712
   142
  final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
   143
  {
wenzelm@57912
   144
    override def toString: String =
wenzelm@38425
   145
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
   146
wenzelm@34286
   147
wenzelm@38425
   148
    /* transform offsets */
wenzelm@34286
   149
wenzelm@38426
   150
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
   151
      if (i < start) i
wenzelm@43425
   152
      else if (do_insert) i + text.length
wenzelm@38426
   153
      else (i - text.length) max start
wenzelm@34286
   154
wenzelm@43425
   155
    def convert(i: Offset): Offset = transform(is_insert, i)
wenzelm@43425
   156
    def revert(i: Offset): Offset = transform(!is_insert, i)
wenzelm@38425
   157
wenzelm@34286
   158
wenzelm@38425
   159
    /* edit strings */
wenzelm@38425
   160
wenzelm@38426
   161
    private def insert(i: Offset, string: String): String =
wenzelm@38426
   162
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
   163
wenzelm@38426
   164
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
   165
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
   166
wenzelm@38425
   167
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
   168
      shift <= start && start < shift + string.length
wenzelm@38425
   169
wenzelm@38425
   170
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
   171
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
   172
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
   173
      else {
wenzelm@38426
   174
        val i = start - shift
wenzelm@38426
   175
        val count = text.length min (string.length - i)
wenzelm@38425
   176
        val rest =
wenzelm@38425
   177
          if (count == text.length) None
wenzelm@38425
   178
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
   179
        (rest, remove(i, count, string))
wenzelm@38425
   180
      }
wenzelm@38425
   181
  }
wenzelm@34276
   182
}