src/Pure/PIDE/text.scala
author wenzelm
Wed Jul 23 16:20:07 2014 +0200 (2014-07-23)
changeset 57620 c30ab960875e
parent 56746 d37a5d09a277
child 57912 dd9550f84106
permissions -rw-r--r--
more explicit treatment of cleared nodes (removal is implicit);
     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     def inflate_singularity: Range = if (is_singularity) Range(start, start + 1) else this
    53 
    54     def contains(i: Offset): Boolean = start == i || start < i && i < stop
    55     def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
    56     def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
    57     def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
    58 
    59     def apart(that: Range): Boolean =
    60       (this.start max that.start) > (this.stop min that.stop)
    61 
    62     def restrict(that: Range): Range =
    63       Range(this.start max that.start, this.stop min that.stop)
    64 
    65     def try_restrict(that: Range): Option[Range] =
    66       if (this apart that) None
    67       else Some(restrict(that))
    68 
    69     def try_join(that: Range): Option[Range] =
    70       if (this apart that) None
    71       else Some(Range(this.start min that.start, this.stop max that.stop))
    72   }
    73 
    74 
    75   /* perspective */
    76 
    77   object Perspective
    78   {
    79     val empty: Perspective = Perspective(Nil)
    80 
    81     def full: Perspective = Perspective(List(Range(0, Integer.MAX_VALUE / 2)))
    82 
    83     def apply(ranges: Seq[Range]): Perspective =
    84     {
    85       val result = new mutable.ListBuffer[Text.Range]
    86       var last: Option[Text.Range] = None
    87       def ship(next: Option[Range]) { result ++= last; last = next }
    88 
    89       for (range <- ranges.sortBy(_.start))
    90       {
    91         last match {
    92           case None => ship(Some(range))
    93           case Some(last_range) =>
    94             last_range.try_join(range) match {
    95               case None => ship(Some(range))
    96               case joined => last = joined
    97             }
    98         }
    99       }
   100       ship(None)
   101       new Perspective(result.toList)
   102     }
   103   }
   104 
   105   final class Perspective private(
   106     val ranges: List[Range]) // visible text partitioning in canonical order
   107   {
   108     def is_empty: Boolean = ranges.isEmpty
   109     def range: Range =
   110       if (is_empty) Range(0)
   111       else Range(ranges.head.start, ranges.last.stop)
   112 
   113     override def hashCode: Int = ranges.hashCode
   114     override def equals(that: Any): Boolean =
   115       that match {
   116         case other: Perspective => ranges == other.ranges
   117         case _ => false
   118       }
   119     override def toString = ranges.toString
   120   }
   121 
   122 
   123   /* information associated with text range */
   124 
   125   sealed case class Info[A](val range: Text.Range, val info: A)
   126   {
   127     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
   128     def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
   129   }
   130 
   131   type Markup = Info[XML.Elem]
   132 
   133 
   134   /* editing */
   135 
   136   object Edit
   137   {
   138     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   139     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   140   }
   141 
   142   final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
   143   {
   144     override def toString =
   145       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   146 
   147 
   148     /* transform offsets */
   149 
   150     private def transform(do_insert: Boolean, i: Offset): Offset =
   151       if (i < start) i
   152       else if (do_insert) i + text.length
   153       else (i - text.length) max start
   154 
   155     def convert(i: Offset): Offset = transform(is_insert, i)
   156     def revert(i: Offset): Offset = transform(!is_insert, i)
   157 
   158 
   159     /* edit strings */
   160 
   161     private def insert(i: Offset, string: String): String =
   162       string.substring(0, i) + text + string.substring(i)
   163 
   164     private def remove(i: Offset, count: Int, string: String): String =
   165       string.substring(0, i) + string.substring(i + count)
   166 
   167     def can_edit(string: String, shift: Int): Boolean =
   168       shift <= start && start < shift + string.length
   169 
   170     def edit(string: String, shift: Int): (Option[Edit], String) =
   171       if (!can_edit(string, shift)) (Some(this), string)
   172       else if (is_insert) (None, insert(start - shift, string))
   173       else {
   174         val i = start - shift
   175         val count = text.length min (string.length - i)
   176         val rest =
   177           if (count == text.length) None
   178           else Some(Edit.remove(start, text.substring(count)))
   179         (rest, remove(i, count, string))
   180       }
   181   }
   182 }