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