src/Pure/PIDE/document.scala
author wenzelm
Wed, 04 Apr 2012 14:19:47 +0200
changeset 47336 bed4b2738d8a
parent 46997 395b7277ed76
child 47388 fe4b245af74c
permissions -rw-r--r--
separate module for prover command execution;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36676
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 36012
diff changeset
     1
/*  Title:      Pure/PIDE/document.scala
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 36012
diff changeset
     2
    Author:     Makarius
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 36012
diff changeset
     3
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
     4
Document as collection of named nodes, each consisting of an editable
38418
9a7af64d71bb more explicit / functional ML version of document model;
wenzelm
parents: 38417
diff changeset
     5
list of commands, associated with asynchronous execution process.
36676
ac7961d42ac3 some rearrangement of Scala sources;
wenzelm
parents: 36012
diff changeset
     6
*/
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34390
diff changeset
     7
34871
e596a0b71f3c incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
wenzelm
parents: 34868
diff changeset
     8
package isabelle
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
     9
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
    10
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
    11
import scala.collection.mutable
37073
5e42e36a6693 @tailrec annotation;
wenzelm
parents: 37072
diff changeset
    12
5e42e36a6693 @tailrec annotation;
wenzelm
parents: 37072
diff changeset
    13
34823
2f3ea37c5958 renamed Proof_Document to Document;
wenzelm
parents: 34819
diff changeset
    14
object Document
34483
0923926022d7 superficial tuning;
wenzelm
parents: 34482
diff changeset
    15
{
38227
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
    16
  /* formal identifiers */
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
    17
38363
af7f41a8a0a8 clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents: 38361
diff changeset
    18
  type ID = Long
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents: 43722
diff changeset
    19
  val ID = Properties.Value.Long
38414
49f1f657adc2 more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents: 38374
diff changeset
    20
38373
wenzelm
parents: 38370
diff changeset
    21
  type Version_ID = ID
38363
af7f41a8a0a8 clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents: 38361
diff changeset
    22
  type Command_ID = ID
38373
wenzelm
parents: 38370
diff changeset
    23
  type Exec_ID = ID
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
    24
43662
e3175ec00311 Document.no_id/new_id as in ML (new_id *could* be session-specific but it isn't right now);
wenzelm
parents: 43427
diff changeset
    25
  val no_id: ID = 0
45243
27466646a7a3 class Counter as abstract datatype;
wenzelm
parents: 44960
diff changeset
    26
  val new_id = Counter()
38355
8cb265fb12fe represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents: 38227
diff changeset
    27
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
    28
34818
7df68a8f0e3e register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents: 34815
diff changeset
    29
38424
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
    30
  /** document structure **/
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
    31
46723
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
    32
  /* individual nodes */
38424
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
    33
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
    34
  type Edit[A, B] = (Node.Name, Node.Edit[A, B])
44384
8f6054a63f96 some support for editor perspective;
wenzelm
parents: 44383
diff changeset
    35
  type Edit_Text = Edit[Text.Edit, Text.Perspective]
8f6054a63f96 some support for editor perspective;
wenzelm
parents: 44383
diff changeset
    36
  type Edit_Command = Edit[(Option[Command], Option[Command]), Command.Perspective]
38151
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
    37
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 46723
diff changeset
    38
  type Node_Header = Exn.Result[Node.Deps]
44182
ecb51b457064 clarified node header -- exclude master_dir;
wenzelm
parents: 44163
diff changeset
    39
38151
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
    40
  object Node
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
    41
  {
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46814
diff changeset
    42
    sealed case class Deps(
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46814
diff changeset
    43
      imports: List[Name],
46940
a40be2f10ca9 explicit Outer_Syntax.Decl;
wenzelm
parents: 46938
diff changeset
    44
      keywords: List[Outer_Syntax.Decl],
46938
cda018294515 some support for outer syntax keyword declarations within theory header;
wenzelm
parents: 46814
diff changeset
    45
      uses: List[(String, Boolean)])
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 46723
diff changeset
    46
44957
098dd95349e7 more elaborate Node_Renderer, which paints node_name.theory only;
wenzelm
parents: 44676
diff changeset
    47
    object Name
098dd95349e7 more elaborate Node_Renderer, which paints node_name.theory only;
wenzelm
parents: 44676
diff changeset
    48
    {
098dd95349e7 more elaborate Node_Renderer, which paints node_name.theory only;
wenzelm
parents: 44676
diff changeset
    49
      val empty = Name("", "", "")
44959
9476c856c4b9 Document.Node.Name convenience;
wenzelm
parents: 44957
diff changeset
    50
      def apply(path: Path): Name =
9476c856c4b9 Document.Node.Name convenience;
wenzelm
parents: 44957
diff changeset
    51
      {
9476c856c4b9 Document.Node.Name convenience;
wenzelm
parents: 44957
diff changeset
    52
        val node = path.implode
9476c856c4b9 Document.Node.Name convenience;
wenzelm
parents: 44957
diff changeset
    53
        val dir = path.dir.implode
9476c856c4b9 Document.Node.Name convenience;
wenzelm
parents: 44957
diff changeset
    54
        val theory = Thy_Header.thy_name(node) getOrElse error("Bad theory file name: " + path)
9476c856c4b9 Document.Node.Name convenience;
wenzelm
parents: 44957
diff changeset
    55
        Name(node, dir, theory)
9476c856c4b9 Document.Node.Name convenience;
wenzelm
parents: 44957
diff changeset
    56
      }
46723
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
    57
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
    58
      object Ordering extends scala.math.Ordering[Name]
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
    59
      {
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
    60
        def compare(name1: Name, name2: Name): Int = name1.node compare name2.node
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
    61
      }
44957
098dd95349e7 more elaborate Node_Renderer, which paints node_name.theory only;
wenzelm
parents: 44676
diff changeset
    62
    }
44616
4beeaf2a226d tuned signature;
wenzelm
parents: 44615
diff changeset
    63
    sealed case class Name(node: String, dir: String, theory: String)
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
    64
    {
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
    65
      override def hashCode: Int = node.hashCode
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
    66
      override def equals(that: Any): Boolean =
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
    67
        that match {
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
    68
          case other: Name => node == other.node
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
    69
          case _ => false
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
    70
        }
44642
446fe2abe252 more redable Document.Node.toString;
wenzelm
parents: 44616
diff changeset
    71
      override def toString: String = theory
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
    72
    }
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
    73
44384
8f6054a63f96 some support for editor perspective;
wenzelm
parents: 44383
diff changeset
    74
    sealed abstract class Edit[A, B]
44156
6aa25b80e1a5 explicit datatypes for document node edits;
wenzelm
parents: 43780
diff changeset
    75
    {
44383
f99906c2a1d3 discontinued redundant Edit_Command_ID;
wenzelm
parents: 44222
diff changeset
    76
      def foreach(f: A => Unit)
f99906c2a1d3 discontinued redundant Edit_Command_ID;
wenzelm
parents: 44222
diff changeset
    77
      {
44156
6aa25b80e1a5 explicit datatypes for document node edits;
wenzelm
parents: 43780
diff changeset
    78
        this match {
44383
f99906c2a1d3 discontinued redundant Edit_Command_ID;
wenzelm
parents: 44222
diff changeset
    79
          case Edits(es) => es.foreach(f)
f99906c2a1d3 discontinued redundant Edit_Command_ID;
wenzelm
parents: 44222
diff changeset
    80
          case _ =>
44156
6aa25b80e1a5 explicit datatypes for document node edits;
wenzelm
parents: 43780
diff changeset
    81
        }
44383
f99906c2a1d3 discontinued redundant Edit_Command_ID;
wenzelm
parents: 44222
diff changeset
    82
      }
44156
6aa25b80e1a5 explicit datatypes for document node edits;
wenzelm
parents: 43780
diff changeset
    83
    }
44384
8f6054a63f96 some support for editor perspective;
wenzelm
parents: 44383
diff changeset
    84
    case class Clear[A, B]() extends Edit[A, B]
8f6054a63f96 some support for editor perspective;
wenzelm
parents: 44383
diff changeset
    85
    case class Edits[A, B](edits: List[A]) extends Edit[A, B]
8f6054a63f96 some support for editor perspective;
wenzelm
parents: 44383
diff changeset
    86
    case class Header[A, B](header: Node_Header) extends Edit[A, B]
8f6054a63f96 some support for editor perspective;
wenzelm
parents: 44383
diff changeset
    87
    case class Perspective[A, B](perspective: B) extends Edit[A, B]
44156
6aa25b80e1a5 explicit datatypes for document node edits;
wenzelm
parents: 43780
diff changeset
    88
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    89
    def command_starts(commands: Iterator[Command], offset: Text.Offset = 0)
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
    90
      : Iterator[(Command, Text.Offset)] =
38227
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
    91
    {
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
    92
      var i = offset
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
    93
      for (command <- commands) yield {
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
    94
        val start = i
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
    95
        i += command.length
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
    96
        (command, start)
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
    97
      }
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
    98
    }
46680
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
    99
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   100
    private val block_size = 1024
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   101
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   102
    val empty: Node = new Node()
38151
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   103
  }
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   104
46712
8650d9a95736 prefer final ADTs -- prevent ooddities;
wenzelm
parents: 46684
diff changeset
   105
  final class Node private(
46677
wenzelm
parents: 46208
diff changeset
   106
    val header: Node_Header = Exn.Exn(ERROR("Bad theory header")),
wenzelm
parents: 46208
diff changeset
   107
    val perspective: Command.Perspective = Command.Perspective.empty,
wenzelm
parents: 46208
diff changeset
   108
    val blobs: Map[String, Blob] = Map.empty,
wenzelm
parents: 46208
diff changeset
   109
    val commands: Linear_Set[Command] = Linear_Set.empty)
38151
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   110
  {
46680
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   111
    def clear: Node = new Node(header = header)
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   112
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   113
    def update_header(new_header: Node_Header): Node =
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   114
      new Node(new_header, perspective, blobs, commands)
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   115
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   116
    def update_perspective(new_perspective: Command.Perspective): Node =
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   117
      new Node(header, new_perspective, blobs, commands)
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   118
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   119
    def update_blobs(new_blobs: Map[String, Blob]): Node =
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   120
      new Node(header, perspective, new_blobs, commands)
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   121
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   122
    def update_commands(new_commands: Linear_Set[Command]): Node =
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   123
      new Node(header, perspective, blobs, new_commands)
44443
35d67b2056cc clarified Document.Node.clear -- retain header (cf. ML version);
wenzelm
parents: 44442
diff changeset
   124
46942
f5c2d66faa04 basic support for outer syntax keywords in theory header;
wenzelm
parents: 46941
diff changeset
   125
    def imports: List[Node.Name] =
f5c2d66faa04 basic support for outer syntax keywords in theory header;
wenzelm
parents: 46941
diff changeset
   126
      header match { case Exn.Res(deps) => deps.imports case _ => Nil }
f5c2d66faa04 basic support for outer syntax keywords in theory header;
wenzelm
parents: 46941
diff changeset
   127
f5c2d66faa04 basic support for outer syntax keywords in theory header;
wenzelm
parents: 46941
diff changeset
   128
    def keywords: List[Outer_Syntax.Decl] =
f5c2d66faa04 basic support for outer syntax keywords in theory header;
wenzelm
parents: 46941
diff changeset
   129
      header match { case Exn.Res(deps) => deps.keywords case _ => Nil }
f5c2d66faa04 basic support for outer syntax keywords in theory header;
wenzelm
parents: 46941
diff changeset
   130
44443
35d67b2056cc clarified Document.Node.clear -- retain header (cf. ML version);
wenzelm
parents: 44442
diff changeset
   131
43697
77ce24aa1770 explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents: 43662
diff changeset
   132
    /* commands */
77ce24aa1770 explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents: 43662
diff changeset
   133
38879
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   134
    private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   135
    {
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   136
      val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   137
      var next_block = 0
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   138
      var last_stop = 0
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   139
      for ((command, start) <- Node.command_starts(commands.iterator)) {
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   140
        last_stop = start + command.length
38885
06582837872b Node.full_index: allow command spans larger than block_size;
wenzelm
parents: 38882
diff changeset
   141
        while (last_stop + 1 > next_block) {
38879
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   142
          blocks += (command -> start)
46680
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   143
          next_block += Node.block_size
38879
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   144
        }
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   145
      }
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   146
      (blocks.toArray, Text.Range(0, last_stop))
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   147
    }
38151
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   148
38879
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   149
    def full_range: Text.Range = full_index._2
38151
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   150
38569
9d480f6a2589 tuned signatures;
wenzelm
parents: 38427
diff changeset
   151
    def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
38879
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   152
    {
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   153
      if (!commands.isEmpty && full_range.contains(i)) {
46680
234f1730582d more abstract class Document.Node;
wenzelm
parents: 46679
diff changeset
   154
        val (cmd0, start0) = full_index._1(i / Node.block_size)
38879
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   155
        Node.command_starts(commands.iterator(cmd0), start0) dropWhile {
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   156
          case (cmd, start) => start + cmd.length <= i }
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   157
      }
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   158
      else Iterator.empty
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   159
    }
38151
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   160
38569
9d480f6a2589 tuned signatures;
wenzelm
parents: 38427
diff changeset
   161
    def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] =
9d480f6a2589 tuned signatures;
wenzelm
parents: 38427
diff changeset
   162
      command_range(range.start) takeWhile { case (_, start) => start < range.stop }
38151
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   163
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   164
    def command_at(i: Text.Offset): Option[(Command, Text.Offset)] =
38151
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   165
    {
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   166
      val range = command_range(i)
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   167
      if (range.hasNext) Some(range.next) else None
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   168
    }
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   169
38879
dde403450419 Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents: 38872
diff changeset
   170
    def command_start(cmd: Command): Option[Text.Offset] =
46677
wenzelm
parents: 46208
diff changeset
   171
      Node.command_starts(commands.iterator).find(_._1 == cmd).map(_._2)
38151
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   172
  }
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   173
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
   174
46723
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   175
  /* development graph */
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   176
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   177
  object Nodes
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   178
  {
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   179
    val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering))
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   180
  }
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   181
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   182
  final class Nodes private(graph: Graph[Node.Name, Node])
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   183
  {
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   184
    def get_name(s: String): Option[Node.Name] =
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   185
      graph.keys.find(name => name.node == s)
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   186
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   187
    def apply(name: Node.Name): Node =
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   188
      graph.default_node(name, Node.empty).get_node(name)
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   189
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   190
    def + (entry: (Node.Name, Node)): Nodes =
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   191
    {
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   192
      val (name, node) = entry
46942
f5c2d66faa04 basic support for outer syntax keywords in theory header;
wenzelm
parents: 46941
diff changeset
   193
      val imports = node.imports
46723
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   194
      val graph1 =
46739
6024353549ca clarified document nodes (full import graph) vs. node_status (non-preloaded theories);
wenzelm
parents: 46737
diff changeset
   195
        (graph.default_node(name, Node.empty) /: imports)((g, p) => g.default_node(p, Node.empty))
46723
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   196
      val graph2 = (graph1 /: graph1.imm_preds(name))((g, dep) => g.del_edge(dep, name))
46739
6024353549ca clarified document nodes (full import graph) vs. node_status (non-preloaded theories);
wenzelm
parents: 46737
diff changeset
   197
      val graph3 = (graph2 /: imports)((g, dep) => g.add_edge(dep, name))
46723
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   198
      new Nodes(graph3.map_node(name, _ => node))
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   199
    }
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   200
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   201
    def entries: Iterator[(Node.Name, Node)] =
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   202
      graph.entries.map({ case (name, (node, _)) => (name, node) })
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   203
46942
f5c2d66faa04 basic support for outer syntax keywords in theory header;
wenzelm
parents: 46941
diff changeset
   204
    def descendants(names: List[Node.Name]): List[Node.Name] = graph.all_succs(names)
46723
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   205
    def topological_order: List[Node.Name] = graph.topological_order
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   206
  }
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   207
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   208
38424
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   209
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   210
  /** versioning **/
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   211
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   212
  /* particular document versions */
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   213
38417
b8922ae21111 renamed class Document to Document.Version etc.;
wenzelm
parents: 38414
diff changeset
   214
  object Version
b8922ae21111 renamed class Document to Document.Version etc.;
wenzelm
parents: 38414
diff changeset
   215
  {
46681
c083a3f621c0 more abstract class Document.Version;
wenzelm
parents: 46680
diff changeset
   216
    val init: Version = new Version()
c083a3f621c0 more abstract class Document.Version;
wenzelm
parents: 46680
diff changeset
   217
46941
c0f776b661fa maintain Version.syntax within document state;
wenzelm
parents: 46940
diff changeset
   218
    def make(syntax: Outer_Syntax, nodes: Nodes): Version =
c0f776b661fa maintain Version.syntax within document state;
wenzelm
parents: 46940
diff changeset
   219
      new Version(new_id(), syntax, nodes)
38417
b8922ae21111 renamed class Document to Document.Version etc.;
wenzelm
parents: 38414
diff changeset
   220
  }
b8922ae21111 renamed class Document to Document.Version etc.;
wenzelm
parents: 38414
diff changeset
   221
46712
8650d9a95736 prefer final ADTs -- prevent ooddities;
wenzelm
parents: 46684
diff changeset
   222
  final class Version private(
46677
wenzelm
parents: 46208
diff changeset
   223
    val id: Version_ID = no_id,
46941
c0f776b661fa maintain Version.syntax within document state;
wenzelm
parents: 46940
diff changeset
   224
    val syntax: Outer_Syntax = Outer_Syntax.empty,
46723
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   225
    val nodes: Nodes = Nodes.empty)
46941
c0f776b661fa maintain Version.syntax within document state;
wenzelm
parents: 46940
diff changeset
   226
  {
c0f776b661fa maintain Version.syntax within document state;
wenzelm
parents: 46940
diff changeset
   227
    def is_init: Boolean = id == no_id
c0f776b661fa maintain Version.syntax within document state;
wenzelm
parents: 46940
diff changeset
   228
  }
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34657
diff changeset
   229
34859
f986d84dd44b renamed Command.content to source;
wenzelm
parents: 34855
diff changeset
   230
38424
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   231
  /* changes of plain text, eventually resulting in document edits */
38227
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
   232
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
   233
  object Change
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
   234
  {
46678
c2dba08548f9 more abstract class Document.Change;
wenzelm
parents: 46677
diff changeset
   235
    val init: Change = new Change()
c2dba08548f9 more abstract class Document.Change;
wenzelm
parents: 46677
diff changeset
   236
c2dba08548f9 more abstract class Document.Change;
wenzelm
parents: 46677
diff changeset
   237
    def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change =
c2dba08548f9 more abstract class Document.Change;
wenzelm
parents: 46677
diff changeset
   238
      new Change(Some(previous), edits, version)
38227
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
   239
  }
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
   240
46712
8650d9a95736 prefer final ADTs -- prevent ooddities;
wenzelm
parents: 46684
diff changeset
   241
  final class Change private(
46677
wenzelm
parents: 46208
diff changeset
   242
    val previous: Option[Future[Version]] = Some(Future.value(Version.init)),
wenzelm
parents: 46208
diff changeset
   243
    val edits: List[Edit_Text] = Nil,
wenzelm
parents: 46208
diff changeset
   244
    val version: Future[Version] = Future.value(Version.init))
38227
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
   245
  {
44672
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   246
    def is_finished: Boolean =
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   247
      (previous match { case None => true case Some(future) => future.is_finished }) &&
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   248
      version.is_finished
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   249
46678
c2dba08548f9 more abstract class Document.Change;
wenzelm
parents: 46677
diff changeset
   250
    def truncate: Change = new Change(None, Nil, version)
38227
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
   251
  }
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
   252
6bbb42843b6e concentrate structural document notions in document.scala;
wenzelm
parents: 38220
diff changeset
   253
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   254
  /* history navigation */
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   255
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   256
  object History
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   257
  {
46679
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   258
    val init: History = new History()
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   259
  }
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   260
46712
8650d9a95736 prefer final ADTs -- prevent ooddities;
wenzelm
parents: 46684
diff changeset
   261
  final class History private(
46679
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   262
    val undo_list: List[Change] = List(Change.init))  // non-empty list
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   263
  {
46679
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   264
    def tip: Change = undo_list.head
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   265
    def + (change: Change): History = new History(change :: undo_list)
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   266
46679
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   267
    def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] =
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   268
    {
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   269
      val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   270
      val (retained, dropped) = undo_list.splitAt(n max retain)
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   271
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   272
      retained.splitAt(retained.length - 1) match {
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   273
        case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate)))
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   274
        case _ => None
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   275
      }
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   276
    }
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   277
  }
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   278
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   279
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   280
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   281
  /** global state -- document structure, execution process, editing history **/
38424
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   282
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   283
  abstract class Snapshot
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   284
  {
44582
479c07072992 tuned signature;
wenzelm
parents: 44581
diff changeset
   285
    val state: State
38424
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   286
    val version: Version
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   287
    val node: Node
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   288
    val is_outdated: Boolean
38427
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
   289
    def convert(i: Text.Offset): Text.Offset
39177
0468964aec11 simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
wenzelm
parents: 38976
diff changeset
   290
    def convert(range: Text.Range): Text.Range
38427
7066fbd315ae some derived operations on Text.Range;
wenzelm
parents: 38426
diff changeset
   291
    def revert(i: Text.Offset): Text.Offset
39177
0468964aec11 simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
wenzelm
parents: 38976
diff changeset
   292
    def revert(range: Text.Range): Text.Range
46178
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46152
diff changeset
   293
    def cumulate_markup[A](range: Text.Range, info: A, elements: Option[Set[String]],
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46152
diff changeset
   294
      result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]]
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46152
diff changeset
   295
    def select_markup[A](range: Text.Range, elements: Option[Set[String]],
46198
cd040c5772de improved select_markup: include filtering of defined results;
wenzelm
parents: 46178
diff changeset
   296
      result: PartialFunction[Text.Markup, A]): Stream[Text.Info[A]]
38424
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   297
  }
940a404e45e2 moved History/Snapshot to document.scala;
wenzelm
parents: 38418
diff changeset
   298
44476
e8a87398f35d propagate information about last command with exec state assignment through document model;
wenzelm
parents: 44475
diff changeset
   299
  type Assign =
44479
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   300
   (List[(Document.Command_ID, Option[Document.Exec_ID])],  // exec state assignment
44476
e8a87398f35d propagate information about last command with exec state assignment through document model;
wenzelm
parents: 44475
diff changeset
   301
    List[(String, Option[Document.Command_ID])])  // last exec
e8a87398f35d propagate information about last command with exec state assignment through document model;
wenzelm
parents: 44475
diff changeset
   302
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   303
  object State
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   304
  {
38373
wenzelm
parents: 38370
diff changeset
   305
    class Fail(state: State) extends Exception
wenzelm
parents: 38370
diff changeset
   306
44479
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   307
    object Assignment
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   308
    {
46683
fb160aa7a9a8 more abstract class Document.State.Assignment;
wenzelm
parents: 46682
diff changeset
   309
      val init: Assignment = new Assignment()
44479
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   310
    }
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   311
46712
8650d9a95736 prefer final ADTs -- prevent ooddities;
wenzelm
parents: 46684
diff changeset
   312
    final class Assignment private(
46677
wenzelm
parents: 46208
diff changeset
   313
      val command_execs: Map[Command_ID, Exec_ID] = Map.empty,
wenzelm
parents: 46208
diff changeset
   314
      val last_execs: Map[String, Option[Command_ID]] = Map.empty,
wenzelm
parents: 46208
diff changeset
   315
      val is_finished: Boolean = false)
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   316
    {
44477
086bb1083552 maintain last_execs assignment on Scala side;
wenzelm
parents: 44476
diff changeset
   317
      def check_finished: Assignment = { require(is_finished); this }
46683
fb160aa7a9a8 more abstract class Document.State.Assignment;
wenzelm
parents: 46682
diff changeset
   318
      def unfinished: Assignment = new Assignment(command_execs, last_execs, false)
44479
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   319
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   320
      def assign(add_command_execs: List[(Command_ID, Option[Exec_ID])],
44477
086bb1083552 maintain last_execs assignment on Scala side;
wenzelm
parents: 44476
diff changeset
   321
        add_last_execs: List[(String, Option[Command_ID])]): Assignment =
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   322
      {
38976
a4a465dc89d9 Document.State.Assignment: eliminated promise in favour of plain values -- signalling is done via event bus in Session;
wenzelm
parents: 38885
diff changeset
   323
        require(!is_finished)
44479
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   324
        val command_execs1 =
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   325
          (command_execs /: add_command_execs) {
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   326
            case (res, (command_id, None)) => res - command_id
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   327
            case (res, (command_id, Some(exec_id))) => res + (command_id -> exec_id)
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   328
          }
46683
fb160aa7a9a8 more abstract class Document.State.Assignment;
wenzelm
parents: 46682
diff changeset
   329
        new Assignment(command_execs1, last_execs ++ add_last_execs, true)
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   330
      }
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   331
    }
46682
4a74fbd6f28b tuned signature;
wenzelm
parents: 46681
diff changeset
   332
4a74fbd6f28b tuned signature;
wenzelm
parents: 46681
diff changeset
   333
    val init: State =
4a74fbd6f28b tuned signature;
wenzelm
parents: 46681
diff changeset
   334
      State().define_version(Version.init, Assignment.init).assign(Version.init.id)._2
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   335
  }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   336
46712
8650d9a95736 prefer final ADTs -- prevent ooddities;
wenzelm
parents: 46684
diff changeset
   337
  final case class State private(
46677
wenzelm
parents: 46208
diff changeset
   338
    val versions: Map[Version_ID, Version] = Map.empty,
wenzelm
parents: 46208
diff changeset
   339
    val commands: Map[Command_ID, Command.State] = Map.empty,  // static markup from define_command
wenzelm
parents: 46208
diff changeset
   340
    val execs: Map[Exec_ID, Command.State] = Map.empty,  // dynamic markup from execution
wenzelm
parents: 46208
diff changeset
   341
    val assignments: Map[Version_ID, State.Assignment] = Map.empty,
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   342
    val history: History = History.init)
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   343
  {
38373
wenzelm
parents: 38370
diff changeset
   344
    private def fail[A]: A = throw new State.Fail(this)
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   345
44479
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   346
    def define_version(version: Version, assignment: State.Assignment): State =
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   347
    {
38417
b8922ae21111 renamed class Document to Document.Version etc.;
wenzelm
parents: 38414
diff changeset
   348
      val id = version.id
b8922ae21111 renamed class Document to Document.Version etc.;
wenzelm
parents: 38414
diff changeset
   349
      copy(versions = versions + (id -> version),
44479
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   350
        assignments = assignments + (id -> assignment.unfinished))
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   351
    }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   352
38373
wenzelm
parents: 38370
diff changeset
   353
    def define_command(command: Command): State =
wenzelm
parents: 38370
diff changeset
   354
    {
wenzelm
parents: 38370
diff changeset
   355
      val id = command.id
wenzelm
parents: 38370
diff changeset
   356
      copy(commands = commands + (id -> command.empty_state))
wenzelm
parents: 38370
diff changeset
   357
    }
wenzelm
parents: 38370
diff changeset
   358
44582
479c07072992 tuned signature;
wenzelm
parents: 44581
diff changeset
   359
    def defined_command(id: Command_ID): Boolean = commands.isDefinedAt(id)
479c07072992 tuned signature;
wenzelm
parents: 44581
diff changeset
   360
44607
274eff0ea12e maintain name of *the* enclosing node as part of command -- avoid full document traversal;
wenzelm
parents: 44582
diff changeset
   361
    def find_command(version: Version, id: ID): Option[(Node, Command)] =
44582
479c07072992 tuned signature;
wenzelm
parents: 44581
diff changeset
   362
      commands.get(id) orElse execs.get(id) match {
479c07072992 tuned signature;
wenzelm
parents: 44581
diff changeset
   363
        case None => None
479c07072992 tuned signature;
wenzelm
parents: 44581
diff changeset
   364
        case Some(st) =>
479c07072992 tuned signature;
wenzelm
parents: 44581
diff changeset
   365
          val command = st.command
46681
c083a3f621c0 more abstract class Document.Version;
wenzelm
parents: 46680
diff changeset
   366
          val node = version.nodes(command.node_name)
c083a3f621c0 more abstract class Document.Version;
wenzelm
parents: 46680
diff changeset
   367
          Some((node, command))
44582
479c07072992 tuned signature;
wenzelm
parents: 44581
diff changeset
   368
      }
38373
wenzelm
parents: 38370
diff changeset
   369
38417
b8922ae21111 renamed class Document to Document.Version etc.;
wenzelm
parents: 38414
diff changeset
   370
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
44479
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   371
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)  // FIXME rename
44446
f9334afb6945 misc tuning and simplification;
wenzelm
parents: 44443
diff changeset
   372
    def the_exec(id: Exec_ID): Command.State = execs.getOrElse(id, fail)
43722
9b5dadb0c28d propagate header changes to prover process;
wenzelm
parents: 43715
diff changeset
   373
    def the_assignment(version: Version): State.Assignment =
9b5dadb0c28d propagate header changes to prover process;
wenzelm
parents: 43715
diff changeset
   374
      assignments.getOrElse(version.id, fail)
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   375
38872
26c505765024 Command.results: ordered by serial number;
wenzelm
parents: 38848
diff changeset
   376
    def accumulate(id: ID, message: XML.Elem): (Command.State, State) =
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   377
      execs.get(id) match {
44446
f9334afb6945 misc tuning and simplification;
wenzelm
parents: 44443
diff changeset
   378
        case Some(st) =>
46152
793cecd4ffc0 accumulate status as regular markup for command range;
wenzelm
parents: 45900
diff changeset
   379
          val new_st = st + message
44446
f9334afb6945 misc tuning and simplification;
wenzelm
parents: 44443
diff changeset
   380
          (new_st, copy(execs = execs + (id -> new_st)))
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   381
        case None =>
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   382
          commands.get(id) match {
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   383
            case Some(st) =>
46152
793cecd4ffc0 accumulate status as regular markup for command range;
wenzelm
parents: 45900
diff changeset
   384
              val new_st = st + message
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   385
              (new_st, copy(commands = commands + (id -> new_st)))
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   386
            case None => fail
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   387
          }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   388
      }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   389
46682
4a74fbd6f28b tuned signature;
wenzelm
parents: 46681
diff changeset
   390
    def assign(id: Version_ID, arg: Assign = (Nil, Nil)): (List[Command], State) =
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   391
    {
38417
b8922ae21111 renamed class Document to Document.Version etc.;
wenzelm
parents: 38414
diff changeset
   392
      val version = the_version(id)
44477
086bb1083552 maintain last_execs assignment on Scala side;
wenzelm
parents: 44476
diff changeset
   393
      val (command_execs, last_execs) = arg
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   394
44543
ba8f24f7156e de-assigned commands also count as changed;
wenzelm
parents: 44484
diff changeset
   395
      val (changed_commands, new_execs) =
44479
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   396
        ((Nil: List[Command], execs) /: command_execs) {
44543
ba8f24f7156e de-assigned commands also count as changed;
wenzelm
parents: 44484
diff changeset
   397
          case ((commands1, execs1), (cmd_id, exec)) =>
44479
9a04e7502e22 refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents: 44477
diff changeset
   398
            val st = the_command(cmd_id)
44543
ba8f24f7156e de-assigned commands also count as changed;
wenzelm
parents: 44484
diff changeset
   399
            val commands2 = st.command :: commands1
ba8f24f7156e de-assigned commands also count as changed;
wenzelm
parents: 44484
diff changeset
   400
            val execs2 =
ba8f24f7156e de-assigned commands also count as changed;
wenzelm
parents: 44484
diff changeset
   401
              exec match {
ba8f24f7156e de-assigned commands also count as changed;
wenzelm
parents: 44484
diff changeset
   402
                case None => execs1
ba8f24f7156e de-assigned commands also count as changed;
wenzelm
parents: 44484
diff changeset
   403
                case Some(exec_id) => execs1 + (exec_id -> st)
ba8f24f7156e de-assigned commands also count as changed;
wenzelm
parents: 44484
diff changeset
   404
              }
ba8f24f7156e de-assigned commands also count as changed;
wenzelm
parents: 44484
diff changeset
   405
            (commands2, execs2)
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   406
        }
44477
086bb1083552 maintain last_execs assignment on Scala side;
wenzelm
parents: 44476
diff changeset
   407
      val new_assignment = the_assignment(version).assign(command_execs, last_execs)
43722
9b5dadb0c28d propagate header changes to prover process;
wenzelm
parents: 43715
diff changeset
   408
      val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
44476
e8a87398f35d propagate information about last command with exec state assignment through document model;
wenzelm
parents: 44475
diff changeset
   409
44543
ba8f24f7156e de-assigned commands also count as changed;
wenzelm
parents: 44484
diff changeset
   410
      (changed_commands, new_state)
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   411
    }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   412
38417
b8922ae21111 renamed class Document to Document.Version etc.;
wenzelm
parents: 38414
diff changeset
   413
    def is_assigned(version: Version): Boolean =
43722
9b5dadb0c28d propagate header changes to prover process;
wenzelm
parents: 43715
diff changeset
   414
      assignments.get(version.id) match {
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   415
        case Some(assgn) => assgn.is_finished
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   416
        case None => false
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   417
      }
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   418
44436
546adfa8a6fc update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents: 44385
diff changeset
   419
    def is_stable(change: Change): Boolean =
546adfa8a6fc update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents: 44385
diff changeset
   420
      change.is_finished && is_assigned(change.version.get_finished)
546adfa8a6fc update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents: 44385
diff changeset
   421
46944
9fc22eb6408c more recent recent_syntax, e.g. relevant for document rendering during startup;
wenzelm
parents: 46942
diff changeset
   422
    def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail
44672
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   423
    def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail
44436
546adfa8a6fc update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents: 44385
diff changeset
   424
    def tip_stable: Boolean = is_stable(history.tip)
44475
709e1d671483 tuned signature;
wenzelm
parents: 44474
diff changeset
   425
    def tip_version: Version = history.tip.version.get_finished
44436
546adfa8a6fc update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents: 44385
diff changeset
   426
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
   427
    def last_exec_offset(name: Node.Name): Text.Offset =
44484
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   428
    {
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   429
      val version = tip_version
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
   430
      the_assignment(version).last_execs.get(name.node) match {
44484
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   431
        case Some(Some(id)) =>
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   432
          val node = version.nodes(name)
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   433
          val cmd = the_command(id).command
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   434
          node.command_start(cmd) match {
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   435
            case None => 0
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   436
            case Some(start) => start + cmd.length
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   437
          }
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   438
        case _ => 0
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   439
      }
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   440
    }
470f2ee5950e tuned Session.edit_node: update_perspective based on last_exec_offset;
wenzelm
parents: 44479
diff changeset
   441
44446
f9334afb6945 misc tuning and simplification;
wenzelm
parents: 44443
diff changeset
   442
    def continue_history(
f9334afb6945 misc tuning and simplification;
wenzelm
parents: 44443
diff changeset
   443
        previous: Future[Version],
40479
cc06f5528bb1 unified type Document.Edit;
wenzelm
parents: 40478
diff changeset
   444
        edits: List[Edit_Text],
43722
9b5dadb0c28d propagate header changes to prover process;
wenzelm
parents: 43715
diff changeset
   445
        version: Future[Version]): (Change, State) =
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   446
    {
46678
c2dba08548f9 more abstract class Document.Change;
wenzelm
parents: 46677
diff changeset
   447
      val change = Change.make(previous, edits, version)
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   448
      (change, copy(history = history + change))
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   449
    }
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   450
44672
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   451
    def prune_history(retain: Int = 0): (List[Version], State) =
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   452
    {
46679
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   453
      history.prune(is_stable, retain) match {
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   454
        case Some((dropped, history1)) =>
44672
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   455
          val dropped_versions = dropped.map(change => change.version.get_finished)
46679
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   456
          val state1 = copy(history = history1)
44672
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   457
          (dropped_versions, state1)
46679
bce11e807488 more abstract class Document.History;
wenzelm
parents: 46678
diff changeset
   458
        case None => fail
44672
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   459
      }
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   460
    }
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   461
44676
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   462
    def removed_versions(removed: List[Version_ID]): State =
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   463
    {
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   464
      val versions1 = versions -- removed
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   465
      val assignments1 = assignments -- removed
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   466
      var commands1 = Map.empty[Command_ID, Command.State]
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   467
      var execs1 = Map.empty[Exec_ID, Command.State]
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   468
      for {
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   469
        (version_id, version) <- versions1.iterator
46997
395b7277ed76 misc tuning to accomodate scala-2.10.0-M2;
wenzelm
parents: 46944
diff changeset
   470
        command_execs = assignments1(version_id).command_execs
46723
54ea872b60ea more explicit development graph;
wenzelm
parents: 46712
diff changeset
   471
        (_, node) <- version.nodes.entries
44676
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   472
        command <- node.commands.iterator
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   473
      } {
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   474
        val id = command.id
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   475
        if (!commands1.isDefinedAt(id) && commands.isDefinedAt(id))
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   476
          commands1 += (id -> commands(id))
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   477
        if (command_execs.isDefinedAt(id)) {
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   478
          val exec_id = command_execs(id)
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   479
          if (!execs1.isDefinedAt(exec_id) && execs.isDefinedAt(exec_id))
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   480
            execs1 += (exec_id -> execs(exec_id))
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   481
        }
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   482
      }
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   483
      copy(versions = versions1, commands = commands1, execs = execs1, assignments = assignments1)
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   484
    }
7de87f1ae965 Document.removed_versions on Scala side;
wenzelm
parents: 44672
diff changeset
   485
44613
a3255c85327b crude display of node status;
wenzelm
parents: 44607
diff changeset
   486
    def command_state(version: Version, command: Command): Command.State =
a3255c85327b crude display of node status;
wenzelm
parents: 44607
diff changeset
   487
    {
a3255c85327b crude display of node status;
wenzelm
parents: 44607
diff changeset
   488
      require(is_assigned(version))
a3255c85327b crude display of node status;
wenzelm
parents: 44607
diff changeset
   489
      try {
a3255c85327b crude display of node status;
wenzelm
parents: 44607
diff changeset
   490
        the_exec(the_assignment(version).check_finished.command_execs
a3255c85327b crude display of node status;
wenzelm
parents: 44607
diff changeset
   491
          .getOrElse(command.id, fail))
a3255c85327b crude display of node status;
wenzelm
parents: 44607
diff changeset
   492
      }
a3255c85327b crude display of node status;
wenzelm
parents: 44607
diff changeset
   493
      catch { case _: State.Fail => command.empty_state }
a3255c85327b crude display of node status;
wenzelm
parents: 44607
diff changeset
   494
    }
a3255c85327b crude display of node status;
wenzelm
parents: 44607
diff changeset
   495
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   496
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   497
    // persistent user-view
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44613
diff changeset
   498
    def snapshot(name: Node.Name, pending_edits: List[Text.Edit]): Snapshot =
38370
8b15d0f98962 explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents: 38367
diff changeset
   499
    {
44672
07dad1433cd7 some support to prune_history;
wenzelm
parents: 44642
diff changeset
   500
      val stable = recent_stable
44436
546adfa8a6fc update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents: 44385
diff changeset
   501
      val latest = history.tip
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   502
44156
6aa25b80e1a5 explicit datatypes for document node edits;
wenzelm
parents: 43780
diff changeset
   503
      // FIXME proper treatment of removed nodes
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   504
      val edits =
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   505
        (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
44156
6aa25b80e1a5 explicit datatypes for document node edits;
wenzelm
parents: 43780
diff changeset
   506
            (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   507
      lazy val reverse_edits = edits.reverse
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   508
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   509
      new Snapshot
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   510
      {
44582
479c07072992 tuned signature;
wenzelm
parents: 44581
diff changeset
   511
        val state = State.this
39698
625a3bc4417b tuned signature;
wenzelm
parents: 39621
diff changeset
   512
        val version = stable.version.get_finished
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   513
        val node = version.nodes(name)
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   514
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   515
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   516
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   517
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
43425
0a5612040a8b more explicit error message;
wenzelm
parents: 40479
diff changeset
   518
        def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
0a5612040a8b more explicit error message;
wenzelm
parents: 40479
diff changeset
   519
        def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
39177
0468964aec11 simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
wenzelm
parents: 38976
diff changeset
   520
46178
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46152
diff changeset
   521
        def cumulate_markup[A](range: Text.Range, info: A, elements: Option[Set[String]],
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46152
diff changeset
   522
          result: PartialFunction[(A, Text.Markup), A]): Stream[Text.Info[A]] =
45459
a5c1599f664d added markup_cumulate operator;
wenzelm
parents: 45243
diff changeset
   523
        {
45467
3f290b6288cf tuned signature;
wenzelm
parents: 45459
diff changeset
   524
          val former_range = revert(range)
45459
a5c1599f664d added markup_cumulate operator;
wenzelm
parents: 45243
diff changeset
   525
          for {
a5c1599f664d added markup_cumulate operator;
wenzelm
parents: 45243
diff changeset
   526
            (command, command_start) <- node.command_range(former_range).toStream
46208
4cab63a6dc16 tuned signature;
wenzelm
parents: 46198
diff changeset
   527
            Text.Info(r0, a) <- state.command_state(version, command).markup.
46178
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46152
diff changeset
   528
              cumulate[A]((former_range - command_start).restrict(command.range), info, elements,
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46152
diff changeset
   529
                {
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46152
diff changeset
   530
                  case (a, Text.Info(r0, b))
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46152
diff changeset
   531
                  if result.isDefinedAt((a, Text.Info(convert(r0 + command_start), b))) =>
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46152
diff changeset
   532
                    result((a, Text.Info(convert(r0 + command_start), b)))
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46152
diff changeset
   533
                })
45459
a5c1599f664d added markup_cumulate operator;
wenzelm
parents: 45243
diff changeset
   534
          } yield Text.Info(convert(r0 + command_start), a)
a5c1599f664d added markup_cumulate operator;
wenzelm
parents: 45243
diff changeset
   535
        }
a5c1599f664d added markup_cumulate operator;
wenzelm
parents: 45243
diff changeset
   536
46178
1c5c88f6feb5 clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents: 46152
diff changeset
   537
        def select_markup[A](range: Text.Range, elements: Option[Set[String]],
46198
cd040c5772de improved select_markup: include filtering of defined results;
wenzelm
parents: 46178
diff changeset
   538
          result: PartialFunction[Text.Markup, A]): Stream[Text.Info[A]] =
38845
a9e37daf5bd0 added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents: 38841
diff changeset
   539
        {
45468
33e946d3f449 tuned signature;
wenzelm
parents: 45467
diff changeset
   540
          val result1 =
33e946d3f449 tuned signature;
wenzelm
parents: 45467
diff changeset
   541
            new PartialFunction[(Option[A], Text.Markup), Option[A]] {
33e946d3f449 tuned signature;
wenzelm
parents: 45467
diff changeset
   542
              def isDefinedAt(arg: (Option[A], Text.Markup)): Boolean = result.isDefinedAt(arg._2)
33e946d3f449 tuned signature;
wenzelm
parents: 45467
diff changeset
   543
              def apply(arg: (Option[A], Text.Markup)): Option[A] = Some(result(arg._2))
33e946d3f449 tuned signature;
wenzelm
parents: 45467
diff changeset
   544
            }
46198
cd040c5772de improved select_markup: include filtering of defined results;
wenzelm
parents: 46178
diff changeset
   545
          for (Text.Info(r, Some(x)) <- cumulate_markup(range, None, elements, result1))
cd040c5772de improved select_markup: include filtering of defined results;
wenzelm
parents: 46178
diff changeset
   546
            yield Text.Info(r, x)
38845
a9e37daf5bd0 added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents: 38841
diff changeset
   547
        }
38841
4df7b76249a0 include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents: 38569
diff changeset
   548
      }
38150
67fc24df3721 simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents: 37849
diff changeset
   549
    }
34485
6475bfb4ff99 joined Document with ProofDocument;
wenzelm
parents: 34483
diff changeset
   550
  }
34840
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   551
}
6c5560d48561 more precise treatment of document/state assigment;
wenzelm
parents: 34838
diff changeset
   552