src/Pure/PIDE/text.scala
author wenzelm
Fri Aug 20 11:00:15 2010 +0200 (2010-08-20)
changeset 38565 32b924a832c4
parent 38564 a6e2715fac5f
child 38568 f117ba49a59c
permissions -rw-r--r--
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
     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   sealed case class Range(val start: Offset, val stop: Offset)
    21   {
    22     // denotation: {start} Un {i. start < i & i < stop}
    23     require(start <= stop)
    24 
    25     override def toString = "[" + start.toString + ":" + stop.toString + "]"
    26 
    27     def map(f: Offset => Offset): Range = Range(f(start), f(stop))
    28     def +(i: Offset): Range = map(_ + i)
    29     def contains(i: Offset): Boolean = start == i || start < i && i < stop
    30     def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
    31     def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
    32     def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
    33 
    34     def start_range: Range = Range(start, start)
    35     def stop_range: Range = Range(stop, stop)
    36 
    37     def restrict(that: Range): Range =
    38       Range(this.start max that.start, this.stop min that.stop)
    39   }
    40 
    41 
    42   /* editing */
    43 
    44   object Edit
    45   {
    46     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
    47     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
    48   }
    49 
    50   class Edit(val is_insert: Boolean, val start: Offset, val text: String)
    51   {
    52     override def toString =
    53       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
    54 
    55 
    56     /* transform offsets */
    57 
    58     private def transform(do_insert: Boolean, i: Offset): Offset =
    59       if (i < start) i
    60       else if (is_insert == do_insert) i + text.length
    61       else (i - text.length) max start
    62 
    63     def convert(i: Offset): Offset = transform(true, i)
    64     def revert(i: Offset): Offset = transform(false, i)
    65 
    66 
    67     /* edit strings */
    68 
    69     private def insert(i: Offset, string: String): String =
    70       string.substring(0, i) + text + string.substring(i)
    71 
    72     private def remove(i: Offset, count: Int, string: String): String =
    73       string.substring(0, i) + string.substring(i + count)
    74 
    75     def can_edit(string: String, shift: Int): Boolean =
    76       shift <= start && start < shift + string.length
    77 
    78     def edit(string: String, shift: Int): (Option[Edit], String) =
    79       if (!can_edit(string, shift)) (Some(this), string)
    80       else if (is_insert) (None, insert(start - shift, string))
    81       else {
    82         val i = start - shift
    83         val count = text.length min (string.length - i)
    84         val rest =
    85           if (count == text.length) None
    86           else Some(Edit.remove(start, text.substring(count)))
    87         (rest, remove(i, count, string))
    88       }
    89   }
    90 }