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