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