src/Pure/Thy/text_edit.scala
author wenzelm
Tue, 05 Jan 2010 18:20:18 +0100
changeset 34276 12436485c244
child 34286 951aa92d06bb
permissions -rw-r--r--
Basic edits on plain text.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34276
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/text_edit.scala
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
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
     5
Basic edits on plain text.
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
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    11
sealed abstract class Text_Edit
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    12
{
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    13
  val start: Int
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    14
  def before(offset: Int): Int
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    15
  def after(offset: Int): Int
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    16
}
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    17
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    18
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    19
object Text_Edit
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    20
{
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    21
  case class Insert(start: Int, text: String) extends Text_Edit
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    22
  {
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    23
    def before(offset: Int): Int =
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    24
      if (start > offset) offset
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    25
      else (offset - text.length) max start
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    26
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    27
    def after(offset: Int): Int =
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    28
      if (start <= offset) offset + text.length
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    29
      else offset
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    30
  }
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    31
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    32
  case class Remove(start: Int, text: String) extends Text_Edit
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    33
  {
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    34
    def before(offset: Int): Int =
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    35
      if (start <= offset) offset + text.length
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    36
      else offset
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    37
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    38
    def after(offset: Int): Int =
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    39
      if (start > offset) offset
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    40
      else (offset - text.length) max start
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    41
  }
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    42
}
12436485c244 Basic edits on plain text.
wenzelm
parents:
diff changeset
    43