src/Pure/PIDE/document.scala
author wenzelm
Wed Jul 23 14:50:20 2014 +0200 (2014-07-23)
changeset 57616 50ab1db5c0fd
parent 57615 df1b3452d71c
child 57617 335750d989a3
permissions -rw-r--r--
avoid redundant data structure;
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@38424
    16
  /** document structure **/
wenzelm@38424
    17
wenzelm@52977
    18
  /* overlays -- print functions with arguments */
wenzelm@52977
    19
wenzelm@52977
    20
  object Overlays
wenzelm@52977
    21
  {
wenzelm@52977
    22
    val empty = new Overlays(Map.empty)
wenzelm@52977
    23
  }
wenzelm@52977
    24
wenzelm@52977
    25
  final class Overlays private(rep: Map[Node.Name, Node.Overlays])
wenzelm@52977
    26
  {
wenzelm@52977
    27
    def apply(name: Document.Node.Name): Node.Overlays =
wenzelm@52977
    28
      rep.getOrElse(name, Node.Overlays.empty)
wenzelm@52977
    29
wenzelm@52977
    30
    private def update(name: Node.Name, f: Node.Overlays => Node.Overlays): Overlays =
wenzelm@52977
    31
    {
wenzelm@52977
    32
      val node_overlays = f(apply(name))
wenzelm@52977
    33
      new Overlays(if (node_overlays.is_empty) rep - name else rep + (name -> node_overlays))
wenzelm@52977
    34
    }
wenzelm@52977
    35
wenzelm@52977
    36
    def insert(command: Command, fn: String, args: List[String]): Overlays =
wenzelm@52977
    37
      update(command.node_name, _.insert(command, fn, args))
wenzelm@52977
    38
wenzelm@52977
    39
    def remove(command: Command, fn: String, args: List[String]): Overlays =
wenzelm@52977
    40
      update(command.node_name, _.remove(command, fn, args))
wenzelm@55800
    41
wenzelm@55800
    42
    override def toString: String = rep.mkString("Overlays(", ",", ")")
wenzelm@52977
    43
  }
wenzelm@52977
    44
wenzelm@52977
    45
wenzelm@55783
    46
  /* document blobs: auxiliary files */
wenzelm@55783
    47
wenzelm@56746
    48
  sealed case class Blob(bytes: Bytes, chunk: Symbol.Text_Chunk, changed: Boolean)
wenzelm@56335
    49
  {
wenzelm@56335
    50
    def unchanged: Blob = copy(changed = false)
wenzelm@56335
    51
  }
wenzelm@55783
    52
wenzelm@55783
    53
  object Blobs
wenzelm@55783
    54
  {
wenzelm@56336
    55
    def apply(blobs: Map[Node.Name, Blob]): Blobs = new Blobs(blobs)
wenzelm@55783
    56
    val empty: Blobs = apply(Map.empty)
wenzelm@55783
    57
  }
wenzelm@55783
    58
wenzelm@56336
    59
  final class Blobs private(blobs: Map[Node.Name, Blob])
wenzelm@55783
    60
  {
wenzelm@55801
    61
    private lazy val digests: Map[SHA1.Digest, Blob] =
wenzelm@55801
    62
      for ((_, blob) <- blobs) yield (blob.bytes.sha1_digest, blob)
wenzelm@55801
    63
wenzelm@55801
    64
    def get(digest: SHA1.Digest): Option[Blob] = digests.get(digest)
wenzelm@56336
    65
    def get(name: Node.Name): Option[Blob] = blobs.get(name)
wenzelm@55783
    66
wenzelm@55783
    67
    def changed(name: Node.Name): Boolean =
wenzelm@55783
    68
      get(name) match {
wenzelm@55783
    69
        case Some(blob) => blob.changed
wenzelm@55783
    70
        case None => false
wenzelm@55783
    71
      }
wenzelm@55783
    72
wenzelm@55800
    73
    override def toString: String = blobs.mkString("Blobs(", ",", ")")
wenzelm@55783
    74
  }
wenzelm@55783
    75
wenzelm@55783
    76
wenzelm@55783
    77
  /* document nodes: theories and auxiliary files */
wenzelm@38424
    78
wenzelm@44615
    79
  type Edit[A, B] = (Node.Name, Node.Edit[A, B])
wenzelm@44384
    80
  type Edit_Text = Edit[Text.Edit, Text.Perspective]
wenzelm@52849
    81
  type Edit_Command = Edit[Command.Edit, Command.Perspective]
wenzelm@38151
    82
wenzelm@38151
    83
  object Node
wenzelm@38151
    84
  {
wenzelm@52887
    85
    /* header and name */
wenzelm@52887
    86
wenzelm@48707
    87
    sealed case class Header(
wenzelm@46938
    88
      imports: List[Name],
wenzelm@48706
    89
      keywords: Thy_Header.Keywords,
wenzelm@57615
    90
      errors: List[String])
wenzelm@48707
    91
    {
wenzelm@48707
    92
      def error(msg: String): Header = copy(errors = errors ::: List(msg))
wenzelm@54549
    93
wenzelm@54549
    94
      def cat_errors(msg2: String): Header =
wenzelm@54549
    95
        copy(errors = errors.map(msg1 => Library.cat_message(msg1, msg2)))
wenzelm@48707
    96
    }
wenzelm@48707
    97
wenzelm@57615
    98
    val no_header = Header(Nil, Nil, Nil)
wenzelm@51294
    99
    def bad_header(msg: String): Header = Header(Nil, Nil, List(msg))
wenzelm@46737
   100
wenzelm@44957
   101
    object Name
wenzelm@44957
   102
    {
wenzelm@54515
   103
      val empty = Name("")
wenzelm@46723
   104
wenzelm@46723
   105
      object Ordering extends scala.math.Ordering[Name]
wenzelm@46723
   106
      {
wenzelm@46723
   107
        def compare(name1: Name, name2: Name): Int = name1.node compare name2.node
wenzelm@46723
   108
      }
wenzelm@44957
   109
    }
wenzelm@52887
   110
wenzelm@54515
   111
    sealed case class Name(node: String, master_dir: String = "", theory: String = "")
wenzelm@44615
   112
    {
wenzelm@44615
   113
      override def hashCode: Int = node.hashCode
wenzelm@44615
   114
      override def equals(that: Any): Boolean =
wenzelm@44615
   115
        that match {
wenzelm@44615
   116
          case other: Name => node == other.node
wenzelm@44615
   117
          case _ => false
wenzelm@44615
   118
        }
wenzelm@54509
   119
wenzelm@54509
   120
      def is_theory: Boolean = !theory.isEmpty
wenzelm@54510
   121
      override def toString: String = if (is_theory) theory else node
wenzelm@56801
   122
wenzelm@56801
   123
      def map(f: String => String): Name = copy(f(node), f(master_dir), theory)
wenzelm@44615
   124
    }
wenzelm@44615
   125
wenzelm@52887
   126
wenzelm@52977
   127
    /* node overlays */
wenzelm@52887
   128
wenzelm@52887
   129
    object Overlays
wenzelm@52887
   130
    {
wenzelm@52976
   131
      val empty = new Overlays(Multi_Map.empty)
wenzelm@52887
   132
    }
wenzelm@52887
   133
wenzelm@52977
   134
    final class Overlays private(rep: Multi_Map[Command, (String, List[String])])
wenzelm@52887
   135
    {
wenzelm@52887
   136
      def commands: Set[Command] = rep.keySet
wenzelm@52887
   137
      def is_empty: Boolean = rep.isEmpty
wenzelm@52977
   138
      def dest: List[(Command, (String, List[String]))] = rep.iterator.toList
wenzelm@52977
   139
      def insert(cmd: Command, fn: String, args: List[String]): Overlays =
wenzelm@52977
   140
        new Overlays(rep.insert(cmd, (fn, args)))
wenzelm@52977
   141
      def remove(cmd: Command, fn: String, args: List[String]): Overlays =
wenzelm@52977
   142
        new Overlays(rep.remove(cmd, (fn, args)))
wenzelm@55800
   143
wenzelm@55800
   144
      override def toString: String = rep.mkString("Node.Overlays(", ",", ")")
wenzelm@52887
   145
    }
wenzelm@52887
   146
wenzelm@52887
   147
wenzelm@52887
   148
    /* edits */
wenzelm@52887
   149
wenzelm@44384
   150
    sealed abstract class Edit[A, B]
wenzelm@44156
   151
    {
wenzelm@44383
   152
      def foreach(f: A => Unit)
wenzelm@44383
   153
      {
wenzelm@44156
   154
        this match {
wenzelm@44383
   155
          case Edits(es) => es.foreach(f)
wenzelm@44383
   156
          case _ =>
wenzelm@44156
   157
        }
wenzelm@44383
   158
      }
wenzelm@44156
   159
    }
wenzelm@44384
   160
    case class Clear[A, B]() extends Edit[A, B]
wenzelm@56335
   161
    case class Blob[A, B](blob: Document.Blob) extends Edit[A, B]
wenzelm@54562
   162
wenzelm@44384
   163
    case class Edits[A, B](edits: List[A]) extends Edit[A, B]
wenzelm@48707
   164
    case class Deps[A, B](header: Header) extends Edit[A, B]
wenzelm@52849
   165
    case class Perspective[A, B](required: Boolean, visible: B, overlays: Overlays) extends Edit[A, B]
wenzelm@57610
   166
wenzelm@57610
   167
wenzelm@57610
   168
    /* perspective */
wenzelm@57610
   169
wenzelm@52808
   170
    type Perspective_Text = Perspective[Text.Edit, Text.Perspective]
wenzelm@52849
   171
    type Perspective_Command = Perspective[Command.Edit, Command.Perspective]
wenzelm@44156
   172
wenzelm@57615
   173
    val no_perspective_text: Perspective_Text =
wenzelm@57610
   174
      Perspective(false, Text.Perspective.empty, Overlays.empty)
wenzelm@57610
   175
wenzelm@57615
   176
    val no_perspective_command: Perspective_Command =
wenzelm@57614
   177
      Perspective(false, Command.Perspective.empty, Overlays.empty)
wenzelm@57614
   178
wenzelm@57615
   179
    def is_no_perspective_command(perspective: Perspective_Command): Boolean =
wenzelm@57615
   180
      !perspective.required &&
wenzelm@57615
   181
      perspective.visible.is_empty &&
wenzelm@57615
   182
      perspective.overlays.is_empty
wenzelm@57615
   183
wenzelm@52887
   184
wenzelm@52887
   185
    /* commands */
wenzelm@52887
   186
wenzelm@52901
   187
    object Commands
wenzelm@52901
   188
    {
wenzelm@52901
   189
      def apply(commands: Linear_Set[Command]): Commands = new Commands(commands)
wenzelm@52901
   190
      val empty: Commands = apply(Linear_Set.empty)
wenzelm@52901
   191
wenzelm@52901
   192
      def starts(commands: Iterator[Command], offset: Text.Offset = 0)
wenzelm@52901
   193
        : Iterator[(Command, Text.Offset)] =
wenzelm@52901
   194
      {
wenzelm@52901
   195
        var i = offset
wenzelm@52901
   196
        for (command <- commands) yield {
wenzelm@52901
   197
          val start = i
wenzelm@52901
   198
          i += command.length
wenzelm@52901
   199
          (command, start)
wenzelm@52901
   200
        }
wenzelm@52901
   201
      }
wenzelm@52901
   202
wenzelm@56398
   203
      private val block_size = 256
wenzelm@52901
   204
    }
wenzelm@52901
   205
wenzelm@52901
   206
    final class Commands private(val commands: Linear_Set[Command])
wenzelm@38227
   207
    {
wenzelm@56314
   208
      lazy val load_commands: List[Command] =
wenzelm@54513
   209
        commands.iterator.filter(cmd => !cmd.blobs.isEmpty).toList
wenzelm@54462
   210
wenzelm@52901
   211
      private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
wenzelm@52901
   212
      {
wenzelm@52901
   213
        val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
wenzelm@52901
   214
        var next_block = 0
wenzelm@52901
   215
        var last_stop = 0
wenzelm@52901
   216
        for ((command, start) <- Commands.starts(commands.iterator)) {
wenzelm@52901
   217
          last_stop = start + command.length
wenzelm@52901
   218
          while (last_stop + 1 > next_block) {
wenzelm@52901
   219
            blocks += (command -> start)
wenzelm@52901
   220
            next_block += Commands.block_size
wenzelm@52901
   221
          }
wenzelm@52901
   222
        }
wenzelm@52901
   223
        (blocks.toArray, Text.Range(0, last_stop))
wenzelm@52901
   224
      }
wenzelm@52901
   225
wenzelm@52901
   226
      private def full_range: Text.Range = full_index._2
wenzelm@52901
   227
wenzelm@56373
   228
      def iterator(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@52901
   229
      {
wenzelm@52901
   230
        if (!commands.isEmpty && full_range.contains(i)) {
wenzelm@52901
   231
          val (cmd0, start0) = full_index._1(i / Commands.block_size)
wenzelm@52901
   232
          Node.Commands.starts(commands.iterator(cmd0), start0) dropWhile {
wenzelm@52901
   233
            case (cmd, start) => start + cmd.length <= i }
wenzelm@52901
   234
        }
wenzelm@52901
   235
        else Iterator.empty
wenzelm@38227
   236
      }
wenzelm@38227
   237
    }
wenzelm@57614
   238
wenzelm@57614
   239
    val empty: Node = new Node()
wenzelm@38151
   240
  }
wenzelm@38151
   241
wenzelm@46712
   242
  final class Node private(
wenzelm@56335
   243
    val get_blob: Option[Document.Blob] = None,
wenzelm@57615
   244
    val header: Node.Header = Node.no_header,
wenzelm@57615
   245
    val perspective: Node.Perspective_Command = Node.no_perspective_command,
wenzelm@52901
   246
    _commands: Node.Commands = Node.Commands.empty)
wenzelm@38151
   247
  {
wenzelm@57615
   248
    def is_empty: Boolean =
wenzelm@57615
   249
      get_blob.isEmpty &&
wenzelm@57615
   250
      header == Node.no_header &&
wenzelm@57615
   251
      Node.is_no_perspective_command(perspective) &&
wenzelm@57615
   252
      commands.isEmpty
wenzelm@57615
   253
wenzelm@57615
   254
    def commands: Linear_Set[Command] = _commands.commands
wenzelm@57615
   255
    def load_commands: List[Command] = _commands.load_commands
wenzelm@57615
   256
wenzelm@46680
   257
    def clear: Node = new Node(header = header)
wenzelm@46680
   258
wenzelm@56335
   259
    def init_blob(blob: Document.Blob): Node = new Node(Some(blob.unchanged))
wenzelm@55435
   260
wenzelm@48707
   261
    def update_header(new_header: Node.Header): Node =
wenzelm@56335
   262
      new Node(get_blob, new_header, perspective, _commands)
wenzelm@46680
   263
wenzelm@52849
   264
    def update_perspective(new_perspective: Node.Perspective_Command): Node =
wenzelm@56335
   265
      new Node(get_blob, header, new_perspective, _commands)
wenzelm@46680
   266
wenzelm@52849
   267
    def same_perspective(other_perspective: Node.Perspective_Command): Boolean =
wenzelm@52849
   268
      perspective.required == other_perspective.required &&
wenzelm@52849
   269
      perspective.visible.same(other_perspective.visible) &&
wenzelm@52849
   270
      perspective.overlays == other_perspective.overlays
wenzelm@52808
   271
wenzelm@52901
   272
    def update_commands(new_commands: Linear_Set[Command]): Node =
wenzelm@52918
   273
      if (new_commands eq _commands.commands) this
wenzelm@56335
   274
      else new Node(get_blob, header, perspective, Node.Commands(new_commands))
wenzelm@38151
   275
wenzelm@56373
   276
    def command_iterator(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@56373
   277
      _commands.iterator(i)
wenzelm@38151
   278
wenzelm@56373
   279
    def command_iterator(range: Text.Range): Iterator[(Command, Text.Offset)] =
wenzelm@56373
   280
      command_iterator(range.start) takeWhile { case (_, start) => start < range.stop }
wenzelm@38151
   281
wenzelm@38879
   282
    def command_start(cmd: Command): Option[Text.Offset] =
wenzelm@52901
   283
      Node.Commands.starts(commands.iterator).find(_._1 == cmd).map(_._2)
wenzelm@38151
   284
  }
wenzelm@38151
   285
wenzelm@38151
   286
wenzelm@46723
   287
  /* development graph */
wenzelm@46723
   288
wenzelm@46723
   289
  object Nodes
wenzelm@46723
   290
  {
wenzelm@46723
   291
    val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering))
wenzelm@46723
   292
  }
wenzelm@46723
   293
wenzelm@46723
   294
  final class Nodes private(graph: Graph[Node.Name, Node])
wenzelm@46723
   295
  {
wenzelm@46723
   296
    def apply(name: Node.Name): Node =
wenzelm@46723
   297
      graph.default_node(name, Node.empty).get_node(name)
wenzelm@46723
   298
wenzelm@46723
   299
    def + (entry: (Node.Name, Node)): Nodes =
wenzelm@46723
   300
    {
wenzelm@46723
   301
      val (name, node) = entry
wenzelm@48707
   302
      val imports = node.header.imports
wenzelm@46723
   303
      val graph1 =
wenzelm@46739
   304
        (graph.default_node(name, Node.empty) /: imports)((g, p) => g.default_node(p, Node.empty))
wenzelm@46723
   305
      val graph2 = (graph1 /: graph1.imm_preds(name))((g, dep) => g.del_edge(dep, name))
wenzelm@46739
   306
      val graph3 = (graph2 /: imports)((g, dep) => g.add_edge(dep, name))
wenzelm@57616
   307
      new Nodes(
wenzelm@57616
   308
        if (graph3.is_maximal(name) && node.is_empty) graph3.del_node(name)
wenzelm@57616
   309
        else graph3.map_node(name, _ => node)
wenzelm@57616
   310
      )
wenzelm@46723
   311
    }
wenzelm@46723
   312
wenzelm@56372
   313
    def iterator: Iterator[(Node.Name, Node)] =
wenzelm@56372
   314
      graph.iterator.map({ case (name, (node, _)) => (name, node) })
wenzelm@46723
   315
wenzelm@56314
   316
    def load_commands(file_name: Node.Name): List[Command] =
wenzelm@54528
   317
      (for {
wenzelm@56372
   318
        (_, node) <- iterator
wenzelm@56314
   319
        cmd <- node.load_commands.iterator
wenzelm@54530
   320
        name <- cmd.blobs_names.iterator
wenzelm@54528
   321
        if name == file_name
wenzelm@54528
   322
      } yield cmd).toList
wenzelm@54528
   323
wenzelm@46942
   324
    def descendants(names: List[Node.Name]): List[Node.Name] = graph.all_succs(names)
wenzelm@46723
   325
    def topological_order: List[Node.Name] = graph.topological_order
wenzelm@56337
   326
wenzelm@56337
   327
    override def toString: String = topological_order.mkString("Nodes(", ",", ")")
wenzelm@46723
   328
  }
wenzelm@46723
   329
wenzelm@46723
   330
wenzelm@38424
   331
wenzelm@38424
   332
  /** versioning **/
wenzelm@38424
   333
wenzelm@38424
   334
  /* particular document versions */
wenzelm@34485
   335
wenzelm@38417
   336
  object Version
wenzelm@38417
   337
  {
wenzelm@46681
   338
    val init: Version = new Version()
wenzelm@46681
   339
wenzelm@56394
   340
    def make(syntax: Option[Prover.Syntax], nodes: Nodes): Version =
wenzelm@52530
   341
      new Version(Document_ID.make(), syntax, nodes)
wenzelm@38417
   342
  }
wenzelm@38417
   343
wenzelm@46712
   344
  final class Version private(
wenzelm@52530
   345
    val id: Document_ID.Version = Document_ID.none,
wenzelm@56394
   346
    val syntax: Option[Prover.Syntax] = None,
wenzelm@46723
   347
    val nodes: Nodes = Nodes.empty)
wenzelm@46941
   348
  {
wenzelm@55777
   349
    override def toString: String = "Version(" + id + ")"
wenzelm@46941
   350
  }
immler@34660
   351
wenzelm@34859
   352
wenzelm@38424
   353
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   354
wenzelm@38227
   355
  object Change
wenzelm@38227
   356
  {
wenzelm@46678
   357
    val init: Change = new Change()
wenzelm@46678
   358
wenzelm@46678
   359
    def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change =
wenzelm@46678
   360
      new Change(Some(previous), edits, version)
wenzelm@38227
   361
  }
wenzelm@38227
   362
wenzelm@46712
   363
  final class Change private(
wenzelm@46677
   364
    val previous: Option[Future[Version]] = Some(Future.value(Version.init)),
wenzelm@46677
   365
    val edits: List[Edit_Text] = Nil,
wenzelm@46677
   366
    val version: Future[Version] = Future.value(Version.init))
wenzelm@38227
   367
  {
wenzelm@44672
   368
    def is_finished: Boolean =
wenzelm@44672
   369
      (previous match { case None => true case Some(future) => future.is_finished }) &&
wenzelm@44672
   370
      version.is_finished
wenzelm@44672
   371
wenzelm@46678
   372
    def truncate: Change = new Change(None, Nil, version)
wenzelm@38227
   373
  }
wenzelm@38227
   374
wenzelm@38227
   375
wenzelm@38841
   376
  /* history navigation */
wenzelm@38841
   377
wenzelm@38841
   378
  object History
wenzelm@38841
   379
  {
wenzelm@46679
   380
    val init: History = new History()
wenzelm@38841
   381
  }
wenzelm@38841
   382
wenzelm@46712
   383
  final class History private(
wenzelm@46679
   384
    val undo_list: List[Change] = List(Change.init))  // non-empty list
wenzelm@38841
   385
  {
wenzelm@46679
   386
    def tip: Change = undo_list.head
wenzelm@46679
   387
    def + (change: Change): History = new History(change :: undo_list)
wenzelm@38841
   388
wenzelm@46679
   389
    def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] =
wenzelm@46679
   390
    {
wenzelm@46679
   391
      val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1
wenzelm@46679
   392
      val (retained, dropped) = undo_list.splitAt(n max retain)
wenzelm@46679
   393
wenzelm@46679
   394
      retained.splitAt(retained.length - 1) match {
wenzelm@46679
   395
        case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate)))
wenzelm@46679
   396
        case _ => None
wenzelm@46679
   397
      }
wenzelm@46679
   398
    }
wenzelm@38841
   399
  }
wenzelm@38841
   400
wenzelm@38841
   401
wenzelm@55820
   402
  /* snapshot */
wenzelm@38424
   403
wenzelm@52972
   404
  object Snapshot
wenzelm@52972
   405
  {
wenzelm@52972
   406
    val init = State.init.snapshot()
wenzelm@52972
   407
  }
wenzelm@52972
   408
wenzelm@38424
   409
  abstract class Snapshot
wenzelm@38424
   410
  {
wenzelm@44582
   411
    val state: State
wenzelm@38424
   412
    val version: Version
wenzelm@38424
   413
    val is_outdated: Boolean
wenzelm@55649
   414
wenzelm@38427
   415
    def convert(i: Text.Offset): Text.Offset
wenzelm@52889
   416
    def revert(i: Text.Offset): Text.Offset
wenzelm@39177
   417
    def convert(range: Text.Range): Text.Range
wenzelm@39177
   418
    def revert(range: Text.Range): Text.Range
wenzelm@52972
   419
wenzelm@52972
   420
    val node_name: Node.Name
wenzelm@52972
   421
    val node: Node
wenzelm@56314
   422
    val load_commands: List[Command]
wenzelm@56176
   423
    def is_loaded: Boolean
wenzelm@51494
   424
    def eq_content(other: Snapshot): Boolean
wenzelm@55649
   425
wenzelm@55651
   426
    def cumulate[A](
wenzelm@55649
   427
      range: Text.Range,
wenzelm@55649
   428
      info: A,
wenzelm@56743
   429
      elements: Markup.Elements,
wenzelm@56354
   430
      result: List[Command.State] => (A, Text.Markup) => Option[A],
wenzelm@55651
   431
      status: Boolean = false): List[Text.Info[A]]
wenzelm@55651
   432
wenzelm@55651
   433
    def select[A](
wenzelm@55649
   434
      range: Text.Range,
wenzelm@56743
   435
      elements: Markup.Elements,
wenzelm@56354
   436
      result: List[Command.State] => Text.Markup => Option[A],
wenzelm@55651
   437
      status: Boolean = false): List[Text.Info[A]]
wenzelm@38424
   438
  }
wenzelm@38424
   439
wenzelm@55820
   440
wenzelm@55820
   441
wenzelm@55820
   442
  /** global state -- document structure, execution process, editing history **/
wenzelm@55820
   443
wenzelm@52563
   444
  type Assign_Update =
wenzelm@52563
   445
    List[(Document_ID.Command, List[Document_ID.Exec])]  // update of exec state assignment
wenzelm@44476
   446
wenzelm@38370
   447
  object State
wenzelm@38370
   448
  {
wenzelm@38373
   449
    class Fail(state: State) extends Exception
wenzelm@38373
   450
wenzelm@44479
   451
    object Assignment
wenzelm@44479
   452
    {
wenzelm@46683
   453
      val init: Assignment = new Assignment()
wenzelm@44479
   454
    }
wenzelm@38370
   455
wenzelm@46712
   456
    final class Assignment private(
wenzelm@52530
   457
      val command_execs: Map[Document_ID.Command, List[Document_ID.Exec]] = Map.empty,
wenzelm@46677
   458
      val is_finished: Boolean = false)
wenzelm@38370
   459
    {
wenzelm@44477
   460
      def check_finished: Assignment = { require(is_finished); this }
wenzelm@47388
   461
      def unfinished: Assignment = new Assignment(command_execs, false)
wenzelm@44479
   462
wenzelm@52563
   463
      def assign(update: Assign_Update): Assignment =
wenzelm@38370
   464
      {
wenzelm@38976
   465
        require(!is_finished)
wenzelm@44479
   466
        val command_execs1 =
wenzelm@52563
   467
          (command_execs /: update) {
wenzelm@52563
   468
            case (res, (command_id, exec_ids)) =>
wenzelm@52563
   469
              if (exec_ids.isEmpty) res - command_id
wenzelm@52563
   470
              else res + (command_id -> exec_ids)
wenzelm@44479
   471
          }
wenzelm@47388
   472
        new Assignment(command_execs1, true)
wenzelm@38150
   473
      }
wenzelm@38370
   474
    }
wenzelm@46682
   475
wenzelm@46682
   476
    val init: State =
wenzelm@52563
   477
      State().define_version(Version.init, Assignment.init).assign(Version.init.id, Nil)._2
wenzelm@38370
   478
  }
wenzelm@38370
   479
wenzelm@46712
   480
  final case class State private(
wenzelm@56470
   481
    /*reachable versions*/
wenzelm@52530
   482
    val versions: Map[Document_ID.Version, Version] = Map.empty,
wenzelm@56470
   483
    /*inlined auxiliary files*/
wenzelm@56470
   484
    val blobs: Set[SHA1.Digest] = Set.empty,
wenzelm@56470
   485
    /*static markup from define_command*/
wenzelm@56470
   486
    val commands: Map[Document_ID.Command, Command.State] = Map.empty,
wenzelm@56470
   487
    /*dynamic markup from execution*/
wenzelm@56470
   488
    val execs: Map[Document_ID.Exec, Command.State] = Map.empty,
wenzelm@56470
   489
    /*command-exec assignment for each version*/
wenzelm@52530
   490
    val assignments: Map[Document_ID.Version, State.Assignment] = Map.empty,
wenzelm@56470
   491
    /*commands with markup produced by other commands (imm_succs)*/
wenzelm@56475
   492
    val commands_redirection: Graph[Document_ID.Command, Unit] = Graph.long,
wenzelm@56470
   493
    /*explicit (linear) history*/
wenzelm@38841
   494
    val history: History = History.init)
wenzelm@38370
   495
  {
wenzelm@38373
   496
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   497
wenzelm@44479
   498
    def define_version(version: Version, assignment: State.Assignment): State =
wenzelm@38370
   499
    {
wenzelm@38417
   500
      val id = version.id
wenzelm@38417
   501
      copy(versions = versions + (id -> version),
wenzelm@44479
   502
        assignments = assignments + (id -> assignment.unfinished))
wenzelm@38370
   503
    }
wenzelm@38370
   504
wenzelm@54519
   505
    def define_blob(digest: SHA1.Digest): State = copy(blobs = blobs + digest)
wenzelm@54519
   506
    def defined_blob(digest: SHA1.Digest): Boolean = blobs.contains(digest)
wenzelm@54519
   507
wenzelm@38373
   508
    def define_command(command: Command): State =
wenzelm@38373
   509
    {
wenzelm@38373
   510
      val id = command.id
wenzelm@49414
   511
      copy(commands = commands + (id -> command.init_state))
wenzelm@38373
   512
    }
wenzelm@38373
   513
wenzelm@52530
   514
    def defined_command(id: Document_ID.Command): Boolean = commands.isDefinedAt(id)
wenzelm@44582
   515
wenzelm@52531
   516
    def find_command(version: Version, id: Document_ID.Generic): Option[(Node, Command)] =
wenzelm@44582
   517
      commands.get(id) orElse execs.get(id) match {
wenzelm@44582
   518
        case None => None
wenzelm@44582
   519
        case Some(st) =>
wenzelm@44582
   520
          val command = st.command
wenzelm@46681
   521
          val node = version.nodes(command.node_name)
wenzelm@54328
   522
          if (node.commands.contains(command)) Some((node, command)) else None
wenzelm@44582
   523
      }
wenzelm@38373
   524
wenzelm@52530
   525
    def the_version(id: Document_ID.Version): Version = versions.getOrElse(id, fail)
wenzelm@52564
   526
    def the_static_state(id: Document_ID.Command): Command.State = commands.getOrElse(id, fail)
wenzelm@52564
   527
    def the_dynamic_state(id: Document_ID.Exec): Command.State = execs.getOrElse(id, fail)
wenzelm@52563
   528
    def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version.id, fail)
wenzelm@38370
   529
wenzelm@56470
   530
    private def self_id(st: Command.State)(id: Document_ID.Generic): Boolean =
wenzelm@56295
   531
      id == st.command.id ||
wenzelm@56295
   532
      (execs.get(id) match { case Some(st1) => st1.command.id == st.command.id case None => false })
wenzelm@56295
   533
wenzelm@56746
   534
    private def other_id(id: Document_ID.Generic)
wenzelm@56746
   535
      : Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)] = None
wenzelm@56514
   536
    /* FIXME
wenzelm@56470
   537
      (execs.get(id) orElse commands.get(id)).map(st =>
wenzelm@56746
   538
        ((Symbol.Text_Chunk.Id(st.command.id), st.command.chunk)))
wenzelm@56514
   539
    */
wenzelm@56470
   540
wenzelm@56475
   541
    private def redirection(st: Command.State): Graph[Document_ID.Command, Unit] =
wenzelm@56475
   542
      (commands_redirection /: st.markups.redirection_iterator)({ case (graph, id) =>
wenzelm@56470
   543
        graph.default_node(id, ()).default_node(st.command.id, ()).add_edge(id, st.command.id) })
wenzelm@56470
   544
wenzelm@52531
   545
    def accumulate(id: Document_ID.Generic, message: XML.Elem): (Command.State, State) =
wenzelm@56470
   546
    {
wenzelm@38370
   547
      execs.get(id) match {
wenzelm@44446
   548
        case Some(st) =>
wenzelm@56470
   549
          val new_st = st.accumulate(self_id(st), other_id _, message)
wenzelm@56470
   550
          val execs1 = execs + (id -> new_st)
wenzelm@56475
   551
          (new_st, copy(execs = execs1, commands_redirection = redirection(new_st)))
wenzelm@38370
   552
        case None =>
wenzelm@38370
   553
          commands.get(id) match {
wenzelm@38370
   554
            case Some(st) =>
wenzelm@56470
   555
              val new_st = st.accumulate(self_id(st), other_id _, message)
wenzelm@56470
   556
              val commands1 = commands + (id -> new_st)
wenzelm@56475
   557
              (new_st, copy(commands = commands1, commands_redirection = redirection(new_st)))
wenzelm@38370
   558
            case None => fail
wenzelm@38370
   559
          }
wenzelm@38370
   560
      }
wenzelm@56470
   561
    }
wenzelm@38370
   562
wenzelm@52563
   563
    def assign(id: Document_ID.Version, update: Assign_Update): (List[Command], State) =
wenzelm@38370
   564
    {
wenzelm@38417
   565
      val version = the_version(id)
wenzelm@38370
   566
wenzelm@52566
   567
      def upd(exec_id: Document_ID.Exec, st: Command.State)
wenzelm@52566
   568
          : Option[(Document_ID.Exec, Command.State)] =
wenzelm@52566
   569
        if (execs.isDefinedAt(exec_id)) None else Some(exec_id -> st)
wenzelm@52566
   570
wenzelm@44543
   571
      val (changed_commands, new_execs) =
wenzelm@52563
   572
        ((Nil: List[Command], execs) /: update) {
wenzelm@52566
   573
          case ((commands1, execs1), (command_id, exec)) =>
wenzelm@52566
   574
            val st = the_static_state(command_id)
wenzelm@52566
   575
            val command = st.command
wenzelm@52527
   576
            val commands2 = command :: commands1
wenzelm@44543
   577
            val execs2 =
wenzelm@44543
   578
              exec match {
wenzelm@52527
   579
                case Nil => execs1
wenzelm@52527
   580
                case eval_id :: print_ids =>
wenzelm@52566
   581
                  execs1 ++ upd(eval_id, st) ++
wenzelm@52566
   582
                    (for (id <- print_ids; up <- upd(id, command.empty_state)) yield up)
wenzelm@44543
   583
              }
wenzelm@44543
   584
            (commands2, execs2)
wenzelm@38370
   585
        }
wenzelm@52563
   586
      val new_assignment = the_assignment(version).assign(update)
wenzelm@43722
   587
      val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
wenzelm@44476
   588
wenzelm@44543
   589
      (changed_commands, new_state)
wenzelm@38370
   590
    }
wenzelm@38370
   591
wenzelm@38417
   592
    def is_assigned(version: Version): Boolean =
wenzelm@43722
   593
      assignments.get(version.id) match {
wenzelm@38370
   594
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   595
        case None => false
wenzelm@38370
   596
      }
wenzelm@38370
   597
wenzelm@44436
   598
    def is_stable(change: Change): Boolean =
wenzelm@44436
   599
      change.is_finished && is_assigned(change.version.get_finished)
wenzelm@44436
   600
wenzelm@46944
   601
    def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail
wenzelm@44672
   602
    def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
wenzelm@44436
   603
    def tip_stable: Boolean = is_stable(history.tip)
wenzelm@44475
   604
    def tip_version: Version = history.tip.version.get_finished
wenzelm@44436
   605
wenzelm@44446
   606
    def continue_history(
wenzelm@56711
   607
      previous: Future[Version],
wenzelm@56711
   608
      edits: List[Edit_Text],
wenzelm@56711
   609
      version: Future[Version]): State =
wenzelm@38841
   610
    {
wenzelm@46678
   611
      val change = Change.make(previous, edits, version)
wenzelm@56711
   612
      copy(history = history + change)
wenzelm@38841
   613
    }
wenzelm@38841
   614
wenzelm@44672
   615
    def prune_history(retain: Int = 0): (List[Version], State) =
wenzelm@44672
   616
    {
wenzelm@46679
   617
      history.prune(is_stable, retain) match {
wenzelm@46679
   618
        case Some((dropped, history1)) =>
wenzelm@44672
   619
          val dropped_versions = dropped.map(change => change.version.get_finished)
wenzelm@46679
   620
          val state1 = copy(history = history1)
wenzelm@44672
   621
          (dropped_versions, state1)
wenzelm@46679
   622
        case None => fail
wenzelm@44672
   623
      }
wenzelm@44672
   624
    }
wenzelm@44672
   625
wenzelm@52530
   626
    def removed_versions(removed: List[Document_ID.Version]): State =
wenzelm@44676
   627
    {
wenzelm@44676
   628
      val versions1 = versions -- removed
wenzelm@44676
   629
      val assignments1 = assignments -- removed
wenzelm@54519
   630
      var blobs1 = Set.empty[SHA1.Digest]
wenzelm@52530
   631
      var commands1 = Map.empty[Document_ID.Command, Command.State]
wenzelm@52530
   632
      var execs1 = Map.empty[Document_ID.Exec, Command.State]
wenzelm@44676
   633
      for {
wenzelm@44676
   634
        (version_id, version) <- versions1.iterator
wenzelm@46997
   635
        command_execs = assignments1(version_id).command_execs
wenzelm@56372
   636
        (_, node) <- version.nodes.iterator
wenzelm@44676
   637
        command <- node.commands.iterator
wenzelm@44676
   638
      } {
wenzelm@54519
   639
        for (digest <- command.blobs_digests; if !blobs1.contains(digest))
wenzelm@54519
   640
          blobs1 += digest
wenzelm@54519
   641
wenzelm@52568
   642
        if (!commands1.isDefinedAt(command.id))
wenzelm@52568
   643
          commands.get(command.id).foreach(st => commands1 += (command.id -> st))
wenzelm@54519
   644
wenzelm@52568
   645
        for (exec_id <- command_execs.getOrElse(command.id, Nil)) {
wenzelm@52527
   646
          if (!execs1.isDefinedAt(exec_id))
wenzelm@52527
   647
            execs.get(exec_id).foreach(st => execs1 += (exec_id -> st))
wenzelm@44676
   648
        }
wenzelm@44676
   649
      }
wenzelm@56470
   650
      copy(
wenzelm@56470
   651
        versions = versions1,
wenzelm@56470
   652
        blobs = blobs1,
wenzelm@56470
   653
        commands = commands1,
wenzelm@56470
   654
        execs = execs1,
wenzelm@56475
   655
        commands_redirection = commands_redirection.restrict(commands1.isDefinedAt(_)),
wenzelm@54519
   656
        assignments = assignments1)
wenzelm@44676
   657
    }
wenzelm@44676
   658
wenzelm@56470
   659
    private def command_states_self(version: Version, command: Command)
wenzelm@56470
   660
      : List[(Document_ID.Generic, Command.State)] =
wenzelm@44613
   661
    {
wenzelm@44613
   662
      require(is_assigned(version))
wenzelm@44613
   663
      try {
wenzelm@52527
   664
        the_assignment(version).check_finished.command_execs.getOrElse(command.id, Nil)
wenzelm@56470
   665
          .map(id => id -> the_dynamic_state(id)) match {
wenzelm@52527
   666
            case Nil => fail
wenzelm@56470
   667
            case res => res
wenzelm@52527
   668
          }
wenzelm@44613
   669
      }
wenzelm@47395
   670
      catch {
wenzelm@47395
   671
        case _: State.Fail =>
wenzelm@56470
   672
          try { List(command.id -> the_static_state(command.id)) }
wenzelm@56470
   673
          catch { case _: State.Fail => List(command.id -> command.init_state) }
wenzelm@47395
   674
      }
wenzelm@44613
   675
    }
wenzelm@44613
   676
wenzelm@56470
   677
    def command_states(version: Version, command: Command): List[Command.State] =
wenzelm@56470
   678
    {
wenzelm@56470
   679
      val self = command_states_self(version, command)
wenzelm@56470
   680
      val others =
wenzelm@56475
   681
        if (commands_redirection.defined(command.id)) {
wenzelm@56470
   682
          (for {
wenzelm@56475
   683
            command_id <- commands_redirection.imm_succs(command.id).iterator
wenzelm@56470
   684
            (id, st) <- command_states_self(version, the_static_state(command_id).command)
wenzelm@56470
   685
            if !self.exists(_._1 == id)
wenzelm@56470
   686
          } yield (id, st)).toMap.valuesIterator.toList
wenzelm@56470
   687
        }
wenzelm@56470
   688
        else Nil
wenzelm@56489
   689
      self.map(_._2) ::: others.flatMap(_.redirect(command))
wenzelm@56470
   690
    }
wenzelm@56470
   691
wenzelm@56299
   692
    def command_results(version: Version, command: Command): Command.Results =
wenzelm@56299
   693
      Command.State.merge_results(command_states(version, command))
wenzelm@56299
   694
wenzelm@56301
   695
    def command_markup(version: Version, command: Command, index: Command.Markup_Index,
wenzelm@56743
   696
        range: Text.Range, elements: Markup.Elements): Markup_Tree =
wenzelm@56301
   697
      Command.State.merge_markup(command_states(version, command), index, range, elements)
wenzelm@56299
   698
wenzelm@56743
   699
    def markup_to_XML(version: Version, node: Node, elements: Markup.Elements): XML.Body =
wenzelm@56299
   700
      (for {
wenzelm@56299
   701
        command <- node.commands.iterator
wenzelm@56301
   702
        markup =
wenzelm@56301
   703
          command_markup(version, command, Command.Markup_Index.markup, command.range, elements)
wenzelm@56300
   704
        tree <- markup.to_XML(command.range, command.source, elements)
wenzelm@56299
   705
      } yield tree).toList
wenzelm@38841
   706
wenzelm@38841
   707
    // persistent user-view
wenzelm@49346
   708
    def snapshot(name: Node.Name = Node.Name.empty, pending_edits: List[Text.Edit] = Nil)
wenzelm@49346
   709
      : Snapshot =
wenzelm@38370
   710
    {
wenzelm@44672
   711
      val stable = recent_stable
wenzelm@44436
   712
      val latest = history.tip
wenzelm@38841
   713
wenzelm@44156
   714
      // FIXME proper treatment of removed nodes
wenzelm@38841
   715
      val edits =
wenzelm@38841
   716
        (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@44156
   717
            (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
wenzelm@38841
   718
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   719
wenzelm@38841
   720
      new Snapshot
wenzelm@38841
   721
      {
wenzelm@52972
   722
        /* global information */
wenzelm@52972
   723
wenzelm@44582
   724
        val state = State.this
wenzelm@39698
   725
        val version = stable.version.get_finished
wenzelm@38841
   726
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@38841
   727
wenzelm@55649
   728
wenzelm@55649
   729
        /* local node content */
wenzelm@55649
   730
wenzelm@38841
   731
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38841
   732
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@43425
   733
        def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
wenzelm@43425
   734
        def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
wenzelm@39177
   735
wenzelm@52972
   736
        val node_name = name
wenzelm@52972
   737
        val node = version.nodes(name)
wenzelm@52972
   738
wenzelm@56314
   739
        val load_commands: List[Command] =
wenzelm@55432
   740
          if (node_name.is_theory) Nil
wenzelm@56314
   741
          else version.nodes.load_commands(node_name)
wenzelm@55432
   742
wenzelm@56314
   743
        val is_loaded: Boolean = node_name.is_theory || !load_commands.isEmpty
wenzelm@56176
   744
wenzelm@51494
   745
        def eq_content(other: Snapshot): Boolean =
wenzelm@55434
   746
        {
wenzelm@55434
   747
          def eq_commands(commands: (Command, Command)): Boolean =
wenzelm@56299
   748
          {
wenzelm@56299
   749
            val states1 = state.command_states(version, commands._1)
wenzelm@56299
   750
            val states2 = other.state.command_states(other.version, commands._2)
wenzelm@56299
   751
            states1.length == states2.length &&
wenzelm@56299
   752
            (states1 zip states2).forall({ case (st1, st2) => st1 eq_content st2 })
wenzelm@56299
   753
          }
wenzelm@55434
   754
wenzelm@49346
   755
          !is_outdated && !other.is_outdated &&
wenzelm@55434
   756
          node.commands.size == other.node.commands.size &&
wenzelm@55434
   757
          (node.commands.iterator zip other.node.commands.iterator).forall(eq_commands) &&
wenzelm@56314
   758
          load_commands.length == other.load_commands.length &&
wenzelm@56314
   759
          (load_commands zip other.load_commands).forall(eq_commands)
wenzelm@55434
   760
        }
wenzelm@49346
   761
wenzelm@55649
   762
wenzelm@55649
   763
        /* cumulate markup */
wenzelm@55649
   764
wenzelm@55651
   765
        def cumulate[A](
wenzelm@55649
   766
          range: Text.Range,
wenzelm@55649
   767
          info: A,
wenzelm@56743
   768
          elements: Markup.Elements,
wenzelm@56354
   769
          result: List[Command.State] => (A, Text.Markup) => Option[A],
wenzelm@55651
   770
          status: Boolean = false): List[Text.Info[A]] =
wenzelm@45459
   771
        {
wenzelm@56590
   772
          val former_range = revert(range).inflate_singularity
wenzelm@56462
   773
          val (chunk_name, command_iterator) =
wenzelm@56314
   774
            load_commands match {
wenzelm@56746
   775
              case command :: _ => (Symbol.Text_Chunk.File(node_name.node), Iterator((command, 0)))
wenzelm@56746
   776
              case _ => (Symbol.Text_Chunk.Default, node.command_iterator(former_range))
wenzelm@56309
   777
            }
wenzelm@56462
   778
          val markup_index = Command.Markup_Index(status, chunk_name)
wenzelm@56309
   779
          (for {
wenzelm@56373
   780
            (command, command_start) <- command_iterator
wenzelm@56462
   781
            chunk <- command.chunks.get(chunk_name).iterator
wenzelm@56309
   782
            states = state.command_states(version, command)
wenzelm@56354
   783
            res = result(states)
wenzelm@56590
   784
            markup_range <- (former_range - command_start).try_restrict(chunk.range).iterator
wenzelm@56590
   785
            markup = Command.State.merge_markup(states, markup_index, markup_range, elements)
wenzelm@56590
   786
            Text.Info(r0, a) <- markup.cumulate[A](markup_range, info, elements,
wenzelm@56309
   787
              {
wenzelm@56309
   788
                case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0 + command_start), b))
wenzelm@56309
   789
              }).iterator
wenzelm@56590
   790
            r1 <- convert(r0 + command_start).try_restrict(range).iterator
wenzelm@56590
   791
          } yield Text.Info(r1, a)).toList
wenzelm@45459
   792
        }
wenzelm@45459
   793
wenzelm@55651
   794
        def select[A](
wenzelm@55651
   795
          range: Text.Range,
wenzelm@56743
   796
          elements: Markup.Elements,
wenzelm@56354
   797
          result: List[Command.State] => Text.Markup => Option[A],
wenzelm@55651
   798
          status: Boolean = false): List[Text.Info[A]] =
wenzelm@38845
   799
        {
wenzelm@56354
   800
          def result1(states: List[Command.State]): (Option[A], Text.Markup) => Option[Option[A]] =
wenzelm@50500
   801
          {
wenzelm@56354
   802
            val res = result(states)
wenzelm@52889
   803
            (_: Option[A], x: Text.Markup) =>
wenzelm@52889
   804
              res(x) match {
wenzelm@52889
   805
                case None => None
wenzelm@52889
   806
                case some => Some(some)
wenzelm@52889
   807
              }
wenzelm@50500
   808
          }
wenzelm@55651
   809
          for (Text.Info(r, Some(x)) <- cumulate(range, None, elements, result1 _, status))
wenzelm@46198
   810
            yield Text.Info(r, x)
wenzelm@38845
   811
        }
wenzelm@55800
   812
wenzelm@55800
   813
wenzelm@55800
   814
        /* output */
wenzelm@55800
   815
wenzelm@55800
   816
        override def toString: String =
wenzelm@55800
   817
          "Snapshot(node = " + node_name.node + ", version = " + version.id +
wenzelm@55800
   818
            (if (is_outdated) ", outdated" else "") + ")"
wenzelm@38841
   819
      }
wenzelm@38150
   820
    }
wenzelm@34485
   821
  }
wenzelm@34840
   822
}
wenzelm@34840
   823