src/Pure/PIDE/text.scala
author wenzelm
Thu Dec 13 18:00:24 2012 +0100 (2012-12-13 ago)
changeset 50503 50f141b34bb7
parent 48677 bd4d132e32cf
child 56172 31289387fdf8
permissions -rw-r--r--
enable Isabelle/ML to produce uninterpreted result messages as well;
     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     object Ordering extends scala.math.Ordering[Text.Range]
    30     {
    31       def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
    32     }
    33   }
    34 
    35   sealed case class Range(val start: Offset, val stop: Offset)
    36   {
    37     // denotation: {start} Un {i. start < i & i < stop}
    38     if (start > stop)
    39       error("Bad range: [" + start.toString + ":" + stop.toString + "]")
    40 
    41     override def toString = "[" + start.toString + ":" + stop.toString + "]"
    42 
    43     def length: Int = stop - start
    44 
    45     def map(f: Offset => Offset): Range = Range(f(start), f(stop))
    46     def +(i: Offset): Range = map(_ + i)
    47     def -(i: Offset): Range = map(_ - i)
    48 
    49     def is_singularity: Boolean = start == stop
    50 
    51     def contains(i: Offset): Boolean = start == i || start < i && i < stop
    52     def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
    53     def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
    54     def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
    55 
    56     def apart(that: Range): Boolean =
    57       (this.start max that.start) > (this.stop min that.stop)
    58 
    59     def restrict(that: Range): Range =
    60       Range(this.start max that.start, this.stop min that.stop)
    61 
    62     def try_restrict(that: Range): Option[Range] =
    63       if (this apart that) None
    64       else Some(restrict(that))
    65 
    66     def try_join(that: Range): Option[Range] =
    67       if (this apart that) None
    68       else Some(Range(this.start min that.start, this.stop max that.stop))
    69   }
    70 
    71 
    72   /* perspective */
    73 
    74   object Perspective
    75   {
    76     val empty: Perspective = Perspective(Nil)
    77 
    78     def full: Perspective = Perspective(List(Range(0, Integer.MAX_VALUE / 2)))
    79 
    80     def apply(ranges: Seq[Range]): Perspective =
    81     {
    82       val result = new mutable.ListBuffer[Text.Range]
    83       var last: Option[Text.Range] = None
    84       def ship(next: Option[Range]) { result ++= last; last = next }
    85 
    86       for (range <- ranges.sortBy(_.start))
    87       {
    88         last match {
    89           case None => ship(Some(range))
    90           case Some(last_range) =>
    91             last_range.try_join(range) match {
    92               case None => ship(Some(range))
    93               case joined => last = joined
    94             }
    95         }
    96       }
    97       ship(None)
    98       new Perspective(result.toList)
    99     }
   100   }
   101 
   102   final class Perspective private(
   103     val ranges: List[Range]) // visible text partitioning in canonical order
   104   {
   105     def is_empty: Boolean = ranges.isEmpty
   106     def range: Range =
   107       if (is_empty) Range(0)
   108       else Range(ranges.head.start, ranges.last.stop)
   109 
   110     override def hashCode: Int = ranges.hashCode
   111     override def equals(that: Any): Boolean =
   112       that match {
   113         case other: Perspective => ranges == other.ranges
   114         case _ => false
   115       }
   116     override def toString = ranges.toString
   117   }
   118 
   119 
   120   /* information associated with text range */
   121 
   122   sealed case class Info[A](val range: Text.Range, val info: A)
   123   {
   124     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
   125     def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
   126   }
   127 
   128   type Markup = Info[XML.Elem]
   129 
   130 
   131   /* editing */
   132 
   133   object Edit
   134   {
   135     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   136     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   137   }
   138 
   139   final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
   140   {
   141     override def toString =
   142       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   143 
   144 
   145     /* transform offsets */
   146 
   147     private def transform(do_insert: Boolean, i: Offset): Offset =
   148       if (i < start) i
   149       else if (do_insert) i + text.length
   150       else (i - text.length) max start
   151 
   152     def convert(i: Offset): Offset = transform(is_insert, i)
   153     def revert(i: Offset): Offset = transform(!is_insert, i)
   154     def convert(range: Range): Range = range.map(convert)
   155     def revert(range: Range): Range = range.map(revert)
   156 
   157 
   158     /* edit strings */
   159 
   160     private def insert(i: Offset, string: String): String =
   161       string.substring(0, i) + text + string.substring(i)
   162 
   163     private def remove(i: Offset, count: Int, string: String): String =
   164       string.substring(0, i) + string.substring(i + count)
   165 
   166     def can_edit(string: String, shift: Int): Boolean =
   167       shift <= start && start < shift + string.length
   168 
   169     def edit(string: String, shift: Int): (Option[Edit], String) =
   170       if (!can_edit(string, shift)) (Some(this), string)
   171       else if (is_insert) (None, insert(start - shift, string))
   172       else {
   173         val i = start - shift
   174         val count = text.length min (string.length - i)
   175         val rest =
   176           if (count == text.length) None
   177           else Some(Edit.remove(start, text.substring(count)))
   178         (rest, remove(i, count, string))
   179       }
   180   }
   181 }