src/Pure/PIDE/text.scala
author wenzelm
Tue Sep 07 15:03:59 2010 +0200 (2010-09-07)
changeset 39170 04ad0fed81f5
parent 38725 3d9d5ff80f6f
child 43425 0a5612040a8b
permissions -rw-r--r--
Isar_Document.reported_positions: exclude proof state output;
     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     require(start <= stop)
    29 
    30     override def toString = "[" + start.toString + ":" + stop.toString + "]"
    31 
    32     def map(f: Offset => Offset): Range = Range(f(start), f(stop))
    33     def +(i: Offset): Range = map(_ + i)
    34     def -(i: Offset): Range = map(_ - i)
    35 
    36     def is_singularity: Boolean = start == stop
    37 
    38     def contains(i: Offset): Boolean = start == i || start < i && i < stop
    39     def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop
    40     def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start)
    41     def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start
    42 
    43     def restrict(that: Range): Range =
    44       Range(this.start max that.start, this.stop min that.stop)
    45   }
    46 
    47 
    48   /* information associated with text range */
    49 
    50   case class Info[A](val range: Text.Range, val info: A)
    51   {
    52     def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info)
    53   }
    54 
    55 
    56   /* editing */
    57 
    58   object Edit
    59   {
    60     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
    61     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
    62   }
    63 
    64   class Edit(val is_insert: Boolean, val start: Offset, val text: String)
    65   {
    66     override def toString =
    67       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
    68 
    69 
    70     /* transform offsets */
    71 
    72     private def transform(do_insert: Boolean, i: Offset): Offset =
    73       if (i < start) i
    74       else if (is_insert == do_insert) i + text.length
    75       else (i - text.length) max start
    76 
    77     def convert(i: Offset): Offset = transform(true, i)
    78     def revert(i: Offset): Offset = transform(false, i)
    79 
    80 
    81     /* edit strings */
    82 
    83     private def insert(i: Offset, string: String): String =
    84       string.substring(0, i) + text + string.substring(i)
    85 
    86     private def remove(i: Offset, count: Int, string: String): String =
    87       string.substring(0, i) + string.substring(i + count)
    88 
    89     def can_edit(string: String, shift: Int): Boolean =
    90       shift <= start && start < shift + string.length
    91 
    92     def edit(string: String, shift: Int): (Option[Edit], String) =
    93       if (!can_edit(string, shift)) (Some(this), string)
    94       else if (is_insert) (None, insert(start - shift, string))
    95       else {
    96         val i = start - shift
    97         val count = text.length min (string.length - i)
    98         val rest =
    99           if (count == text.length) None
   100           else Some(Edit.remove(start, text.substring(count)))
   101         (rest, remove(i, count, string))
   102       }
   103   }
   104 }