src/Pure/PIDE/text.scala
author wenzelm
Thu Aug 19 17:37:13 2010 +0200 (2010-08-19)
changeset 38485 c5eae9fc1fa4
parent 38477 f01f4ab2a0af
child 38562 3de5f0424caa
permissions -rw-r--r--
Text.Range: improved handling of singularities;
     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: interval with total quasi-ordering */
    19 
    20   object Range
    21   {
    22     object Ordering extends scala.math.Ordering[Range]
    23     {
    24       override def compare(r1: Range, r2: Range): Int = r1 compare r2
    25     }
    26   }
    27 
    28   sealed case class Range(val start: Offset, val stop: Offset)
    29   {
    30     require(start <= stop)
    31 
    32     def map(f: Offset => Offset): Range = Range(f(start), f(stop))
    33     def +(i: Offset): Range = map(_ + i)
    34     def contains(i: Offset): Boolean = start <= i && i < stop
    35     def contains(that: Range): Boolean =
    36       this == that || this.contains(that.start) && that.stop <= this.stop
    37     def overlaps(that: Range): Boolean =
    38       this == that || this.contains(that.start) || that.contains(this.start)
    39     def compare(that: Range): Int =
    40       if (overlaps(that)) 0
    41       else if (this.start < that.start) -1
    42       else 1
    43 
    44     def start_range: Range = Range(start, start)
    45     def stop_range: Range = Range(stop, stop)
    46 
    47     def intersect(that: Range): Range =
    48       Range(this.start max that.start, this.stop min that.stop)
    49   }
    50 
    51 
    52   /* editing */
    53 
    54   object Edit
    55   {
    56     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
    57     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
    58   }
    59 
    60   class Edit(val is_insert: Boolean, val start: Offset, val text: String)
    61   {
    62     override def toString =
    63       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
    64 
    65 
    66     /* transform offsets */
    67 
    68     private def transform(do_insert: Boolean, i: Offset): Offset =
    69       if (i < start) i
    70       else if (is_insert == do_insert) i + text.length
    71       else (i - text.length) max start
    72 
    73     def convert(i: Offset): Offset = transform(true, i)
    74     def revert(i: Offset): Offset = transform(false, i)
    75 
    76 
    77     /* edit strings */
    78 
    79     private def insert(i: Offset, string: String): String =
    80       string.substring(0, i) + text + string.substring(i)
    81 
    82     private def remove(i: Offset, count: Int, string: String): String =
    83       string.substring(0, i) + string.substring(i + count)
    84 
    85     def can_edit(string: String, shift: Int): Boolean =
    86       shift <= start && start < shift + string.length
    87 
    88     def edit(string: String, shift: Int): (Option[Edit], String) =
    89       if (!can_edit(string, shift)) (Some(this), string)
    90       else if (is_insert) (None, insert(start - shift, string))
    91       else {
    92         val i = start - shift
    93         val count = text.length min (string.length - i)
    94         val rest =
    95           if (count == text.length) None
    96           else Some(Edit.remove(start, text.substring(count)))
    97         (rest, remove(i, count, string))
    98       }
    99   }
   100 }