src/Pure/PIDE/document.scala
author wenzelm
Sun Aug 15 18:41:23 2010 +0200 (2010-08-15)
changeset 38418 9a7af64d71bb
parent 38417 b8922ae21111
child 38424 940a404e45e2
permissions -rw-r--r--
more explicit / functional ML version of document model;
tuned;
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@38227
    33
  /** named document nodes **/
wenzelm@38151
    34
wenzelm@38151
    35
  object Node
wenzelm@38151
    36
  {
wenzelm@38151
    37
    val empty: Node = new Node(Linear_Set())
wenzelm@38227
    38
wenzelm@38227
    39
    def command_starts(commands: Iterator[Command], offset: Int = 0): Iterator[(Command, Int)] =
wenzelm@38227
    40
    {
wenzelm@38227
    41
      var i = offset
wenzelm@38227
    42
      for (command <- commands) yield {
wenzelm@38227
    43
        val start = i
wenzelm@38227
    44
        i += command.length
wenzelm@38227
    45
        (command, start)
wenzelm@38227
    46
      }
wenzelm@38227
    47
    }
wenzelm@38151
    48
  }
wenzelm@38151
    49
wenzelm@38151
    50
  class Node(val commands: Linear_Set[Command])
wenzelm@38151
    51
  {
wenzelm@38151
    52
    /* command ranges */
wenzelm@38151
    53
wenzelm@38151
    54
    def command_starts: Iterator[(Command, Int)] =
wenzelm@38227
    55
      Node.command_starts(commands.iterator)
wenzelm@38151
    56
wenzelm@38151
    57
    def command_start(cmd: Command): Option[Int] =
wenzelm@38151
    58
      command_starts.find(_._1 == cmd).map(_._2)
wenzelm@38151
    59
wenzelm@38151
    60
    def command_range(i: Int): Iterator[(Command, Int)] =
wenzelm@38151
    61
      command_starts dropWhile { case (cmd, start) => start + cmd.length <= i }
wenzelm@38151
    62
wenzelm@38151
    63
    def command_range(i: Int, j: Int): Iterator[(Command, Int)] =
wenzelm@38151
    64
      command_range(i) takeWhile { case (_, start) => start < j }
wenzelm@38151
    65
wenzelm@38151
    66
    def command_at(i: Int): Option[(Command, Int)] =
wenzelm@38151
    67
    {
wenzelm@38151
    68
      val range = command_range(i)
wenzelm@38151
    69
      if (range.hasNext) Some(range.next) else None
wenzelm@38151
    70
    }
wenzelm@38151
    71
wenzelm@38151
    72
    def proper_command_at(i: Int): Option[Command] =
wenzelm@38151
    73
      command_at(i) match {
wenzelm@38151
    74
        case Some((command, _)) =>
wenzelm@38151
    75
          commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
wenzelm@38151
    76
        case None => None
wenzelm@38151
    77
      }
wenzelm@38151
    78
  }
wenzelm@38151
    79
wenzelm@38151
    80
wenzelm@38417
    81
  /* document versions */
wenzelm@34485
    82
wenzelm@38417
    83
  object Version
wenzelm@38417
    84
  {
wenzelm@38417
    85
    val init: Version = new Version(NO_ID, Map().withDefaultValue(Node.empty))
wenzelm@38417
    86
  }
wenzelm@38417
    87
wenzelm@38417
    88
  class Version(val id: Version_ID, val nodes: Map[String, Node])
immler@34660
    89
wenzelm@34859
    90
wenzelm@34859
    91
wenzelm@38227
    92
  /** changes of plain text, eventually resulting in document edits **/
wenzelm@38151
    93
wenzelm@38151
    94
  type Node_Text_Edit = (String, List[Text_Edit])  // FIXME None: remove
wenzelm@38151
    95
wenzelm@38151
    96
  type Edit[C] =
wenzelm@38151
    97
   (String,  // node name
wenzelm@38151
    98
    Option[List[(Option[C], Option[C])]])  // None: remove, Some: insert/remove commands
wenzelm@34859
    99
wenzelm@38227
   100
  abstract class Snapshot
wenzelm@38227
   101
  {
wenzelm@38417
   102
    val version: Version
wenzelm@38417
   103
    val node: Node
wenzelm@38227
   104
    val is_outdated: Boolean
wenzelm@38227
   105
    def convert(offset: Int): Int
wenzelm@38227
   106
    def revert(offset: Int): Int
wenzelm@38370
   107
    def lookup_command(id: Command_ID): Option[Command]
wenzelm@38361
   108
    def state(command: Command): Command.State
wenzelm@38227
   109
  }
wenzelm@38227
   110
wenzelm@38227
   111
  object Change
wenzelm@38227
   112
  {
wenzelm@38417
   113
    val init = new Change(Future.value(Version.init), Nil, Future.value(Nil, Version.init))
wenzelm@38227
   114
  }
wenzelm@38227
   115
wenzelm@38227
   116
  class Change(
wenzelm@38417
   117
    val previous: Future[Version],
wenzelm@38227
   118
    val edits: List[Node_Text_Edit],
wenzelm@38417
   119
    val result: Future[(List[Edit[Command]], Version)])
wenzelm@38227
   120
  {
wenzelm@38417
   121
    val current: Future[Version] = result.map(_._2)
wenzelm@38417
   122
    def is_finished: Boolean = previous.is_finished && current.is_finished
wenzelm@38227
   123
  }
wenzelm@38227
   124
wenzelm@38227
   125
wenzelm@38227
   126
wenzelm@38418
   127
  /** global state -- document structure and execution process **/
wenzelm@38370
   128
wenzelm@38370
   129
  object State
wenzelm@38370
   130
  {
wenzelm@38373
   131
    class Fail(state: State) extends Exception
wenzelm@38373
   132
wenzelm@38417
   133
    val init = State().define_version(Version.init, Map()).assign(Version.init.id, Nil)
wenzelm@38370
   134
wenzelm@38370
   135
    class Assignment(former_assignment: Map[Command, Exec_ID])
wenzelm@38370
   136
    {
wenzelm@38370
   137
      @volatile private var tmp_assignment = former_assignment
wenzelm@38370
   138
      private val promise = Future.promise[Map[Command, Exec_ID]]
wenzelm@38370
   139
      def is_finished: Boolean = promise.is_finished
wenzelm@38370
   140
      def join: Map[Command, Exec_ID] = promise.join
wenzelm@38370
   141
      def assign(command_execs: List[(Command, Exec_ID)])
wenzelm@38370
   142
      {
wenzelm@38370
   143
        promise.fulfill(tmp_assignment ++ command_execs)
wenzelm@38370
   144
        tmp_assignment = Map()
wenzelm@38150
   145
      }
wenzelm@38370
   146
    }
wenzelm@38370
   147
  }
wenzelm@38370
   148
wenzelm@38370
   149
  case class State(
wenzelm@38417
   150
    val versions: Map[Version_ID, Version] = Map(),
wenzelm@38370
   151
    val commands: Map[Command_ID, Command.State] = Map(),
wenzelm@38417
   152
    val execs: Map[Exec_ID, (Command.State, Set[Version])] = Map(),
wenzelm@38417
   153
    val assignments: Map[Version, State.Assignment] = Map(),
wenzelm@38370
   154
    val disposed: Set[ID] = Set())  // FIXME unused!?
wenzelm@38370
   155
  {
wenzelm@38373
   156
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   157
wenzelm@38417
   158
    def define_version(version: Version, former_assignment: Map[Command, Exec_ID]): State =
wenzelm@38370
   159
    {
wenzelm@38417
   160
      val id = version.id
wenzelm@38417
   161
      if (versions.isDefinedAt(id) || disposed(id)) fail
wenzelm@38417
   162
      copy(versions = versions + (id -> version),
wenzelm@38417
   163
        assignments = assignments + (version -> new State.Assignment(former_assignment)))
wenzelm@38370
   164
    }
wenzelm@38370
   165
wenzelm@38373
   166
    def define_command(command: Command): State =
wenzelm@38373
   167
    {
wenzelm@38373
   168
      val id = command.id
wenzelm@38373
   169
      if (commands.isDefinedAt(id) || disposed(id)) fail
wenzelm@38373
   170
      copy(commands = commands + (id -> command.empty_state))
wenzelm@38373
   171
    }
wenzelm@38373
   172
wenzelm@38370
   173
    def lookup_command(id: Command_ID): Option[Command] = commands.get(id).map(_.command)
wenzelm@38373
   174
wenzelm@38417
   175
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
wenzelm@38370
   176
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)
wenzelm@38370
   177
    def the_exec_state(id: Exec_ID): Command.State = execs.getOrElse(id, fail)._1
wenzelm@38417
   178
    def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version, fail)
wenzelm@38370
   179
wenzelm@38370
   180
    def accumulate(id: ID, message: XML.Tree): (Command.State, State) =
wenzelm@38370
   181
      execs.get(id) match {
wenzelm@38417
   182
        case Some((st, occs)) =>
wenzelm@38370
   183
          val new_st = st.accumulate(message)
wenzelm@38417
   184
          (new_st, copy(execs = execs + (id -> (new_st, occs))))
wenzelm@38370
   185
        case None =>
wenzelm@38370
   186
          commands.get(id) match {
wenzelm@38370
   187
            case Some(st) =>
wenzelm@38370
   188
              val new_st = st.accumulate(message)
wenzelm@38370
   189
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   190
            case None => fail
wenzelm@38370
   191
          }
wenzelm@38370
   192
      }
wenzelm@38370
   193
wenzelm@38370
   194
    def assign(id: Version_ID, edits: List[(Command_ID, Exec_ID)]): State =
wenzelm@38370
   195
    {
wenzelm@38417
   196
      val version = the_version(id)
wenzelm@38417
   197
      val occs = Set(version)  // FIXME unused (!?)
wenzelm@38370
   198
wenzelm@38370
   199
      var new_execs = execs
wenzelm@38370
   200
      val assigned_execs =
wenzelm@38370
   201
        for ((cmd_id, exec_id) <- edits) yield {
wenzelm@38370
   202
          val st = the_command(cmd_id)
wenzelm@38370
   203
          if (new_execs.isDefinedAt(exec_id) || disposed(exec_id)) fail
wenzelm@38417
   204
          new_execs += (exec_id -> (st, occs))
wenzelm@38370
   205
          (st.command, exec_id)
wenzelm@38370
   206
        }
wenzelm@38417
   207
      the_assignment(version).assign(assigned_execs)  // FIXME explicit value instead of promise (!?)
wenzelm@38370
   208
      copy(execs = new_execs)
wenzelm@38370
   209
    }
wenzelm@38370
   210
wenzelm@38417
   211
    def is_assigned(version: Version): Boolean =
wenzelm@38417
   212
      assignments.get(version) match {
wenzelm@38370
   213
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   214
        case None => false
wenzelm@38370
   215
      }
wenzelm@38370
   216
wenzelm@38417
   217
    def command_state(version: Version, command: Command): Command.State =
wenzelm@38370
   218
    {
wenzelm@38417
   219
      val assgn = the_assignment(version)
wenzelm@38370
   220
      require(assgn.is_finished)
wenzelm@38370
   221
      the_exec_state(assgn.join.getOrElse(command, fail))
wenzelm@38150
   222
    }
wenzelm@34485
   223
  }
wenzelm@34840
   224
}
wenzelm@34840
   225