src/Pure/PIDE/document.scala
author wenzelm
Tue Jul 12 19:36:46 2011 +0200 (2011-07-12)
changeset 43780 2cb2310d68b6
parent 43722 9b5dadb0c28d
child 44156 6aa25b80e1a5
permissions -rw-r--r--
more uniform Properties in ML and Scala;
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@40479
    34
  type Edit[A] =
wenzelm@40478
    35
   (String,  // node name
wenzelm@40479
    36
    Option[List[A]])  // None: remove node, Some: edit content
wenzelm@38424
    37
wenzelm@40479
    38
  type Edit_Text = Edit[Text.Edit]
wenzelm@40479
    39
  type Edit_Command = Edit[(Option[Command], Option[Command])]
wenzelm@40479
    40
  type Edit_Command_ID = Edit[(Option[Command_ID], Option[Command_ID])]
wenzelm@38151
    41
wenzelm@38151
    42
  object Node
wenzelm@38151
    43
  {
wenzelm@43715
    44
    sealed case class Header(val master_dir: Path, val thy_header: Exn.Result[Thy_Header.Header])
wenzelm@43715
    45
    val empty_header = Header(Path.current, Exn.Exn(ERROR("Bad theory header")))
wenzelm@43697
    46
wenzelm@43715
    47
    val empty: Node = Node(empty_header, Map(), Linear_Set())
wenzelm@38227
    48
wenzelm@38426
    49
    def command_starts(commands: Iterator[Command], offset: Text.Offset = 0)
wenzelm@38426
    50
      : Iterator[(Command, Text.Offset)] =
wenzelm@38227
    51
    {
wenzelm@38227
    52
      var i = offset
wenzelm@38227
    53
      for (command <- commands) yield {
wenzelm@38227
    54
        val start = i
wenzelm@38227
    55
        i += command.length
wenzelm@38227
    56
        (command, start)
wenzelm@38227
    57
      }
wenzelm@38227
    58
    }
wenzelm@38151
    59
  }
wenzelm@38151
    60
wenzelm@38880
    61
  private val block_size = 1024
wenzelm@38879
    62
wenzelm@43715
    63
  sealed case class Node(
wenzelm@43715
    64
    val header: Node.Header,
wenzelm@43715
    65
    val blobs: Map[String, Blob],
wenzelm@43715
    66
    val commands: Linear_Set[Command])
wenzelm@38151
    67
  {
wenzelm@43697
    68
    /* commands */
wenzelm@43697
    69
wenzelm@38879
    70
    private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
wenzelm@38879
    71
    {
wenzelm@38879
    72
      val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
wenzelm@38879
    73
      var next_block = 0
wenzelm@38879
    74
      var last_stop = 0
wenzelm@38879
    75
      for ((command, start) <- Node.command_starts(commands.iterator)) {
wenzelm@38879
    76
        last_stop = start + command.length
wenzelm@38885
    77
        while (last_stop + 1 > next_block) {
wenzelm@38879
    78
          blocks += (command -> start)
wenzelm@38879
    79
          next_block += block_size
wenzelm@38879
    80
        }
wenzelm@38879
    81
      }
wenzelm@38879
    82
      (blocks.toArray, Text.Range(0, last_stop))
wenzelm@38879
    83
    }
wenzelm@38151
    84
wenzelm@38879
    85
    def full_range: Text.Range = full_index._2
wenzelm@38151
    86
wenzelm@38569
    87
    def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
wenzelm@38879
    88
    {
wenzelm@38879
    89
      if (!commands.isEmpty && full_range.contains(i)) {
wenzelm@38879
    90
        val (cmd0, start0) = full_index._1(i / block_size)
wenzelm@38879
    91
        Node.command_starts(commands.iterator(cmd0), start0) dropWhile {
wenzelm@38879
    92
          case (cmd, start) => start + cmd.length <= i }
wenzelm@38879
    93
      }
wenzelm@38879
    94
      else Iterator.empty
wenzelm@38879
    95
    }
wenzelm@38151
    96
wenzelm@38569
    97
    def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] =
wenzelm@38569
    98
      command_range(range.start) takeWhile { case (_, start) => start < range.stop }
wenzelm@38151
    99
wenzelm@38426
   100
    def command_at(i: Text.Offset): Option[(Command, Text.Offset)] =
wenzelm@38151
   101
    {
wenzelm@38151
   102
      val range = command_range(i)
wenzelm@38151
   103
      if (range.hasNext) Some(range.next) else None
wenzelm@38151
   104
    }
wenzelm@38151
   105
wenzelm@38426
   106
    def proper_command_at(i: Text.Offset): Option[Command] =
wenzelm@38151
   107
      command_at(i) match {
wenzelm@38151
   108
        case Some((command, _)) =>
wenzelm@38151
   109
          commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
wenzelm@38151
   110
        case None => None
wenzelm@38151
   111
      }
wenzelm@38879
   112
wenzelm@38879
   113
    def command_start(cmd: Command): Option[Text.Offset] =
wenzelm@38879
   114
      command_starts.find(_._1 == cmd).map(_._2)
wenzelm@38879
   115
wenzelm@38879
   116
    def command_starts: Iterator[(Command, Text.Offset)] =
wenzelm@38879
   117
      Node.command_starts(commands.iterator)
wenzelm@38151
   118
  }
wenzelm@38151
   119
wenzelm@38151
   120
wenzelm@38424
   121
wenzelm@38424
   122
  /** versioning **/
wenzelm@38424
   123
wenzelm@38424
   124
  /* particular document versions */
wenzelm@34485
   125
wenzelm@38417
   126
  object Version
wenzelm@38417
   127
  {
wenzelm@43722
   128
    val init: Version = Version(no_id, Map().withDefaultValue(Node.empty))
wenzelm@38417
   129
  }
wenzelm@38417
   130
wenzelm@43722
   131
  sealed case class Version(val id: Version_ID, val nodes: Map[String, Node])
immler@34660
   132
wenzelm@34859
   133
wenzelm@38424
   134
  /* changes of plain text, eventually resulting in document edits */
wenzelm@38227
   135
wenzelm@38227
   136
  object Change
wenzelm@38227
   137
  {
wenzelm@43722
   138
    val init = Change(Future.value(Version.init), Nil, Future.value(Version.init))
wenzelm@38227
   139
  }
wenzelm@38227
   140
wenzelm@43722
   141
  sealed case class Change(
wenzelm@38417
   142
    val previous: Future[Version],
wenzelm@40479
   143
    val edits: List[Edit_Text],
wenzelm@43722
   144
    val version: Future[Version])
wenzelm@38227
   145
  {
wenzelm@39698
   146
    def is_finished: Boolean = previous.is_finished && version.is_finished
wenzelm@38227
   147
  }
wenzelm@38227
   148
wenzelm@38227
   149
wenzelm@38841
   150
  /* history navigation */
wenzelm@38841
   151
wenzelm@38841
   152
  object History
wenzelm@38841
   153
  {
wenzelm@38841
   154
    val init = new History(List(Change.init))
wenzelm@38841
   155
  }
wenzelm@38841
   156
wenzelm@38841
   157
  // FIXME pruning, purging of state
wenzelm@38841
   158
  class History(val undo_list: List[Change])
wenzelm@38841
   159
  {
wenzelm@38841
   160
    require(!undo_list.isEmpty)
wenzelm@38841
   161
wenzelm@38841
   162
    def tip: Change = undo_list.head
wenzelm@38841
   163
    def +(change: Change): History = new History(change :: undo_list)
wenzelm@38841
   164
  }
wenzelm@38841
   165
wenzelm@38841
   166
wenzelm@38841
   167
wenzelm@38841
   168
  /** global state -- document structure, execution process, editing history **/
wenzelm@38424
   169
wenzelm@38424
   170
  abstract class Snapshot
wenzelm@38424
   171
  {
wenzelm@38424
   172
    val version: Version
wenzelm@38424
   173
    val node: Node
wenzelm@38424
   174
    val is_outdated: Boolean
wenzelm@38424
   175
    def lookup_command(id: Command_ID): Option[Command]
wenzelm@38424
   176
    def state(command: Command): Command.State
wenzelm@38427
   177
    def convert(i: Text.Offset): Text.Offset
wenzelm@39177
   178
    def convert(range: Text.Range): Text.Range
wenzelm@38427
   179
    def revert(i: Text.Offset): Text.Offset
wenzelm@39177
   180
    def revert(range: Text.Range): Text.Range
wenzelm@39178
   181
    def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A])
wenzelm@39177
   182
      : Stream[Text.Info[Option[A]]]
wenzelm@38424
   183
  }
wenzelm@38424
   184
wenzelm@38370
   185
  object State
wenzelm@38370
   186
  {
wenzelm@38373
   187
    class Fail(state: State) extends Exception
wenzelm@38373
   188
wenzelm@38882
   189
    val init = State().define_version(Version.init, Map()).assign(Version.init.id, Nil)._2
wenzelm@38370
   190
wenzelm@38976
   191
    case class Assignment(
wenzelm@38976
   192
      val assignment: Map[Command, Exec_ID],
wenzelm@38976
   193
      val is_finished: Boolean = false)
wenzelm@38370
   194
    {
wenzelm@38976
   195
      def get_finished: Map[Command, Exec_ID] = { require(is_finished); assignment }
wenzelm@38976
   196
      def assign(command_execs: List[(Command, Exec_ID)]): Assignment =
wenzelm@38370
   197
      {
wenzelm@38976
   198
        require(!is_finished)
wenzelm@38976
   199
        Assignment(assignment ++ command_execs, true)
wenzelm@38150
   200
      }
wenzelm@38370
   201
    }
wenzelm@38370
   202
  }
wenzelm@38370
   203
wenzelm@43715
   204
  sealed case class State(
wenzelm@38417
   205
    val versions: Map[Version_ID, Version] = Map(),
wenzelm@38370
   206
    val commands: Map[Command_ID, Command.State] = Map(),
wenzelm@38417
   207
    val execs: Map[Exec_ID, (Command.State, Set[Version])] = Map(),
wenzelm@43722
   208
    val assignments: Map[Version_ID, State.Assignment] = Map(),
wenzelm@38841
   209
    val disposed: Set[ID] = Set(),  // FIXME unused!?
wenzelm@38841
   210
    val history: History = History.init)
wenzelm@38370
   211
  {
wenzelm@38373
   212
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   213
wenzelm@38417
   214
    def define_version(version: Version, former_assignment: Map[Command, Exec_ID]): State =
wenzelm@38370
   215
    {
wenzelm@38417
   216
      val id = version.id
wenzelm@38417
   217
      if (versions.isDefinedAt(id) || disposed(id)) fail
wenzelm@38417
   218
      copy(versions = versions + (id -> version),
wenzelm@43722
   219
        assignments = assignments + (id -> State.Assignment(former_assignment)))
wenzelm@38370
   220
    }
wenzelm@38370
   221
wenzelm@38373
   222
    def define_command(command: Command): State =
wenzelm@38373
   223
    {
wenzelm@38373
   224
      val id = command.id
wenzelm@38373
   225
      if (commands.isDefinedAt(id) || disposed(id)) fail
wenzelm@38373
   226
      copy(commands = commands + (id -> command.empty_state))
wenzelm@38373
   227
    }
wenzelm@38373
   228
wenzelm@38370
   229
    def lookup_command(id: Command_ID): Option[Command] = commands.get(id).map(_.command)
wenzelm@38373
   230
wenzelm@38417
   231
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
wenzelm@38370
   232
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)
wenzelm@38370
   233
    def the_exec_state(id: Exec_ID): Command.State = execs.getOrElse(id, fail)._1
wenzelm@43722
   234
    def the_assignment(version: Version): State.Assignment =
wenzelm@43722
   235
      assignments.getOrElse(version.id, fail)
wenzelm@38370
   236
wenzelm@38872
   237
    def accumulate(id: ID, message: XML.Elem): (Command.State, State) =
wenzelm@38370
   238
      execs.get(id) match {
wenzelm@38417
   239
        case Some((st, occs)) =>
wenzelm@38370
   240
          val new_st = st.accumulate(message)
wenzelm@38417
   241
          (new_st, copy(execs = execs + (id -> (new_st, occs))))
wenzelm@38370
   242
        case None =>
wenzelm@38370
   243
          commands.get(id) match {
wenzelm@38370
   244
            case Some(st) =>
wenzelm@38370
   245
              val new_st = st.accumulate(message)
wenzelm@38370
   246
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   247
            case None => fail
wenzelm@38370
   248
          }
wenzelm@38370
   249
      }
wenzelm@38370
   250
wenzelm@38882
   251
    def assign(id: Version_ID, edits: List[(Command_ID, Exec_ID)]): (List[Command], State) =
wenzelm@38370
   252
    {
wenzelm@38417
   253
      val version = the_version(id)
wenzelm@38417
   254
      val occs = Set(version)  // FIXME unused (!?)
wenzelm@38370
   255
wenzelm@38370
   256
      var new_execs = execs
wenzelm@38370
   257
      val assigned_execs =
wenzelm@38370
   258
        for ((cmd_id, exec_id) <- edits) yield {
wenzelm@38370
   259
          val st = the_command(cmd_id)
wenzelm@38370
   260
          if (new_execs.isDefinedAt(exec_id) || disposed(exec_id)) fail
wenzelm@38417
   261
          new_execs += (exec_id -> (st, occs))
wenzelm@38370
   262
          (st.command, exec_id)
wenzelm@38370
   263
        }
wenzelm@38976
   264
      val new_assignment = the_assignment(version).assign(assigned_execs)
wenzelm@43722
   265
      val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
wenzelm@38976
   266
      (assigned_execs.map(_._1), new_state)
wenzelm@38370
   267
    }
wenzelm@38370
   268
wenzelm@38417
   269
    def is_assigned(version: Version): Boolean =
wenzelm@43722
   270
      assignments.get(version.id) match {
wenzelm@38370
   271
        case Some(assgn) => assgn.is_finished
wenzelm@38370
   272
        case None => false
wenzelm@38370
   273
      }
wenzelm@38370
   274
wenzelm@38841
   275
    def extend_history(previous: Future[Version],
wenzelm@40479
   276
        edits: List[Edit_Text],
wenzelm@43722
   277
        version: Future[Version]): (Change, State) =
wenzelm@38841
   278
    {
wenzelm@43722
   279
      val change = Change(previous, edits, version)
wenzelm@38841
   280
      (change, copy(history = history + change))
wenzelm@38841
   281
    }
wenzelm@38841
   282
wenzelm@38841
   283
wenzelm@38841
   284
    // persistent user-view
wenzelm@38841
   285
    def snapshot(name: String, pending_edits: List[Text.Edit]): Snapshot =
wenzelm@38370
   286
    {
wenzelm@38841
   287
      val found_stable = history.undo_list.find(change =>
wenzelm@39698
   288
        change.is_finished && is_assigned(change.version.get_finished))
wenzelm@38841
   289
      require(found_stable.isDefined)
wenzelm@38841
   290
      val stable = found_stable.get
wenzelm@38841
   291
      val latest = history.undo_list.head
wenzelm@38841
   292
wenzelm@40478
   293
      // FIXME proper treatment of deleted nodes
wenzelm@38841
   294
      val edits =
wenzelm@38841
   295
        (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
wenzelm@40478
   296
            (for ((a, Some(eds)) <- change.edits if a == name) yield eds).flatten ::: edits)
wenzelm@38841
   297
      lazy val reverse_edits = edits.reverse
wenzelm@38841
   298
wenzelm@38841
   299
      new Snapshot
wenzelm@38841
   300
      {
wenzelm@39698
   301
        val version = stable.version.get_finished
wenzelm@38841
   302
        val node = version.nodes(name)
wenzelm@38841
   303
        val is_outdated = !(pending_edits.isEmpty && latest == stable)
wenzelm@38841
   304
wenzelm@38841
   305
        def lookup_command(id: Command_ID): Option[Command] = State.this.lookup_command(id)
wenzelm@38841
   306
wenzelm@38841
   307
        def state(command: Command): Command.State =
wenzelm@38848
   308
          try { the_exec_state(the_assignment(version).get_finished.getOrElse(command, fail)) }
wenzelm@38841
   309
          catch { case _: State.Fail => command.empty_state }
wenzelm@38841
   310
wenzelm@38841
   311
        def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38841
   312
        def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@43425
   313
        def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
wenzelm@43425
   314
        def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
wenzelm@39177
   315
wenzelm@39178
   316
        def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A])
wenzelm@39177
   317
          : Stream[Text.Info[Option[A]]] =
wenzelm@38845
   318
        {
wenzelm@38845
   319
          val former_range = revert(range)
wenzelm@38845
   320
          for {
wenzelm@39177
   321
            (command, command_start) <- node.command_range(former_range).toStream
wenzelm@38845
   322
            Text.Info(r0, x) <- state(command).markup.
wenzelm@38845
   323
              select((former_range - command_start).restrict(command.range)) {
wenzelm@38845
   324
                case Text.Info(r0, info)
wenzelm@38845
   325
                if result.isDefinedAt(Text.Info(convert(r0 + command_start), info)) =>
wenzelm@38845
   326
                  result(Text.Info(convert(r0 + command_start), info))
wenzelm@39177
   327
              }
wenzelm@43427
   328
          } yield Text.Info(convert(r0 + command_start), x)
wenzelm@38845
   329
        }
wenzelm@38841
   330
      }
wenzelm@38150
   331
    }
wenzelm@34485
   332
  }
wenzelm@34840
   333
}
wenzelm@34840
   334