src/Pure/PIDE/text.scala
author wenzelm
Tue Apr 08 15:12:54 2014 +0200 (2014-04-08)
changeset 56468 30128d1acfbc
parent 56308 ebd3bf053969
child 56469 ffc94a271633
permissions -rw-r--r--
tuned signature -- moved Command.Chunk to Text.Chunk;
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@56468
    74
  /* named chunks */
wenzelm@56468
    75
wenzelm@56468
    76
  abstract class Chunk
wenzelm@56468
    77
  {
wenzelm@56468
    78
    def name: Chunk.Name
wenzelm@56468
    79
    def range: Range
wenzelm@56468
    80
    def symbol_index: Symbol.Index
wenzelm@56468
    81
wenzelm@56468
    82
    private lazy val hash: Int = (name, range, symbol_index).hashCode
wenzelm@56468
    83
    override def hashCode: Int = hash
wenzelm@56468
    84
    override def equals(that: Any): Boolean =
wenzelm@56468
    85
      that match {
wenzelm@56468
    86
        case other: Chunk =>
wenzelm@56468
    87
          hash == other.hash &&
wenzelm@56468
    88
          name == other.name &&
wenzelm@56468
    89
          range == other.range &&
wenzelm@56468
    90
          symbol_index == other.symbol_index
wenzelm@56468
    91
        case _ => false
wenzelm@56468
    92
      }
wenzelm@56468
    93
    override def toString: String = "Text.Chunk(" + name + ")"
wenzelm@56468
    94
wenzelm@56468
    95
    def decode(symbol_offset: Symbol.Offset): Offset = symbol_index.decode(symbol_offset)
wenzelm@56468
    96
    def decode(symbol_range: Symbol.Range): Range = symbol_index.decode(symbol_range)
wenzelm@56468
    97
    def incorporate(symbol_range: Symbol.Range): Option[Range] =
wenzelm@56468
    98
    {
wenzelm@56468
    99
      def in(r: Symbol.Range): Option[Range] =
wenzelm@56468
   100
        range.try_restrict(decode(r)) match {
wenzelm@56468
   101
          case Some(r1) if !r1.is_singularity => Some(r1)
wenzelm@56468
   102
          case _ => None
wenzelm@56468
   103
        }
wenzelm@56468
   104
     in(symbol_range) orElse in(symbol_range - 1)
wenzelm@56468
   105
    }
wenzelm@56468
   106
  }
wenzelm@56468
   107
wenzelm@56468
   108
  object Chunk
wenzelm@56468
   109
  {
wenzelm@56468
   110
    sealed abstract class Name
wenzelm@56468
   111
    case object Default extends Name
wenzelm@56468
   112
    case class File_Name(file_name: String) extends Name
wenzelm@56468
   113
wenzelm@56468
   114
    class File(file_name: String, text: CharSequence) extends Chunk
wenzelm@56468
   115
    {
wenzelm@56468
   116
      val name = File_Name(file_name)
wenzelm@56468
   117
      val range = Range(0, text.length)
wenzelm@56468
   118
      val symbol_index = Symbol.Index(text)
wenzelm@56468
   119
    }
wenzelm@56468
   120
  }
wenzelm@56468
   121
wenzelm@56468
   122
wenzelm@44379
   123
  /* perspective */
wenzelm@44379
   124
wenzelm@44473
   125
  object Perspective
wenzelm@44379
   126
  {
wenzelm@44474
   127
    val empty: Perspective = Perspective(Nil)
wenzelm@44379
   128
wenzelm@46576
   129
    def full: Perspective = Perspective(List(Range(0, Integer.MAX_VALUE / 2)))
wenzelm@46576
   130
wenzelm@44473
   131
    def apply(ranges: Seq[Range]): Perspective =
wenzelm@44379
   132
    {
wenzelm@44473
   133
      val result = new mutable.ListBuffer[Text.Range]
wenzelm@44473
   134
      var last: Option[Text.Range] = None
wenzelm@45240
   135
      def ship(next: Option[Range]) { result ++= last; last = next }
wenzelm@45240
   136
wenzelm@45240
   137
      for (range <- ranges.sortBy(_.start))
wenzelm@44473
   138
      {
wenzelm@44473
   139
        last match {
wenzelm@45240
   140
          case None => ship(Some(range))
wenzelm@45240
   141
          case Some(last_range) =>
wenzelm@45240
   142
            last_range.try_join(range) match {
wenzelm@45240
   143
              case None => ship(Some(range))
wenzelm@45240
   144
              case joined => last = joined
wenzelm@45240
   145
            }
wenzelm@44473
   146
        }
wenzelm@44379
   147
      }
wenzelm@45240
   148
      ship(None)
wenzelm@44473
   149
      new Perspective(result.toList)
wenzelm@44379
   150
    }
wenzelm@44473
   151
  }
wenzelm@44473
   152
wenzelm@46712
   153
  final class Perspective private(
wenzelm@46712
   154
    val ranges: List[Range]) // visible text partitioning in canonical order
wenzelm@44473
   155
  {
wenzelm@44473
   156
    def is_empty: Boolean = ranges.isEmpty
wenzelm@44473
   157
    def range: Range =
wenzelm@44473
   158
      if (is_empty) Range(0)
wenzelm@44473
   159
      else Range(ranges.head.start, ranges.last.stop)
wenzelm@45631
   160
wenzelm@45631
   161
    override def hashCode: Int = ranges.hashCode
wenzelm@45631
   162
    override def equals(that: Any): Boolean =
wenzelm@45631
   163
      that match {
wenzelm@45631
   164
        case other: Perspective => ranges == other.ranges
wenzelm@45631
   165
        case _ => false
wenzelm@45631
   166
      }
wenzelm@45240
   167
    override def toString = ranges.toString
wenzelm@44379
   168
  }
wenzelm@44379
   169
wenzelm@44379
   170
wenzelm@38577
   171
  /* information associated with text range */
wenzelm@38577
   172
wenzelm@43714
   173
  sealed case class Info[A](val range: Text.Range, val info: A)
wenzelm@38577
   174
  {
wenzelm@38577
   175
    def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
wenzelm@46207
   176
    def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
wenzelm@38577
   177
  }
wenzelm@38577
   178
wenzelm@45470
   179
  type Markup = Info[XML.Elem]
wenzelm@45455
   180
wenzelm@38577
   181
wenzelm@38426
   182
  /* editing */
wenzelm@34286
   183
wenzelm@38425
   184
  object Edit
wenzelm@38425
   185
  {
wenzelm@38426
   186
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
   187
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
   188
  }
wenzelm@34286
   189
wenzelm@46712
   190
  final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
   191
  {
wenzelm@38425
   192
    override def toString =
wenzelm@38425
   193
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
   194
wenzelm@34286
   195
wenzelm@38425
   196
    /* transform offsets */
wenzelm@34286
   197
wenzelm@38426
   198
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
   199
      if (i < start) i
wenzelm@43425
   200
      else if (do_insert) i + text.length
wenzelm@38426
   201
      else (i - text.length) max start
wenzelm@34286
   202
wenzelm@43425
   203
    def convert(i: Offset): Offset = transform(is_insert, i)
wenzelm@43425
   204
    def revert(i: Offset): Offset = transform(!is_insert, i)
wenzelm@43425
   205
    def convert(range: Range): Range = range.map(convert)
wenzelm@43425
   206
    def revert(range: Range): Range = range.map(revert)
wenzelm@38425
   207
wenzelm@34286
   208
wenzelm@38425
   209
    /* edit strings */
wenzelm@38425
   210
wenzelm@38426
   211
    private def insert(i: Offset, string: String): String =
wenzelm@38426
   212
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
   213
wenzelm@38426
   214
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
   215
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
   216
wenzelm@38425
   217
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
   218
      shift <= start && start < shift + string.length
wenzelm@38425
   219
wenzelm@38425
   220
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
   221
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
   222
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
   223
      else {
wenzelm@38426
   224
        val i = start - shift
wenzelm@38426
   225
        val count = text.length min (string.length - i)
wenzelm@38425
   226
        val rest =
wenzelm@38425
   227
          if (count == text.length) None
wenzelm@38425
   228
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
   229
        (rest, remove(i, count, string))
wenzelm@38425
   230
      }
wenzelm@38425
   231
  }
wenzelm@34276
   232
}