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