src/Pure/PIDE/text.scala
author wenzelm
Tue Apr 04 18:43:58 2017 +0200 (2017-04-04 ago)
changeset 65374 ce09e947c1d5
parent 65196 e8760a98db78
child 65522 4d7c5df70a14
permissions -rw-r--r--
tuned signature;
     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.util.Sorting
    13 
    14 
    15 object Text
    16 {
    17   /* offset */
    18 
    19   type Offset = Int
    20 
    21 
    22   /* range -- with total quasi-ordering */
    23 
    24   object Range
    25   {
    26     def apply(start: Offset): Range = Range(start, start)
    27 
    28     val full: Range = apply(0, Integer.MAX_VALUE / 2)
    29     val offside: Range = apply(-1)
    30 
    31     object Ordering extends scala.math.Ordering[Range]
    32     {
    33       def compare(r1: Range, r2: Range): Int = r1 compare r2
    34     }
    35   }
    36 
    37   sealed case class Range(start: Offset, 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: Offset = 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.full))
    84 
    85     def apply(ranges: List[Range]): Perspective =
    86     {
    87       val result = new mutable.ListBuffer[Range]
    88       var last: Option[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](range: Range, info: A)
   128   {
   129     def restrict(r: Range): Info[A] = Info(range.restrict(r), info)
   130     def try_restrict(r: Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
   131 
   132     def map[B](f: A => B): Info[B] = Info(range, f(info))
   133   }
   134 
   135   type Markup = Info[XML.Elem]
   136 
   137 
   138   /* editing */
   139 
   140   object Edit
   141   {
   142     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   143     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   144     def inserts(start: Offset, text: String): List[Edit] =
   145       if (text == "") Nil else List(insert(start, text))
   146     def removes(start: Offset, text: String): List[Edit] =
   147       if (text == "") Nil else List(remove(start, text))
   148     def replace(start: Offset, old_text: String, new_text: String): List[Edit] =
   149       if (old_text == new_text) Nil
   150       else removes(start, old_text) ::: inserts(start, new_text)
   151   }
   152 
   153   final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
   154   {
   155     override def toString: String =
   156       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   157 
   158 
   159     /* transform offsets */
   160 
   161     private def transform(do_insert: Boolean, i: Offset): Offset =
   162       if (i < start) i
   163       else if (do_insert) i + text.length
   164       else (i - text.length) max start
   165 
   166     def convert(i: Offset): Offset = transform(is_insert, i)
   167     def revert(i: Offset): Offset = transform(!is_insert, i)
   168 
   169 
   170     /* edit strings */
   171 
   172     private def insert(i: Offset, string: String): String =
   173       string.substring(0, i) + text + string.substring(i)
   174 
   175     private def remove(i: Offset, count: Offset, string: String): String =
   176       string.substring(0, i) + string.substring(i + count)
   177 
   178     def can_edit(string: String, shift: Offset): Boolean =
   179       shift <= start && start < shift + string.length
   180 
   181     def edit(string: String, shift: Offset): (Option[Edit], String) =
   182       if (!can_edit(string, shift)) (Some(this), string)
   183       else if (is_insert) (None, insert(start - shift, string))
   184       else {
   185         val i = start - shift
   186         val count = text.length min (string.length - i)
   187         val rest =
   188           if (count == text.length) None
   189           else Some(Edit.remove(start, text.substring(count)))
   190         (rest, remove(i, count, string))
   191       }
   192   }
   193 }