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