src/Pure/PIDE/text.scala
author wenzelm
Sun, 15 Aug 2010 21:42:13 +0200
changeset 38425 e467db701d78
parent 38154 src/Pure/PIDE/text_edit.scala@343cb5d4034a
child 38426 2858ec7b6dd8
permissions -rw-r--r--
moved Text_Edit to Text.Edit; tuned;
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
{
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    13
  /* edits */
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    14
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    15
  object Edit
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    16
  {
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    17
    def insert(start: Int, text: String): Edit = new Edit(true, start, text)
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    18
    def remove(start: Int, text: String): Edit = new Edit(false, start, text)
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    19
  }
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    20
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    21
  class Edit(val is_insert: Boolean, val start: Int, val text: String)
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    22
  {
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    23
    override def toString =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    24
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    25
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
    /* transform offsets */
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    28
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    29
    private def transform(do_insert: Boolean, offset: Int): Int =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    30
      if (offset < start) offset
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    31
      else if (is_insert == do_insert) offset + text.length
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    32
      else (offset - text.length) max start
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    33
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    34
    def convert(offset: Int): Int = transform(true, offset)
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    35
    def revert(offset: Int): Int = transform(false, offset)
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    36
34286
951aa92d06bb more text edit operations;
wenzelm
parents: 34276
diff changeset
    37
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    38
    /* edit strings */
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    39
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    40
    private def insert(index: Int, string: String): String =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    41
      string.substring(0, index) + text + string.substring(index)
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    42
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    43
    private def remove(index: Int, count: Int, string: String): String =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    44
      string.substring(0, index) + string.substring(index + count)
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    45
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    46
    def can_edit(string: String, shift: Int): Boolean =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    47
      shift <= start && start < shift + string.length
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    48
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    49
    def edit(string: String, shift: Int): (Option[Edit], String) =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    50
      if (!can_edit(string, shift)) (Some(this), string)
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    51
      else if (is_insert) (None, insert(start - shift, string))
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    52
      else {
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    53
        val index = start - shift
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    54
        val count = text.length min (string.length - index)
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    55
        val rest =
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    56
          if (count == text.length) None
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    57
          else Some(Edit.remove(start, text.substring(count)))
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    58
        (rest, remove(index, count, string))
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    59
      }
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38154
diff changeset
    60
  }
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    61
}