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;

/*  Title:      Pure/PIDE/text.scala
    Author:     Fabian Immler, TU Munich
    Author:     Makarius

Basic operations on plain text.
*/

package isabelle


object Text
{
  /* edits */

  object Edit
  {
    def insert(start: Int, text: String): Edit = new Edit(true, start, text)
    def remove(start: Int, text: String): Edit = new Edit(false, start, text)
  }

  class Edit(val is_insert: Boolean, val start: Int, val text: String)
  {
    override def toString =
      (if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")"


    /* transform offsets */

    private def transform(do_insert: Boolean, offset: Int): Int =
      if (offset < start) offset
      else if (is_insert == do_insert) offset + text.length
      else (offset - text.length) max start

    def convert(offset: Int): Int = transform(true, offset)
    def revert(offset: Int): Int = transform(false, offset)


    /* edit strings */

    private def insert(index: Int, string: String): String =
      string.substring(0, index) + text + string.substring(index)

    private def remove(index: Int, count: Int, string: String): String =
      string.substring(0, index) + string.substring(index + count)

    def can_edit(string: String, shift: Int): Boolean =
      shift <= start && start < shift + string.length

    def edit(string: String, shift: Int): (Option[Edit], String) =
      if (!can_edit(string, shift)) (Some(this), string)
      else if (is_insert) (None, insert(start - shift, string))
      else {
        val index = start - shift
        val count = text.length min (string.length - index)
        val rest =
          if (count == text.length) None
          else Some(Edit.remove(start, text.substring(count)))
        (rest, remove(index, count, string))
      }
  }
}