src/Pure/PIDE/text.scala
author wenzelm
Thu Aug 19 22:52:00 2010 +0200 (2010-08-19 ago)
changeset 38564 a6e2715fac5f
parent 38563 f6c9a4f9f66f
child 38565 32b924a832c4
permissions -rw-r--r--
parameterized type Markup_Tree.Node;
Markup_Tree.select: allow arbitrary interpretations, not just filtering;
renamed Text.Range.intersect to Text.Range.restrict -- emphasize that it is not directly related to contains/overlaps;
     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     override def toString = "[" + start.toString + ":" + stop.toString + "]"
    25 
    26     def map(f: Offset => Offset): Range = Range(f(start), f(stop))
    27     def +(i: Offset): Range = map(_ + i)
    28     def contains(i: Offset): Boolean = start <= i && i < stop
    29     def contains(that: Range): Boolean =
    30       this == that || this.contains(that.start) && that.stop <= this.stop
    31     def overlaps(that: Range): Boolean =
    32       this == that || this.contains(that.start) || that.contains(this.start)
    33     def compare(that: Range): Int =
    34       if (overlaps(that)) 0
    35       else if (this.start < that.start) -1
    36       else 1
    37 
    38     def start_range: Range = Range(start, start)
    39     def stop_range: Range = Range(stop, stop)
    40 
    41     def restrict(that: Range): Range =
    42       Range(this.start max that.start, this.stop min that.stop)
    43   }
    44 
    45 
    46   /* editing */
    47 
    48   object Edit
    49   {
    50     def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
    51     def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
    52   }
    53 
    54   class Edit(val is_insert: Boolean, val start: Offset, val text: String)
    55   {
    56     override def toString =
    57       (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
    58 
    59 
    60     /* transform offsets */
    61 
    62     private def transform(do_insert: Boolean, i: Offset): Offset =
    63       if (i < start) i
    64       else if (is_insert == do_insert) i + text.length
    65       else (i - text.length) max start
    66 
    67     def convert(i: Offset): Offset = transform(true, i)
    68     def revert(i: Offset): Offset = transform(false, i)
    69 
    70 
    71     /* edit strings */
    72 
    73     private def insert(i: Offset, string: String): String =
    74       string.substring(0, i) + text + string.substring(i)
    75 
    76     private def remove(i: Offset, count: Int, string: String): String =
    77       string.substring(0, i) + string.substring(i + count)
    78 
    79     def can_edit(string: String, shift: Int): Boolean =
    80       shift <= start && start < shift + string.length
    81 
    82     def edit(string: String, shift: Int): (Option[Edit], String) =
    83       if (!can_edit(string, shift)) (Some(this), string)
    84       else if (is_insert) (None, insert(start - shift, string))
    85       else {
    86         val i = start - shift
    87         val count = text.length min (string.length - i)
    88         val rest =
    89           if (count == text.length) None
    90           else Some(Edit.remove(start, text.substring(count)))
    91         (rest, remove(i, count, string))
    92       }
    93   }
    94 }