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