src/Pure/PIDE/text.scala
author wenzelm
Sun Mar 10 14:19:30 2019 +0100 (4 months ago ago)
changeset 70070 be04e9a053a7
parent 66114 c137a9f038a6
permissions -rw-r--r--
markup and document markers for some meta data from "Dublin Core Metadata Element Set";
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@65154
    31
    object Ordering extends scala.math.Ordering[Range]
wenzelm@44379
    32
    {
wenzelm@65154
    33
      def compare(r1: Range, r2: 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@65155
    45
    def length: Offset = 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@65522
    74
wenzelm@65522
    75
    def substring(text: String): String = text.substring(start, stop)
wenzelm@66114
    76
wenzelm@66114
    77
    def try_substring(text: String): Option[String] =
wenzelm@66114
    78
      try { Some(substring(text)) }
wenzelm@66114
    79
      catch { case _: IndexOutOfBoundsException => None }
wenzelm@38427
    80
  }
wenzelm@38426
    81
wenzelm@38426
    82
wenzelm@44379
    83
  /* perspective */
wenzelm@44379
    84
wenzelm@44473
    85
  object Perspective
wenzelm@44379
    86
  {
wenzelm@44474
    87
    val empty: Perspective = Perspective(Nil)
wenzelm@44379
    88
wenzelm@64678
    89
    def full: Perspective = Perspective(List(Range.full))
wenzelm@46576
    90
wenzelm@65374
    91
    def apply(ranges: List[Range]): Perspective =
wenzelm@44379
    92
    {
wenzelm@65154
    93
      val result = new mutable.ListBuffer[Range]
wenzelm@65154
    94
      var last: Option[Range] = None
wenzelm@45240
    95
      def ship(next: Option[Range]) { result ++= last; last = next }
wenzelm@45240
    96
wenzelm@45240
    97
      for (range <- ranges.sortBy(_.start))
wenzelm@44473
    98
      {
wenzelm@44473
    99
        last match {
wenzelm@45240
   100
          case None => ship(Some(range))
wenzelm@45240
   101
          case Some(last_range) =>
wenzelm@45240
   102
            last_range.try_join(range) match {
wenzelm@45240
   103
              case None => ship(Some(range))
wenzelm@45240
   104
              case joined => last = joined
wenzelm@45240
   105
            }
wenzelm@44473
   106
        }
wenzelm@44379
   107
      }
wenzelm@45240
   108
      ship(None)
wenzelm@44473
   109
      new Perspective(result.toList)
wenzelm@44379
   110
    }
wenzelm@44473
   111
  }
wenzelm@44473
   112
wenzelm@46712
   113
  final class Perspective private(
wenzelm@46712
   114
    val ranges: List[Range]) // visible text partitioning in canonical order
wenzelm@44473
   115
  {
wenzelm@44473
   116
    def is_empty: Boolean = ranges.isEmpty
wenzelm@44473
   117
    def range: Range =
wenzelm@44473
   118
      if (is_empty) Range(0)
wenzelm@44473
   119
      else Range(ranges.head.start, ranges.last.stop)
wenzelm@45631
   120
wenzelm@45631
   121
    override def hashCode: Int = ranges.hashCode
wenzelm@45631
   122
    override def equals(that: Any): Boolean =
wenzelm@45631
   123
      that match {
wenzelm@45631
   124
        case other: Perspective => ranges == other.ranges
wenzelm@45631
   125
        case _ => false
wenzelm@45631
   126
      }
wenzelm@57912
   127
    override def toString: String = ranges.toString
wenzelm@44379
   128
  }
wenzelm@44379
   129
wenzelm@44379
   130
wenzelm@38577
   131
  /* information associated with text range */
wenzelm@38577
   132
wenzelm@65154
   133
  sealed case class Info[A](range: Range, info: A)
wenzelm@38577
   134
  {
wenzelm@65154
   135
    def restrict(r: Range): Info[A] = Info(range.restrict(r), info)
wenzelm@65154
   136
    def try_restrict(r: Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
wenzelm@65132
   137
wenzelm@65132
   138
    def map[B](f: A => B): Info[B] = Info(range, f(info))
wenzelm@38577
   139
  }
wenzelm@38577
   140
wenzelm@45470
   141
  type Markup = Info[XML.Elem]
wenzelm@45455
   142
wenzelm@38577
   143
wenzelm@38426
   144
  /* editing */
wenzelm@34286
   145
wenzelm@38425
   146
  object Edit
wenzelm@38425
   147
  {
wenzelm@38426
   148
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
   149
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@65156
   150
    def inserts(start: Offset, text: String): List[Edit] =
wenzelm@65156
   151
      if (text == "") Nil else List(insert(start, text))
wenzelm@65156
   152
    def removes(start: Offset, text: String): List[Edit] =
wenzelm@65156
   153
      if (text == "") Nil else List(remove(start, text))
wenzelm@64816
   154
    def replace(start: Offset, old_text: String, new_text: String): List[Edit] =
wenzelm@64816
   155
      if (old_text == new_text) Nil
wenzelm@65156
   156
      else removes(start, old_text) ::: inserts(start, new_text)
wenzelm@38425
   157
  }
wenzelm@34286
   158
wenzelm@46712
   159
  final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
   160
  {
wenzelm@57912
   161
    override def toString: String =
wenzelm@38425
   162
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
   163
wenzelm@34286
   164
wenzelm@38425
   165
    /* transform offsets */
wenzelm@34286
   166
wenzelm@38426
   167
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
   168
      if (i < start) i
wenzelm@43425
   169
      else if (do_insert) i + text.length
wenzelm@38426
   170
      else (i - text.length) max start
wenzelm@34286
   171
wenzelm@43425
   172
    def convert(i: Offset): Offset = transform(is_insert, i)
wenzelm@43425
   173
    def revert(i: Offset): Offset = transform(!is_insert, i)
wenzelm@38425
   174
wenzelm@34286
   175
wenzelm@38425
   176
    /* edit strings */
wenzelm@38425
   177
wenzelm@38426
   178
    private def insert(i: Offset, string: String): String =
wenzelm@38426
   179
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
   180
wenzelm@65155
   181
    private def remove(i: Offset, count: Offset, string: String): String =
wenzelm@38426
   182
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
   183
wenzelm@65155
   184
    def can_edit(string: String, shift: Offset): Boolean =
wenzelm@38425
   185
      shift <= start && start < shift + string.length
wenzelm@38425
   186
wenzelm@65155
   187
    def edit(string: String, shift: Offset): (Option[Edit], String) =
wenzelm@38425
   188
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
   189
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
   190
      else {
wenzelm@38426
   191
        val i = start - shift
wenzelm@38426
   192
        val count = text.length min (string.length - i)
wenzelm@38425
   193
        val rest =
wenzelm@38425
   194
          if (count == text.length) None
wenzelm@38425
   195
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
   196
        (rest, remove(i, count, string))
wenzelm@38425
   197
      }
wenzelm@38425
   198
  }
wenzelm@34276
   199
}