src/Pure/PIDE/text.scala
author wenzelm
Tue Apr 08 16:07:02 2014 +0200 (2014-04-08 ago)
changeset 56469 ffc94a271633
parent 56468 30128d1acfbc
child 56470 8eda56033203
permissions -rw-r--r--
avoid data redundancy;
     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 File_Name(file_name: String) extends Name
   110 
   111     class File(text: CharSequence) extends Chunk
   112     {
   113       val range = Range(0, text.length)
   114       val symbol_index = Symbol.Index(text)
   115     }
   116   }
   117 
   118 
   119   /* perspective */
   120 
   121   object Perspective
   122   {
   123     val empty: Perspective = Perspective(Nil)
   124 
   125     def full: Perspective = Perspective(List(Range(0, Integer.MAX_VALUE / 2)))
   126 
   127     def apply(ranges: Seq[Range]): Perspective =
   128     {
   129       val result = new mutable.ListBuffer[Text.Range]
   130       var last: Option[Text.Range] = None
   131       def ship(next: Option[Range]) { result ++= last; last = next }
   132 
   133       for (range <- ranges.sortBy(_.start))
   134       {
   135         last match {
   136           case None => ship(Some(range))
   137           case Some(last_range) =>
   138             last_range.try_join(range) match {
   139               case None => ship(Some(range))
   140               case joined => last = joined
   141             }
   142         }
   143       }
   144       ship(None)
   145       new Perspective(result.toList)
   146     }
   147   }
   148 
   149   final class Perspective private(
   150     val ranges: List[Range]) // visible text partitioning in canonical order
   151   {
   152     def is_empty: Boolean = ranges.isEmpty
   153     def range: Range =
   154       if (is_empty) Range(0)
   155       else Range(ranges.head.start, ranges.last.stop)
   156 
   157     override def hashCode: Int = ranges.hashCode
   158     override def equals(that: Any): Boolean =
   159       that match {
   160         case other: Perspective => ranges == other.ranges
   161         case _ => false
   162       }
   163     override def toString = ranges.toString
   164   }
   165 
   166 
   167   /* information associated with text range */
   168 
   169   sealed case class Info[A](val range: Text.Range, val info: A)
   170   {
   171     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
   172     def try_restrict(r: Text.Range): Option[Info[A]] = range.try_restrict(r).map(Info(_, info))
   173   }
   174 
   175   type Markup = Info[XML.Elem]
   176 
   177 
   178   /* editing */
   179 
   180   object Edit
   181   {
   182     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   183     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   184   }
   185 
   186   final class Edit private(val is_insert: Boolean, val start: Offset, val text: String)
   187   {
   188     override def toString =
   189       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   190 
   191 
   192     /* transform offsets */
   193 
   194     private def transform(do_insert: Boolean, i: Offset): Offset =
   195       if (i < start) i
   196       else if (do_insert) i + text.length
   197       else (i - text.length) max start
   198 
   199     def convert(i: Offset): Offset = transform(is_insert, i)
   200     def revert(i: Offset): Offset = transform(!is_insert, i)
   201     def convert(range: Range): Range = range.map(convert)
   202     def revert(range: Range): Range = range.map(revert)
   203 
   204 
   205     /* edit strings */
   206 
   207     private def insert(i: Offset, string: String): String =
   208       string.substring(0, i) + text + string.substring(i)
   209 
   210     private def remove(i: Offset, count: Int, string: String): String =
   211       string.substring(0, i) + string.substring(i + count)
   212 
   213     def can_edit(string: String, shift: Int): Boolean =
   214       shift <= start && start < shift + string.length
   215 
   216     def edit(string: String, shift: Int): (Option[Edit], String) =
   217       if (!can_edit(string, shift)) (Some(this), string)
   218       else if (is_insert) (None, insert(start - shift, string))
   219       else {
   220         val i = start - shift
   221         val count = text.length min (string.length - i)
   222         val rest =
   223           if (count == text.length) None
   224           else Some(Edit.remove(start, text.substring(count)))
   225         (rest, remove(i, count, string))
   226       }
   227   }
   228 }