src/Pure/PIDE/text.scala
author wenzelm
Fri Jun 17 23:18:22 2011 +0200 (2011-06-17)
changeset 43425 0a5612040a8b
parent 38725 3d9d5ff80f6f
child 43428 b41dea5772c6
permissions -rw-r--r--
more explicit error message;
convert/revert range;
tuned;
     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 object Text
    12 {
    13   /* offset */
    14 
    15   type Offset = Int
    16 
    17 
    18   /* range -- with total quasi-ordering */
    19 
    20   object Range
    21   {
    22     def apply(start: Offset): Range = Range(start, start)
    23   }
    24 
    25   sealed case class Range(val start: Offset, val stop: Offset)
    26   {
    27     // denotation: {start} Un {i. start < i & i < stop}
    28     if (start > stop)
    29       error("Bad range: [" + start.toString + ":" + stop.toString + "]")
    30 
    31     override def toString = "[" + start.toString + ":" + stop.toString + "]"
    32 
    33     def map(f: Offset => Offset): Range = Range(f(start), f(stop))
    34     def +(i: Offset): Range = map(_ + i)
    35     def -(i: Offset): Range = map(_ - i)
    36 
    37     def is_singularity: Boolean = start == stop
    38 
    39     def contains(i: Offset): Boolean = start == i || start < i && i < stop
    40     def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
    41     def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
    42     def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
    43 
    44     def restrict(that: Range): Range =
    45       Range(this.start max that.start, this.stop min that.stop)
    46   }
    47 
    48 
    49   /* information associated with text range */
    50 
    51   case class Info[A](val range: Text.Range, val info: A)
    52   {
    53     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
    54   }
    55 
    56 
    57   /* editing */
    58 
    59   object Edit
    60   {
    61     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
    62     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
    63   }
    64 
    65   class Edit(val is_insert: Boolean, val start: Offset, val text: String)
    66   {
    67     override def toString =
    68       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
    69 
    70 
    71     /* transform offsets */
    72 
    73     private def transform(do_insert: Boolean, i: Offset): Offset =
    74       if (i < start) i
    75       else if (do_insert) i + text.length
    76       else (i - text.length) max start
    77 
    78     def convert(i: Offset): Offset = transform(is_insert, i)
    79     def revert(i: Offset): Offset = transform(!is_insert, i)
    80     def convert(range: Range): Range = range.map(convert)
    81     def revert(range: Range): Range = range.map(revert)
    82 
    83 
    84     /* edit strings */
    85 
    86     private def insert(i: Offset, string: String): String =
    87       string.substring(0, i) + text + string.substring(i)
    88 
    89     private def remove(i: Offset, count: Int, string: String): String =
    90       string.substring(0, i) + string.substring(i + count)
    91 
    92     def can_edit(string: String, shift: Int): Boolean =
    93       shift <= start && start < shift + string.length
    94 
    95     def edit(string: String, shift: Int): (Option[Edit], String) =
    96       if (!can_edit(string, shift)) (Some(this), string)
    97       else if (is_insert) (None, insert(start - shift, string))
    98       else {
    99         val i = start - shift
   100         val count = text.length min (string.length - i)
   101         val rest =
   102           if (count == text.length) None
   103           else Some(Edit.remove(start, text.substring(count)))
   104         (rest, remove(i, count, string))
   105       }
   106   }
   107 }