src/Pure/PIDE/text.scala
author wenzelm
Thu Aug 25 11:41:48 2011 +0200 (2011-08-25)
changeset 44474 681447a9ffe5
parent 44473 4f264fdf8d0e
child 45240 9d97bd3c086a
permissions -rw-r--r--
slightly more abstract Command.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   object Perspective
    66   {
    67     val empty: Perspective = Perspective(Nil)
    68 
    69     def apply(ranges: Seq[Range]): Perspective =
    70     {
    71       val sorted_ranges = ranges.toArray
    72       Sorting.quickSort(sorted_ranges)(Range.Ordering)
    73 
    74       val result = new mutable.ListBuffer[Text.Range]
    75       var last: Option[Text.Range] = None
    76       for (range <- sorted_ranges)
    77       {
    78         last match {
    79           case Some(last_range)
    80           if ((last_range overlaps range) || last_range.stop == range.start) =>
    81             last = Some(Text.Range(last_range.start, range.stop))
    82           case _ =>
    83             result ++= last
    84             last = Some(range)
    85         }
    86       }
    87       result ++= last
    88       new Perspective(result.toList)
    89     }
    90   }
    91 
    92   sealed case class Perspective(ranges: List[Range]) // visible text partitioning in canonical order
    93   {
    94     def is_empty: Boolean = ranges.isEmpty
    95     def range: Range =
    96       if (is_empty) Range(0)
    97       else Range(ranges.head.start, ranges.last.stop)
    98   }
    99 
   100 
   101   /* information associated with text range */
   102 
   103   sealed case class Info[A](val range: Text.Range, val info: A)
   104   {
   105     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
   106     def try_restrict(r: Text.Range): Option[Info[A]] =
   107       try { Some(Info(range.restrict(r), info)) }
   108       catch { case ERROR(_) => None }
   109   }
   110 
   111 
   112   /* editing */
   113 
   114   object Edit
   115   {
   116     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
   117     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
   118   }
   119 
   120   class Edit(val is_insert: Boolean, val start: Offset, val text: String)
   121   {
   122     override def toString =
   123       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
   124 
   125 
   126     /* transform offsets */
   127 
   128     private def transform(do_insert: Boolean, i: Offset): Offset =
   129       if (i < start) i
   130       else if (do_insert) i + text.length
   131       else (i - text.length) max start
   132 
   133     def convert(i: Offset): Offset = transform(is_insert, i)
   134     def revert(i: Offset): Offset = transform(!is_insert, i)
   135     def convert(range: Range): Range = range.map(convert)
   136     def revert(range: Range): Range = range.map(revert)
   137 
   138 
   139     /* edit strings */
   140 
   141     private def insert(i: Offset, string: String): String =
   142       string.substring(0, i) + text + string.substring(i)
   143 
   144     private def remove(i: Offset, count: Int, string: String): String =
   145       string.substring(0, i) + string.substring(i + count)
   146 
   147     def can_edit(string: String, shift: Int): Boolean =
   148       shift <= start && start < shift + string.length
   149 
   150     def edit(string: String, shift: Int): (Option[Edit], String) =
   151       if (!can_edit(string, shift)) (Some(this), string)
   152       else if (is_insert) (None, insert(start - shift, string))
   153       else {
   154         val i = start - shift
   155         val count = text.length min (string.length - i)
   156         val rest =
   157           if (count == text.length) None
   158           else Some(Edit.remove(start, text.substring(count)))
   159         (rest, remove(i, count, string))
   160       }
   161   }
   162 }