src/Pure/PIDE/document.scala
author wenzelm
Sun Aug 15 21:03:13 2010 +0200 (2010-08-15)
changeset 38424 940a404e45e2
parent 38418 9a7af64d71bb
child 38425 e467db701d78
permissions -rw-r--r--
moved History/Snapshot to document.scala;
wenzelm@36676
     1
/*  Title:      Pure/PIDE/document.scala
wenzelm@36676
     2
    Author:     Makarius
wenzelm@36676
     3
wenzelm@38150
     4
Document as collection of named nodes, each consisting of an editable
wenzelm@38418
     5
list of commands, associated with asynchronous execution process.
wenzelm@36676
     6
*/
wenzelm@34407
     7
wenzelm@34871
     8
package isabelle
wenzelm@34318
     9
wenzelm@34760
    10
wenzelm@38150
    11
import scala.collection.mutable
wenzelm@37073
    12
wenzelm@37073
    13
wenzelm@34823
    14
object Document
wenzelm@34483
    15
{
wenzelm@38227
    16
  /* formal identifiers */
wenzelm@38150
    17
wenzelm@38363
    18
  type ID = Long
wenzelm@38414
    19
wenzelm@38414
    20
  object ID {
wenzelm@38414
    21
    def apply(id: ID): String = Markup.Long.apply(id)
wenzelm@38414
    22
    def unapply(s: String): Option[ID] = Markup.Long.unapply(s)
wenzelm@38414
    23
  }
wenzelm@38414
    24
wenzelm@38373
    25
  type Version_ID = ID
wenzelm@38363
    26
  type Command_ID = ID
wenzelm@38373
    27
  type Exec_ID = ID
wenzelm@38150
    28
wenzelm@38363
    29
  val NO_ID: ID = 0
wenzelm@38355
    30
wenzelm@38150
    31
wenzelm@34818
    32
wenzelm@38424
    33
  /** document structure **/
wenzelm@38424
    34
wenzelm@38424
    35
  /* named nodes -- development graph */
wenzelm@38424
    36
wenzelm@38424
    37
  type Node_Text_Edit = (String, List[Text_Edit])  // FIXME None: remove
wenzelm@38424
    38
wenzelm@38424
    39
  type Edit[C] =
wenzelm@38424
    40
   (String,  // node name
wenzelm@38424
    41
    Option[List[(Option[C], Option[C])]])  // None: remove, Some: insert/remove commands
wenzelm@38151
    42
wenzelm@38151
    43
  object Node
wenzelm@38151
    44
  {
wenzelm@38151
    45
    val empty: Node = new Node(Linear_Set())
wenzelm@38227
    46
wenzelm@38424
    47
    // FIXME not scalable
wenzelm@38227
    48
    def command_starts(commands: Iterator[Command], offset: Int = 0): Iterator[(Command, Int)] =
wenzelm@38227
    49
    {
wenzelm@38227
    50
      var i = offset
wenzelm@38227
    51
      for (command <- commands) yield {
wenzelm@38227
    52
        val start = i
wenzelm@38227
    53
        i += command.length
wenzelm@38227
    54
        (command, start)
wenzelm@38227
    55
      }
wenzelm@38227
    56
    }
wenzelm@38151
    57
  }
wenzelm@38151
    58
wenzelm@38151
    59
  class Node(val commands: Linear_Set[Command])
wenzelm@38151
    60
  {
wenzelm@38151
    61
    def command_starts: Iterator[(Command, Int)] =
wenzelm@38227
    62
      Node.command_starts(commands.iterator)
wenzelm@38151
    63
wenzelm@38151
    64
    def command_start(cmd: Command): Option[Int] =
wenzelm@38151
    65
      command_starts.find(_._1 == cmd).map(_._2)
wenzelm@38151
    66
wenzelm@38151
    67
    def command_range(i: Int): Iterator[(Command, Int)] =
wenzelm@38151
    68
      command_starts dropWhile { case (cmd, start) => start + cmd.length <= i }
wenzelm@38151
    69
wenzelm@38151
    70
    def command_range(i: Int, j: Int): Iterator[(Command, Int)] =
wenzelm@38151
    71
      command_range(i) takeWhile { case (_, start) => start < j }
wenzelm@38151
    72
wenzelm@38151
    73
    def command_at(i: Int): Option[(Command, Int)] =
wenzelm@38151
    74
    {
wenzelm@38151
    75
      val range = command_range(i)
wenzelm@38151
    76
      if (range.hasNext) Some(range.next) else None
wenzelm@38151
    77
    }
wenzelm@38151
    78
wenzelm@38151
    79
    def proper_command_at(i: Int): Option[Command] =
wenzelm@38151
    80
      command_at(i) match {
wenzelm@38151
    81
        case Some((command, _)) =>
wenzelm@38151
    82
          commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
wenzelm@38151
    83
        case None => None
wenzelm@38151
    84
      }
wenzelm@38151
    85
  }
wenzelm@38151
    86
wenzelm@38151
    87
wenzelm@38424
    88
wenzelm@38424
    89
  /** versioning **/
wenzelm@38424
    90
wenzelm@38424
    91
  /* particular document versions */
wenzelm@34485
    92
wenzelm@38417
    93
  object Version
wenzelm@38417
    94
  {
wenzelm@38417
    95
    val init: Version = new Version(NO_ID, Map().withDefaultValue(Node.empty))
wenzelm@38417
    96
  }
wenzelm@38417
    97
wenzelm@38417
    98
  class Version(val id: Version_ID, val nodes: Map[String, Node])
immler@34660
    99
wenzelm@34859
   100
wenzelm@38424
   101
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   102
wenzelm@38227
   103
  object Change
wenzelm@38227
   104
  {
wenzelm@38417
   105
    val init = new Change(Future.value(Version.init), Nil, Future.value(Nil, Version.init))
wenzelm@38227
   106
  }
wenzelm@38227
   107
wenzelm@38227
   108
  class Change(
wenzelm@38417
   109
    val previous: Future[Version],
wenzelm@38227
   110
    val edits: List[Node_Text_Edit],
wenzelm@38417
   111
    val result: Future[(List[Edit[Command]], Version)])
wenzelm@38227
   112
  {
wenzelm@38417
   113
    val current: Future[Version] = result.map(_._2)
wenzelm@38417
   114
    def is_finished: Boolean = previous.is_finished && current.is_finished
wenzelm@38227
   115
  }
wenzelm@38227
   116
wenzelm@38227
   117
wenzelm@38424
   118
  /* history navigation and persistent snapshots */
wenzelm@38424
   119
wenzelm@38424
   120
  abstract class Snapshot
wenzelm@38424
   121
  {
wenzelm@38424
   122
    val version: Version
wenzelm@38424
   123
    val node: Node
wenzelm@38424
   124
    val is_outdated: Boolean
wenzelm@38424
   125
    def convert(offset: Int): Int
wenzelm@38424
   126
    def revert(offset: Int): Int
wenzelm@38424
   127
    def lookup_command(id: Command_ID): Option[Command]
wenzelm@38424
   128
    def state(command: Command): Command.State
wenzelm@38424
   129
  }
wenzelm@38424
   130
wenzelm@38424
   131
  object History
wenzelm@38424
   132
  {
wenzelm@38424
   133
    val init = new History(List(Change.init))
wenzelm@38424
   134
  }
wenzelm@38424
   135
wenzelm@38424
   136
  // FIXME pruning, purging of state
wenzelm@38424
   137
  class History(undo_list: List[Change])
wenzelm@38424
   138
  {
wenzelm@38424
   139
    require(!undo_list.isEmpty)
wenzelm@38424
   140
wenzelm@38424
   141
    def tip: Change = undo_list.head
wenzelm@38424
   142
    def +(ch: Change): History = new History(ch :: undo_list)
wenzelm@38424
   143
wenzelm@38424
   144
    def snapshot(name: String, pending_edits: List[Text_Edit], state_snapshot: State): Snapshot =
wenzelm@38424
   145
    {
wenzelm@38424
   146
      val found_stable = undo_list.find(change =>
wenzelm@38424
   147
        change.is_finished && state_snapshot.is_assigned(change.current.join))
wenzelm@38424
   148
      require(found_stable.isDefined)
wenzelm@38424
   149
      val stable = found_stable.get
wenzelm@38424
   150
      val latest = undo_list.head
wenzelm@38424
   151
wenzelm@38424
   152
      val edits =
wenzelm@38424
   153
        (pending_edits /: undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@38424
   154
            (for ((a, eds) <- change.edits if a == name) yield eds).flatten ::: edits)
wenzelm@38424
   155
      lazy val reverse_edits = edits.reverse
wenzelm@38424
   156
wenzelm@38424
   157
      new Snapshot
wenzelm@38424
   158
      {
wenzelm@38424
   159
        val version = stable.current.join
wenzelm@38424
   160
        val node = version.nodes(name)
wenzelm@38424
   161
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@38424
   162
        def convert(offset: Int): Int = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38424
   163
        def revert(offset: Int): Int = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@38424
   164
        def lookup_command(id: Command_ID): Option[Command] = state_snapshot.lookup_command(id)
wenzelm@38424
   165
        def state(command: Command): Command.State =
wenzelm@38424
   166
          try { state_snapshot.command_state(version, command) }
wenzelm@38424
   167
          catch { case _: State.Fail => command.empty_state }
wenzelm@38424
   168
      }
wenzelm@38424
   169
    }
wenzelm@38424
   170
  }
wenzelm@38424
   171
wenzelm@38424
   172
wenzelm@38227
   173
wenzelm@38418
   174
  /** global state -- document structure and execution process **/
wenzelm@38370
   175
wenzelm@38370
   176
  object State
wenzelm@38370
   177
  {
wenzelm@38373
   178
    class Fail(state: State) extends Exception
wenzelm@38373
   179
wenzelm@38417
   180
    val init = State().define_version(Version.init, Map()).assign(Version.init.id, Nil)
wenzelm@38370
   181
wenzelm@38370
   182
    class Assignment(former_assignment: Map[Command, Exec_ID])
wenzelm@38370
   183
    {
wenzelm@38370
   184
      @volatile private var tmp_assignment = former_assignment
wenzelm@38370
   185
      private val promise = Future.promise[Map[Command, Exec_ID]]
wenzelm@38370
   186
      def is_finished: Boolean = promise.is_finished
wenzelm@38370
   187
      def join: Map[Command, Exec_ID] = promise.join
wenzelm@38370
   188
      def assign(command_execs: List[(Command, Exec_ID)])
wenzelm@38370
   189
      {
wenzelm@38370
   190
        promise.fulfill(tmp_assignment ++ command_execs)
wenzelm@38370
   191
        tmp_assignment = Map()
wenzelm@38150
   192
      }
wenzelm@38370
   193
    }
wenzelm@38370
   194
  }
wenzelm@38370
   195
wenzelm@38370
   196
  case class State(
wenzelm@38417
   197
    val versions: Map[Version_ID, Version] = Map(),
wenzelm@38370
   198
    val commands: Map[Command_ID, Command.State] = Map(),
wenzelm@38417
   199
    val execs: Map[Exec_ID, (Command.State, Set[Version])] = Map(),
wenzelm@38417
   200
    val assignments: Map[Version, State.Assignment] = Map(),
wenzelm@38370
   201
    val disposed: Set[ID] = Set())  // FIXME unused!?
wenzelm@38370
   202
  {
wenzelm@38373
   203
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   204
wenzelm@38417
   205
    def define_version(version: Version, former_assignment: Map[Command, Exec_ID]): State =
wenzelm@38370
   206
    {
wenzelm@38417
   207
      val id = version.id
wenzelm@38417
   208
      if (versions.isDefinedAt(id) || disposed(id)) fail
wenzelm@38417
   209
      copy(versions = versions + (id -> version),
wenzelm@38417
   210
        assignments = assignments + (version -> new State.Assignment(former_assignment)))
wenzelm@38370
   211
    }
wenzelm@38370
   212
wenzelm@38373
   213
    def define_command(command: Command): State =
wenzelm@38373
   214
    {
wenzelm@38373
   215
      val id = command.id
wenzelm@38373
   216
      if (commands.isDefinedAt(id) || disposed(id)) fail
wenzelm@38373
   217
      copy(commands = commands + (id -> command.empty_state))
wenzelm@38373
   218
    }
wenzelm@38373
   219
wenzelm@38370
   220
    def lookup_command(id: Command_ID): Option[Command] = commands.get(id).map(_.command)
wenzelm@38373
   221
wenzelm@38417
   222
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
wenzelm@38370
   223
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)
wenzelm@38370
   224
    def the_exec_state(id: Exec_ID): Command.State = execs.getOrElse(id, fail)._1
wenzelm@38417
   225
    def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version, fail)
wenzelm@38370
   226
wenzelm@38370
   227
    def accumulate(id: ID, message: XML.Tree): (Command.State, State) =
wenzelm@38370
   228
      execs.get(id) match {
wenzelm@38417
   229
        case Some((st, occs)) =>
wenzelm@38370
   230
          val new_st = st.accumulate(message)
wenzelm@38417
   231
          (new_st, copy(execs = execs + (id -> (new_st, occs))))
wenzelm@38370
   232
        case None =>
wenzelm@38370
   233
          commands.get(id) match {
wenzelm@38370
   234
            case Some(st) =>
wenzelm@38370
   235
              val new_st = st.accumulate(message)
wenzelm@38370
   236
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   237
            case None => fail
wenzelm@38370
   238
          }
wenzelm@38370
   239
      }
wenzelm@38370
   240
wenzelm@38370
   241
    def assign(id: Version_ID, edits: List[(Command_ID, Exec_ID)]): State =
wenzelm@38370
   242
    {
wenzelm@38417
   243
      val version = the_version(id)
wenzelm@38417
   244
      val occs = Set(version)  // FIXME unused (!?)
wenzelm@38370
   245
wenzelm@38370
   246
      var new_execs = execs
wenzelm@38370
   247
      val assigned_execs =
wenzelm@38370
   248
        for ((cmd_id, exec_id) <- edits) yield {
wenzelm@38370
   249
          val st = the_command(cmd_id)
wenzelm@38370
   250
          if (new_execs.isDefinedAt(exec_id) || disposed(exec_id)) fail
wenzelm@38417
   251
          new_execs += (exec_id -> (st, occs))
wenzelm@38370
   252
          (st.command, exec_id)
wenzelm@38370
   253
        }
wenzelm@38417
   254
      the_assignment(version).assign(assigned_execs)  // FIXME explicit value instead of promise (!?)
wenzelm@38370
   255
      copy(execs = new_execs)
wenzelm@38370
   256
    }
wenzelm@38370
   257
wenzelm@38417
   258
    def is_assigned(version: Version): Boolean =
wenzelm@38417
   259
      assignments.get(version) match {
wenzelm@38370
   260
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   261
        case None => false
wenzelm@38370
   262
      }
wenzelm@38370
   263
wenzelm@38417
   264
    def command_state(version: Version, command: Command): Command.State =
wenzelm@38370
   265
    {
wenzelm@38417
   266
      val assgn = the_assignment(version)
wenzelm@38370
   267
      require(assgn.is_finished)
wenzelm@38370
   268
      the_exec_state(assgn.join.getOrElse(command, fail))
wenzelm@38150
   269
    }
wenzelm@34485
   270
  }
wenzelm@34840
   271
}
wenzelm@34840
   272