src/Pure/PIDE/text.scala
author wenzelm
Tue Apr 08 19:17:28 2014 +0200 (2014-04-08 ago)
changeset 56470 8eda56033203
parent 56469 ffc94a271633
child 56473 5b5c750e9763
permissions -rw-r--r--
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
     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 
    53     def contains(i: Offset): Boolean = start == i || start < i && i < stop
    54     def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
    55     def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
    56     def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
    57 
    58     def apart(that: Range): Boolean =
    59       (this.start max that.start) > (this.stop min that.stop)
    60 
    61     def restrict(that: Range): Range =
    62       Range(this.start max that.start, this.stop min that.stop)
    63 
    64     def try_restrict(that: Range): Option[Range] =
    65       if (this apart that) None
    66       else Some(restrict(that))
    67 
    68     def try_join(that: Range): Option[Range] =
    69       if (this apart that) None
    70       else Some(Range(this.start min that.start, this.stop max that.stop))
    71   }
    72 
    73 
    74   /* chunks with symbol index */
    75 
    76   abstract class Chunk
    77   {
    78     def range: Range
    79     def symbol_index: Symbol.Index
    80 
    81     private lazy val hash: Int = (range, symbol_index).hashCode
    82     override def hashCode: Int = hash
    83     override def equals(that: Any): Boolean =
    84       that match {
    85         case other: Chunk =>
    86           hash == other.hash &&
    87           range == other.range &&
    88           symbol_index == other.symbol_index
    89         case _ => false
    90       }
    91 
    92     def decode(symbol_offset: Symbol.Offset): Offset = symbol_index.decode(symbol_offset)
    93     def decode(symbol_range: Symbol.Range): Range = symbol_index.decode(symbol_range)
    94     def incorporate(symbol_range: Symbol.Range): Option[Range] =
    95     {
    96       def in(r: Symbol.Range): Option[Range] =
    97         range.try_restrict(decode(r)) match {
    98           case Some(r1) if !r1.is_singularity => Some(r1)
    99           case _ => None
   100         }
   101      in(symbol_range) orElse in(symbol_range - 1)
   102     }
   103   }
   104 
   105   object Chunk
   106   {
   107     sealed abstract class Name
   108     case object Default extends Name
   109     case class Id(id: Document_ID.Generic) extends Name
   110     case class File_Name(file_name: String) extends Name
   111 
   112     class File(text: CharSequence) extends Chunk
   113     {
   114       val range = Range(0, text.length)
   115       val symbol_index = Symbol.Index(text)
   116     }
   117   }
   118 
   119 
   120   /* perspective */
   121 
   122   object Perspective
   123   {
   124     val empty: Perspective = Perspective(Nil)
   125 
   126     def full: Perspective = Perspective(List(Range(0, Integer.MAX_VALUE / 2)))
   127 
   128     def apply(ranges: Seq[Range]): Perspective =
   129     {
   130       val result = new mutable.ListBuffer[Text.Range]
   131       var last: Option[Text.Range] = None
   132       def ship(next: Option[Range]) { result ++= last; last = next }
   133 
   134       for (range <- ranges.sortBy(_.start))
   135       {
   136         last match {
   137           case None => ship(Some(range))
   138           case Some(last_range) =>
   139             last_range.try_join(range) match {
   140               case None => ship(Some(range))
   141               case joined => last = joined
   142             }
   143         }
   144       }
   145       ship(None)
   146       new Perspective(result.toList)
   147     }
   148   }
   149 
   150   final class Perspective private(
   151     val ranges: List[Range]) // visible text partitioning in canonical order
   152   {
   153     def is_empty: Boolean = ranges.isEmpty
   154     def range: Range =
   155       if (is_empty) Range(0)
   156       else Range(ranges.head.start, ranges.last.stop)
   157 
   158     override def hashCode: Int = ranges.hashCode
   159     override def equals(that: Any): Boolean =
   160       that match {
   161         case other: Perspective => ranges == other.ranges
   162         case _ => false
   163       }
   164     override def toString = ranges.toString
   165   }
   166 
   167 
   168   /* information associated with text range */
   169 
   170   sealed case class Info[A](val range: Text.Range, val info: A)
   171   {
   172     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
   173     def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
   174   }
   175 
   176   type Markup = Info[XML.Elem]
   177 
   178 
   179   /* editing */
   180 
   181   object Edit
   182   {
   183     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   184     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   185   }
   186 
   187   final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
   188   {
   189     override def toString =
   190       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   191 
   192 
   193     /* transform offsets */
   194 
   195     private def transform(do_insert: Boolean, i: Offset): Offset =
   196       if (i < start) i
   197       else if (do_insert) i + text.length
   198       else (i - text.length) max start
   199 
   200     def convert(i: Offset): Offset = transform(is_insert, i)
   201     def revert(i: Offset): Offset = transform(!is_insert, i)
   202     def convert(range: Range): Range = range.map(convert)
   203     def revert(range: Range): Range = range.map(revert)
   204 
   205 
   206     /* edit strings */
   207 
   208     private def insert(i: Offset, string: String): String =
   209       string.substring(0, i) + text + string.substring(i)
   210 
   211     private def remove(i: Offset, count: Int, string: String): String =
   212       string.substring(0, i) + string.substring(i + count)
   213 
   214     def can_edit(string: String, shift: Int): Boolean =
   215       shift <= start && start < shift + string.length
   216 
   217     def edit(string: String, shift: Int): (Option[Edit], String) =
   218       if (!can_edit(string, shift)) (Some(this), string)
   219       else if (is_insert) (None, insert(start - shift, string))
   220       else {
   221         val i = start - shift
   222         val count = text.length min (string.length - i)
   223         val rest =
   224           if (count == text.length) None
   225           else Some(Edit.remove(start, text.substring(count)))
   226         (rest, remove(i, count, string))
   227       }
   228   }
   229 }