src/Pure/PIDE/document.scala
author wenzelm
Tue Aug 30 16:04:26 2011 +0200 (2011-08-30)
changeset 44582 479c07072992
parent 44581 7daef43592d0
child 44607 274eff0ea12e
permissions -rw-r--r--
tuned signature;
wenzelm@36676
     1
/*  Title:      Pure/PIDE/document.scala
wenzelm@36676
     2
    Author:     Makarius
wenzelm@36676
     3
wenzelm@38150
     4
Document as collection of named nodes, each consisting of an editable
wenzelm@38418
     5
list of commands, associated with asynchronous execution process.
wenzelm@36676
     6
*/
wenzelm@34407
     7
wenzelm@34871
     8
package isabelle
wenzelm@34318
     9
wenzelm@34760
    10
wenzelm@38150
    11
import scala.collection.mutable
wenzelm@37073
    12
wenzelm@37073
    13
wenzelm@34823
    14
object Document
wenzelm@34483
    15
{
wenzelm@38227
    16
  /* formal identifiers */
wenzelm@38150
    17
wenzelm@38363
    18
  type ID = Long
wenzelm@43780
    19
  val ID = Properties.Value.Long
wenzelm@38414
    20
wenzelm@38373
    21
  type Version_ID = ID
wenzelm@38363
    22
  type Command_ID = ID
wenzelm@38373
    23
  type Exec_ID = ID
wenzelm@38150
    24
wenzelm@43662
    25
  val no_id: ID = 0
wenzelm@43662
    26
  val new_id = new Counter
wenzelm@38355
    27
wenzelm@38150
    28
wenzelm@34818
    29
wenzelm@38424
    30
  /** document structure **/
wenzelm@38424
    31
wenzelm@38424
    32
  /* named nodes -- development graph */
wenzelm@38424
    33
wenzelm@44384
    34
  type Edit[A, B] = (String, Node.Edit[A, B])
wenzelm@44384
    35
  type Edit_Text = Edit[Text.Edit, Text.Perspective]
wenzelm@44384
    36
  type Edit_Command = Edit[(Option[Command], Option[Command]), Command.Perspective]
wenzelm@38151
    37
wenzelm@44182
    38
  type Node_Header = Exn.Result[Thy_Header]
wenzelm@44182
    39
wenzelm@38151
    40
  object Node
wenzelm@38151
    41
  {
wenzelm@44384
    42
    sealed abstract class Edit[A, B]
wenzelm@44156
    43
    {
wenzelm@44383
    44
      def foreach(f: A => Unit)
wenzelm@44383
    45
      {
wenzelm@44156
    46
        this match {
wenzelm@44383
    47
          case Edits(es) => es.foreach(f)
wenzelm@44383
    48
          case _ =>
wenzelm@44156
    49
        }
wenzelm@44383
    50
      }
wenzelm@44156
    51
    }
wenzelm@44384
    52
    case class Clear[A, B]() extends Edit[A, B]
wenzelm@44384
    53
    case class Edits[A, B](edits: List[A]) extends Edit[A, B]
wenzelm@44384
    54
    case class Header[A, B](header: Node_Header) extends Edit[A, B]
wenzelm@44384
    55
    case class Perspective[A, B](perspective: B) extends Edit[A, B]
wenzelm@44156
    56
wenzelm@44442
    57
    def norm_header(f: String => String, g: String => String, header: Node_Header): Node_Header =
wenzelm@44182
    58
      header match {
wenzelm@44442
    59
        case Exn.Res(h) => Exn.capture { h.norm_deps(f, g) }
wenzelm@44442
    60
        case exn => exn
wenzelm@44182
    61
      }
wenzelm@43697
    62
wenzelm@44474
    63
    val empty: Node =
wenzelm@44474
    64
      Node(Exn.Exn(ERROR("Bad theory header")), Command.Perspective.empty, Map(), Linear_Set())
wenzelm@38227
    65
wenzelm@38426
    66
    def command_starts(commands: Iterator[Command], offset: Text.Offset = 0)
wenzelm@38426
    67
      : Iterator[(Command, Text.Offset)] =
wenzelm@38227
    68
    {
wenzelm@38227
    69
      var i = offset
wenzelm@38227
    70
      for (command <- commands) yield {
wenzelm@38227
    71
        val start = i
wenzelm@38227
    72
        i += command.length
wenzelm@38227
    73
        (command, start)
wenzelm@38227
    74
      }
wenzelm@38227
    75
    }
wenzelm@38151
    76
  }
wenzelm@38151
    77
wenzelm@38880
    78
  private val block_size = 1024
wenzelm@38879
    79
wenzelm@43715
    80
  sealed case class Node(
wenzelm@44182
    81
    val header: Node_Header,
wenzelm@44385
    82
    val perspective: Command.Perspective,
wenzelm@43715
    83
    val blobs: Map[String, Blob],
wenzelm@43715
    84
    val commands: Linear_Set[Command])
wenzelm@38151
    85
  {
wenzelm@44443
    86
    def clear: Node = Node.empty.copy(header = header)
wenzelm@44443
    87
wenzelm@44443
    88
wenzelm@43697
    89
    /* commands */
wenzelm@43697
    90
wenzelm@38879
    91
    private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
wenzelm@38879
    92
    {
wenzelm@38879
    93
      val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
wenzelm@38879
    94
      var next_block = 0
wenzelm@38879
    95
      var last_stop = 0
wenzelm@38879
    96
      for ((command, start) <- Node.command_starts(commands.iterator)) {
wenzelm@38879
    97
        last_stop = start + command.length
wenzelm@38885
    98
        while (last_stop + 1 > next_block) {
wenzelm@38879
    99
          blocks += (command -> start)
wenzelm@38879
   100
          next_block += block_size
wenzelm@38879
   101
        }
wenzelm@38879
   102
      }
wenzelm@38879
   103
      (blocks.toArray, Text.Range(0, last_stop))
wenzelm@38879
   104
    }
wenzelm@38151
   105
wenzelm@38879
   106
    def full_range: Text.Range = full_index._2
wenzelm@38151
   107
wenzelm@38569
   108
    def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@38879
   109
    {
wenzelm@38879
   110
      if (!commands.isEmpty && full_range.contains(i)) {
wenzelm@38879
   111
        val (cmd0, start0) = full_index._1(i / block_size)
wenzelm@38879
   112
        Node.command_starts(commands.iterator(cmd0), start0) dropWhile {
wenzelm@38879
   113
          case (cmd, start) => start + cmd.length <= i }
wenzelm@38879
   114
      }
wenzelm@38879
   115
      else Iterator.empty
wenzelm@38879
   116
    }
wenzelm@38151
   117
wenzelm@38569
   118
    def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] =
wenzelm@38569
   119
      command_range(range.start) takeWhile { case (_, start) => start < range.stop }
wenzelm@38151
   120
wenzelm@38426
   121
    def command_at(i: Text.Offset): Option[(Command, Text.Offset)] =
wenzelm@38151
   122
    {
wenzelm@38151
   123
      val range = command_range(i)
wenzelm@38151
   124
      if (range.hasNext) Some(range.next) else None
wenzelm@38151
   125
    }
wenzelm@38151
   126
wenzelm@38426
   127
    def proper_command_at(i: Text.Offset): Option[Command] =
wenzelm@38151
   128
      command_at(i) match {
wenzelm@38151
   129
        case Some((command, _)) =>
wenzelm@38151
   130
          commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
wenzelm@38151
   131
        case None => None
wenzelm@38151
   132
      }
wenzelm@38879
   133
wenzelm@38879
   134
    def command_start(cmd: Command): Option[Text.Offset] =
wenzelm@38879
   135
      command_starts.find(_._1 == cmd).map(_._2)
wenzelm@38879
   136
wenzelm@38879
   137
    def command_starts: Iterator[(Command, Text.Offset)] =
wenzelm@38879
   138
      Node.command_starts(commands.iterator)
wenzelm@38151
   139
  }
wenzelm@38151
   140
wenzelm@38151
   141
wenzelm@38424
   142
wenzelm@38424
   143
  /** versioning **/
wenzelm@38424
   144
wenzelm@38424
   145
  /* particular document versions */
wenzelm@34485
   146
wenzelm@38417
   147
  object Version
wenzelm@38417
   148
  {
wenzelm@43722
   149
    val init: Version = Version(no_id, Map().withDefaultValue(Node.empty))
wenzelm@38417
   150
  }
wenzelm@38417
   151
wenzelm@44436
   152
  type Nodes = Map[String, Node]
wenzelm@44436
   153
  sealed case class Version(val id: Version_ID, val nodes: Nodes)
immler@34660
   154
wenzelm@34859
   155
wenzelm@38424
   156
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   157
wenzelm@38227
   158
  object Change
wenzelm@38227
   159
  {
wenzelm@44479
   160
    val init: Change = Change(Future.value(Version.init), Nil, Future.value(Version.init))
wenzelm@38227
   161
  }
wenzelm@38227
   162
wenzelm@43722
   163
  sealed case class Change(
wenzelm@38417
   164
    val previous: Future[Version],
wenzelm@40479
   165
    val edits: List[Edit_Text],
wenzelm@43722
   166
    val version: Future[Version])
wenzelm@38227
   167
  {
wenzelm@39698
   168
    def is_finished: Boolean = previous.is_finished && version.is_finished
wenzelm@38227
   169
  }
wenzelm@38227
   170
wenzelm@38227
   171
wenzelm@38841
   172
  /* history navigation */
wenzelm@38841
   173
wenzelm@38841
   174
  object History
wenzelm@38841
   175
  {
wenzelm@44479
   176
    val init: History = new History(List(Change.init))
wenzelm@38841
   177
  }
wenzelm@38841
   178
wenzelm@38841
   179
  // FIXME pruning, purging of state
wenzelm@38841
   180
  class History(val undo_list: List[Change])
wenzelm@38841
   181
  {
wenzelm@38841
   182
    require(!undo_list.isEmpty)
wenzelm@38841
   183
wenzelm@38841
   184
    def tip: Change = undo_list.head
wenzelm@38841
   185
    def +(change: Change): History = new History(change :: undo_list)
wenzelm@38841
   186
  }
wenzelm@38841
   187
wenzelm@38841
   188
wenzelm@38841
   189
wenzelm@38841
   190
  /** global state -- document structure, execution process, editing history **/
wenzelm@38424
   191
wenzelm@38424
   192
  abstract class Snapshot
wenzelm@38424
   193
  {
wenzelm@44582
   194
    val state: State
wenzelm@38424
   195
    val version: Version
wenzelm@38424
   196
    val node: Node
wenzelm@38424
   197
    val is_outdated: Boolean
wenzelm@44582
   198
    def command_state(command: Command): Command.State
wenzelm@38427
   199
    def convert(i: Text.Offset): Text.Offset
wenzelm@39177
   200
    def convert(range: Text.Range): Text.Range
wenzelm@38427
   201
    def revert(i: Text.Offset): Text.Offset
wenzelm@39177
   202
    def revert(range: Text.Range): Text.Range
wenzelm@39178
   203
    def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A])
wenzelm@39177
   204
      : Stream[Text.Info[Option[A]]]
wenzelm@38424
   205
  }
wenzelm@38424
   206
wenzelm@44476
   207
  type Assign =
wenzelm@44479
   208
   (List[(Document.Command_ID, Option[Document.Exec_ID])],  // exec state assignment
wenzelm@44476
   209
    List[(String, Option[Document.Command_ID])])  // last exec
wenzelm@44476
   210
wenzelm@44476
   211
  val no_assign: Assign = (Nil, Nil)
wenzelm@44476
   212
wenzelm@38370
   213
  object State
wenzelm@38370
   214
  {
wenzelm@38373
   215
    class Fail(state: State) extends Exception
wenzelm@38373
   216
wenzelm@44479
   217
    val init: State =
wenzelm@44479
   218
      State().define_version(Version.init, Assignment.init).assign(Version.init.id, no_assign)._2
wenzelm@44479
   219
wenzelm@44479
   220
    object Assignment
wenzelm@44479
   221
    {
wenzelm@44479
   222
      val init: Assignment = Assignment(Map.empty, Map.empty, false)
wenzelm@44479
   223
    }
wenzelm@38370
   224
wenzelm@38976
   225
    case class Assignment(
wenzelm@44477
   226
      val command_execs: Map[Command_ID, Exec_ID],
wenzelm@44477
   227
      val last_execs: Map[String, Option[Command_ID]],
wenzelm@44477
   228
      val is_finished: Boolean)
wenzelm@38370
   229
    {
wenzelm@44477
   230
      def check_finished: Assignment = { require(is_finished); this }
wenzelm@44479
   231
      def unfinished: Assignment = copy(is_finished = false)
wenzelm@44479
   232
wenzelm@44479
   233
      def assign(add_command_execs: List[(Command_ID, Option[Exec_ID])],
wenzelm@44477
   234
        add_last_execs: List[(String, Option[Command_ID])]): Assignment =
wenzelm@38370
   235
      {
wenzelm@38976
   236
        require(!is_finished)
wenzelm@44479
   237
        val command_execs1 =
wenzelm@44479
   238
          (command_execs /: add_command_execs) {
wenzelm@44479
   239
            case (res, (command_id, None)) => res - command_id
wenzelm@44479
   240
            case (res, (command_id, Some(exec_id))) => res + (command_id -> exec_id)
wenzelm@44479
   241
          }
wenzelm@44479
   242
        Assignment(command_execs1, last_execs ++ add_last_execs, true)
wenzelm@38150
   243
      }
wenzelm@38370
   244
    }
wenzelm@38370
   245
  }
wenzelm@38370
   246
wenzelm@43715
   247
  sealed case class State(
wenzelm@38417
   248
    val versions: Map[Version_ID, Version] = Map(),
wenzelm@44446
   249
    val commands: Map[Command_ID, Command.State] = Map(),  // static markup from define_command
wenzelm@44446
   250
    val execs: Map[Exec_ID, Command.State] = Map(),  // dynamic markup from execution
wenzelm@43722
   251
    val assignments: Map[Version_ID, State.Assignment] = Map(),
wenzelm@38841
   252
    val disposed: Set[ID] = Set(),  // FIXME unused!?
wenzelm@38841
   253
    val history: History = History.init)
wenzelm@38370
   254
  {
wenzelm@38373
   255
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   256
wenzelm@44479
   257
    def define_version(version: Version, assignment: State.Assignment): State =
wenzelm@38370
   258
    {
wenzelm@38417
   259
      val id = version.id
wenzelm@38417
   260
      if (versions.isDefinedAt(id) || disposed(id)) fail
wenzelm@38417
   261
      copy(versions = versions + (id -> version),
wenzelm@44479
   262
        assignments = assignments + (id -> assignment.unfinished))
wenzelm@38370
   263
    }
wenzelm@38370
   264
wenzelm@38373
   265
    def define_command(command: Command): State =
wenzelm@38373
   266
    {
wenzelm@38373
   267
      val id = command.id
wenzelm@38373
   268
      if (commands.isDefinedAt(id) || disposed(id)) fail
wenzelm@38373
   269
      copy(commands = commands + (id -> command.empty_state))
wenzelm@38373
   270
    }
wenzelm@38373
   271
wenzelm@44582
   272
    def defined_command(id: Command_ID): Boolean = commands.isDefinedAt(id)
wenzelm@44582
   273
wenzelm@44582
   274
    def find_command(version: Version, id: ID): Option[(String, Node, Command)] =
wenzelm@44582
   275
      commands.get(id) orElse execs.get(id) match {
wenzelm@44582
   276
        case None => None
wenzelm@44582
   277
        case Some(st) =>
wenzelm@44582
   278
          val command = st.command
wenzelm@44582
   279
          version.nodes.find({ case (_, node) => node.commands(command) })
wenzelm@44582
   280
            .map({ case (name, node) => (name, node, command) })
wenzelm@44582
   281
      }
wenzelm@38373
   282
wenzelm@38417
   283
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
wenzelm@44479
   284
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)  // FIXME rename
wenzelm@44446
   285
    def the_exec(id: Exec_ID): Command.State = execs.getOrElse(id, fail)
wenzelm@43722
   286
    def the_assignment(version: Version): State.Assignment =
wenzelm@43722
   287
      assignments.getOrElse(version.id, fail)
wenzelm@38370
   288
wenzelm@38872
   289
    def accumulate(id: ID, message: XML.Elem): (Command.State, State) =
wenzelm@38370
   290
      execs.get(id) match {
wenzelm@44446
   291
        case Some(st) =>
wenzelm@38370
   292
          val new_st = st.accumulate(message)
wenzelm@44446
   293
          (new_st, copy(execs = execs + (id -> new_st)))
wenzelm@38370
   294
        case None =>
wenzelm@38370
   295
          commands.get(id) match {
wenzelm@38370
   296
            case Some(st) =>
wenzelm@38370
   297
              val new_st = st.accumulate(message)
wenzelm@38370
   298
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   299
            case None => fail
wenzelm@38370
   300
          }
wenzelm@38370
   301
      }
wenzelm@38370
   302
wenzelm@44476
   303
    def assign(id: Version_ID, arg: Assign): (List[Command], State) =
wenzelm@38370
   304
    {
wenzelm@38417
   305
      val version = the_version(id)
wenzelm@44477
   306
      val (command_execs, last_execs) = arg
wenzelm@38370
   307
wenzelm@44543
   308
      val (changed_commands, new_execs) =
wenzelm@44479
   309
        ((Nil: List[Command], execs) /: command_execs) {
wenzelm@44543
   310
          case ((commands1, execs1), (cmd_id, exec)) =>
wenzelm@44479
   311
            val st = the_command(cmd_id)
wenzelm@44543
   312
            val commands2 = st.command :: commands1
wenzelm@44543
   313
            val execs2 =
wenzelm@44543
   314
              exec match {
wenzelm@44543
   315
                case None => execs1
wenzelm@44543
   316
                case Some(exec_id) => execs1 + (exec_id -> st)
wenzelm@44543
   317
              }
wenzelm@44543
   318
            (commands2, execs2)
wenzelm@38370
   319
        }
wenzelm@44477
   320
      val new_assignment = the_assignment(version).assign(command_execs, last_execs)
wenzelm@43722
   321
      val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
wenzelm@44476
   322
wenzelm@44543
   323
      (changed_commands, new_state)
wenzelm@38370
   324
    }
wenzelm@38370
   325
wenzelm@38417
   326
    def is_assigned(version: Version): Boolean =
wenzelm@43722
   327
      assignments.get(version.id) match {
wenzelm@38370
   328
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   329
        case None => false
wenzelm@38370
   330
      }
wenzelm@38370
   331
wenzelm@44436
   332
    def is_stable(change: Change): Boolean =
wenzelm@44436
   333
      change.is_finished && is_assigned(change.version.get_finished)
wenzelm@44436
   334
wenzelm@44475
   335
    def recent_stable: Option[Change] = history.undo_list.find(is_stable)
wenzelm@44436
   336
    def tip_stable: Boolean = is_stable(history.tip)
wenzelm@44475
   337
    def tip_version: Version = history.tip.version.get_finished
wenzelm@44436
   338
wenzelm@44484
   339
    def last_exec_offset(name: String): Text.Offset =
wenzelm@44484
   340
    {
wenzelm@44484
   341
      val version = tip_version
wenzelm@44484
   342
      the_assignment(version).last_execs.get(name) match {
wenzelm@44484
   343
        case Some(Some(id)) =>
wenzelm@44484
   344
          val node = version.nodes(name)
wenzelm@44484
   345
          val cmd = the_command(id).command
wenzelm@44484
   346
          node.command_start(cmd) match {
wenzelm@44484
   347
            case None => 0
wenzelm@44484
   348
            case Some(start) => start + cmd.length
wenzelm@44484
   349
          }
wenzelm@44484
   350
        case _ => 0
wenzelm@44484
   351
      }
wenzelm@44484
   352
    }
wenzelm@44484
   353
wenzelm@44446
   354
    def continue_history(
wenzelm@44446
   355
        previous: Future[Version],
wenzelm@40479
   356
        edits: List[Edit_Text],
wenzelm@43722
   357
        version: Future[Version]): (Change, State) =
wenzelm@38841
   358
    {
wenzelm@43722
   359
      val change = Change(previous, edits, version)
wenzelm@38841
   360
      (change, copy(history = history + change))
wenzelm@38841
   361
    }
wenzelm@38841
   362
wenzelm@38841
   363
wenzelm@38841
   364
    // persistent user-view
wenzelm@38841
   365
    def snapshot(name: String, pending_edits: List[Text.Edit]): Snapshot =
wenzelm@38370
   366
    {
wenzelm@44436
   367
      val stable = recent_stable.get
wenzelm@44436
   368
      val latest = history.tip
wenzelm@38841
   369
wenzelm@44156
   370
      // FIXME proper treatment of removed nodes
wenzelm@38841
   371
      val edits =
wenzelm@38841
   372
        (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@44156
   373
            (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
wenzelm@38841
   374
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   375
wenzelm@38841
   376
      new Snapshot
wenzelm@38841
   377
      {
wenzelm@44582
   378
        val state = State.this
wenzelm@39698
   379
        val version = stable.version.get_finished
wenzelm@38841
   380
        val node = version.nodes(name)
wenzelm@38841
   381
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@38841
   382
wenzelm@44582
   383
        def command_state(command: Command): Command.State =
wenzelm@44477
   384
          try {
wenzelm@44477
   385
            the_exec(the_assignment(version).check_finished.command_execs
wenzelm@44477
   386
              .getOrElse(command.id, fail))
wenzelm@44477
   387
          }
wenzelm@38841
   388
          catch { case _: State.Fail => command.empty_state }
wenzelm@38841
   389
wenzelm@38841
   390
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38841
   391
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@43425
   392
        def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
wenzelm@43425
   393
        def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
wenzelm@39177
   394
wenzelm@39178
   395
        def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A])
wenzelm@39177
   396
          : Stream[Text.Info[Option[A]]] =
wenzelm@38845
   397
        {
wenzelm@38845
   398
          val former_range = revert(range)
wenzelm@38845
   399
          for {
wenzelm@39177
   400
            (command, command_start) <- node.command_range(former_range).toStream
wenzelm@44582
   401
            Text.Info(r0, x) <- command_state(command).markup.
wenzelm@38845
   402
              select((former_range - command_start).restrict(command.range)) {
wenzelm@38845
   403
                case Text.Info(r0, info)
wenzelm@38845
   404
                if result.isDefinedAt(Text.Info(convert(r0 + command_start), info)) =>
wenzelm@38845
   405
                  result(Text.Info(convert(r0 + command_start), info))
wenzelm@39177
   406
              }
wenzelm@43427
   407
          } yield Text.Info(convert(r0 + command_start), x)
wenzelm@38845
   408
        }
wenzelm@38841
   409
      }
wenzelm@38150
   410
    }
wenzelm@34485
   411
  }
wenzelm@34840
   412
}
wenzelm@34840
   413