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