src/Pure/PIDE/document.scala
author wenzelm
Tue Sep 07 23:06:52 2010 +0200 (2010-09-07)
changeset 39178 83e9f3ccea9f
parent 39177 0468964aec11
child 39621 20bba9cc4b51
permissions -rw-r--r--
concentrate Isabelle specific physical rendering markup selection in isabelle_markup.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@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@38880
    59
  private val block_size = 1024
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@38885
    70
        while (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@39177
   172
    def convert(range: Text.Range): Text.Range
wenzelm@38427
   173
    def revert(i: Text.Offset): Text.Offset
wenzelm@39177
   174
    def revert(range: Text.Range): Text.Range
wenzelm@39178
   175
    def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A])
wenzelm@39177
   176
      : Stream[Text.Info[Option[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@38882
   183
    val init = State().define_version(Version.init, Map()).assign(Version.init.id, Nil)._2
wenzelm@38370
   184
wenzelm@38976
   185
    case class Assignment(
wenzelm@38976
   186
      val assignment: Map[Command, Exec_ID],
wenzelm@38976
   187
      val is_finished: Boolean = false)
wenzelm@38370
   188
    {
wenzelm@38976
   189
      def get_finished: Map[Command, Exec_ID] = { require(is_finished); assignment }
wenzelm@38976
   190
      def assign(command_execs: List[(Command, Exec_ID)]): Assignment =
wenzelm@38370
   191
      {
wenzelm@38976
   192
        require(!is_finished)
wenzelm@38976
   193
        Assignment(assignment ++ command_execs, true)
wenzelm@38150
   194
      }
wenzelm@38370
   195
    }
wenzelm@38370
   196
  }
wenzelm@38370
   197
wenzelm@38370
   198
  case class State(
wenzelm@38417
   199
    val versions: Map[Version_ID, Version] = Map(),
wenzelm@38370
   200
    val commands: Map[Command_ID, Command.State] = Map(),
wenzelm@38417
   201
    val execs: Map[Exec_ID, (Command.State, Set[Version])] = Map(),
wenzelm@38417
   202
    val assignments: Map[Version, State.Assignment] = Map(),
wenzelm@38841
   203
    val disposed: Set[ID] = Set(),  // FIXME unused!?
wenzelm@38841
   204
    val history: History = History.init)
wenzelm@38370
   205
  {
wenzelm@38373
   206
    private def fail[A]: A = throw new State.Fail(this)
wenzelm@38370
   207
wenzelm@38417
   208
    def define_version(version: Version, former_assignment: Map[Command, Exec_ID]): State =
wenzelm@38370
   209
    {
wenzelm@38417
   210
      val id = version.id
wenzelm@38417
   211
      if (versions.isDefinedAt(id) || disposed(id)) fail
wenzelm@38417
   212
      copy(versions = versions + (id -> version),
wenzelm@38976
   213
        assignments = assignments + (version -> State.Assignment(former_assignment)))
wenzelm@38370
   214
    }
wenzelm@38370
   215
wenzelm@38373
   216
    def define_command(command: Command): State =
wenzelm@38373
   217
    {
wenzelm@38373
   218
      val id = command.id
wenzelm@38373
   219
      if (commands.isDefinedAt(id) || disposed(id)) fail
wenzelm@38373
   220
      copy(commands = commands + (id -> command.empty_state))
wenzelm@38373
   221
    }
wenzelm@38373
   222
wenzelm@38370
   223
    def lookup_command(id: Command_ID): Option[Command] = commands.get(id).map(_.command)
wenzelm@38373
   224
wenzelm@38417
   225
    def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
wenzelm@38370
   226
    def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)
wenzelm@38370
   227
    def the_exec_state(id: Exec_ID): Command.State = execs.getOrElse(id, fail)._1
wenzelm@38417
   228
    def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version, fail)
wenzelm@38370
   229
wenzelm@38872
   230
    def accumulate(id: ID, message: XML.Elem): (Command.State, State) =
wenzelm@38370
   231
      execs.get(id) match {
wenzelm@38417
   232
        case Some((st, occs)) =>
wenzelm@38370
   233
          val new_st = st.accumulate(message)
wenzelm@38417
   234
          (new_st, copy(execs = execs + (id -> (new_st, occs))))
wenzelm@38370
   235
        case None =>
wenzelm@38370
   236
          commands.get(id) match {
wenzelm@38370
   237
            case Some(st) =>
wenzelm@38370
   238
              val new_st = st.accumulate(message)
wenzelm@38370
   239
              (new_st, copy(commands = commands + (id -> new_st)))
wenzelm@38370
   240
            case None => fail
wenzelm@38370
   241
          }
wenzelm@38370
   242
      }
wenzelm@38370
   243
wenzelm@38882
   244
    def assign(id: Version_ID, edits: List[(Command_ID, Exec_ID)]): (List[Command], State) =
wenzelm@38370
   245
    {
wenzelm@38417
   246
      val version = the_version(id)
wenzelm@38417
   247
      val occs = Set(version)  // FIXME unused (!?)
wenzelm@38370
   248
wenzelm@38370
   249
      var new_execs = execs
wenzelm@38370
   250
      val assigned_execs =
wenzelm@38370
   251
        for ((cmd_id, exec_id) <- edits) yield {
wenzelm@38370
   252
          val st = the_command(cmd_id)
wenzelm@38370
   253
          if (new_execs.isDefinedAt(exec_id) || disposed(exec_id)) fail
wenzelm@38417
   254
          new_execs += (exec_id -> (st, occs))
wenzelm@38370
   255
          (st.command, exec_id)
wenzelm@38370
   256
        }
wenzelm@38976
   257
      val new_assignment = the_assignment(version).assign(assigned_execs)
wenzelm@38976
   258
      val new_state =
wenzelm@38976
   259
        copy(assignments = assignments + (version -> new_assignment), execs = new_execs)
wenzelm@38976
   260
      (assigned_execs.map(_._1), new_state)
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@39177
   307
        def convert(range: Text.Range): Text.Range =
wenzelm@39177
   308
          if (edits.isEmpty) range else range.map(convert(_))
wenzelm@39177
   309
wenzelm@39177
   310
        def revert(range: Text.Range): Text.Range =
wenzelm@39177
   311
          if (edits.isEmpty) range else range.map(revert(_))
wenzelm@39177
   312
wenzelm@39178
   313
        def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A])
wenzelm@39177
   314
          : Stream[Text.Info[Option[A]]] =
wenzelm@38845
   315
        {
wenzelm@38845
   316
          val former_range = revert(range)
wenzelm@38845
   317
          for {
wenzelm@39177
   318
            (command, command_start) <- node.command_range(former_range).toStream
wenzelm@38845
   319
            Text.Info(r0, x) <- state(command).markup.
wenzelm@38845
   320
              select((former_range - command_start).restrict(command.range)) {
wenzelm@38845
   321
                case Text.Info(r0, info)
wenzelm@38845
   322
                if result.isDefinedAt(Text.Info(convert(r0 + command_start), info)) =>
wenzelm@38845
   323
                  result(Text.Info(convert(r0 + command_start), info))
wenzelm@39177
   324
              }
wenzelm@38845
   325
            val r = convert(r0 + command_start)
wenzelm@38845
   326
            if !r.is_singularity
wenzelm@38845
   327
          } yield Text.Info(r, x)
wenzelm@38845
   328
        }
wenzelm@38841
   329
      }
wenzelm@38150
   330
    }
wenzelm@34485
   331
  }
wenzelm@34840
   332
}
wenzelm@34840
   333