src/Pure/PIDE/text.scala
author wenzelm
Thu Mar 27 21:16:08 2014 +0100 (2014-03-27)
changeset 56308 ebd3bf053969
parent 56172 31289387fdf8
child 56468 30128d1acfbc
permissions -rw-r--r--
tuned;
     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   /* perspective */
    75 
    76   object Perspective
    77   {
    78     val empty: Perspective = Perspective(Nil)
    79 
    80     def full: Perspective = Perspective(List(Range(0, Integer.MAX_VALUE / 2)))
    81 
    82     def apply(ranges: Seq[Range]): Perspective =
    83     {
    84       val result = new mutable.ListBuffer[Text.Range]
    85       var last: Option[Text.Range] = None
    86       def ship(next: Option[Range]) { result ++= last; last = next }
    87 
    88       for (range <- ranges.sortBy(_.start))
    89       {
    90         last match {
    91           case None => ship(Some(range))
    92           case Some(last_range) =>
    93             last_range.try_join(range) match {
    94               case None => ship(Some(range))
    95               case joined => last = joined
    96             }
    97         }
    98       }
    99       ship(None)
   100       new Perspective(result.toList)
   101     }
   102   }
   103 
   104   final class Perspective private(
   105     val ranges: List[Range]) // visible text partitioning in canonical order
   106   {
   107     def is_empty: Boolean = ranges.isEmpty
   108     def range: Range =
   109       if (is_empty) Range(0)
   110       else Range(ranges.head.start, ranges.last.stop)
   111 
   112     override def hashCode: Int = ranges.hashCode
   113     override def equals(that: Any): Boolean =
   114       that match {
   115         case other: Perspective => ranges == other.ranges
   116         case _ => false
   117       }
   118     override def toString = ranges.toString
   119   }
   120 
   121 
   122   /* information associated with text range */
   123 
   124   sealed case class Info[A](val range: Text.Range, val info: A)
   125   {
   126     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
   127     def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
   128   }
   129 
   130   type Markup = Info[XML.Elem]
   131 
   132 
   133   /* editing */
   134 
   135   object Edit
   136   {
   137     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   138     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   139   }
   140 
   141   final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
   142   {
   143     override def toString =
   144       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   145 
   146 
   147     /* transform offsets */
   148 
   149     private def transform(do_insert: Boolean, i: Offset): Offset =
   150       if (i < start) i
   151       else if (do_insert) i + text.length
   152       else (i - text.length) max start
   153 
   154     def convert(i: Offset): Offset = transform(is_insert, i)
   155     def revert(i: Offset): Offset = transform(!is_insert, i)
   156     def convert(range: Range): Range = range.map(convert)
   157     def revert(range: Range): Range = range.map(revert)
   158 
   159 
   160     /* edit strings */
   161 
   162     private def insert(i: Offset, string: String): String =
   163       string.substring(0, i) + text + string.substring(i)
   164 
   165     private def remove(i: Offset, count: Int, string: String): String =
   166       string.substring(0, i) + string.substring(i + count)
   167 
   168     def can_edit(string: String, shift: Int): Boolean =
   169       shift <= start && start < shift + string.length
   170 
   171     def edit(string: String, shift: Int): (Option[Edit], String) =
   172       if (!can_edit(string, shift)) (Some(this), string)
   173       else if (is_insert) (None, insert(start - shift, string))
   174       else {
   175         val i = start - shift
   176         val count = text.length min (string.length - i)
   177         val rest =
   178           if (count == text.length) None
   179           else Some(Edit.remove(start, text.substring(count)))
   180         (rest, remove(i, count, string))
   181       }
   182   }
   183 }