src/Tools/jEdit/src/proofdocument/Change.scala
changeset 34660 e0561943bfc9
child 34667 3f20110dfe2f
equal deleted inserted replaced
34659:e888d0cdda9c 34660:e0561943bfc9
       
     1 /*
       
     2  * Changes in text
       
     3  *
       
     4  * @author Johannes Hölzl, TU Munich
       
     5  * @author Fabian Immler, TU Munich
       
     6  */
       
     7 
       
     8 package isabelle.proofdocument
       
     9 
       
    10 abstract class Edit {
       
    11   val start: Int
       
    12   def from_where (x: Int): Int //where has x been before applying this edit
       
    13   def where_to(x: Int): Int //where will x be when we apply this edit
       
    14 }
       
    15 
       
    16 case class Insert(start: Int, added: String) extends Edit {
       
    17   def from_where(x: Int) =
       
    18     if (start <= x && start + added.length <= x) x - added.length else x
       
    19 
       
    20   def where_to(x: Int) =
       
    21     if (start <= x) x + added.length else x
       
    22 }
       
    23 
       
    24 case class Remove(start: Int, removed: String) extends Edit {
       
    25   def from_where(x: Int) =
       
    26     if (start <= x) x + removed.length else x
       
    27 
       
    28   def where_to(x: Int) =
       
    29     if (start <= x && start + removed.length <= x) x - removed.length else x
       
    30 }
       
    31 // TODO: merge multiple inserts?
       
    32 
       
    33 class Change(
       
    34   val id: String,
       
    35   val parent: Option[Change],
       
    36   edits: List[Edit]) extends Iterable[Edit]
       
    37 {
       
    38   override def elements = edits.reverse.elements
       
    39 
       
    40   def ancestors(root_p: Change => Boolean): List[Change] =
       
    41     if (root_p(this)) Nil
       
    42     else this :: parent.map(_.ancestors(root_p)).getOrElse(Nil)
       
    43   def ancestors: List[Change] = ancestors(_ => false)
       
    44 
       
    45   override def toString =
       
    46     "Change(" + id + " <- " + parent.map(_.id) + ", " + edits + ")"
       
    47 }