src/Pure/PIDE/text.scala
author wenzelm
Wed Aug 18 14:02:32 2010 +0200 (2010-08-18)
changeset 38477 f01f4ab2a0af
parent 38427 7066fbd315ae
child 38485 c5eae9fc1fa4
permissions -rw-r--r--
refined notion of Text.Range;
     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 = start <= that.start && that.stop <= stop
    36     def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
    37     def compare(that: Range): Int =
    38       if (overlaps(that)) 0
    39       else if (this.start < that.start) -1
    40       else 1
    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 }