src/Pure/PIDE/document.scala
author wenzelm
Sun Aug 29 19:04:29 2010 +0200 (2010-08-29)
changeset 38848 9483bb678d96
parent 38845 a9e37daf5bd0
child 38872 26c505765024
permissions -rw-r--r--
use Future.get_finished where this is the intended meaning -- prefer immediate crash over deadlock due to promises that are never fulfilled;
clarified session signalling;
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@38845
   150
    def select_markup[A](range: Text.Range)
wenzelm@38845
   151
      (result: PartialFunction[Text.Info[Any], A])(default: A): Iterator[Text.Info[A]]
wenzelm@38424
   152
  }
wenzelm@38424
   153
wenzelm@38370
   154
  object State
wenzelm@38370
   155
  {
wenzelm@38373
   156
    class Fail(state: State) extends Exception
wenzelm@38373
   157
wenzelm@38417
   158
    val init = State().define_version(Version.init, Map()).assign(Version.init.id, Nil)
wenzelm@38370
   159
wenzelm@38370
   160
    class Assignment(former_assignment: Map[Command, Exec_ID])
wenzelm@38370
   161
    {
wenzelm@38370
   162
      @volatile private var tmp_assignment = former_assignment
wenzelm@38370
   163
      private val promise = Future.promise[Map[Command, Exec_ID]]
wenzelm@38370
   164
      def is_finished: Boolean = promise.is_finished
wenzelm@38370
   165
      def join: Map[Command, Exec_ID] = promise.join
wenzelm@38848
   166
      def get_finished: Map[Command, Exec_ID] = promise.get_finished
wenzelm@38370
   167
      def assign(command_execs: List[(Command, Exec_ID)])
wenzelm@38370
   168
      {
wenzelm@38370
   169
        promise.fulfill(tmp_assignment ++ command_execs)
wenzelm@38370
   170
        tmp_assignment = Map()
wenzelm@38150
   171
      }
wenzelm@38370
   172
    }
wenzelm@38370
   173
  }
wenzelm@38370
   174
wenzelm@38370
   175
  case class State(
wenzelm@38417
   176
    val versions: Map[Version_ID, Version] = Map(),
wenzelm@38370
   177
    val commands: Map[Command_ID, Command.State] = Map(),
wenzelm@38417
   178
    val execs: Map[Exec_ID, (Command.State, Set[Version])] = Map(),
wenzelm@38417
   179
    val assignments: Map[Version, State.Assignment] = Map(),
wenzelm@38841
   180
    val disposed: Set[ID] = Set(),  // FIXME unused!?
wenzelm@38841
   181
    val history: History = History.init)
wenzelm@38370
   182
  {
wenzelm@38373
   183
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   184
wenzelm@38417
   185
    def define_version(version: Version, former_assignment: Map[Command, Exec_ID]): State =
wenzelm@38370
   186
    {
wenzelm@38417
   187
      val id = version.id
wenzelm@38417
   188
      if (versions.isDefinedAt(id) || disposed(id)) fail
wenzelm@38417
   189
      copy(versions = versions + (id -> version),
wenzelm@38417
   190
        assignments = assignments + (version -> new State.Assignment(former_assignment)))
wenzelm@38370
   191
    }
wenzelm@38370
   192
wenzelm@38373
   193
    def define_command(command: Command): State =
wenzelm@38373
   194
    {
wenzelm@38373
   195
      val id = command.id
wenzelm@38373
   196
      if (commands.isDefinedAt(id) || disposed(id)) fail
wenzelm@38373
   197
      copy(commands = commands + (id -> command.empty_state))
wenzelm@38373
   198
    }
wenzelm@38373
   199
wenzelm@38370
   200
    def lookup_command(id: Command_ID): Option[Command] = commands.get(id).map(_.command)
wenzelm@38373
   201
wenzelm@38417
   202
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
wenzelm@38370
   203
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)
wenzelm@38370
   204
    def the_exec_state(id: Exec_ID): Command.State = execs.getOrElse(id, fail)._1
wenzelm@38417
   205
    def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version, fail)
wenzelm@38370
   206
wenzelm@38370
   207
    def accumulate(id: ID, message: XML.Tree): (Command.State, State) =
wenzelm@38370
   208
      execs.get(id) match {
wenzelm@38417
   209
        case Some((st, occs)) =>
wenzelm@38370
   210
          val new_st = st.accumulate(message)
wenzelm@38417
   211
          (new_st, copy(execs = execs + (id -> (new_st, occs))))
wenzelm@38370
   212
        case None =>
wenzelm@38370
   213
          commands.get(id) match {
wenzelm@38370
   214
            case Some(st) =>
wenzelm@38370
   215
              val new_st = st.accumulate(message)
wenzelm@38370
   216
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   217
            case None => fail
wenzelm@38370
   218
          }
wenzelm@38370
   219
      }
wenzelm@38370
   220
wenzelm@38370
   221
    def assign(id: Version_ID, edits: List[(Command_ID, Exec_ID)]): State =
wenzelm@38370
   222
    {
wenzelm@38417
   223
      val version = the_version(id)
wenzelm@38417
   224
      val occs = Set(version)  // FIXME unused (!?)
wenzelm@38370
   225
wenzelm@38370
   226
      var new_execs = execs
wenzelm@38370
   227
      val assigned_execs =
wenzelm@38370
   228
        for ((cmd_id, exec_id) <- edits) yield {
wenzelm@38370
   229
          val st = the_command(cmd_id)
wenzelm@38370
   230
          if (new_execs.isDefinedAt(exec_id) || disposed(exec_id)) fail
wenzelm@38417
   231
          new_execs += (exec_id -> (st, occs))
wenzelm@38370
   232
          (st.command, exec_id)
wenzelm@38370
   233
        }
wenzelm@38417
   234
      the_assignment(version).assign(assigned_execs)  // FIXME explicit value instead of promise (!?)
wenzelm@38370
   235
      copy(execs = new_execs)
wenzelm@38370
   236
    }
wenzelm@38370
   237
wenzelm@38417
   238
    def is_assigned(version: Version): Boolean =
wenzelm@38417
   239
      assignments.get(version) match {
wenzelm@38370
   240
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   241
        case None => false
wenzelm@38370
   242
      }
wenzelm@38370
   243
wenzelm@38841
   244
    def extend_history(previous: Future[Version],
wenzelm@38841
   245
        edits: List[Node_Text_Edit],
wenzelm@38841
   246
        result: Future[(List[Edit[Command]], Version)]): (Change, State) =
wenzelm@38841
   247
    {
wenzelm@38841
   248
      val change = new Change(previous, edits, result)
wenzelm@38841
   249
      (change, copy(history = history + change))
wenzelm@38841
   250
    }
wenzelm@38841
   251
wenzelm@38841
   252
wenzelm@38841
   253
    // persistent user-view
wenzelm@38841
   254
    def snapshot(name: String, pending_edits: List[Text.Edit]): Snapshot =
wenzelm@38370
   255
    {
wenzelm@38841
   256
      val found_stable = history.undo_list.find(change =>
wenzelm@38848
   257
        change.is_finished && is_assigned(change.current.get_finished))
wenzelm@38841
   258
      require(found_stable.isDefined)
wenzelm@38841
   259
      val stable = found_stable.get
wenzelm@38841
   260
      val latest = history.undo_list.head
wenzelm@38841
   261
wenzelm@38841
   262
      val edits =
wenzelm@38841
   263
        (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@38841
   264
            (for ((a, eds) <- change.edits if a == name) yield eds).flatten ::: edits)
wenzelm@38841
   265
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   266
wenzelm@38841
   267
      new Snapshot
wenzelm@38841
   268
      {
wenzelm@38848
   269
        val version = stable.current.get_finished
wenzelm@38841
   270
        val node = version.nodes(name)
wenzelm@38841
   271
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@38841
   272
wenzelm@38841
   273
        def lookup_command(id: Command_ID): Option[Command] = State.this.lookup_command(id)
wenzelm@38841
   274
wenzelm@38841
   275
        def state(command: Command): Command.State =
wenzelm@38848
   276
          try { the_exec_state(the_assignment(version).get_finished.getOrElse(command, fail)) }
wenzelm@38841
   277
          catch { case _: State.Fail => command.empty_state }
wenzelm@38841
   278
wenzelm@38841
   279
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38841
   280
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@38845
   281
wenzelm@38845
   282
        def select_markup[A](range: Text.Range)
wenzelm@38845
   283
          (result: PartialFunction[Text.Info[Any], A])(default: A): Iterator[Text.Info[A]] =
wenzelm@38845
   284
        {
wenzelm@38845
   285
          val former_range = revert(range)
wenzelm@38845
   286
          for {
wenzelm@38845
   287
            (command, command_start) <- node.command_range(former_range)
wenzelm@38845
   288
            Text.Info(r0, x) <- state(command).markup.
wenzelm@38845
   289
              select((former_range - command_start).restrict(command.range)) {
wenzelm@38845
   290
                case Text.Info(r0, info)
wenzelm@38845
   291
                if result.isDefinedAt(Text.Info(convert(r0 + command_start), info)) =>
wenzelm@38845
   292
                  result(Text.Info(convert(r0 + command_start), info))
wenzelm@38845
   293
              } { default }
wenzelm@38845
   294
            val r = convert(r0 + command_start)
wenzelm@38845
   295
            if !r.is_singularity
wenzelm@38845
   296
          } yield Text.Info(r, x)
wenzelm@38845
   297
        }
wenzelm@38841
   298
      }
wenzelm@38150
   299
    }
wenzelm@34485
   300
  }
wenzelm@34840
   301
}
wenzelm@34840
   302