src/Pure/PIDE/text.scala
author wenzelm
Tue Apr 08 20:03:00 2014 +0200 (2014-04-08 ago)
changeset 56473 5b5c750e9763
parent 56470 8eda56033203
child 56590 d01d183e84ea
permissions -rw-r--r--
simplified Text.Chunk -- eliminated ooddities;
afford strict symbol_index, which is usually empty anyway;
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@38563
    43
    override def toString = "[" + 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@38662
    52
wenzelm@38565
    53
    def contains(i: Offset): Boolean = start == i || start < i && i < stop
wenzelm@38565
    54
    def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
wenzelm@38565
    55
    def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
wenzelm@38565
    56
    def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
wenzelm@38485
    57
wenzelm@45240
    58
    def apart(that: Range): Boolean =
wenzelm@45240
    59
      (this.start max that.start) > (this.stop min that.stop)
wenzelm@45240
    60
wenzelm@38564
    61
    def restrict(that: Range): Range =
wenzelm@38485
    62
      Range(this.start max that.start, this.stop min that.stop)
wenzelm@43428
    63
wenzelm@43428
    64
    def try_restrict(that: Range): Option[Range] =
wenzelm@45240
    65
      if (this apart that) None
wenzelm@45240
    66
      else Some(restrict(that))
wenzelm@45240
    67
wenzelm@45240
    68
    def try_join(that: Range): Option[Range] =
wenzelm@45240
    69
      if (this apart that) None
wenzelm@45240
    70
      else Some(Range(this.start min that.start, this.stop max that.stop))
wenzelm@38427
    71
  }
wenzelm@38426
    72
wenzelm@38426
    73
wenzelm@56473
    74
  /* named chunks with sparse symbol index */
wenzelm@56468
    75
wenzelm@56473
    76
  object Chunk
wenzelm@56468
    77
  {
wenzelm@56473
    78
    sealed abstract class Name
wenzelm@56473
    79
    case object Default extends Name
wenzelm@56473
    80
    case class Id(id: Document_ID.Generic) extends Name
wenzelm@56473
    81
    case class File(name: String) extends Name
wenzelm@56468
    82
wenzelm@56473
    83
    def apply(text: CharSequence): Chunk =
wenzelm@56473
    84
      new Chunk(Range(0, text.length), Symbol.Index(text))
wenzelm@56473
    85
  }
wenzelm@56473
    86
wenzelm@56473
    87
  final class Chunk private(val range: Range, private val symbol_index: Symbol.Index)
wenzelm@56473
    88
  {
wenzelm@56473
    89
    override def hashCode: Int = (range, symbol_index).hashCode
wenzelm@56468
    90
    override def equals(that: Any): Boolean =
wenzelm@56468
    91
      that match {
wenzelm@56468
    92
        case other: Chunk =>
wenzelm@56468
    93
          range == other.range &&
wenzelm@56468
    94
          symbol_index == other.symbol_index
wenzelm@56468
    95
        case _ => false
wenzelm@56468
    96
      }
wenzelm@56468
    97
wenzelm@56468
    98
    def decode(symbol_offset: Symbol.Offset): Offset = symbol_index.decode(symbol_offset)
wenzelm@56468
    99
    def decode(symbol_range: Symbol.Range): Range = symbol_index.decode(symbol_range)
wenzelm@56468
   100
    def incorporate(symbol_range: Symbol.Range): Option[Range] =
wenzelm@56468
   101
    {
wenzelm@56468
   102
      def in(r: Symbol.Range): Option[Range] =
wenzelm@56468
   103
        range.try_restrict(decode(r)) match {
wenzelm@56468
   104
          case Some(r1) if !r1.is_singularity => Some(r1)
wenzelm@56468
   105
          case _ => None
wenzelm@56468
   106
        }
wenzelm@56468
   107
     in(symbol_range) orElse in(symbol_range - 1)
wenzelm@56468
   108
    }
wenzelm@56468
   109
  }
wenzelm@56468
   110
wenzelm@56468
   111
wenzelm@44379
   112
  /* perspective */
wenzelm@44379
   113
wenzelm@44473
   114
  object Perspective
wenzelm@44379
   115
  {
wenzelm@44474
   116
    val empty: Perspective = Perspective(Nil)
wenzelm@44379
   117
wenzelm@46576
   118
    def full: Perspective = Perspective(List(Range(0, Integer.MAX_VALUE / 2)))
wenzelm@46576
   119
wenzelm@44473
   120
    def apply(ranges: Seq[Range]): Perspective =
wenzelm@44379
   121
    {
wenzelm@44473
   122
      val result = new mutable.ListBuffer[Text.Range]
wenzelm@44473
   123
      var last: Option[Text.Range] = None
wenzelm@45240
   124
      def ship(next: Option[Range]) { result ++= last; last = next }
wenzelm@45240
   125
wenzelm@45240
   126
      for (range <- ranges.sortBy(_.start))
wenzelm@44473
   127
      {
wenzelm@44473
   128
        last match {
wenzelm@45240
   129
          case None => ship(Some(range))
wenzelm@45240
   130
          case Some(last_range) =>
wenzelm@45240
   131
            last_range.try_join(range) match {
wenzelm@45240
   132
              case None => ship(Some(range))
wenzelm@45240
   133
              case joined => last = joined
wenzelm@45240
   134
            }
wenzelm@44473
   135
        }
wenzelm@44379
   136
      }
wenzelm@45240
   137
      ship(None)
wenzelm@44473
   138
      new Perspective(result.toList)
wenzelm@44379
   139
    }
wenzelm@44473
   140
  }
wenzelm@44473
   141
wenzelm@46712
   142
  final class Perspective private(
wenzelm@46712
   143
    val ranges: List[Range]) // visible text partitioning in canonical order
wenzelm@44473
   144
  {
wenzelm@44473
   145
    def is_empty: Boolean = ranges.isEmpty
wenzelm@44473
   146
    def range: Range =
wenzelm@44473
   147
      if (is_empty) Range(0)
wenzelm@44473
   148
      else Range(ranges.head.start, ranges.last.stop)
wenzelm@45631
   149
wenzelm@45631
   150
    override def hashCode: Int = ranges.hashCode
wenzelm@45631
   151
    override def equals(that: Any): Boolean =
wenzelm@45631
   152
      that match {
wenzelm@45631
   153
        case other: Perspective => ranges == other.ranges
wenzelm@45631
   154
        case _ => false
wenzelm@45631
   155
      }
wenzelm@45240
   156
    override def toString = ranges.toString
wenzelm@44379
   157
  }
wenzelm@44379
   158
wenzelm@44379
   159
wenzelm@38577
   160
  /* information associated with text range */
wenzelm@38577
   161
wenzelm@43714
   162
  sealed case class Info[A](val range: Text.Range, val info: A)
wenzelm@38577
   163
  {
wenzelm@38577
   164
    def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
wenzelm@46207
   165
    def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
wenzelm@38577
   166
  }
wenzelm@38577
   167
wenzelm@45470
   168
  type Markup = Info[XML.Elem]
wenzelm@45455
   169
wenzelm@38577
   170
wenzelm@38426
   171
  /* editing */
wenzelm@34286
   172
wenzelm@38425
   173
  object Edit
wenzelm@38425
   174
  {
wenzelm@38426
   175
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
   176
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
   177
  }
wenzelm@34286
   178
wenzelm@46712
   179
  final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
   180
  {
wenzelm@38425
   181
    override def toString =
wenzelm@38425
   182
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
   183
wenzelm@34286
   184
wenzelm@38425
   185
    /* transform offsets */
wenzelm@34286
   186
wenzelm@38426
   187
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
   188
      if (i < start) i
wenzelm@43425
   189
      else if (do_insert) i + text.length
wenzelm@38426
   190
      else (i - text.length) max start
wenzelm@34286
   191
wenzelm@43425
   192
    def convert(i: Offset): Offset = transform(is_insert, i)
wenzelm@43425
   193
    def revert(i: Offset): Offset = transform(!is_insert, i)
wenzelm@43425
   194
    def convert(range: Range): Range = range.map(convert)
wenzelm@43425
   195
    def revert(range: Range): Range = range.map(revert)
wenzelm@38425
   196
wenzelm@34286
   197
wenzelm@38425
   198
    /* edit strings */
wenzelm@38425
   199
wenzelm@38426
   200
    private def insert(i: Offset, string: String): String =
wenzelm@38426
   201
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
   202
wenzelm@38426
   203
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
   204
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
   205
wenzelm@38425
   206
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
   207
      shift <= start && start < shift + string.length
wenzelm@38425
   208
wenzelm@38425
   209
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
   210
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
   211
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
   212
      else {
wenzelm@38426
   213
        val i = start - shift
wenzelm@38426
   214
        val count = text.length min (string.length - i)
wenzelm@38425
   215
        val rest =
wenzelm@38425
   216
          if (count == text.length) None
wenzelm@38425
   217
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
   218
        (rest, remove(i, count, string))
wenzelm@38425
   219
      }
wenzelm@38425
   220
  }
wenzelm@34276
   221
}