src/Pure/PIDE/document.scala
author wenzelm
Sun Aug 15 22:48:56 2010 +0200 (2010-08-15)
changeset 38426 2858ec7b6dd8
parent 38425 e467db701d78
child 38427 7066fbd315ae
permissions -rw-r--r--
specific types Text.Offset and Text.Range;
minor 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@38426
    68
    def command_range(i: Text.Offset): Iterator[(Command, Text.Offset)] =
wenzelm@38151
    69
      command_starts dropWhile { case (cmd, start) => start + cmd.length <= i }
wenzelm@38151
    70
wenzelm@38426
    71
    def command_range(i: Text.Offset, j: Text.Offset): Iterator[(Command, Text.Offset)] =
wenzelm@38151
    72
      command_range(i) takeWhile { case (_, start) => start < j }
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@38424
   119
  /* history navigation and persistent snapshots */
wenzelm@38424
   120
wenzelm@38424
   121
  abstract class Snapshot
wenzelm@38424
   122
  {
wenzelm@38424
   123
    val version: Version
wenzelm@38424
   124
    val node: Node
wenzelm@38424
   125
    val is_outdated: Boolean
wenzelm@38426
   126
    def convert(i: Text.Offset): Text.Offset
wenzelm@38426
   127
    def revert(i: Text.Offset): Text.Offset
wenzelm@38424
   128
    def lookup_command(id: Command_ID): Option[Command]
wenzelm@38424
   129
    def state(command: Command): Command.State
wenzelm@38424
   130
  }
wenzelm@38424
   131
wenzelm@38424
   132
  object History
wenzelm@38424
   133
  {
wenzelm@38424
   134
    val init = new History(List(Change.init))
wenzelm@38424
   135
  }
wenzelm@38424
   136
wenzelm@38424
   137
  // FIXME pruning, purging of state
wenzelm@38424
   138
  class History(undo_list: List[Change])
wenzelm@38424
   139
  {
wenzelm@38424
   140
    require(!undo_list.isEmpty)
wenzelm@38424
   141
wenzelm@38424
   142
    def tip: Change = undo_list.head
wenzelm@38424
   143
    def +(ch: Change): History = new History(ch :: undo_list)
wenzelm@38424
   144
wenzelm@38425
   145
    def snapshot(name: String, pending_edits: List[Text.Edit], state_snapshot: State): Snapshot =
wenzelm@38424
   146
    {
wenzelm@38424
   147
      val found_stable = undo_list.find(change =>
wenzelm@38424
   148
        change.is_finished && state_snapshot.is_assigned(change.current.join))
wenzelm@38424
   149
      require(found_stable.isDefined)
wenzelm@38424
   150
      val stable = found_stable.get
wenzelm@38424
   151
      val latest = undo_list.head
wenzelm@38424
   152
wenzelm@38424
   153
      val edits =
wenzelm@38424
   154
        (pending_edits /: undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@38424
   155
            (for ((a, eds) <- change.edits if a == name) yield eds).flatten ::: edits)
wenzelm@38424
   156
      lazy val reverse_edits = edits.reverse
wenzelm@38424
   157
wenzelm@38424
   158
      new Snapshot
wenzelm@38424
   159
      {
wenzelm@38424
   160
        val version = stable.current.join
wenzelm@38424
   161
        val node = version.nodes(name)
wenzelm@38424
   162
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@38426
   163
wenzelm@38426
   164
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38426
   165
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@38426
   166
wenzelm@38424
   167
        def lookup_command(id: Command_ID): Option[Command] = state_snapshot.lookup_command(id)
wenzelm@38424
   168
        def state(command: Command): Command.State =
wenzelm@38424
   169
          try { state_snapshot.command_state(version, command) }
wenzelm@38424
   170
          catch { case _: State.Fail => command.empty_state }
wenzelm@38424
   171
      }
wenzelm@38424
   172
    }
wenzelm@38424
   173
  }
wenzelm@38424
   174
wenzelm@38424
   175
wenzelm@38227
   176
wenzelm@38418
   177
  /** global state -- document structure and execution process **/
wenzelm@38370
   178
wenzelm@38370
   179
  object State
wenzelm@38370
   180
  {
wenzelm@38373
   181
    class Fail(state: State) extends Exception
wenzelm@38373
   182
wenzelm@38417
   183
    val init = State().define_version(Version.init, Map()).assign(Version.init.id, Nil)
wenzelm@38370
   184
wenzelm@38370
   185
    class Assignment(former_assignment: Map[Command, Exec_ID])
wenzelm@38370
   186
    {
wenzelm@38370
   187
      @volatile private var tmp_assignment = former_assignment
wenzelm@38370
   188
      private val promise = Future.promise[Map[Command, Exec_ID]]
wenzelm@38370
   189
      def is_finished: Boolean = promise.is_finished
wenzelm@38370
   190
      def join: Map[Command, Exec_ID] = promise.join
wenzelm@38370
   191
      def assign(command_execs: List[(Command, Exec_ID)])
wenzelm@38370
   192
      {
wenzelm@38370
   193
        promise.fulfill(tmp_assignment ++ command_execs)
wenzelm@38370
   194
        tmp_assignment = Map()
wenzelm@38150
   195
      }
wenzelm@38370
   196
    }
wenzelm@38370
   197
  }
wenzelm@38370
   198
wenzelm@38370
   199
  case class State(
wenzelm@38417
   200
    val versions: Map[Version_ID, Version] = Map(),
wenzelm@38370
   201
    val commands: Map[Command_ID, Command.State] = Map(),
wenzelm@38417
   202
    val execs: Map[Exec_ID, (Command.State, Set[Version])] = Map(),
wenzelm@38417
   203
    val assignments: Map[Version, State.Assignment] = Map(),
wenzelm@38370
   204
    val disposed: Set[ID] = Set())  // FIXME unused!?
wenzelm@38370
   205
  {
wenzelm@38373
   206
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   207
wenzelm@38417
   208
    def define_version(version: Version, former_assignment: Map[Command, Exec_ID]): State =
wenzelm@38370
   209
    {
wenzelm@38417
   210
      val id = version.id
wenzelm@38417
   211
      if (versions.isDefinedAt(id) || disposed(id)) fail
wenzelm@38417
   212
      copy(versions = versions + (id -> version),
wenzelm@38417
   213
        assignments = assignments + (version -> new State.Assignment(former_assignment)))
wenzelm@38370
   214
    }
wenzelm@38370
   215
wenzelm@38373
   216
    def define_command(command: Command): State =
wenzelm@38373
   217
    {
wenzelm@38373
   218
      val id = command.id
wenzelm@38373
   219
      if (commands.isDefinedAt(id) || disposed(id)) fail
wenzelm@38373
   220
      copy(commands = commands + (id -> command.empty_state))
wenzelm@38373
   221
    }
wenzelm@38373
   222
wenzelm@38370
   223
    def lookup_command(id: Command_ID): Option[Command] = commands.get(id).map(_.command)
wenzelm@38373
   224
wenzelm@38417
   225
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
wenzelm@38370
   226
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)
wenzelm@38370
   227
    def the_exec_state(id: Exec_ID): Command.State = execs.getOrElse(id, fail)._1
wenzelm@38417
   228
    def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version, fail)
wenzelm@38370
   229
wenzelm@38370
   230
    def accumulate(id: ID, message: XML.Tree): (Command.State, State) =
wenzelm@38370
   231
      execs.get(id) match {
wenzelm@38417
   232
        case Some((st, occs)) =>
wenzelm@38370
   233
          val new_st = st.accumulate(message)
wenzelm@38417
   234
          (new_st, copy(execs = execs + (id -> (new_st, occs))))
wenzelm@38370
   235
        case None =>
wenzelm@38370
   236
          commands.get(id) match {
wenzelm@38370
   237
            case Some(st) =>
wenzelm@38370
   238
              val new_st = st.accumulate(message)
wenzelm@38370
   239
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   240
            case None => fail
wenzelm@38370
   241
          }
wenzelm@38370
   242
      }
wenzelm@38370
   243
wenzelm@38370
   244
    def assign(id: Version_ID, edits: List[(Command_ID, Exec_ID)]): State =
wenzelm@38370
   245
    {
wenzelm@38417
   246
      val version = the_version(id)
wenzelm@38417
   247
      val occs = Set(version)  // FIXME unused (!?)
wenzelm@38370
   248
wenzelm@38370
   249
      var new_execs = execs
wenzelm@38370
   250
      val assigned_execs =
wenzelm@38370
   251
        for ((cmd_id, exec_id) <- edits) yield {
wenzelm@38370
   252
          val st = the_command(cmd_id)
wenzelm@38370
   253
          if (new_execs.isDefinedAt(exec_id) || disposed(exec_id)) fail
wenzelm@38417
   254
          new_execs += (exec_id -> (st, occs))
wenzelm@38370
   255
          (st.command, exec_id)
wenzelm@38370
   256
        }
wenzelm@38417
   257
      the_assignment(version).assign(assigned_execs)  // FIXME explicit value instead of promise (!?)
wenzelm@38370
   258
      copy(execs = new_execs)
wenzelm@38370
   259
    }
wenzelm@38370
   260
wenzelm@38417
   261
    def is_assigned(version: Version): Boolean =
wenzelm@38417
   262
      assignments.get(version) match {
wenzelm@38370
   263
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   264
        case None => false
wenzelm@38370
   265
      }
wenzelm@38370
   266
wenzelm@38417
   267
    def command_state(version: Version, command: Command): Command.State =
wenzelm@38370
   268
    {
wenzelm@38417
   269
      val assgn = the_assignment(version)
wenzelm@38370
   270
      require(assgn.is_finished)
wenzelm@38370
   271
      the_exec_state(assgn.join.getOrElse(command, fail))
wenzelm@38150
   272
    }
wenzelm@34485
   273
  }
wenzelm@34840
   274
}
wenzelm@34840
   275