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