src/Pure/PIDE/document.scala
author wenzelm
Wed Jul 03 15:19:36 2013 +0200 (2013-07-03)
changeset 52508 98475be0b1a2
parent 51494 8f3d1a7bee26
child 52527 dbac84eab3bc
permissions -rw-r--r--
tuned;
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@46723
    32
  /* individual nodes */
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@38151
    38
  object Node
wenzelm@38151
    39
  {
wenzelm@48707
    40
    sealed case class Header(
wenzelm@46938
    41
      imports: List[Name],
wenzelm@48706
    42
      keywords: Thy_Header.Keywords,
wenzelm@48707
    43
      errors: List[String] = Nil)
wenzelm@48707
    44
    {
wenzelm@48707
    45
      def error(msg: String): Header = copy(errors = errors ::: List(msg))
wenzelm@48707
    46
    }
wenzelm@48707
    47
wenzelm@51294
    48
    def bad_header(msg: String): Header = Header(Nil, Nil, List(msg))
wenzelm@46737
    49
wenzelm@44957
    50
    object Name
wenzelm@44957
    51
    {
wenzelm@44957
    52
      val empty = Name("", "", "")
wenzelm@46723
    53
wenzelm@46723
    54
      object Ordering extends scala.math.Ordering[Name]
wenzelm@46723
    55
      {
wenzelm@46723
    56
        def compare(name1: Name, name2: Name): Int = name1.node compare name2.node
wenzelm@46723
    57
      }
wenzelm@44957
    58
    }
wenzelm@44616
    59
    sealed case class Name(node: String, dir: String, theory: String)
wenzelm@44615
    60
    {
wenzelm@44615
    61
      override def hashCode: Int = node.hashCode
wenzelm@44615
    62
      override def equals(that: Any): Boolean =
wenzelm@44615
    63
        that match {
wenzelm@44615
    64
          case other: Name => node == other.node
wenzelm@44615
    65
          case _ => false
wenzelm@44615
    66
        }
wenzelm@44642
    67
      override def toString: String = theory
wenzelm@44615
    68
    }
wenzelm@44615
    69
wenzelm@44384
    70
    sealed abstract class Edit[A, B]
wenzelm@44156
    71
    {
wenzelm@44383
    72
      def foreach(f: A => Unit)
wenzelm@44383
    73
      {
wenzelm@44156
    74
        this match {
wenzelm@44383
    75
          case Edits(es) => es.foreach(f)
wenzelm@44383
    76
          case _ =>
wenzelm@44156
    77
        }
wenzelm@44383
    78
      }
wenzelm@44156
    79
    }
wenzelm@44384
    80
    case class Clear[A, B]() extends Edit[A, B]
wenzelm@44384
    81
    case class Edits[A, B](edits: List[A]) extends Edit[A, B]
wenzelm@48707
    82
    case class Deps[A, B](header: Header) extends Edit[A, B]
wenzelm@44384
    83
    case class Perspective[A, B](perspective: B) extends Edit[A, B]
wenzelm@44156
    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@46712
   101
  final class Node private(
wenzelm@48707
   102
    val header: Node.Header = Node.bad_header("Bad theory header"),
wenzelm@46677
   103
    val perspective: Command.Perspective = Command.Perspective.empty,
wenzelm@46677
   104
    val commands: Linear_Set[Command] = Linear_Set.empty)
wenzelm@38151
   105
  {
wenzelm@46680
   106
    def clear: Node = new Node(header = header)
wenzelm@46680
   107
wenzelm@48707
   108
    def update_header(new_header: Node.Header): Node =
wenzelm@49678
   109
      new Node(new_header, perspective, commands)
wenzelm@46680
   110
wenzelm@46680
   111
    def update_perspective(new_perspective: Command.Perspective): Node =
wenzelm@49678
   112
      new Node(header, new_perspective, commands)
wenzelm@46680
   113
wenzelm@46680
   114
    def update_commands(new_commands: Linear_Set[Command]): Node =
wenzelm@49678
   115
      new Node(header, perspective, new_commands)
wenzelm@44443
   116
wenzelm@44443
   117
wenzelm@43697
   118
    /* commands */
wenzelm@43697
   119
wenzelm@38879
   120
    private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
wenzelm@38879
   121
    {
wenzelm@38879
   122
      val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
wenzelm@38879
   123
      var next_block = 0
wenzelm@38879
   124
      var last_stop = 0
wenzelm@38879
   125
      for ((command, start) <- Node.command_starts(commands.iterator)) {
wenzelm@38879
   126
        last_stop = start + command.length
wenzelm@38885
   127
        while (last_stop + 1 > next_block) {
wenzelm@38879
   128
          blocks += (command -> start)
wenzelm@46680
   129
          next_block += Node.block_size
wenzelm@38879
   130
        }
wenzelm@38879
   131
      }
wenzelm@38879
   132
      (blocks.toArray, Text.Range(0, last_stop))
wenzelm@38879
   133
    }
wenzelm@38151
   134
wenzelm@38879
   135
    def full_range: Text.Range = full_index._2
wenzelm@38151
   136
wenzelm@38569
   137
    def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@38879
   138
    {
wenzelm@38879
   139
      if (!commands.isEmpty && full_range.contains(i)) {
wenzelm@46680
   140
        val (cmd0, start0) = full_index._1(i / Node.block_size)
wenzelm@38879
   141
        Node.command_starts(commands.iterator(cmd0), start0) dropWhile {
wenzelm@38879
   142
          case (cmd, start) => start + cmd.length <= i }
wenzelm@38879
   143
      }
wenzelm@38879
   144
      else Iterator.empty
wenzelm@38879
   145
    }
wenzelm@38151
   146
wenzelm@38569
   147
    def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] =
wenzelm@38569
   148
      command_range(range.start) takeWhile { case (_, start) => start < range.stop }
wenzelm@38151
   149
wenzelm@38426
   150
    def command_at(i: Text.Offset): Option[(Command, Text.Offset)] =
wenzelm@38151
   151
    {
wenzelm@38151
   152
      val range = command_range(i)
wenzelm@38151
   153
      if (range.hasNext) Some(range.next) else None
wenzelm@38151
   154
    }
wenzelm@38151
   155
wenzelm@38879
   156
    def command_start(cmd: Command): Option[Text.Offset] =
wenzelm@46677
   157
      Node.command_starts(commands.iterator).find(_._1 == cmd).map(_._2)
wenzelm@38151
   158
  }
wenzelm@38151
   159
wenzelm@38151
   160
wenzelm@46723
   161
  /* development graph */
wenzelm@46723
   162
wenzelm@46723
   163
  object Nodes
wenzelm@46723
   164
  {
wenzelm@46723
   165
    val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering))
wenzelm@46723
   166
  }
wenzelm@46723
   167
wenzelm@46723
   168
  final class Nodes private(graph: Graph[Node.Name, Node])
wenzelm@46723
   169
  {
wenzelm@46723
   170
    def get_name(s: String): Option[Node.Name] =
wenzelm@46723
   171
      graph.keys.find(name => name.node == s)
wenzelm@46723
   172
wenzelm@46723
   173
    def apply(name: Node.Name): Node =
wenzelm@46723
   174
      graph.default_node(name, Node.empty).get_node(name)
wenzelm@46723
   175
wenzelm@46723
   176
    def + (entry: (Node.Name, Node)): Nodes =
wenzelm@46723
   177
    {
wenzelm@46723
   178
      val (name, node) = entry
wenzelm@48707
   179
      val imports = node.header.imports
wenzelm@46723
   180
      val graph1 =
wenzelm@46739
   181
        (graph.default_node(name, Node.empty) /: imports)((g, p) => g.default_node(p, Node.empty))
wenzelm@46723
   182
      val graph2 = (graph1 /: graph1.imm_preds(name))((g, dep) => g.del_edge(dep, name))
wenzelm@46739
   183
      val graph3 = (graph2 /: imports)((g, dep) => g.add_edge(dep, name))
wenzelm@46723
   184
      new Nodes(graph3.map_node(name, _ => node))
wenzelm@46723
   185
    }
wenzelm@46723
   186
wenzelm@46723
   187
    def entries: Iterator[(Node.Name, Node)] =
wenzelm@46723
   188
      graph.entries.map({ case (name, (node, _)) => (name, node) })
wenzelm@46723
   189
wenzelm@46942
   190
    def descendants(names: List[Node.Name]): List[Node.Name] = graph.all_succs(names)
wenzelm@46723
   191
    def topological_order: List[Node.Name] = graph.topological_order
wenzelm@46723
   192
  }
wenzelm@46723
   193
wenzelm@46723
   194
wenzelm@38424
   195
wenzelm@38424
   196
  /** versioning **/
wenzelm@38424
   197
wenzelm@38424
   198
  /* particular document versions */
wenzelm@34485
   199
wenzelm@38417
   200
  object Version
wenzelm@38417
   201
  {
wenzelm@46681
   202
    val init: Version = new Version()
wenzelm@46681
   203
wenzelm@46941
   204
    def make(syntax: Outer_Syntax, nodes: Nodes): Version =
wenzelm@46941
   205
      new Version(new_id(), syntax, nodes)
wenzelm@38417
   206
  }
wenzelm@38417
   207
wenzelm@46712
   208
  final class Version private(
wenzelm@46677
   209
    val id: Version_ID = no_id,
wenzelm@46941
   210
    val syntax: Outer_Syntax = Outer_Syntax.empty,
wenzelm@46723
   211
    val nodes: Nodes = Nodes.empty)
wenzelm@46941
   212
  {
wenzelm@46941
   213
    def is_init: Boolean = id == no_id
wenzelm@46941
   214
  }
immler@34660
   215
wenzelm@34859
   216
wenzelm@38424
   217
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   218
wenzelm@38227
   219
  object Change
wenzelm@38227
   220
  {
wenzelm@46678
   221
    val init: Change = new Change()
wenzelm@46678
   222
wenzelm@46678
   223
    def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change =
wenzelm@46678
   224
      new Change(Some(previous), edits, version)
wenzelm@38227
   225
  }
wenzelm@38227
   226
wenzelm@46712
   227
  final class Change private(
wenzelm@46677
   228
    val previous: Option[Future[Version]] = Some(Future.value(Version.init)),
wenzelm@46677
   229
    val edits: List[Edit_Text] = Nil,
wenzelm@46677
   230
    val version: Future[Version] = Future.value(Version.init))
wenzelm@38227
   231
  {
wenzelm@44672
   232
    def is_finished: Boolean =
wenzelm@44672
   233
      (previous match { case None => true case Some(future) => future.is_finished }) &&
wenzelm@44672
   234
      version.is_finished
wenzelm@44672
   235
wenzelm@46678
   236
    def truncate: Change = new Change(None, Nil, version)
wenzelm@38227
   237
  }
wenzelm@38227
   238
wenzelm@38227
   239
wenzelm@38841
   240
  /* history navigation */
wenzelm@38841
   241
wenzelm@38841
   242
  object History
wenzelm@38841
   243
  {
wenzelm@46679
   244
    val init: History = new History()
wenzelm@38841
   245
  }
wenzelm@38841
   246
wenzelm@46712
   247
  final class History private(
wenzelm@46679
   248
    val undo_list: List[Change] = List(Change.init))  // non-empty list
wenzelm@38841
   249
  {
wenzelm@46679
   250
    def tip: Change = undo_list.head
wenzelm@46679
   251
    def + (change: Change): History = new History(change :: undo_list)
wenzelm@38841
   252
wenzelm@46679
   253
    def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] =
wenzelm@46679
   254
    {
wenzelm@46679
   255
      val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1
wenzelm@46679
   256
      val (retained, dropped) = undo_list.splitAt(n max retain)
wenzelm@46679
   257
wenzelm@46679
   258
      retained.splitAt(retained.length - 1) match {
wenzelm@46679
   259
        case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate)))
wenzelm@46679
   260
        case _ => None
wenzelm@46679
   261
      }
wenzelm@46679
   262
    }
wenzelm@38841
   263
  }
wenzelm@38841
   264
wenzelm@38841
   265
wenzelm@38841
   266
wenzelm@38841
   267
  /** global state -- document structure, execution process, editing history **/
wenzelm@38424
   268
wenzelm@38424
   269
  abstract class Snapshot
wenzelm@38424
   270
  {
wenzelm@44582
   271
    val state: State
wenzelm@38424
   272
    val version: Version
wenzelm@48923
   273
    val node_name: Node.Name
wenzelm@38424
   274
    val node: Node
wenzelm@38424
   275
    val is_outdated: Boolean
wenzelm@38427
   276
    def convert(i: Text.Offset): Text.Offset
wenzelm@39177
   277
    def convert(range: Text.Range): Text.Range
wenzelm@38427
   278
    def revert(i: Text.Offset): Text.Offset
wenzelm@39177
   279
    def revert(range: Text.Range): Text.Range
wenzelm@51494
   280
    def eq_content(other: Snapshot): Boolean
wenzelm@52508
   281
    def cumulate_markup[A](
wenzelm@52508
   282
      range: Text.Range,
wenzelm@52508
   283
      info: A,
wenzelm@52508
   284
      elements: Option[Set[String]],
wenzelm@50500
   285
      result: Command.State => PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]]
wenzelm@52508
   286
    def select_markup[A](
wenzelm@52508
   287
      range: Text.Range,
wenzelm@52508
   288
      elements: Option[Set[String]],
wenzelm@50500
   289
      result: Command.State => PartialFunction[Text.Markup, A]): Stream[Text.Info[A]]
wenzelm@38424
   290
  }
wenzelm@38424
   291
wenzelm@47388
   292
  type Assign = List[(Document.Command_ID, Option[Document.Exec_ID])]  // exec state assignment
wenzelm@44476
   293
wenzelm@38370
   294
  object State
wenzelm@38370
   295
  {
wenzelm@38373
   296
    class Fail(state: State) extends Exception
wenzelm@38373
   297
wenzelm@44479
   298
    object Assignment
wenzelm@44479
   299
    {
wenzelm@46683
   300
      val init: Assignment = new Assignment()
wenzelm@44479
   301
    }
wenzelm@38370
   302
wenzelm@46712
   303
    final class Assignment private(
wenzelm@46677
   304
      val command_execs: Map[Command_ID, Exec_ID] = Map.empty,
wenzelm@46677
   305
      val is_finished: Boolean = false)
wenzelm@38370
   306
    {
wenzelm@44477
   307
      def check_finished: Assignment = { require(is_finished); this }
wenzelm@47388
   308
      def unfinished: Assignment = new Assignment(command_execs, false)
wenzelm@44479
   309
wenzelm@47388
   310
      def assign(add_command_execs: List[(Command_ID, Option[Exec_ID])]): Assignment =
wenzelm@38370
   311
      {
wenzelm@38976
   312
        require(!is_finished)
wenzelm@44479
   313
        val command_execs1 =
wenzelm@44479
   314
          (command_execs /: add_command_execs) {
wenzelm@44479
   315
            case (res, (command_id, None)) => res - command_id
wenzelm@44479
   316
            case (res, (command_id, Some(exec_id))) => res + (command_id -> exec_id)
wenzelm@44479
   317
          }
wenzelm@47388
   318
        new Assignment(command_execs1, true)
wenzelm@38150
   319
      }
wenzelm@38370
   320
    }
wenzelm@46682
   321
wenzelm@46682
   322
    val init: State =
wenzelm@46682
   323
      State().define_version(Version.init, Assignment.init).assign(Version.init.id)._2
wenzelm@38370
   324
  }
wenzelm@38370
   325
wenzelm@46712
   326
  final case class State private(
wenzelm@46677
   327
    val versions: Map[Version_ID, Version] = Map.empty,
wenzelm@46677
   328
    val commands: Map[Command_ID, Command.State] = Map.empty,  // static markup from define_command
wenzelm@46677
   329
    val execs: Map[Exec_ID, Command.State] = Map.empty,  // dynamic markup from execution
wenzelm@46677
   330
    val assignments: Map[Version_ID, State.Assignment] = Map.empty,
wenzelm@38841
   331
    val history: History = History.init)
wenzelm@38370
   332
  {
wenzelm@38373
   333
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   334
wenzelm@44479
   335
    def define_version(version: Version, assignment: State.Assignment): State =
wenzelm@38370
   336
    {
wenzelm@38417
   337
      val id = version.id
wenzelm@38417
   338
      copy(versions = versions + (id -> version),
wenzelm@44479
   339
        assignments = assignments + (id -> assignment.unfinished))
wenzelm@38370
   340
    }
wenzelm@38370
   341
wenzelm@38373
   342
    def define_command(command: Command): State =
wenzelm@38373
   343
    {
wenzelm@38373
   344
      val id = command.id
wenzelm@49414
   345
      copy(commands = commands + (id -> command.init_state))
wenzelm@38373
   346
    }
wenzelm@38373
   347
wenzelm@44582
   348
    def defined_command(id: Command_ID): Boolean = commands.isDefinedAt(id)
wenzelm@44582
   349
wenzelm@44607
   350
    def find_command(version: Version, id: ID): Option[(Node, Command)] =
wenzelm@44582
   351
      commands.get(id) orElse execs.get(id) match {
wenzelm@44582
   352
        case None => None
wenzelm@44582
   353
        case Some(st) =>
wenzelm@44582
   354
          val command = st.command
wenzelm@46681
   355
          val node = version.nodes(command.node_name)
wenzelm@46681
   356
          Some((node, command))
wenzelm@44582
   357
      }
wenzelm@38373
   358
wenzelm@38417
   359
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
wenzelm@47395
   360
    def the_command_state(id: Command_ID): Command.State = commands.getOrElse(id, fail)
wenzelm@44446
   361
    def the_exec(id: Exec_ID): Command.State = execs.getOrElse(id, fail)
wenzelm@43722
   362
    def the_assignment(version: Version): State.Assignment =
wenzelm@43722
   363
      assignments.getOrElse(version.id, fail)
wenzelm@38370
   364
wenzelm@38872
   365
    def accumulate(id: ID, message: XML.Elem): (Command.State, State) =
wenzelm@38370
   366
      execs.get(id) match {
wenzelm@44446
   367
        case Some(st) =>
wenzelm@49527
   368
          val new_st = st + (id, message)
wenzelm@44446
   369
          (new_st, copy(execs = execs + (id -> new_st)))
wenzelm@38370
   370
        case None =>
wenzelm@38370
   371
          commands.get(id) match {
wenzelm@38370
   372
            case Some(st) =>
wenzelm@49527
   373
              val new_st = st + (id, message)
wenzelm@38370
   374
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   375
            case None => fail
wenzelm@38370
   376
          }
wenzelm@38370
   377
      }
wenzelm@38370
   378
wenzelm@47388
   379
    def assign(id: Version_ID, command_execs: Assign = Nil): (List[Command], State) =
wenzelm@38370
   380
    {
wenzelm@38417
   381
      val version = the_version(id)
wenzelm@38370
   382
wenzelm@44543
   383
      val (changed_commands, new_execs) =
wenzelm@44479
   384
        ((Nil: List[Command], execs) /: command_execs) {
wenzelm@44543
   385
          case ((commands1, execs1), (cmd_id, exec)) =>
wenzelm@47395
   386
            val st = the_command_state(cmd_id)
wenzelm@44543
   387
            val commands2 = st.command :: commands1
wenzelm@44543
   388
            val execs2 =
wenzelm@44543
   389
              exec match {
wenzelm@44543
   390
                case None => execs1
wenzelm@44543
   391
                case Some(exec_id) => execs1 + (exec_id -> st)
wenzelm@44543
   392
              }
wenzelm@44543
   393
            (commands2, execs2)
wenzelm@38370
   394
        }
wenzelm@47388
   395
      val new_assignment = the_assignment(version).assign(command_execs)
wenzelm@43722
   396
      val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
wenzelm@44476
   397
wenzelm@44543
   398
      (changed_commands, new_state)
wenzelm@38370
   399
    }
wenzelm@38370
   400
wenzelm@38417
   401
    def is_assigned(version: Version): Boolean =
wenzelm@43722
   402
      assignments.get(version.id) match {
wenzelm@38370
   403
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   404
        case None => false
wenzelm@38370
   405
      }
wenzelm@38370
   406
wenzelm@44436
   407
    def is_stable(change: Change): Boolean =
wenzelm@44436
   408
      change.is_finished && is_assigned(change.version.get_finished)
wenzelm@44436
   409
wenzelm@46944
   410
    def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail
wenzelm@44672
   411
    def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
wenzelm@44436
   412
    def tip_stable: Boolean = is_stable(history.tip)
wenzelm@44475
   413
    def tip_version: Version = history.tip.version.get_finished
wenzelm@44436
   414
wenzelm@44446
   415
    def continue_history(
wenzelm@44446
   416
        previous: Future[Version],
wenzelm@40479
   417
        edits: List[Edit_Text],
wenzelm@43722
   418
        version: Future[Version]): (Change, State) =
wenzelm@38841
   419
    {
wenzelm@46678
   420
      val change = Change.make(previous, edits, version)
wenzelm@38841
   421
      (change, copy(history = history + change))
wenzelm@38841
   422
    }
wenzelm@38841
   423
wenzelm@44672
   424
    def prune_history(retain: Int = 0): (List[Version], State) =
wenzelm@44672
   425
    {
wenzelm@46679
   426
      history.prune(is_stable, retain) match {
wenzelm@46679
   427
        case Some((dropped, history1)) =>
wenzelm@44672
   428
          val dropped_versions = dropped.map(change => change.version.get_finished)
wenzelm@46679
   429
          val state1 = copy(history = history1)
wenzelm@44672
   430
          (dropped_versions, state1)
wenzelm@46679
   431
        case None => fail
wenzelm@44672
   432
      }
wenzelm@44672
   433
    }
wenzelm@44672
   434
wenzelm@44676
   435
    def removed_versions(removed: List[Version_ID]): State =
wenzelm@44676
   436
    {
wenzelm@44676
   437
      val versions1 = versions -- removed
wenzelm@44676
   438
      val assignments1 = assignments -- removed
wenzelm@44676
   439
      var commands1 = Map.empty[Command_ID, Command.State]
wenzelm@44676
   440
      var execs1 = Map.empty[Exec_ID, Command.State]
wenzelm@44676
   441
      for {
wenzelm@44676
   442
        (version_id, version) <- versions1.iterator
wenzelm@46997
   443
        command_execs = assignments1(version_id).command_execs
wenzelm@46723
   444
        (_, node) <- version.nodes.entries
wenzelm@44676
   445
        command <- node.commands.iterator
wenzelm@44676
   446
      } {
wenzelm@44676
   447
        val id = command.id
wenzelm@44676
   448
        if (!commands1.isDefinedAt(id) && commands.isDefinedAt(id))
wenzelm@44676
   449
          commands1 += (id -> commands(id))
wenzelm@44676
   450
        if (command_execs.isDefinedAt(id)) {
wenzelm@44676
   451
          val exec_id = command_execs(id)
wenzelm@44676
   452
          if (!execs1.isDefinedAt(exec_id) && execs.isDefinedAt(exec_id))
wenzelm@44676
   453
            execs1 += (exec_id -> execs(exec_id))
wenzelm@44676
   454
        }
wenzelm@44676
   455
      }
wenzelm@44676
   456
      copy(versions = versions1, commands = commands1, execs = execs1, assignments = assignments1)
wenzelm@44676
   457
    }
wenzelm@44676
   458
wenzelm@44613
   459
    def command_state(version: Version, command: Command): Command.State =
wenzelm@44613
   460
    {
wenzelm@44613
   461
      require(is_assigned(version))
wenzelm@44613
   462
      try {
wenzelm@44613
   463
        the_exec(the_assignment(version).check_finished.command_execs
wenzelm@44613
   464
          .getOrElse(command.id, fail))
wenzelm@44613
   465
      }
wenzelm@47395
   466
      catch {
wenzelm@47395
   467
        case _: State.Fail =>
wenzelm@47395
   468
          try { the_command_state(command.id) }
wenzelm@49414
   469
          catch { case _: State.Fail => command.init_state }
wenzelm@47395
   470
      }
wenzelm@44613
   471
    }
wenzelm@44613
   472
wenzelm@49645
   473
    def markup_to_XML(version: Version, node: Node, filter: XML.Elem => Boolean): XML.Body =
wenzelm@49645
   474
      node.commands.toList.map(cmd => command_state(version, cmd).markup_to_XML(filter)).flatten
wenzelm@38841
   475
wenzelm@38841
   476
    // persistent user-view
wenzelm@49346
   477
    def snapshot(name: Node.Name = Node.Name.empty, pending_edits: List[Text.Edit] = Nil)
wenzelm@49346
   478
      : Snapshot =
wenzelm@38370
   479
    {
wenzelm@44672
   480
      val stable = recent_stable
wenzelm@44436
   481
      val latest = history.tip
wenzelm@38841
   482
wenzelm@44156
   483
      // FIXME proper treatment of removed nodes
wenzelm@38841
   484
      val edits =
wenzelm@38841
   485
        (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@44156
   486
            (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
wenzelm@38841
   487
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   488
wenzelm@38841
   489
      new Snapshot
wenzelm@38841
   490
      {
wenzelm@44582
   491
        val state = State.this
wenzelm@39698
   492
        val version = stable.version.get_finished
wenzelm@48923
   493
        val node_name = name
wenzelm@38841
   494
        val node = version.nodes(name)
wenzelm@38841
   495
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@38841
   496
wenzelm@38841
   497
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38841
   498
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@43425
   499
        def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
wenzelm@43425
   500
        def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
wenzelm@39177
   501
wenzelm@51494
   502
        def eq_content(other: Snapshot): Boolean =
wenzelm@49346
   503
          !is_outdated && !other.is_outdated &&
wenzelm@49346
   504
            node.commands.size == other.node.commands.size &&
wenzelm@49346
   505
            ((node.commands.iterator zip other.node.commands.iterator) forall {
wenzelm@49346
   506
              case (cmd1, cmd2) =>
wenzelm@51494
   507
                state.command_state(version, cmd1) eq_content
wenzelm@51494
   508
                  other.state.command_state(other.version, cmd2)
wenzelm@49346
   509
            })
wenzelm@49346
   510
wenzelm@46178
   511
        def cumulate_markup[A](range: Text.Range, info: A, elements: Option[Set[String]],
wenzelm@50500
   512
          result: Command.State => PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
wenzelm@45459
   513
        {
wenzelm@45467
   514
          val former_range = revert(range)
wenzelm@45459
   515
          for {
wenzelm@45459
   516
            (command, command_start) <- node.command_range(former_range).toStream
wenzelm@50500
   517
            st = state.command_state(version, command)
wenzelm@50500
   518
            res = result(st)
wenzelm@50500
   519
            Text.Info(r0, a) <- st.markup.
wenzelm@46178
   520
              cumulate[A]((former_range - command_start).restrict(command.range), info, elements,
wenzelm@46178
   521
                {
wenzelm@46178
   522
                  case (a, Text.Info(r0, b))
wenzelm@50500
   523
                  if res.isDefinedAt((a, Text.Info(convert(r0 + command_start), b))) =>
wenzelm@50500
   524
                    res((a, Text.Info(convert(r0 + command_start), b)))
wenzelm@46178
   525
                })
wenzelm@45459
   526
          } yield Text.Info(convert(r0 + command_start), a)
wenzelm@45459
   527
        }
wenzelm@45459
   528
wenzelm@46178
   529
        def select_markup[A](range: Text.Range, elements: Option[Set[String]],
wenzelm@50500
   530
          result: Command.State => PartialFunction[Text.Markup, A]): Stream[Text.Info[A]] =
wenzelm@38845
   531
        {
wenzelm@50500
   532
          def result1(st: Command.State) =
wenzelm@50500
   533
          {
wenzelm@50500
   534
            val res = result(st)
wenzelm@45468
   535
            new PartialFunction[(Option[A], Text.Markup), Option[A]] {
wenzelm@50500
   536
              def isDefinedAt(arg: (Option[A], Text.Markup)): Boolean = res.isDefinedAt(arg._2)
wenzelm@50500
   537
              def apply(arg: (Option[A], Text.Markup)): Option[A] = Some(res(arg._2))
wenzelm@45468
   538
            }
wenzelm@50500
   539
          }
wenzelm@46198
   540
          for (Text.Info(r, Some(x)) <- cumulate_markup(range, None, elements, result1))
wenzelm@46198
   541
            yield Text.Info(r, x)
wenzelm@38845
   542
        }
wenzelm@38841
   543
      }
wenzelm@38150
   544
    }
wenzelm@34485
   545
  }
wenzelm@34840
   546
}
wenzelm@34840
   547