src/Pure/PIDE/text.scala
author wenzelm
Tue Apr 15 16:44:06 2014 +0200 (2014-04-15)
changeset 56590 d01d183e84ea
parent 56473 5b5c750e9763
child 56746 d37a5d09a277
permissions -rw-r--r--
clarified treatment of markup ranges wrt. revert/convert: inflate_singularity allows to retrieve information like language_context more reliably during editing;
     1 /*  Title:      Pure/PIDE/text.scala
     2     Module:     PIDE
     3     Author:     Fabian Immler, TU Munich
     4     Author:     Makarius
     5 
     6 Basic operations on plain text.
     7 */
     8 
     9 package isabelle
    10 
    11 
    12 import scala.collection.mutable
    13 import scala.util.Sorting
    14 
    15 
    16 object Text
    17 {
    18   /* offset */
    19 
    20   type Offset = Int
    21 
    22 
    23   /* range -- with total quasi-ordering */
    24 
    25   object Range
    26   {
    27     def apply(start: Offset): Range = Range(start, start)
    28 
    29     val offside: Range = apply(-1)
    30 
    31     object Ordering extends scala.math.Ordering[Text.Range]
    32     {
    33       def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
    34     }
    35   }
    36 
    37   sealed case class Range(val start: Offset, val stop: Offset)
    38   {
    39     // denotation: {start} Un {i. start < i & i < stop}
    40     if (start > stop)
    41       error("Bad range: [" + start.toString + ":" + stop.toString + "]")
    42 
    43     override def toString = "[" + start.toString + ":" + stop.toString + "]"
    44 
    45     def length: Int = stop - start
    46 
    47     def map(f: Offset => Offset): Range = Range(f(start), f(stop))
    48     def +(i: Offset): Range = if (i == 0) this else map(_ + i)
    49     def -(i: Offset): Range = if (i == 0) this else map(_ - i)
    50 
    51     def is_singularity: Boolean = start == stop
    52     def inflate_singularity: Range = if (is_singularity) Range(start, start + 1) else this
    53 
    54     def contains(i: Offset): Boolean = start == i || start < i && i < stop
    55     def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
    56     def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
    57     def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
    58 
    59     def apart(that: Range): Boolean =
    60       (this.start max that.start) > (this.stop min that.stop)
    61 
    62     def restrict(that: Range): Range =
    63       Range(this.start max that.start, this.stop min that.stop)
    64 
    65     def try_restrict(that: Range): Option[Range] =
    66       if (this apart that) None
    67       else Some(restrict(that))
    68 
    69     def try_join(that: Range): Option[Range] =
    70       if (this apart that) None
    71       else Some(Range(this.start min that.start, this.stop max that.stop))
    72   }
    73 
    74 
    75   /* named chunks with sparse symbol index */
    76 
    77   object Chunk
    78   {
    79     sealed abstract class Name
    80     case object Default extends Name
    81     case class Id(id: Document_ID.Generic) extends Name
    82     case class File(name: String) extends Name
    83 
    84     def apply(text: CharSequence): Chunk =
    85       new Chunk(Range(0, text.length), Symbol.Index(text))
    86   }
    87 
    88   final class Chunk private(val range: Range, private val symbol_index: Symbol.Index)
    89   {
    90     override def hashCode: Int = (range, symbol_index).hashCode
    91     override def equals(that: Any): Boolean =
    92       that match {
    93         case other: Chunk =>
    94           range == other.range &&
    95           symbol_index == other.symbol_index
    96         case _ => false
    97       }
    98 
    99     def decode(symbol_offset: Symbol.Offset): Offset = symbol_index.decode(symbol_offset)
   100     def decode(symbol_range: Symbol.Range): Range = symbol_index.decode(symbol_range)
   101     def incorporate(symbol_range: Symbol.Range): Option[Range] =
   102     {
   103       def in(r: Symbol.Range): Option[Range] =
   104         range.try_restrict(decode(r)) match {
   105           case Some(r1) if !r1.is_singularity => Some(r1)
   106           case _ => None
   107         }
   108      in(symbol_range) orElse in(symbol_range - 1)
   109     }
   110   }
   111 
   112 
   113   /* perspective */
   114 
   115   object Perspective
   116   {
   117     val empty: Perspective = Perspective(Nil)
   118 
   119     def full: Perspective = Perspective(List(Range(0, Integer.MAX_VALUE / 2)))
   120 
   121     def apply(ranges: Seq[Range]): Perspective =
   122     {
   123       val result = new mutable.ListBuffer[Text.Range]
   124       var last: Option[Text.Range] = None
   125       def ship(next: Option[Range]) { result ++= last; last = next }
   126 
   127       for (range <- ranges.sortBy(_.start))
   128       {
   129         last match {
   130           case None => ship(Some(range))
   131           case Some(last_range) =>
   132             last_range.try_join(range) match {
   133               case None => ship(Some(range))
   134               case joined => last = joined
   135             }
   136         }
   137       }
   138       ship(None)
   139       new Perspective(result.toList)
   140     }
   141   }
   142 
   143   final class Perspective private(
   144     val ranges: List[Range]) // visible text partitioning in canonical order
   145   {
   146     def is_empty: Boolean = ranges.isEmpty
   147     def range: Range =
   148       if (is_empty) Range(0)
   149       else Range(ranges.head.start, ranges.last.stop)
   150 
   151     override def hashCode: Int = ranges.hashCode
   152     override def equals(that: Any): Boolean =
   153       that match {
   154         case other: Perspective => ranges == other.ranges
   155         case _ => false
   156       }
   157     override def toString = ranges.toString
   158   }
   159 
   160 
   161   /* information associated with text range */
   162 
   163   sealed case class Info[A](val range: Text.Range, val info: A)
   164   {
   165     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
   166     def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
   167   }
   168 
   169   type Markup = Info[XML.Elem]
   170 
   171 
   172   /* editing */
   173 
   174   object Edit
   175   {
   176     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   177     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   178   }
   179 
   180   final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
   181   {
   182     override def toString =
   183       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   184 
   185 
   186     /* transform offsets */
   187 
   188     private def transform(do_insert: Boolean, i: Offset): Offset =
   189       if (i < start) i
   190       else if (do_insert) i + text.length
   191       else (i - text.length) max start
   192 
   193     def convert(i: Offset): Offset = transform(is_insert, i)
   194     def revert(i: Offset): Offset = transform(!is_insert, i)
   195     def convert(range: Range): Range = range.map(convert)
   196     def revert(range: Range): Range = range.map(revert)
   197 
   198 
   199     /* edit strings */
   200 
   201     private def insert(i: Offset, string: String): String =
   202       string.substring(0, i) + text + string.substring(i)
   203 
   204     private def remove(i: Offset, count: Int, string: String): String =
   205       string.substring(0, i) + string.substring(i + count)
   206 
   207     def can_edit(string: String, shift: Int): Boolean =
   208       shift <= start && start < shift + string.length
   209 
   210     def edit(string: String, shift: Int): (Option[Edit], String) =
   211       if (!can_edit(string, shift)) (Some(this), string)
   212       else if (is_insert) (None, insert(start - shift, string))
   213       else {
   214         val i = start - shift
   215         val count = text.length min (string.length - i)
   216         val rest =
   217           if (count == text.length) None
   218           else Some(Edit.remove(start, text.substring(count)))
   219         (rest, remove(i, count, string))
   220       }
   221   }
   222 }