src/Pure/PIDE/text.scala
author wenzelm
Sat Jan 14 14:34:42 2012 +0100 (2012-01-14 ago)
changeset 46207 e76b77356ed6
parent 45673 cd41e3903fbf
child 46576 ae9286f64574
permissions -rw-r--r--
clarified partial restrict operation;
     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]] = range.try_restrict(r).map(Info(_, info))
   122   }
   123 
   124   type Markup = Info[XML.Elem]
   125 
   126 
   127   /* editing */
   128 
   129   object Edit
   130   {
   131     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   132     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   133   }
   134 
   135   class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
   136   {
   137     override def toString =
   138       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   139 
   140 
   141     /* transform offsets */
   142 
   143     private def transform(do_insert: Boolean, i: Offset): Offset =
   144       if (i < start) i
   145       else if (do_insert) i + text.length
   146       else (i - text.length) max start
   147 
   148     def convert(i: Offset): Offset = transform(is_insert, i)
   149     def revert(i: Offset): Offset = transform(!is_insert, i)
   150     def convert(range: Range): Range = range.map(convert)
   151     def revert(range: Range): Range = range.map(revert)
   152 
   153 
   154     /* edit strings */
   155 
   156     private def insert(i: Offset, string: String): String =
   157       string.substring(0, i) + text + string.substring(i)
   158 
   159     private def remove(i: Offset, count: Int, string: String): String =
   160       string.substring(0, i) + string.substring(i + count)
   161 
   162     def can_edit(string: String, shift: Int): Boolean =
   163       shift <= start && start < shift + string.length
   164 
   165     def edit(string: String, shift: Int): (Option[Edit], String) =
   166       if (!can_edit(string, shift)) (Some(this), string)
   167       else if (is_insert) (None, insert(start - shift, string))
   168       else {
   169         val i = start - shift
   170         val count = text.length min (string.length - i)
   171         val rest =
   172           if (count == text.length) None
   173           else Some(Edit.remove(start, text.substring(count)))
   174         (rest, remove(i, count, string))
   175       }
   176   }
   177 }