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