src/Pure/PIDE/text.scala
author wenzelm
Fri Nov 11 14:24:38 2011 +0100 (2011-11-11)
changeset 45455 4f974c0c5c2f
parent 45250 feef63bcd787
child 45470 81b85d4ed269
permissions -rw-r--r--
prefer statically typed Text.Markup;
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.math.Ordering
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@44379
    29
    object Ordering extends scala.math.Ordering[Text.Range]
wenzelm@44379
    30
    {
wenzelm@44379
    31
      def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
wenzelm@44379
    32
    }
wenzelm@38568
    33
  }
wenzelm@38568
    34
wenzelm@38426
    35
  sealed case class Range(val start: Offset, val stop: Offset)
wenzelm@38427
    36
  {
wenzelm@38565
    37
    // denotation: {start} Un {i. start < i & i < stop}
wenzelm@43425
    38
    if (start > stop)
wenzelm@43425
    39
      error("Bad range: [" + start.toString + ":" + stop.toString + "]")
wenzelm@38477
    40
wenzelm@38563
    41
    override def toString = "[" + start.toString + ":" + stop.toString + "]"
wenzelm@38563
    42
wenzelm@38427
    43
    def map(f: Offset => Offset): Range = Range(f(start), f(stop))
wenzelm@38427
    44
    def +(i: Offset): Range = map(_ + i)
wenzelm@38570
    45
    def -(i: Offset): Range = map(_ - i)
wenzelm@38662
    46
wenzelm@38725
    47
    def is_singularity: Boolean = start == stop
wenzelm@38662
    48
wenzelm@38565
    49
    def contains(i: Offset): Boolean = start == i || start < i && i < stop
wenzelm@38565
    50
    def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
wenzelm@38565
    51
    def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
wenzelm@38565
    52
    def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
wenzelm@38485
    53
wenzelm@45240
    54
    def apart(that: Range): Boolean =
wenzelm@45240
    55
      (this.start max that.start) > (this.stop min that.stop)
wenzelm@45240
    56
wenzelm@38564
    57
    def restrict(that: Range): Range =
wenzelm@38485
    58
      Range(this.start max that.start, this.stop min that.stop)
wenzelm@43428
    59
wenzelm@43428
    60
    def try_restrict(that: Range): Option[Range] =
wenzelm@45240
    61
      if (this apart that) None
wenzelm@45240
    62
      else Some(restrict(that))
wenzelm@45240
    63
wenzelm@45240
    64
    def try_join(that: Range): Option[Range] =
wenzelm@45240
    65
      if (this apart that) None
wenzelm@45240
    66
      else Some(Range(this.start min that.start, this.stop max that.stop))
wenzelm@38427
    67
  }
wenzelm@38426
    68
wenzelm@38426
    69
wenzelm@44379
    70
  /* perspective */
wenzelm@44379
    71
wenzelm@44473
    72
  object Perspective
wenzelm@44379
    73
  {
wenzelm@44474
    74
    val empty: Perspective = Perspective(Nil)
wenzelm@44379
    75
wenzelm@44473
    76
    def apply(ranges: Seq[Range]): Perspective =
wenzelm@44379
    77
    {
wenzelm@44473
    78
      val result = new mutable.ListBuffer[Text.Range]
wenzelm@44473
    79
      var last: Option[Text.Range] = None
wenzelm@45240
    80
      def ship(next: Option[Range]) { result ++= last; last = next }
wenzelm@45240
    81
wenzelm@45240
    82
      for (range <- ranges.sortBy(_.start))
wenzelm@44473
    83
      {
wenzelm@44473
    84
        last match {
wenzelm@45240
    85
          case None => ship(Some(range))
wenzelm@45240
    86
          case Some(last_range) =>
wenzelm@45240
    87
            last_range.try_join(range) match {
wenzelm@45240
    88
              case None => ship(Some(range))
wenzelm@45240
    89
              case joined => last = joined
wenzelm@45240
    90
            }
wenzelm@44473
    91
        }
wenzelm@44379
    92
      }
wenzelm@45240
    93
      ship(None)
wenzelm@44473
    94
      new Perspective(result.toList)
wenzelm@44379
    95
    }
wenzelm@44473
    96
  }
wenzelm@44473
    97
wenzelm@45240
    98
  class Perspective private(val ranges: List[Range]) // visible text partitioning in canonical order
wenzelm@44473
    99
  {
wenzelm@44473
   100
    def is_empty: Boolean = ranges.isEmpty
wenzelm@44473
   101
    def range: Range =
wenzelm@44473
   102
      if (is_empty) Range(0)
wenzelm@44473
   103
      else Range(ranges.head.start, ranges.last.stop)
wenzelm@45240
   104
    override def toString = ranges.toString
wenzelm@44379
   105
  }
wenzelm@44379
   106
wenzelm@44379
   107
wenzelm@38577
   108
  /* information associated with text range */
wenzelm@38577
   109
wenzelm@43714
   110
  sealed case class Info[A](val range: Text.Range, val info: A)
wenzelm@38577
   111
  {
wenzelm@38577
   112
    def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
wenzelm@43428
   113
    def try_restrict(r: Text.Range): Option[Info[A]] =
wenzelm@43428
   114
      try { Some(Info(range.restrict(r), info)) }
wenzelm@43650
   115
      catch { case ERROR(_) => None }
wenzelm@38577
   116
  }
wenzelm@38577
   117
wenzelm@45455
   118
  type Markup = Info[XML.Tree]
wenzelm@45455
   119
wenzelm@38577
   120
wenzelm@38426
   121
  /* editing */
wenzelm@34286
   122
wenzelm@38425
   123
  object Edit
wenzelm@38425
   124
  {
wenzelm@38426
   125
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
wenzelm@38426
   126
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
wenzelm@38425
   127
  }
wenzelm@34286
   128
wenzelm@45250
   129
  class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
wenzelm@38425
   130
  {
wenzelm@38425
   131
    override def toString =
wenzelm@38425
   132
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
wenzelm@34286
   133
wenzelm@34286
   134
wenzelm@38425
   135
    /* transform offsets */
wenzelm@34286
   136
wenzelm@38426
   137
    private def transform(do_insert: Boolean, i: Offset): Offset =
wenzelm@38426
   138
      if (i < start) i
wenzelm@43425
   139
      else if (do_insert) i + text.length
wenzelm@38426
   140
      else (i - text.length) max start
wenzelm@34286
   141
wenzelm@43425
   142
    def convert(i: Offset): Offset = transform(is_insert, i)
wenzelm@43425
   143
    def revert(i: Offset): Offset = transform(!is_insert, i)
wenzelm@43425
   144
    def convert(range: Range): Range = range.map(convert)
wenzelm@43425
   145
    def revert(range: Range): Range = range.map(revert)
wenzelm@38425
   146
wenzelm@34286
   147
wenzelm@38425
   148
    /* edit strings */
wenzelm@38425
   149
wenzelm@38426
   150
    private def insert(i: Offset, string: String): String =
wenzelm@38426
   151
      string.substring(0, i) + text + string.substring(i)
wenzelm@34276
   152
wenzelm@38426
   153
    private def remove(i: Offset, count: Int, string: String): String =
wenzelm@38426
   154
      string.substring(0, i) + string.substring(i + count)
wenzelm@38425
   155
wenzelm@38425
   156
    def can_edit(string: String, shift: Int): Boolean =
wenzelm@38425
   157
      shift <= start && start < shift + string.length
wenzelm@38425
   158
wenzelm@38425
   159
    def edit(string: String, shift: Int): (Option[Edit], String) =
wenzelm@38425
   160
      if (!can_edit(string, shift)) (Some(this), string)
wenzelm@38425
   161
      else if (is_insert) (None, insert(start - shift, string))
wenzelm@38425
   162
      else {
wenzelm@38426
   163
        val i = start - shift
wenzelm@38426
   164
        val count = text.length min (string.length - i)
wenzelm@38425
   165
        val rest =
wenzelm@38425
   166
          if (count == text.length) None
wenzelm@38425
   167
          else Some(Edit.remove(start, text.substring(count)))
wenzelm@38426
   168
        (rest, remove(i, count, string))
wenzelm@38425
   169
      }
wenzelm@38425
   170
  }
wenzelm@34276
   171
}