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