src/Pure/PIDE/text.scala
author wenzelm
Wed Apr 18 20:22:44 2012 +0200 (2012-04-18 ago)
changeset 47542 26d0a76fef0a
parent 46712 8650d9a95736
child 48677 bd4d132e32cf
permissions -rw-r--r--
more robust Sendback handling: JVM/jEdit paranoia for case matching, treat Pretty body not just XML.Text, replace proper_range only (without trailing whitespace);
     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 length: Int = stop - start
    45 
    46     def map(f: Offset => Offset): Range = Range(f(start), f(stop))
    47     def +(i: Offset): Range = map(_ + i)
    48     def -(i: Offset): Range = map(_ - i)
    49 
    50     def is_singularity: Boolean = start == stop
    51 
    52     def contains(i: Offset): Boolean = start == i || start < i && i < stop
    53     def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
    54     def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
    55     def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
    56 
    57     def apart(that: Range): Boolean =
    58       (this.start max that.start) > (this.stop min that.stop)
    59 
    60     def restrict(that: Range): Range =
    61       Range(this.start max that.start, this.stop min that.stop)
    62 
    63     def try_restrict(that: Range): Option[Range] =
    64       if (this apart that) None
    65       else Some(restrict(that))
    66 
    67     def try_join(that: Range): Option[Range] =
    68       if (this apart that) None
    69       else Some(Range(this.start min that.start, this.stop max that.stop))
    70   }
    71 
    72 
    73   /* perspective */
    74 
    75   object Perspective
    76   {
    77     val empty: Perspective = Perspective(Nil)
    78 
    79     def full: Perspective = Perspective(List(Range(0, Integer.MAX_VALUE / 2)))
    80 
    81     def apply(ranges: Seq[Range]): Perspective =
    82     {
    83       val result = new mutable.ListBuffer[Text.Range]
    84       var last: Option[Text.Range] = None
    85       def ship(next: Option[Range]) { result ++= last; last = next }
    86 
    87       for (range <- ranges.sortBy(_.start))
    88       {
    89         last match {
    90           case None => ship(Some(range))
    91           case Some(last_range) =>
    92             last_range.try_join(range) match {
    93               case None => ship(Some(range))
    94               case joined => last = joined
    95             }
    96         }
    97       }
    98       ship(None)
    99       new Perspective(result.toList)
   100     }
   101   }
   102 
   103   final class Perspective private(
   104     val ranges: List[Range]) // visible text partitioning in canonical order
   105   {
   106     def is_empty: Boolean = ranges.isEmpty
   107     def range: Range =
   108       if (is_empty) Range(0)
   109       else Range(ranges.head.start, ranges.last.stop)
   110 
   111     override def hashCode: Int = ranges.hashCode
   112     override def equals(that: Any): Boolean =
   113       that match {
   114         case other: Perspective => ranges == other.ranges
   115         case _ => false
   116       }
   117     override def toString = ranges.toString
   118   }
   119 
   120 
   121   /* information associated with text range */
   122 
   123   sealed case class Info[A](val range: Text.Range, val info: A)
   124   {
   125     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
   126     def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
   127   }
   128 
   129   type Markup = Info[XML.Elem]
   130 
   131 
   132   /* editing */
   133 
   134   object Edit
   135   {
   136     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   137     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   138   }
   139 
   140   final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
   141   {
   142     override def toString =
   143       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   144 
   145 
   146     /* transform offsets */
   147 
   148     private def transform(do_insert: Boolean, i: Offset): Offset =
   149       if (i < start) i
   150       else if (do_insert) i + text.length
   151       else (i - text.length) max start
   152 
   153     def convert(i: Offset): Offset = transform(is_insert, i)
   154     def revert(i: Offset): Offset = transform(!is_insert, i)
   155     def convert(range: Range): Range = range.map(convert)
   156     def revert(range: Range): Range = range.map(revert)
   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 }