src/Pure/PIDE/document.scala
author wenzelm
Sat Aug 28 17:27:38 2010 +0200 (2010-08-28 ago)
changeset 38841 4df7b76249a0
parent 38569 9d480f6a2589
child 38845 a9e37daf5bd0
permissions -rw-r--r--
include Document.History in Document.State -- just one universal session state maintained by main actor;
Session.command_change_buffer: thread actor ensures asynchronous dispatch;
misc tuning;
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@38425
    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@38426
    48
    def command_starts(commands: Iterator[Command], offset: Text.Offset = 0)
wenzelm@38426
    49
      : Iterator[(Command, Text.Offset)] =
wenzelm@38227
    50
    {
wenzelm@38227
    51
      var i = offset
wenzelm@38227
    52
      for (command <- commands) yield {
wenzelm@38227
    53
        val start = i
wenzelm@38227
    54
        i += command.length
wenzelm@38227
    55
        (command, start)
wenzelm@38227
    56
      }
wenzelm@38227
    57
    }
wenzelm@38151
    58
  }
wenzelm@38151
    59
wenzelm@38151
    60
  class Node(val commands: Linear_Set[Command])
wenzelm@38151
    61
  {
wenzelm@38426
    62
    def command_starts: Iterator[(Command, Text.Offset)] =
wenzelm@38227
    63
      Node.command_starts(commands.iterator)
wenzelm@38151
    64
wenzelm@38426
    65
    def command_start(cmd: Command): Option[Text.Offset] =
wenzelm@38151
    66
      command_starts.find(_._1 == cmd).map(_._2)
wenzelm@38151
    67
wenzelm@38569
    68
    def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@38151
    69
      command_starts dropWhile { case (cmd, start) => start + cmd.length <= i }
wenzelm@38151
    70
wenzelm@38569
    71
    def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] =
wenzelm@38569
    72
      command_range(range.start) takeWhile { case (_, start) => start < range.stop }
wenzelm@38151
    73
wenzelm@38426
    74
    def command_at(i: Text.Offset): Option[(Command, Text.Offset)] =
wenzelm@38151
    75
    {
wenzelm@38151
    76
      val range = command_range(i)
wenzelm@38151
    77
      if (range.hasNext) Some(range.next) else None
wenzelm@38151
    78
    }
wenzelm@38151
    79
wenzelm@38426
    80
    def proper_command_at(i: Text.Offset): Option[Command] =
wenzelm@38151
    81
      command_at(i) match {
wenzelm@38151
    82
        case Some((command, _)) =>
wenzelm@38151
    83
          commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
wenzelm@38151
    84
        case None => None
wenzelm@38151
    85
      }
wenzelm@38151
    86
  }
wenzelm@38151
    87
wenzelm@38151
    88
wenzelm@38424
    89
wenzelm@38424
    90
  /** versioning **/
wenzelm@38424
    91
wenzelm@38424
    92
  /* particular document versions */
wenzelm@34485
    93
wenzelm@38417
    94
  object Version
wenzelm@38417
    95
  {
wenzelm@38417
    96
    val init: Version = new Version(NO_ID, Map().withDefaultValue(Node.empty))
wenzelm@38417
    97
  }
wenzelm@38417
    98
wenzelm@38417
    99
  class Version(val id: Version_ID, val nodes: Map[String, Node])
immler@34660
   100
wenzelm@34859
   101
wenzelm@38424
   102
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   103
wenzelm@38227
   104
  object Change
wenzelm@38227
   105
  {
wenzelm@38417
   106
    val init = new Change(Future.value(Version.init), Nil, Future.value(Nil, Version.init))
wenzelm@38227
   107
  }
wenzelm@38227
   108
wenzelm@38227
   109
  class Change(
wenzelm@38417
   110
    val previous: Future[Version],
wenzelm@38227
   111
    val edits: List[Node_Text_Edit],
wenzelm@38417
   112
    val result: Future[(List[Edit[Command]], Version)])
wenzelm@38227
   113
  {
wenzelm@38417
   114
    val current: Future[Version] = result.map(_._2)
wenzelm@38417
   115
    def is_finished: Boolean = previous.is_finished && current.is_finished
wenzelm@38227
   116
  }
wenzelm@38227
   117
wenzelm@38227
   118
wenzelm@38841
   119
  /* history navigation */
wenzelm@38841
   120
wenzelm@38841
   121
  object History
wenzelm@38841
   122
  {
wenzelm@38841
   123
    val init = new History(List(Change.init))
wenzelm@38841
   124
  }
wenzelm@38841
   125
wenzelm@38841
   126
  // FIXME pruning, purging of state
wenzelm@38841
   127
  class History(val undo_list: List[Change])
wenzelm@38841
   128
  {
wenzelm@38841
   129
    require(!undo_list.isEmpty)
wenzelm@38841
   130
wenzelm@38841
   131
    def tip: Change = undo_list.head
wenzelm@38841
   132
    def +(change: Change): History = new History(change :: undo_list)
wenzelm@38841
   133
  }
wenzelm@38841
   134
wenzelm@38841
   135
wenzelm@38841
   136
wenzelm@38841
   137
  /** global state -- document structure, execution process, editing history **/
wenzelm@38424
   138
wenzelm@38424
   139
  abstract class Snapshot
wenzelm@38424
   140
  {
wenzelm@38424
   141
    val version: Version
wenzelm@38424
   142
    val node: Node
wenzelm@38424
   143
    val is_outdated: Boolean
wenzelm@38424
   144
    def lookup_command(id: Command_ID): Option[Command]
wenzelm@38424
   145
    def state(command: Command): Command.State
wenzelm@38427
   146
    def convert(i: Text.Offset): Text.Offset
wenzelm@38427
   147
    def convert(range: Text.Range): Text.Range = range.map(convert(_))
wenzelm@38427
   148
    def revert(i: Text.Offset): Text.Offset
wenzelm@38427
   149
    def revert(range: Text.Range): Text.Range = range.map(revert(_))
wenzelm@38424
   150
  }
wenzelm@38424
   151
wenzelm@38370
   152
  object State
wenzelm@38370
   153
  {
wenzelm@38373
   154
    class Fail(state: State) extends Exception
wenzelm@38373
   155
wenzelm@38417
   156
    val init = State().define_version(Version.init, Map()).assign(Version.init.id, Nil)
wenzelm@38370
   157
wenzelm@38370
   158
    class Assignment(former_assignment: Map[Command, Exec_ID])
wenzelm@38370
   159
    {
wenzelm@38370
   160
      @volatile private var tmp_assignment = former_assignment
wenzelm@38370
   161
      private val promise = Future.promise[Map[Command, Exec_ID]]
wenzelm@38370
   162
      def is_finished: Boolean = promise.is_finished
wenzelm@38370
   163
      def join: Map[Command, Exec_ID] = promise.join
wenzelm@38370
   164
      def assign(command_execs: List[(Command, Exec_ID)])
wenzelm@38370
   165
      {
wenzelm@38370
   166
        promise.fulfill(tmp_assignment ++ command_execs)
wenzelm@38370
   167
        tmp_assignment = Map()
wenzelm@38150
   168
      }
wenzelm@38370
   169
    }
wenzelm@38370
   170
  }
wenzelm@38370
   171
wenzelm@38370
   172
  case class State(
wenzelm@38417
   173
    val versions: Map[Version_ID, Version] = Map(),
wenzelm@38370
   174
    val commands: Map[Command_ID, Command.State] = Map(),
wenzelm@38417
   175
    val execs: Map[Exec_ID, (Command.State, Set[Version])] = Map(),
wenzelm@38417
   176
    val assignments: Map[Version, State.Assignment] = Map(),
wenzelm@38841
   177
    val disposed: Set[ID] = Set(),  // FIXME unused!?
wenzelm@38841
   178
    val history: History = History.init)
wenzelm@38370
   179
  {
wenzelm@38373
   180
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   181
wenzelm@38417
   182
    def define_version(version: Version, former_assignment: Map[Command, Exec_ID]): State =
wenzelm@38370
   183
    {
wenzelm@38417
   184
      val id = version.id
wenzelm@38417
   185
      if (versions.isDefinedAt(id) || disposed(id)) fail
wenzelm@38417
   186
      copy(versions = versions + (id -> version),
wenzelm@38417
   187
        assignments = assignments + (version -> new State.Assignment(former_assignment)))
wenzelm@38370
   188
    }
wenzelm@38370
   189
wenzelm@38373
   190
    def define_command(command: Command): State =
wenzelm@38373
   191
    {
wenzelm@38373
   192
      val id = command.id
wenzelm@38373
   193
      if (commands.isDefinedAt(id) || disposed(id)) fail
wenzelm@38373
   194
      copy(commands = commands + (id -> command.empty_state))
wenzelm@38373
   195
    }
wenzelm@38373
   196
wenzelm@38370
   197
    def lookup_command(id: Command_ID): Option[Command] = commands.get(id).map(_.command)
wenzelm@38373
   198
wenzelm@38417
   199
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
wenzelm@38370
   200
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)
wenzelm@38370
   201
    def the_exec_state(id: Exec_ID): Command.State = execs.getOrElse(id, fail)._1
wenzelm@38417
   202
    def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version, fail)
wenzelm@38370
   203
wenzelm@38370
   204
    def accumulate(id: ID, message: XML.Tree): (Command.State, State) =
wenzelm@38370
   205
      execs.get(id) match {
wenzelm@38417
   206
        case Some((st, occs)) =>
wenzelm@38370
   207
          val new_st = st.accumulate(message)
wenzelm@38417
   208
          (new_st, copy(execs = execs + (id -> (new_st, occs))))
wenzelm@38370
   209
        case None =>
wenzelm@38370
   210
          commands.get(id) match {
wenzelm@38370
   211
            case Some(st) =>
wenzelm@38370
   212
              val new_st = st.accumulate(message)
wenzelm@38370
   213
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   214
            case None => fail
wenzelm@38370
   215
          }
wenzelm@38370
   216
      }
wenzelm@38370
   217
wenzelm@38370
   218
    def assign(id: Version_ID, edits: List[(Command_ID, Exec_ID)]): State =
wenzelm@38370
   219
    {
wenzelm@38417
   220
      val version = the_version(id)
wenzelm@38417
   221
      val occs = Set(version)  // FIXME unused (!?)
wenzelm@38370
   222
wenzelm@38370
   223
      var new_execs = execs
wenzelm@38370
   224
      val assigned_execs =
wenzelm@38370
   225
        for ((cmd_id, exec_id) <- edits) yield {
wenzelm@38370
   226
          val st = the_command(cmd_id)
wenzelm@38370
   227
          if (new_execs.isDefinedAt(exec_id) || disposed(exec_id)) fail
wenzelm@38417
   228
          new_execs += (exec_id -> (st, occs))
wenzelm@38370
   229
          (st.command, exec_id)
wenzelm@38370
   230
        }
wenzelm@38417
   231
      the_assignment(version).assign(assigned_execs)  // FIXME explicit value instead of promise (!?)
wenzelm@38370
   232
      copy(execs = new_execs)
wenzelm@38370
   233
    }
wenzelm@38370
   234
wenzelm@38417
   235
    def is_assigned(version: Version): Boolean =
wenzelm@38417
   236
      assignments.get(version) match {
wenzelm@38370
   237
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   238
        case None => false
wenzelm@38370
   239
      }
wenzelm@38370
   240
wenzelm@38841
   241
    def extend_history(previous: Future[Version],
wenzelm@38841
   242
        edits: List[Node_Text_Edit],
wenzelm@38841
   243
        result: Future[(List[Edit[Command]], Version)]): (Change, State) =
wenzelm@38841
   244
    {
wenzelm@38841
   245
      val change = new Change(previous, edits, result)
wenzelm@38841
   246
      (change, copy(history = history + change))
wenzelm@38841
   247
    }
wenzelm@38841
   248
wenzelm@38841
   249
wenzelm@38841
   250
    // persistent user-view
wenzelm@38841
   251
    def snapshot(name: String, pending_edits: List[Text.Edit]): Snapshot =
wenzelm@38370
   252
    {
wenzelm@38841
   253
      val found_stable = history.undo_list.find(change =>
wenzelm@38841
   254
        change.is_finished && is_assigned(change.current.join))
wenzelm@38841
   255
      require(found_stable.isDefined)
wenzelm@38841
   256
      val stable = found_stable.get
wenzelm@38841
   257
      val latest = history.undo_list.head
wenzelm@38841
   258
wenzelm@38841
   259
      val edits =
wenzelm@38841
   260
        (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@38841
   261
            (for ((a, eds) <- change.edits if a == name) yield eds).flatten ::: edits)
wenzelm@38841
   262
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   263
wenzelm@38841
   264
      new Snapshot
wenzelm@38841
   265
      {
wenzelm@38841
   266
        val version = stable.current.join
wenzelm@38841
   267
        val node = version.nodes(name)
wenzelm@38841
   268
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@38841
   269
wenzelm@38841
   270
        def lookup_command(id: Command_ID): Option[Command] = State.this.lookup_command(id)
wenzelm@38841
   271
wenzelm@38841
   272
        def state(command: Command): Command.State =
wenzelm@38841
   273
          try { the_exec_state(the_assignment(version).join.getOrElse(command, fail)) }
wenzelm@38841
   274
          catch { case _: State.Fail => command.empty_state }
wenzelm@38841
   275
wenzelm@38841
   276
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38841
   277
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@38841
   278
      }
wenzelm@38150
   279
    }
wenzelm@34485
   280
  }
wenzelm@34840
   281
}
wenzelm@34840
   282