src/Pure/PIDE/text.scala
author wenzelm
Sat Nov 12 17:52:28 2011 +0100 (2011-11-12)
changeset 45470 81b85d4ed269
parent 45455 4f974c0c5c2f
child 45631 6bdf8b926f50
permissions -rw-r--r--
more precise type;
     1 /*  Title:      Pure/PIDE/text.scala
     2     Author:     Fabian Immler, TU Munich
     3     Author:     Makarius
     4 
     5 Basic operations on plain text.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 import scala.collection.mutable
    12 import scala.math.Ordering
    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 map(f: Offset => Offset): Range = Range(f(start), f(stop))
    44     def +(i: Offset): Range = map(_ + i)
    45     def -(i: Offset): Range = map(_ - i)
    46 
    47     def is_singularity: Boolean = start == stop
    48 
    49     def contains(i: Offset): Boolean = start == i || start < i && i < stop
    50     def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
    51     def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
    52     def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
    53 
    54     def apart(that: Range): Boolean =
    55       (this.start max that.start) > (this.stop min that.stop)
    56 
    57     def restrict(that: Range): Range =
    58       Range(this.start max that.start, this.stop min that.stop)
    59 
    60     def try_restrict(that: Range): Option[Range] =
    61       if (this apart that) None
    62       else Some(restrict(that))
    63 
    64     def try_join(that: Range): Option[Range] =
    65       if (this apart that) None
    66       else Some(Range(this.start min that.start, this.stop max that.stop))
    67   }
    68 
    69 
    70   /* perspective */
    71 
    72   object Perspective
    73   {
    74     val empty: Perspective = Perspective(Nil)
    75 
    76     def apply(ranges: Seq[Range]): Perspective =
    77     {
    78       val result = new mutable.ListBuffer[Text.Range]
    79       var last: Option[Text.Range] = None
    80       def ship(next: Option[Range]) { result ++= last; last = next }
    81 
    82       for (range <- ranges.sortBy(_.start))
    83       {
    84         last match {
    85           case None => ship(Some(range))
    86           case Some(last_range) =>
    87             last_range.try_join(range) match {
    88               case None => ship(Some(range))
    89               case joined => last = joined
    90             }
    91         }
    92       }
    93       ship(None)
    94       new Perspective(result.toList)
    95     }
    96   }
    97 
    98   class Perspective private(val ranges: List[Range]) // visible text partitioning in canonical order
    99   {
   100     def is_empty: Boolean = ranges.isEmpty
   101     def range: Range =
   102       if (is_empty) Range(0)
   103       else Range(ranges.head.start, ranges.last.stop)
   104     override def toString = ranges.toString
   105   }
   106 
   107 
   108   /* information associated with text range */
   109 
   110   sealed case class Info[A](val range: Text.Range, val info: A)
   111   {
   112     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
   113     def try_restrict(r: Text.Range): Option[Info[A]] =
   114       try { Some(Info(range.restrict(r), info)) }
   115       catch { case ERROR(_) => None }
   116   }
   117 
   118   type Markup = Info[XML.Elem]
   119 
   120 
   121   /* editing */
   122 
   123   object Edit
   124   {
   125     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   126     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   127   }
   128 
   129   class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
   130   {
   131     override def toString =
   132       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   133 
   134 
   135     /* transform offsets */
   136 
   137     private def transform(do_insert: Boolean, i: Offset): Offset =
   138       if (i < start) i
   139       else if (do_insert) i + text.length
   140       else (i - text.length) max start
   141 
   142     def convert(i: Offset): Offset = transform(is_insert, i)
   143     def revert(i: Offset): Offset = transform(!is_insert, i)
   144     def convert(range: Range): Range = range.map(convert)
   145     def revert(range: Range): Range = range.map(revert)
   146 
   147 
   148     /* edit strings */
   149 
   150     private def insert(i: Offset, string: String): String =
   151       string.substring(0, i) + text + string.substring(i)
   152 
   153     private def remove(i: Offset, count: Int, string: String): String =
   154       string.substring(0, i) + string.substring(i + count)
   155 
   156     def can_edit(string: String, shift: Int): Boolean =
   157       shift <= start && start < shift + string.length
   158 
   159     def edit(string: String, shift: Int): (Option[Edit], String) =
   160       if (!can_edit(string, shift)) (Some(this), string)
   161       else if (is_insert) (None, insert(start - shift, string))
   162       else {
   163         val i = start - shift
   164         val count = text.length min (string.length - i)
   165         val rest =
   166           if (count == text.length) None
   167           else Some(Edit.remove(start, text.substring(count)))
   168         (rest, remove(i, count, string))
   169       }
   170   }
   171 }