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