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