src/Pure/PIDE/text.scala
author wenzelm
Mon Aug 22 16:12:23 2011 +0200 (2011-08-22)
changeset 44379 1079ab6b342b
parent 43714 3749d1e6dde9
child 44384 8f6054a63f96
permissions -rw-r--r--
added official Text.Range.Ordering;
some support for text perspective;
     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.math.Ordering
    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     object Ordering extends scala.math.Ordering[Text.Range]
    30     {
    31       def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2
    32     }
    33   }
    34 
    35   sealed case class Range(val start: Offset, val stop: Offset)
    36   {
    37     // denotation: {start} Un {i. start < i & i < stop}
    38     if (start > stop)
    39       error("Bad range: [" + start.toString + ":" + stop.toString + "]")
    40 
    41     override def toString = "[" + start.toString + ":" + stop.toString + "]"
    42 
    43     def map(f: Offset => Offset): Range = Range(f(start), f(stop))
    44     def +(i: Offset): Range = map(_ + i)
    45     def -(i: Offset): Range = map(_ - i)
    46 
    47     def is_singularity: Boolean = start == stop
    48 
    49     def contains(i: Offset): Boolean = start == i || start < i && i < stop
    50     def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
    51     def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
    52     def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
    53 
    54     def restrict(that: Range): Range =
    55       Range(this.start max that.start, this.stop min that.stop)
    56 
    57     def try_restrict(that: Range): Option[Range] =
    58       try { Some (restrict(that)) }
    59       catch { case ERROR(_) => None }
    60   }
    61 
    62 
    63   /* perspective */
    64 
    65   type Perspective = List[Range]  // partitioning in canonical order
    66 
    67   def perspective(ranges: Seq[Range]): Perspective =
    68   {
    69     val sorted_ranges = ranges.toArray
    70     Sorting.quickSort(sorted_ranges)(Range.Ordering)
    71 
    72     val result = new mutable.ListBuffer[Text.Range]
    73     var last: Option[Text.Range] = None
    74     for (range <- sorted_ranges)
    75     {
    76       last match {
    77         case Some(last_range)
    78         if ((last_range overlaps range) || last_range.stop == range.start) =>
    79           last = Some(Text.Range(last_range.start, range.stop))
    80         case _ =>
    81           result ++= last
    82           last = Some(range)
    83       }
    84     }
    85     result ++= last
    86     result.toList
    87   }
    88 
    89 
    90   /* information associated with text range */
    91 
    92   sealed case class Info[A](val range: Text.Range, val info: A)
    93   {
    94     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
    95     def try_restrict(r: Text.Range): Option[Info[A]] =
    96       try { Some(Info(range.restrict(r), info)) }
    97       catch { case ERROR(_) => None }
    98   }
    99 
   100 
   101   /* editing */
   102 
   103   object Edit
   104   {
   105     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   106     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   107   }
   108 
   109   class Edit(val is_insert: Boolean, val start: Offset, val text: String)
   110   {
   111     override def toString =
   112       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   113 
   114 
   115     /* transform offsets */
   116 
   117     private def transform(do_insert: Boolean, i: Offset): Offset =
   118       if (i < start) i
   119       else if (do_insert) i + text.length
   120       else (i - text.length) max start
   121 
   122     def convert(i: Offset): Offset = transform(is_insert, i)
   123     def revert(i: Offset): Offset = transform(!is_insert, i)
   124     def convert(range: Range): Range = range.map(convert)
   125     def revert(range: Range): Range = range.map(revert)
   126 
   127 
   128     /* edit strings */
   129 
   130     private def insert(i: Offset, string: String): String =
   131       string.substring(0, i) + text + string.substring(i)
   132 
   133     private def remove(i: Offset, count: Int, string: String): String =
   134       string.substring(0, i) + string.substring(i + count)
   135 
   136     def can_edit(string: String, shift: Int): Boolean =
   137       shift <= start && start < shift + string.length
   138 
   139     def edit(string: String, shift: Int): (Option[Edit], String) =
   140       if (!can_edit(string, shift)) (Some(this), string)
   141       else if (is_insert) (None, insert(start - shift, string))
   142       else {
   143         val i = start - shift
   144         val count = text.length min (string.length - i)
   145         val rest =
   146           if (count == text.length) None
   147           else Some(Edit.remove(start, text.substring(count)))
   148         (rest, remove(i, count, string))
   149       }
   150   }
   151 }