src/Pure/PIDE/document.scala
author wenzelm
Sat Jan 07 12:39:46 2012 +0100 (2012-01-07 ago)
changeset 46152 793cecd4ffc0
parent 45900 793bf5fa5fbf
child 46178 1c5c88f6feb5
permissions -rw-r--r--
accumulate status as regular markup for command range;
tuned signature;
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@43780
    19
  val ID = Properties.Value.Long
wenzelm@38414
    20
wenzelm@38373
    21
  type Version_ID = ID
wenzelm@38363
    22
  type Command_ID = ID
wenzelm@38373
    23
  type Exec_ID = ID
wenzelm@38150
    24
wenzelm@43662
    25
  val no_id: ID = 0
wenzelm@45243
    26
  val new_id = Counter()
wenzelm@38355
    27
wenzelm@38150
    28
wenzelm@34818
    29
wenzelm@38424
    30
  /** document structure **/
wenzelm@38424
    31
wenzelm@38424
    32
  /* named nodes -- development graph */
wenzelm@38424
    33
wenzelm@44615
    34
  type Edit[A, B] = (Node.Name, Node.Edit[A, B])
wenzelm@44384
    35
  type Edit_Text = Edit[Text.Edit, Text.Perspective]
wenzelm@44384
    36
  type Edit_Command = Edit[(Option[Command], Option[Command]), Command.Perspective]
wenzelm@38151
    37
wenzelm@44182
    38
  type Node_Header = Exn.Result[Thy_Header]
wenzelm@44182
    39
wenzelm@38151
    40
  object Node
wenzelm@38151
    41
  {
wenzelm@44957
    42
    object Name
wenzelm@44957
    43
    {
wenzelm@44957
    44
      val empty = Name("", "", "")
wenzelm@44959
    45
      def apply(path: Path): Name =
wenzelm@44959
    46
      {
wenzelm@44959
    47
        val node = path.implode
wenzelm@44959
    48
        val dir = path.dir.implode
wenzelm@44959
    49
        val theory = Thy_Header.thy_name(node) getOrElse error("Bad theory file name: " + path)
wenzelm@44959
    50
        Name(node, dir, theory)
wenzelm@44959
    51
      }
wenzelm@44957
    52
    }
wenzelm@44616
    53
    sealed case class Name(node: String, dir: String, theory: String)
wenzelm@44615
    54
    {
wenzelm@44615
    55
      override def hashCode: Int = node.hashCode
wenzelm@44615
    56
      override def equals(that: Any): Boolean =
wenzelm@44615
    57
        that match {
wenzelm@44615
    58
          case other: Name => node == other.node
wenzelm@44615
    59
          case _ => false
wenzelm@44615
    60
        }
wenzelm@44642
    61
      override def toString: String = theory
wenzelm@44615
    62
    }
wenzelm@44615
    63
wenzelm@44384
    64
    sealed abstract class Edit[A, B]
wenzelm@44156
    65
    {
wenzelm@44383
    66
      def foreach(f: A => Unit)
wenzelm@44383
    67
      {
wenzelm@44156
    68
        this match {
wenzelm@44383
    69
          case Edits(es) => es.foreach(f)
wenzelm@44383
    70
          case _ =>
wenzelm@44156
    71
        }
wenzelm@44383
    72
      }
wenzelm@44156
    73
    }
wenzelm@44384
    74
    case class Clear[A, B]() extends Edit[A, B]
wenzelm@44384
    75
    case class Edits[A, B](edits: List[A]) extends Edit[A, B]
wenzelm@44384
    76
    case class Header[A, B](header: Node_Header) extends Edit[A, B]
wenzelm@44384
    77
    case class Perspective[A, B](perspective: B) extends Edit[A, B]
wenzelm@44156
    78
wenzelm@44442
    79
    def norm_header(f: String => String, g: String => String, header: Node_Header): Node_Header =
wenzelm@44182
    80
      header match {
wenzelm@44442
    81
        case Exn.Res(h) => Exn.capture { h.norm_deps(f, g) }
wenzelm@44442
    82
        case exn => exn
wenzelm@44182
    83
      }
wenzelm@43697
    84
wenzelm@44474
    85
    val empty: Node =
wenzelm@44474
    86
      Node(Exn.Exn(ERROR("Bad theory header")), Command.Perspective.empty, Map(), Linear_Set())
wenzelm@38227
    87
wenzelm@38426
    88
    def command_starts(commands: Iterator[Command], offset: Text.Offset = 0)
wenzelm@38426
    89
      : Iterator[(Command, Text.Offset)] =
wenzelm@38227
    90
    {
wenzelm@38227
    91
      var i = offset
wenzelm@38227
    92
      for (command <- commands) yield {
wenzelm@38227
    93
        val start = i
wenzelm@38227
    94
        i += command.length
wenzelm@38227
    95
        (command, start)
wenzelm@38227
    96
      }
wenzelm@38227
    97
    }
wenzelm@38151
    98
  }
wenzelm@38151
    99
wenzelm@38880
   100
  private val block_size = 1024
wenzelm@38879
   101
wenzelm@43715
   102
  sealed case class Node(
wenzelm@44182
   103
    val header: Node_Header,
wenzelm@44385
   104
    val perspective: Command.Perspective,
wenzelm@43715
   105
    val blobs: Map[String, Blob],
wenzelm@43715
   106
    val commands: Linear_Set[Command])
wenzelm@38151
   107
  {
wenzelm@44443
   108
    def clear: Node = Node.empty.copy(header = header)
wenzelm@44443
   109
wenzelm@44443
   110
wenzelm@43697
   111
    /* commands */
wenzelm@43697
   112
wenzelm@38879
   113
    private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
wenzelm@38879
   114
    {
wenzelm@38879
   115
      val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
wenzelm@38879
   116
      var next_block = 0
wenzelm@38879
   117
      var last_stop = 0
wenzelm@38879
   118
      for ((command, start) <- Node.command_starts(commands.iterator)) {
wenzelm@38879
   119
        last_stop = start + command.length
wenzelm@38885
   120
        while (last_stop + 1 > next_block) {
wenzelm@38879
   121
          blocks += (command -> start)
wenzelm@38879
   122
          next_block += block_size
wenzelm@38879
   123
        }
wenzelm@38879
   124
      }
wenzelm@38879
   125
      (blocks.toArray, Text.Range(0, last_stop))
wenzelm@38879
   126
    }
wenzelm@38151
   127
wenzelm@38879
   128
    def full_range: Text.Range = full_index._2
wenzelm@38151
   129
wenzelm@38569
   130
    def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@38879
   131
    {
wenzelm@38879
   132
      if (!commands.isEmpty && full_range.contains(i)) {
wenzelm@38879
   133
        val (cmd0, start0) = full_index._1(i / block_size)
wenzelm@38879
   134
        Node.command_starts(commands.iterator(cmd0), start0) dropWhile {
wenzelm@38879
   135
          case (cmd, start) => start + cmd.length <= i }
wenzelm@38879
   136
      }
wenzelm@38879
   137
      else Iterator.empty
wenzelm@38879
   138
    }
wenzelm@38151
   139
wenzelm@38569
   140
    def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] =
wenzelm@38569
   141
      command_range(range.start) takeWhile { case (_, start) => start < range.stop }
wenzelm@38151
   142
wenzelm@38426
   143
    def command_at(i: Text.Offset): Option[(Command, Text.Offset)] =
wenzelm@38151
   144
    {
wenzelm@38151
   145
      val range = command_range(i)
wenzelm@38151
   146
      if (range.hasNext) Some(range.next) else None
wenzelm@38151
   147
    }
wenzelm@38151
   148
wenzelm@38426
   149
    def proper_command_at(i: Text.Offset): Option[Command] =
wenzelm@38151
   150
      command_at(i) match {
wenzelm@38151
   151
        case Some((command, _)) =>
wenzelm@38151
   152
          commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
wenzelm@38151
   153
        case None => None
wenzelm@38151
   154
      }
wenzelm@38879
   155
wenzelm@38879
   156
    def command_start(cmd: Command): Option[Text.Offset] =
wenzelm@38879
   157
      command_starts.find(_._1 == cmd).map(_._2)
wenzelm@38879
   158
wenzelm@38879
   159
    def command_starts: Iterator[(Command, Text.Offset)] =
wenzelm@38879
   160
      Node.command_starts(commands.iterator)
wenzelm@38151
   161
  }
wenzelm@38151
   162
wenzelm@38151
   163
wenzelm@38424
   164
wenzelm@38424
   165
  /** versioning **/
wenzelm@38424
   166
wenzelm@38424
   167
  /* particular document versions */
wenzelm@34485
   168
wenzelm@38417
   169
  object Version
wenzelm@38417
   170
  {
wenzelm@43722
   171
    val init: Version = Version(no_id, Map().withDefaultValue(Node.empty))
wenzelm@38417
   172
  }
wenzelm@38417
   173
wenzelm@44615
   174
  type Nodes = Map[Node.Name, Node]
wenzelm@44436
   175
  sealed case class Version(val id: Version_ID, val nodes: Nodes)
wenzelm@44960
   176
  {
wenzelm@44960
   177
    def topological_order: List[Node.Name] =
wenzelm@44960
   178
    {
wenzelm@44960
   179
      val names = nodes.map({ case (name, node) => (name.node -> name) })
wenzelm@44960
   180
      def next(x: Node.Name): List[Node.Name] =
wenzelm@44960
   181
        nodes(x).header match {
wenzelm@44960
   182
          case Exn.Res(header) =>
wenzelm@44960
   183
            for (imp <- header.imports; name <- names.get(imp)) yield(name)
wenzelm@44960
   184
          case Exn.Exn(_) => Nil
wenzelm@44960
   185
        }
wenzelm@45900
   186
      Library.topological_order(next, nodes.keys.toList.sortBy(_.node))
wenzelm@44960
   187
    }
wenzelm@44960
   188
  }
immler@34660
   189
wenzelm@34859
   190
wenzelm@38424
   191
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   192
wenzelm@38227
   193
  object Change
wenzelm@38227
   194
  {
wenzelm@44672
   195
    val init: Change = Change(Some(Future.value(Version.init)), Nil, Future.value(Version.init))
wenzelm@38227
   196
  }
wenzelm@38227
   197
wenzelm@43722
   198
  sealed case class Change(
wenzelm@44672
   199
    val previous: Option[Future[Version]],
wenzelm@40479
   200
    val edits: List[Edit_Text],
wenzelm@43722
   201
    val version: Future[Version])
wenzelm@38227
   202
  {
wenzelm@44672
   203
    def is_finished: Boolean =
wenzelm@44672
   204
      (previous match { case None => true case Some(future) => future.is_finished }) &&
wenzelm@44672
   205
      version.is_finished
wenzelm@44672
   206
wenzelm@44672
   207
    def truncate: Change = copy(previous = None, edits = Nil)
wenzelm@38227
   208
  }
wenzelm@38227
   209
wenzelm@38227
   210
wenzelm@38841
   211
  /* history navigation */
wenzelm@38841
   212
wenzelm@38841
   213
  object History
wenzelm@38841
   214
  {
wenzelm@44672
   215
    val init: History = History(List(Change.init))
wenzelm@38841
   216
  }
wenzelm@38841
   217
wenzelm@38841
   218
  // FIXME pruning, purging of state
wenzelm@44672
   219
  sealed case class History(val undo_list: List[Change])
wenzelm@38841
   220
  {
wenzelm@38841
   221
    require(!undo_list.isEmpty)
wenzelm@38841
   222
wenzelm@38841
   223
    def tip: Change = undo_list.head
wenzelm@44672
   224
    def +(change: Change): History = copy(undo_list = change :: undo_list)
wenzelm@38841
   225
  }
wenzelm@38841
   226
wenzelm@38841
   227
wenzelm@38841
   228
wenzelm@38841
   229
  /** global state -- document structure, execution process, editing history **/
wenzelm@38424
   230
wenzelm@38424
   231
  abstract class Snapshot
wenzelm@38424
   232
  {
wenzelm@44582
   233
    val state: State
wenzelm@38424
   234
    val version: Version
wenzelm@38424
   235
    val node: Node
wenzelm@38424
   236
    val is_outdated: Boolean
wenzelm@44582
   237
    def command_state(command: Command): Command.State
wenzelm@38427
   238
    def convert(i: Text.Offset): Text.Offset
wenzelm@39177
   239
    def convert(range: Text.Range): Text.Range
wenzelm@38427
   240
    def revert(i: Text.Offset): Text.Offset
wenzelm@39177
   241
    def revert(range: Text.Range): Text.Range
wenzelm@45467
   242
    def cumulate_markup[A](range: Text.Range)(body: Markup_Tree.Cumulate[A]): Stream[Text.Info[A]]
wenzelm@45468
   243
    def select_markup[A](range: Text.Range)(body: Markup_Tree.Select[A])
wenzelm@39177
   244
      : Stream[Text.Info[Option[A]]]
wenzelm@38424
   245
  }
wenzelm@38424
   246
wenzelm@44476
   247
  type Assign =
wenzelm@44479
   248
   (List[(Document.Command_ID, Option[Document.Exec_ID])],  // exec state assignment
wenzelm@44476
   249
    List[(String, Option[Document.Command_ID])])  // last exec
wenzelm@44476
   250
wenzelm@44476
   251
  val no_assign: Assign = (Nil, Nil)
wenzelm@44476
   252
wenzelm@38370
   253
  object State
wenzelm@38370
   254
  {
wenzelm@38373
   255
    class Fail(state: State) extends Exception
wenzelm@38373
   256
wenzelm@44479
   257
    val init: State =
wenzelm@44479
   258
      State().define_version(Version.init, Assignment.init).assign(Version.init.id, no_assign)._2
wenzelm@44479
   259
wenzelm@44479
   260
    object Assignment
wenzelm@44479
   261
    {
wenzelm@44479
   262
      val init: Assignment = Assignment(Map.empty, Map.empty, false)
wenzelm@44479
   263
    }
wenzelm@38370
   264
wenzelm@38976
   265
    case class Assignment(
wenzelm@44477
   266
      val command_execs: Map[Command_ID, Exec_ID],
wenzelm@44477
   267
      val last_execs: Map[String, Option[Command_ID]],
wenzelm@44477
   268
      val is_finished: Boolean)
wenzelm@38370
   269
    {
wenzelm@44477
   270
      def check_finished: Assignment = { require(is_finished); this }
wenzelm@44479
   271
      def unfinished: Assignment = copy(is_finished = false)
wenzelm@44479
   272
wenzelm@44479
   273
      def assign(add_command_execs: List[(Command_ID, Option[Exec_ID])],
wenzelm@44477
   274
        add_last_execs: List[(String, Option[Command_ID])]): Assignment =
wenzelm@38370
   275
      {
wenzelm@38976
   276
        require(!is_finished)
wenzelm@44479
   277
        val command_execs1 =
wenzelm@44479
   278
          (command_execs /: add_command_execs) {
wenzelm@44479
   279
            case (res, (command_id, None)) => res - command_id
wenzelm@44479
   280
            case (res, (command_id, Some(exec_id))) => res + (command_id -> exec_id)
wenzelm@44479
   281
          }
wenzelm@44479
   282
        Assignment(command_execs1, last_execs ++ add_last_execs, true)
wenzelm@38150
   283
      }
wenzelm@38370
   284
    }
wenzelm@38370
   285
  }
wenzelm@38370
   286
wenzelm@43715
   287
  sealed case class State(
wenzelm@38417
   288
    val versions: Map[Version_ID, Version] = Map(),
wenzelm@44446
   289
    val commands: Map[Command_ID, Command.State] = Map(),  // static markup from define_command
wenzelm@44446
   290
    val execs: Map[Exec_ID, Command.State] = Map(),  // dynamic markup from execution
wenzelm@43722
   291
    val assignments: Map[Version_ID, State.Assignment] = Map(),
wenzelm@38841
   292
    val history: History = History.init)
wenzelm@38370
   293
  {
wenzelm@38373
   294
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   295
wenzelm@44479
   296
    def define_version(version: Version, assignment: State.Assignment): State =
wenzelm@38370
   297
    {
wenzelm@38417
   298
      val id = version.id
wenzelm@38417
   299
      copy(versions = versions + (id -> version),
wenzelm@44479
   300
        assignments = assignments + (id -> assignment.unfinished))
wenzelm@38370
   301
    }
wenzelm@38370
   302
wenzelm@38373
   303
    def define_command(command: Command): State =
wenzelm@38373
   304
    {
wenzelm@38373
   305
      val id = command.id
wenzelm@38373
   306
      copy(commands = commands + (id -> command.empty_state))
wenzelm@38373
   307
    }
wenzelm@38373
   308
wenzelm@44582
   309
    def defined_command(id: Command_ID): Boolean = commands.isDefinedAt(id)
wenzelm@44582
   310
wenzelm@44607
   311
    def find_command(version: Version, id: ID): Option[(Node, Command)] =
wenzelm@44582
   312
      commands.get(id) orElse execs.get(id) match {
wenzelm@44582
   313
        case None => None
wenzelm@44582
   314
        case Some(st) =>
wenzelm@44582
   315
          val command = st.command
wenzelm@44607
   316
          version.nodes.get(command.node_name).map((_, command))
wenzelm@44582
   317
      }
wenzelm@38373
   318
wenzelm@38417
   319
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
wenzelm@44479
   320
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)  // FIXME rename
wenzelm@44446
   321
    def the_exec(id: Exec_ID): Command.State = execs.getOrElse(id, fail)
wenzelm@43722
   322
    def the_assignment(version: Version): State.Assignment =
wenzelm@43722
   323
      assignments.getOrElse(version.id, fail)
wenzelm@38370
   324
wenzelm@38872
   325
    def accumulate(id: ID, message: XML.Elem): (Command.State, State) =
wenzelm@38370
   326
      execs.get(id) match {
wenzelm@44446
   327
        case Some(st) =>
wenzelm@46152
   328
          val new_st = st + message
wenzelm@44446
   329
          (new_st, copy(execs = execs + (id -> new_st)))
wenzelm@38370
   330
        case None =>
wenzelm@38370
   331
          commands.get(id) match {
wenzelm@38370
   332
            case Some(st) =>
wenzelm@46152
   333
              val new_st = st + message
wenzelm@38370
   334
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   335
            case None => fail
wenzelm@38370
   336
          }
wenzelm@38370
   337
      }
wenzelm@38370
   338
wenzelm@44476
   339
    def assign(id: Version_ID, arg: Assign): (List[Command], State) =
wenzelm@38370
   340
    {
wenzelm@38417
   341
      val version = the_version(id)
wenzelm@44477
   342
      val (command_execs, last_execs) = arg
wenzelm@38370
   343
wenzelm@44543
   344
      val (changed_commands, new_execs) =
wenzelm@44479
   345
        ((Nil: List[Command], execs) /: command_execs) {
wenzelm@44543
   346
          case ((commands1, execs1), (cmd_id, exec)) =>
wenzelm@44479
   347
            val st = the_command(cmd_id)
wenzelm@44543
   348
            val commands2 = st.command :: commands1
wenzelm@44543
   349
            val execs2 =
wenzelm@44543
   350
              exec match {
wenzelm@44543
   351
                case None => execs1
wenzelm@44543
   352
                case Some(exec_id) => execs1 + (exec_id -> st)
wenzelm@44543
   353
              }
wenzelm@44543
   354
            (commands2, execs2)
wenzelm@38370
   355
        }
wenzelm@44477
   356
      val new_assignment = the_assignment(version).assign(command_execs, last_execs)
wenzelm@43722
   357
      val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
wenzelm@44476
   358
wenzelm@44543
   359
      (changed_commands, new_state)
wenzelm@38370
   360
    }
wenzelm@38370
   361
wenzelm@38417
   362
    def is_assigned(version: Version): Boolean =
wenzelm@43722
   363
      assignments.get(version.id) match {
wenzelm@38370
   364
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   365
        case None => false
wenzelm@38370
   366
      }
wenzelm@38370
   367
wenzelm@44436
   368
    def is_stable(change: Change): Boolean =
wenzelm@44436
   369
      change.is_finished && is_assigned(change.version.get_finished)
wenzelm@44436
   370
wenzelm@44672
   371
    def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
wenzelm@44436
   372
    def tip_stable: Boolean = is_stable(history.tip)
wenzelm@44475
   373
    def tip_version: Version = history.tip.version.get_finished
wenzelm@44436
   374
wenzelm@44615
   375
    def last_exec_offset(name: Node.Name): Text.Offset =
wenzelm@44484
   376
    {
wenzelm@44484
   377
      val version = tip_version
wenzelm@44615
   378
      the_assignment(version).last_execs.get(name.node) match {
wenzelm@44484
   379
        case Some(Some(id)) =>
wenzelm@44484
   380
          val node = version.nodes(name)
wenzelm@44484
   381
          val cmd = the_command(id).command
wenzelm@44484
   382
          node.command_start(cmd) match {
wenzelm@44484
   383
            case None => 0
wenzelm@44484
   384
            case Some(start) => start + cmd.length
wenzelm@44484
   385
          }
wenzelm@44484
   386
        case _ => 0
wenzelm@44484
   387
      }
wenzelm@44484
   388
    }
wenzelm@44484
   389
wenzelm@44446
   390
    def continue_history(
wenzelm@44446
   391
        previous: Future[Version],
wenzelm@40479
   392
        edits: List[Edit_Text],
wenzelm@43722
   393
        version: Future[Version]): (Change, State) =
wenzelm@38841
   394
    {
wenzelm@44672
   395
      val change = Change(Some(previous), edits, version)
wenzelm@38841
   396
      (change, copy(history = history + change))
wenzelm@38841
   397
    }
wenzelm@38841
   398
wenzelm@44672
   399
    def prune_history(retain: Int = 0): (List[Version], State) =
wenzelm@44672
   400
    {
wenzelm@44672
   401
      val undo_list = history.undo_list
wenzelm@44672
   402
      val n = undo_list.iterator.zipWithIndex.find(p => is_stable(p._1)).get._2 + 1
wenzelm@44672
   403
      val (retained, dropped) = undo_list.splitAt(n max retain)
wenzelm@44672
   404
wenzelm@44672
   405
      retained.splitAt(retained.length - 1) match {
wenzelm@44672
   406
        case (prefix, List(last)) =>
wenzelm@44672
   407
          val dropped_versions = dropped.map(change => change.version.get_finished)
wenzelm@44672
   408
          val state1 = copy(history = History(prefix ::: List(last.truncate)))
wenzelm@44672
   409
          (dropped_versions, state1)
wenzelm@44672
   410
        case _ => fail
wenzelm@44672
   411
      }
wenzelm@44672
   412
    }
wenzelm@44672
   413
wenzelm@44676
   414
    def removed_versions(removed: List[Version_ID]): State =
wenzelm@44676
   415
    {
wenzelm@44676
   416
      val versions1 = versions -- removed
wenzelm@44676
   417
      val assignments1 = assignments -- removed
wenzelm@44676
   418
      var commands1 = Map.empty[Command_ID, Command.State]
wenzelm@44676
   419
      var execs1 = Map.empty[Exec_ID, Command.State]
wenzelm@44676
   420
      for {
wenzelm@44676
   421
        (version_id, version) <- versions1.iterator
wenzelm@44676
   422
        val command_execs = assignments1(version_id).command_execs
wenzelm@44676
   423
        (_, node) <- version.nodes.iterator
wenzelm@44676
   424
        command <- node.commands.iterator
wenzelm@44676
   425
      } {
wenzelm@44676
   426
        val id = command.id
wenzelm@44676
   427
        if (!commands1.isDefinedAt(id) && commands.isDefinedAt(id))
wenzelm@44676
   428
          commands1 += (id -> commands(id))
wenzelm@44676
   429
        if (command_execs.isDefinedAt(id)) {
wenzelm@44676
   430
          val exec_id = command_execs(id)
wenzelm@44676
   431
          if (!execs1.isDefinedAt(exec_id) && execs.isDefinedAt(exec_id))
wenzelm@44676
   432
            execs1 += (exec_id -> execs(exec_id))
wenzelm@44676
   433
        }
wenzelm@44676
   434
      }
wenzelm@44676
   435
      copy(versions = versions1, commands = commands1, execs = execs1, assignments = assignments1)
wenzelm@44676
   436
    }
wenzelm@44676
   437
wenzelm@44613
   438
    def command_state(version: Version, command: Command): Command.State =
wenzelm@44613
   439
    {
wenzelm@44613
   440
      require(is_assigned(version))
wenzelm@44613
   441
      try {
wenzelm@44613
   442
        the_exec(the_assignment(version).check_finished.command_execs
wenzelm@44613
   443
          .getOrElse(command.id, fail))
wenzelm@44613
   444
      }
wenzelm@44613
   445
      catch { case _: State.Fail => command.empty_state }
wenzelm@44613
   446
    }
wenzelm@44613
   447
wenzelm@38841
   448
wenzelm@38841
   449
    // persistent user-view
wenzelm@44615
   450
    def snapshot(name: Node.Name, pending_edits: List[Text.Edit]): Snapshot =
wenzelm@38370
   451
    {
wenzelm@44672
   452
      val stable = recent_stable
wenzelm@44436
   453
      val latest = history.tip
wenzelm@38841
   454
wenzelm@44156
   455
      // FIXME proper treatment of removed nodes
wenzelm@38841
   456
      val edits =
wenzelm@38841
   457
        (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@44156
   458
            (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
wenzelm@38841
   459
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   460
wenzelm@38841
   461
      new Snapshot
wenzelm@38841
   462
      {
wenzelm@44582
   463
        val state = State.this
wenzelm@39698
   464
        val version = stable.version.get_finished
wenzelm@38841
   465
        val node = version.nodes(name)
wenzelm@38841
   466
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@44613
   467
        def command_state(command: Command): Command.State = state.command_state(version, command)
wenzelm@38841
   468
wenzelm@38841
   469
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38841
   470
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@43425
   471
        def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
wenzelm@43425
   472
        def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
wenzelm@39177
   473
wenzelm@45467
   474
        def cumulate_markup[A](range: Text.Range)(body: Markup_Tree.Cumulate[A])
wenzelm@45459
   475
          : Stream[Text.Info[A]] =
wenzelm@45459
   476
        {
wenzelm@45467
   477
          val info = body.info
wenzelm@45467
   478
          val result = body.result
wenzelm@45467
   479
wenzelm@45467
   480
          val former_range = revert(range)
wenzelm@45459
   481
          for {
wenzelm@45459
   482
            (command, command_start) <- node.command_range(former_range).toStream
wenzelm@45459
   483
            Text.Info(r0, a) <- command_state(command).markup.
wenzelm@45467
   484
              cumulate((former_range - command_start).restrict(command.range))(
wenzelm@45467
   485
                Markup_Tree.Cumulate[A](info,
wenzelm@45467
   486
                  {
wenzelm@45467
   487
                    case (a, Text.Info(r0, b))
wenzelm@45467
   488
                    if result.isDefinedAt((a, Text.Info(convert(r0 + command_start), b))) =>
wenzelm@45467
   489
                      result((a, Text.Info(convert(r0 + command_start), b)))
wenzelm@45474
   490
                  },
wenzelm@45474
   491
                  body.elements))
wenzelm@45459
   492
          } yield Text.Info(convert(r0 + command_start), a)
wenzelm@45459
   493
        }
wenzelm@45459
   494
wenzelm@45468
   495
        def select_markup[A](range: Text.Range)(body: Markup_Tree.Select[A])
wenzelm@39177
   496
          : Stream[Text.Info[Option[A]]] =
wenzelm@38845
   497
        {
wenzelm@45468
   498
          val result = body.result
wenzelm@45468
   499
          val result1 =
wenzelm@45468
   500
            new PartialFunction[(Option[A], Text.Markup), Option[A]] {
wenzelm@45468
   501
              def isDefinedAt(arg: (Option[A], Text.Markup)): Boolean = result.isDefinedAt(arg._2)
wenzelm@45468
   502
              def apply(arg: (Option[A], Text.Markup)): Option[A] = Some(result(arg._2))
wenzelm@45468
   503
            }
wenzelm@45474
   504
          cumulate_markup(range)(Markup_Tree.Cumulate[Option[A]](None, result1, body.elements))
wenzelm@38845
   505
        }
wenzelm@38841
   506
      }
wenzelm@38150
   507
    }
wenzelm@34485
   508
  }
wenzelm@34840
   509
}
wenzelm@34840
   510