src/Tools/jEdit/src/proofdocument/Change.scala
author wenzelm
Sun, 06 Sep 2009 22:27:32 +0200
changeset 34717 3f32e08bbb6c
parent 34703 ff037c17332a
permissions -rw-r--r--
sidekick root data: set buffer length to avoid crash of initial caret move; separate Markup_Node, Markup_Tree, Markup_Text; added Markup_Text.flatten; Command.type_at: null-free version; eliminated Command.RootInfo; simplified printing of TypeInfo, RefInfo; added Command.content(Int, Int);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
     1
/*
34703
ff037c17332a minor tuning;
wenzelm
parents: 34695
diff changeset
     2
 * Changes of plain text
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
     3
 *
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
     4
 * @author Johannes Hölzl, TU Munich
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
     5
 * @author Fabian Immler, TU Munich
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
     6
 */
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
     7
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
     8
package isabelle.proofdocument
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
     9
34703
ff037c17332a minor tuning;
wenzelm
parents: 34695
diff changeset
    10
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34667
diff changeset
    11
sealed abstract class Edit
3e995f100ad2 sealed Edit;
wenzelm
parents: 34667
diff changeset
    12
{
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    13
  val start: Int
34695
e799546c6928 minor tuning;
wenzelm
parents: 34693
diff changeset
    14
  def before(offset: Int): Int
e799546c6928 minor tuning;
wenzelm
parents: 34693
diff changeset
    15
  def after(offset: Int): Int
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    16
}
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    17
34703
ff037c17332a minor tuning;
wenzelm
parents: 34695
diff changeset
    18
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34667
diff changeset
    19
case class Insert(start: Int, text: String) extends Edit
3e995f100ad2 sealed Edit;
wenzelm
parents: 34667
diff changeset
    20
{
34695
e799546c6928 minor tuning;
wenzelm
parents: 34693
diff changeset
    21
  def before(offset: Int): Int =
e799546c6928 minor tuning;
wenzelm
parents: 34693
diff changeset
    22
    if (start > offset) offset
e799546c6928 minor tuning;
wenzelm
parents: 34693
diff changeset
    23
    else (offset - text.length) max start
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    24
34695
e799546c6928 minor tuning;
wenzelm
parents: 34693
diff changeset
    25
  def after(offset: Int): Int =
e799546c6928 minor tuning;
wenzelm
parents: 34693
diff changeset
    26
    if (start <= offset) offset + text.length else offset
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    27
}
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    28
34703
ff037c17332a minor tuning;
wenzelm
parents: 34695
diff changeset
    29
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34667
diff changeset
    30
case class Remove(start: Int, text: String) extends Edit
3e995f100ad2 sealed Edit;
wenzelm
parents: 34667
diff changeset
    31
{
34695
e799546c6928 minor tuning;
wenzelm
parents: 34693
diff changeset
    32
  def before(offset: Int): Int =
e799546c6928 minor tuning;
wenzelm
parents: 34693
diff changeset
    33
    if (start <= offset) offset + text.length else offset
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    34
34695
e799546c6928 minor tuning;
wenzelm
parents: 34693
diff changeset
    35
  def after(offset: Int): Int =
e799546c6928 minor tuning;
wenzelm
parents: 34693
diff changeset
    36
    if (start > offset) offset
e799546c6928 minor tuning;
wenzelm
parents: 34693
diff changeset
    37
    else (offset - text.length) max start
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    38
}
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    39
// TODO: merge multiple inserts?
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    40
34703
ff037c17332a minor tuning;
wenzelm
parents: 34695
diff changeset
    41
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    42
class Change(
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    43
  val id: String,
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    44
  val parent: Option[Change],
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34667
diff changeset
    45
  val edits: List[Edit])
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    46
{
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34667
diff changeset
    47
  def ancestors(done: Change => Boolean): List[Change] =
3e995f100ad2 sealed Edit;
wenzelm
parents: 34667
diff changeset
    48
    if (done(this)) Nil
3e995f100ad2 sealed Edit;
wenzelm
parents: 34667
diff changeset
    49
    else this :: parent.map(_.ancestors(done)).getOrElse(Nil)
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    50
  def ancestors: List[Change] = ancestors(_ => false)
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    51
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    52
  override def toString =
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    53
    "Change(" + id + " <- " + parent.map(_.id) + ", " + edits + ")"
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents:
diff changeset
    54
}