src/Pure/PIDE/text.scala
author wenzelm
Sun, 15 Aug 2010 23:07:22 +0200
changeset 38427 7066fbd315ae
parent 38426 2858ec7b6dd8
child 38477 f01f4ab2a0af
permissions -rw-r--r--
some derived operations on Text.Range;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
     1
/*  Title:      Pure/PIDE/text.scala
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     2
    Author:     Fabian Immler, TU Munich
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     4
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
     5
Basic operations on plain text.
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     6
*/
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     7
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     8
package isabelle
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     9
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    10
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    11
object Text
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    12
{
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    13
  /* offset and range */
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    14
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    15
  type Offset = Int
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    16
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    17
  sealed case class Range(val start: Offset, val stop: Offset)
38427
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    18
  {
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    19
    def contains(i: Offset): Boolean = start <= i && i < stop
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    20
    def map(f: Offset => Offset): Range = Range(f(start), f(stop))
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    21
    def +(i: Offset): Range = map(_ + i)
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
    22
  }
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    23
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    24
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    25
  /* editing */
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    26
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    27
  object Edit
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    28
  {
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    29
    def insert(start: Offset, text: String): Edit = new Edit(true, start, text)
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    30
    def remove(start: Offset, text: String): Edit = new Edit(false, start, text)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    31
  }
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    32
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    33
  class Edit(val is_insert: Boolean, val start: Offset, val text: String)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    34
  {
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    35
    override def toString =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    36
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    37
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    38
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    39
    /* transform offsets */
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    40
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    41
    private def transform(do_insert: Boolean, i: Offset): Offset =
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    42
      if (i < start) i
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    43
      else if (is_insert == do_insert) i + text.length
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    44
      else (i - text.length) max start
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    45
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    46
    def convert(i: Offset): Offset = transform(true, i)
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    47
    def revert(i: Offset): Offset = transform(false, i)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    48
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    49
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    50
    /* edit strings */
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    51
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    52
    private def insert(i: Offset, string: String): String =
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    53
      string.substring(0, i) + text + string.substring(i)
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    54
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    55
    private def remove(i: Offset, count: Int, string: String): String =
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    56
      string.substring(0, i) + string.substring(i + count)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    57
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    58
    def can_edit(string: String, shift: Int): Boolean =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    59
      shift <= start && start < shift + string.length
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    60
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    61
    def edit(string: String, shift: Int): (Option[Edit], String) =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    62
      if (!can_edit(string, shift)) (Some(this), string)
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    63
      else if (is_insert) (None, insert(start - shift, string))
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    64
      else {
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    65
        val i = start - shift
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    66
        val count = text.length min (string.length - i)
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    67
        val rest =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    68
          if (count == text.length) None
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    69
          else Some(Edit.remove(start, text.substring(count)))
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    70
        (rest, remove(i, count, string))
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    71
      }
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    72
  }
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    73
}