src/Pure/PIDE/text.scala
author wenzelm
Mon Mar 06 17:10:37 2017 +0100 (2017-03-06 ago)
changeset 65132 60e7072b8dbe
parent 64816 e306cab8edf9
child 65154 ba1929b749f0
permissions -rw-r--r--
tuned;
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.util.Sorting
wenzelm@44379
    13
wenzelm@44379
    14
wenzelm@38425
    15
object Text
wenzelm@34276
    16
{
wenzelm@38477
    17
  /* offset */
wenzelm@38426
    18
wenzelm@38426
    19
  type Offset = Int
wenzelm@38426
    20
wenzelm@38477
    21
wenzelm@38565
    22
  /* range -- with total quasi-ordering */
wenzelm@38477
    23
wenzelm@38568
    24
  object Range
wenzelm@38568
    25
  {
wenzelm@38568
    26
    def apply(start: Offset): Range = Range(start, start)
wenzelm@44379
    27
wenzelm@64678
    28
    val full: Range = apply(0, Integer.MAX_VALUE / 2)
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@60215
    37
  sealed case class Range(start: Offset, stop: Offset)
wenzelm@38427
    38
  {
wenzelm@38565
    39
    // denotation: {start} Un {i. start < i & i < stop}
wenzelm@43425
    40
    if (start > stop)
wenzelm@64546
    41
      error("Bad range: [" + start.toString + ".." + stop.toString + "]")
wenzelm@38477
    42
wenzelm@64546
    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@58749
    54
    def touches(i: Offset): Boolean = start <= i && i <= stop
wenzelm@58749
    55
wenzelm@38565
    56
    def contains(i: Offset): Boolean = start == i || start < i && i < stop
wenzelm@38565
    57
    def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
wenzelm@38565
    58
    def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
wenzelm@38565
    59
    def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
wenzelm@38485
    60
wenzelm@45240
    61
    def apart(that: Range): Boolean =
wenzelm@45240
    62
      (this.start max that.start) > (this.stop min that.stop)
wenzelm@45240
    63
wenzelm@38564
    64
    def restrict(that: Range): Range =
wenzelm@38485
    65
      Range(this.start max that.start, this.stop min that.stop)
wenzelm@43428
    66
wenzelm@43428
    67
    def try_restrict(that: Range): Option[Range] =
wenzelm@45240
    68
      if (this apart that) None
wenzelm@45240
    69
      else Some(restrict(that))
wenzelm@45240
    70
wenzelm@45240
    71
    def try_join(that: Range): Option[Range] =
wenzelm@45240
    72
      if (this apart that) None
wenzelm@45240
    73
      else Some(Range(this.start min that.start, this.stop max that.stop))
wenzelm@38427
    74
  }
wenzelm@38426
    75
wenzelm@38426
    76
wenzelm@44379
    77
  /* perspective */
wenzelm@44379
    78
wenzelm@44473
    79
  object Perspective
wenzelm@44379
    80
  {
wenzelm@44474
    81
    val empty: Perspective = Perspective(Nil)
wenzelm@44379
    82
wenzelm@64678
    83
    def full: Perspective = Perspective(List(Range.full))
wenzelm@46576
    84
wenzelm@44473
    85
    def apply(ranges: Seq[Range]): Perspective =
wenzelm@44379
    86
    {
wenzelm@44473
    87
      val result = new mutable.ListBuffer[Text.Range]
wenzelm@44473
    88
      var last: Option[Text.Range] = None
wenzelm@45240
    89
      def ship(next: Option[Range]) { result ++= last; last = next }
wenzelm@45240
    90
wenzelm@45240
    91
      for (range <- ranges.sortBy(_.start))
wenzelm@44473
    92
      {
wenzelm@44473
    93
        last match {
wenzelm@45240
    94
          case None => ship(Some(range))
wenzelm@45240
    95
          case Some(last_range) =>
wenzelm@45240
    96
            last_range.try_join(range) match {
wenzelm@45240
    97
              case None => ship(Some(range))
wenzelm@45240
    98
              case joined => last = joined
wenzelm@45240
    99
            }
wenzelm@44473
   100
        }
wenzelm@44379
   101
      }
wenzelm@45240
   102
      ship(None)
wenzelm@44473
   103
      new Perspective(result.toList)
wenzelm@44379
   104
    }
wenzelm@44473
   105
  }
wenzelm@44473
   106
wenzelm@46712
   107
  final class Perspective private(
wenzelm@46712
   108
    val ranges: List[Range]) // visible text partitioning in canonical order
wenzelm@44473
   109
  {
wenzelm@44473
   110
    def is_empty: Boolean = ranges.isEmpty
wenzelm@44473
   111
    def range: Range =
wenzelm@44473
   112
      if (is_empty) Range(0)
wenzelm@44473
   113
      else Range(ranges.head.start, ranges.last.stop)
wenzelm@45631
   114
wenzelm@45631
   115
    override def hashCode: Int = ranges.hashCode
wenzelm@45631
   116
    override def equals(that: Any): Boolean =
wenzelm@45631
   117
      that match {
wenzelm@45631
   118
        case other: Perspective => ranges == other.ranges
wenzelm@45631
   119
        case _ => false
wenzelm@45631
   120
      }
wenzelm@57912
   121
    override def toString: String = ranges.toString
wenzelm@44379
   122
  }
wenzelm@44379
   123
wenzelm@44379
   124
wenzelm@38577
   125
  /* information associated with text range */
wenzelm@38577
   126
wenzelm@60215
   127
  sealed case class Info[A](range: Text.Range, info: A)
wenzelm@38577
   128
  {
wenzelm@38577
   129
    def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
wenzelm@46207
   130
    def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
wenzelm@65132
   131
wenzelm@65132
   132
    def map[B](f: A => B): Info[B] = Info(range, f(info))
wenzelm@38577
   133
  }
wenzelm@38577
   134
wenzelm@45470
   135
  type Markup = Info[XML.Elem]
wenzelm@45455
   136
wenzelm@38577
   137
wenzelm@38426
   138
  /* editing */
wenzelm@34286
   139
wenzelm@38425
   140
  object Edit
wenzelm@38425
   141
  {
wenzelm@38426
   142
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
   143
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@64816
   144
    def replace(start: Offset, old_text: String, new_text: String): List[Edit] =
wenzelm@64816
   145
      if (old_text == new_text) Nil
wenzelm@64816
   146
      else if (old_text == "") List(insert(start, new_text))
wenzelm@64816
   147
      else List(remove(start, old_text), insert(start, new_text))
wenzelm@38425
   148
  }
wenzelm@34286
   149
wenzelm@46712
   150
  final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
   151
  {
wenzelm@57912
   152
    override def toString: String =
wenzelm@38425
   153
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
   154
wenzelm@34286
   155
wenzelm@38425
   156
    /* transform offsets */
wenzelm@34286
   157
wenzelm@38426
   158
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
   159
      if (i < start) i
wenzelm@43425
   160
      else if (do_insert) i + text.length
wenzelm@38426
   161
      else (i - text.length) max start
wenzelm@34286
   162
wenzelm@43425
   163
    def convert(i: Offset): Offset = transform(is_insert, i)
wenzelm@43425
   164
    def revert(i: Offset): Offset = transform(!is_insert, i)
wenzelm@38425
   165
wenzelm@34286
   166
wenzelm@38425
   167
    /* edit strings */
wenzelm@38425
   168
wenzelm@38426
   169
    private def insert(i: Offset, string: String): String =
wenzelm@38426
   170
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
   171
wenzelm@38426
   172
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
   173
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
   174
wenzelm@38425
   175
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
   176
      shift <= start && start < shift + string.length
wenzelm@38425
   177
wenzelm@38425
   178
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
   179
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
   180
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
   181
      else {
wenzelm@38426
   182
        val i = start - shift
wenzelm@38426
   183
        val count = text.length min (string.length - i)
wenzelm@38425
   184
        val rest =
wenzelm@38425
   185
          if (count == text.length) None
wenzelm@38425
   186
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
   187
        (rest, remove(i, count, string))
wenzelm@38425
   188
      }
wenzelm@38425
   189
  }
wenzelm@64682
   190
wenzelm@64682
   191
wenzelm@64682
   192
  /* text length wrt. encoding */
wenzelm@64682
   193
wenzelm@64682
   194
  trait Length
wenzelm@64682
   195
  {
wenzelm@64682
   196
    def apply(text: String): Int
wenzelm@64682
   197
    def offset(text: String, i: Int): Option[Text.Offset]
wenzelm@64682
   198
  }
wenzelm@64682
   199
wenzelm@64682
   200
  object Length extends Length
wenzelm@64682
   201
  {
wenzelm@64682
   202
    def apply(text: String): Int = text.length
wenzelm@64682
   203
    def offset(text: String, i: Int): Option[Text.Offset] =
wenzelm@64682
   204
      if (0 <= i && i <= text.length) Some(i) else None
wenzelm@64682
   205
wenzelm@64682
   206
    val encodings: List[(String, Length)] =
wenzelm@64682
   207
      List(
wenzelm@64682
   208
        "UTF-16" -> Length,
wenzelm@64682
   209
        "UTF-8" -> UTF8.Length,
wenzelm@64682
   210
        "codepoints" -> Codepoint.Length,
wenzelm@64682
   211
        "symbols" -> Symbol.Length)
wenzelm@64682
   212
wenzelm@64682
   213
    def encoding(name: String): Length =
wenzelm@64682
   214
      encodings.collectFirst({ case (a, length) if name == a => length }) getOrElse
wenzelm@64682
   215
        error("Bad text length encoding: " + quote(name) +
wenzelm@64682
   216
          " (expected " + commas_quote(encodings.map(_._1)) + ")")
wenzelm@64682
   217
  }
wenzelm@34276
   218
}