src/Pure/PIDE/document.scala
author wenzelm
Wed Aug 11 22:41:26 2010 +0200 (2010-08-11 ago)
changeset 38355 8cb265fb12fe
parent 38227 6bbb42843b6e
child 38356 443fb83a21e8
permissions -rw-r--r--
represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
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@38150
     5
list of commands.
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
import scala.annotation.tailrec
wenzelm@37073
    13
wenzelm@37073
    14
wenzelm@34823
    15
object Document
wenzelm@34483
    16
{
wenzelm@38227
    17
  /* formal identifiers */
wenzelm@38150
    18
wenzelm@38355
    19
  type Version_ID = Long
wenzelm@38355
    20
  type Command_ID = Long
wenzelm@38355
    21
  type State_ID = Long
wenzelm@38150
    22
wenzelm@38355
    23
  val NO_ID = 0L
wenzelm@38355
    24
wenzelm@38355
    25
  def parse_id(s: String): Long = java.lang.Long.parseLong(s)
wenzelm@38355
    26
  def print_id(id: Long): String = id.toString
wenzelm@38150
    27
wenzelm@38150
    28
wenzelm@34818
    29
wenzelm@38227
    30
  /** named document nodes **/
wenzelm@38151
    31
wenzelm@38151
    32
  object Node
wenzelm@38151
    33
  {
wenzelm@38151
    34
    val empty: Node = new Node(Linear_Set())
wenzelm@38227
    35
wenzelm@38227
    36
    def command_starts(commands: Iterator[Command], offset: Int = 0): Iterator[(Command, Int)] =
wenzelm@38227
    37
    {
wenzelm@38227
    38
      var i = offset
wenzelm@38227
    39
      for (command <- commands) yield {
wenzelm@38227
    40
        val start = i
wenzelm@38227
    41
        i += command.length
wenzelm@38227
    42
        (command, start)
wenzelm@38227
    43
      }
wenzelm@38227
    44
    }
wenzelm@38151
    45
  }
wenzelm@38151
    46
wenzelm@38151
    47
  class Node(val commands: Linear_Set[Command])
wenzelm@38151
    48
  {
wenzelm@38151
    49
    /* command ranges */
wenzelm@38151
    50
wenzelm@38151
    51
    def command_starts: Iterator[(Command, Int)] =
wenzelm@38227
    52
      Node.command_starts(commands.iterator)
wenzelm@38151
    53
wenzelm@38151
    54
    def command_start(cmd: Command): Option[Int] =
wenzelm@38151
    55
      command_starts.find(_._1 == cmd).map(_._2)
wenzelm@38151
    56
wenzelm@38151
    57
    def command_range(i: Int): Iterator[(Command, Int)] =
wenzelm@38151
    58
      command_starts dropWhile { case (cmd, start) => start + cmd.length <= i }
wenzelm@38151
    59
wenzelm@38151
    60
    def command_range(i: Int, j: Int): Iterator[(Command, Int)] =
wenzelm@38151
    61
      command_range(i) takeWhile { case (_, start) => start < j }
wenzelm@38151
    62
wenzelm@38151
    63
    def command_at(i: Int): Option[(Command, Int)] =
wenzelm@38151
    64
    {
wenzelm@38151
    65
      val range = command_range(i)
wenzelm@38151
    66
      if (range.hasNext) Some(range.next) else None
wenzelm@38151
    67
    }
wenzelm@38151
    68
wenzelm@38151
    69
    def proper_command_at(i: Int): Option[Command] =
wenzelm@38151
    70
      command_at(i) match {
wenzelm@38151
    71
        case Some((command, _)) =>
wenzelm@38151
    72
          commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
wenzelm@38151
    73
        case None => None
wenzelm@38151
    74
      }
wenzelm@38151
    75
  }
wenzelm@38151
    76
wenzelm@38151
    77
wenzelm@38150
    78
  /* initial document */
wenzelm@34485
    79
wenzelm@38150
    80
  val init: Document =
wenzelm@34835
    81
  {
wenzelm@38151
    82
    val doc = new Document(NO_ID, Map().withDefaultValue(Node.empty), Map())
wenzelm@34835
    83
    doc.assign_states(Nil)
wenzelm@34835
    84
    doc
wenzelm@34835
    85
  }
immler@34660
    86
wenzelm@34859
    87
wenzelm@34859
    88
wenzelm@38227
    89
  /** changes of plain text, eventually resulting in document edits **/
wenzelm@38151
    90
wenzelm@38151
    91
  type Node_Text_Edit = (String, List[Text_Edit])  // FIXME None: remove
wenzelm@38151
    92
wenzelm@38151
    93
  type Edit[C] =
wenzelm@38151
    94
   (String,  // node name
wenzelm@38151
    95
    Option[List[(Option[C], Option[C])]])  // None: remove, Some: insert/remove commands
wenzelm@34859
    96
wenzelm@38227
    97
  abstract class Snapshot
wenzelm@38227
    98
  {
wenzelm@38227
    99
    val document: Document
wenzelm@38227
   100
    val node: Document.Node
wenzelm@38227
   101
    val is_outdated: Boolean
wenzelm@38227
   102
    def convert(offset: Int): Int
wenzelm@38227
   103
    def revert(offset: Int): Int
wenzelm@38227
   104
  }
wenzelm@38227
   105
wenzelm@38227
   106
  object Change
wenzelm@38227
   107
  {
wenzelm@38227
   108
    val init = new Change(NO_ID, None, Nil, Future.value(Nil, Document.init))
wenzelm@38227
   109
  }
wenzelm@38227
   110
wenzelm@38227
   111
  class Change(
wenzelm@38227
   112
    val id: Version_ID,
wenzelm@38227
   113
    val parent: Option[Change],
wenzelm@38227
   114
    val edits: List[Node_Text_Edit],
wenzelm@38227
   115
    val result: Future[(List[Edit[Command]], Document)])
wenzelm@38227
   116
  {
wenzelm@38227
   117
    def ancestors: Iterator[Change] = new Iterator[Change]
wenzelm@38227
   118
    {
wenzelm@38227
   119
      private var state: Option[Change] = Some(Change.this)
wenzelm@38227
   120
      def hasNext = state.isDefined
wenzelm@38227
   121
      def next =
wenzelm@38227
   122
        state match {
wenzelm@38227
   123
          case Some(change) => state = change.parent; change
wenzelm@38227
   124
          case None => throw new NoSuchElementException("next on empty iterator")
wenzelm@38227
   125
        }
wenzelm@38227
   126
    }
wenzelm@38227
   127
wenzelm@38227
   128
    def join_document: Document = result.join._2
wenzelm@38227
   129
    def is_assigned: Boolean = result.is_finished && join_document.assignment.is_finished
wenzelm@38227
   130
wenzelm@38227
   131
    def snapshot(name: String, pending_edits: List[Text_Edit]): Snapshot =
wenzelm@38227
   132
    {
wenzelm@38227
   133
      val latest = Change.this
wenzelm@38227
   134
      val stable = latest.ancestors.find(_.is_assigned)
wenzelm@38227
   135
      require(stable.isDefined)
wenzelm@38227
   136
wenzelm@38227
   137
      val edits =
wenzelm@38227
   138
        (pending_edits /: latest.ancestors.takeWhile(_ != stable.get))((edits, change) =>
wenzelm@38227
   139
            (for ((a, eds) <- change.edits if a == name) yield eds).flatten ::: edits)
wenzelm@38227
   140
      lazy val reverse_edits = edits.reverse
wenzelm@38227
   141
wenzelm@38227
   142
      new Snapshot {
wenzelm@38227
   143
        val document = stable.get.join_document
wenzelm@38227
   144
        val node = document.nodes(name)
wenzelm@38227
   145
        val is_outdated = !(pending_edits.isEmpty && latest == stable.get)
wenzelm@38227
   146
        def convert(offset: Int): Int = (offset /: edits)((i, edit) => edit.convert(i))
wenzelm@38227
   147
        def revert(offset: Int): Int = (offset /: reverse_edits)((i, edit) => edit.revert(i))
wenzelm@38227
   148
      }
wenzelm@38227
   149
    }
wenzelm@38227
   150
  }
wenzelm@38227
   151
wenzelm@38227
   152
wenzelm@38227
   153
wenzelm@38227
   154
  /** editing **/
wenzelm@38227
   155
wenzelm@38150
   156
  def text_edits(session: Session, old_doc: Document, new_id: Version_ID,
wenzelm@38151
   157
      edits: List[Node_Text_Edit]): (List[Edit[Command]], Document) =
wenzelm@34824
   158
  {
wenzelm@34840
   159
    require(old_doc.assignment.is_finished)
wenzelm@34840
   160
wenzelm@34859
   161
wenzelm@34859
   162
    /* unparsed dummy commands */
immler@34538
   163
wenzelm@34859
   164
    def unparsed(source: String) =
wenzelm@38220
   165
      new Command(NO_ID, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@34859
   166
wenzelm@38220
   167
    def is_unparsed(command: Command) = command.id == NO_ID
wenzelm@34835
   168
wenzelm@34859
   169
wenzelm@34859
   170
    /* phase 1: edit individual command source */
wenzelm@34859
   171
wenzelm@38150
   172
    @tailrec def edit_text(eds: List[Text_Edit],
wenzelm@38150
   173
        commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@34859
   174
    {
wenzelm@34859
   175
      eds match {
wenzelm@34859
   176
        case e :: es =>
wenzelm@38227
   177
          Node.command_starts(commands.iterator).find {
wenzelm@34866
   178
            case (cmd, cmd_start) =>
wenzelm@37685
   179
              e.can_edit(cmd.source, cmd_start) ||
wenzelm@37685
   180
                e.is_insert && e.start == cmd_start + cmd.length
wenzelm@34863
   181
          } match {
wenzelm@34866
   182
            case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@34866
   183
              val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@34866
   184
              val new_commands = commands.insert_after(Some(cmd), unparsed(text)) - cmd
wenzelm@34863
   185
              edit_text(rest.toList ::: es, new_commands)
wenzelm@34318
   186
wenzelm@34866
   187
            case Some((cmd, cmd_start)) =>
wenzelm@34866
   188
              edit_text(es, commands.insert_after(Some(cmd), unparsed(e.text)))
wenzelm@34866
   189
wenzelm@34859
   190
            case None =>
wenzelm@34866
   191
              require(e.is_insert && e.start == 0)
wenzelm@34863
   192
              edit_text(es, commands.insert_after(None, unparsed(e.text)))
wenzelm@34859
   193
          }
wenzelm@34863
   194
        case Nil => commands
wenzelm@34318
   195
      }
wenzelm@34318
   196
    }
wenzelm@34582
   197
wenzelm@34818
   198
wenzelm@34866
   199
    /* phase 2: recover command spans */
wenzelm@34859
   200
wenzelm@37073
   201
    @tailrec def parse_spans(commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@34859
   202
    {
wenzelm@36011
   203
      commands.iterator.find(is_unparsed) match {
wenzelm@34859
   204
        case Some(first_unparsed) =>
wenzelm@37071
   205
          val first =
wenzelm@37072
   206
            commands.reverse_iterator(first_unparsed).find(_.is_command) getOrElse commands.head
wenzelm@37071
   207
          val last =
wenzelm@37071
   208
            commands.iterator(first_unparsed).find(_.is_command) getOrElse commands.last
wenzelm@37071
   209
          val range =
wenzelm@37071
   210
            commands.iterator(first).takeWhile(_ != last).toList ::: List(last)
wenzelm@34859
   211
wenzelm@37071
   212
          val sources = range.flatMap(_.span.map(_.source))
wenzelm@34866
   213
          val spans0 = Thy_Syntax.parse_spans(session.current_syntax.scan(sources.mkString))
wenzelm@34485
   214
wenzelm@34859
   215
          val (before_edit, spans1) =
wenzelm@37071
   216
            if (!spans0.isEmpty && first.is_command && first.span == spans0.head)
wenzelm@37071
   217
              (Some(first), spans0.tail)
wenzelm@37071
   218
            else (commands.prev(first), spans0)
immler@34544
   219
wenzelm@34859
   220
          val (after_edit, spans2) =
wenzelm@37071
   221
            if (!spans1.isEmpty && last.is_command && last.span == spans1.last)
wenzelm@37071
   222
              (Some(last), spans1.take(spans1.length - 1))
wenzelm@37071
   223
            else (commands.next(last), spans1)
wenzelm@34859
   224
wenzelm@34863
   225
          val inserted = spans2.map(span => new Command(session.create_id(), span))
wenzelm@34863
   226
          val new_commands =
wenzelm@34863
   227
            commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted)
wenzelm@34866
   228
          parse_spans(new_commands)
wenzelm@34485
   229
wenzelm@34863
   230
        case None => commands
wenzelm@34485
   231
      }
wenzelm@34485
   232
    }
wenzelm@34739
   233
immler@34554
   234
wenzelm@34859
   235
    /* phase 3: resulting document edits */
wenzelm@34859
   236
wenzelm@38150
   237
    {
wenzelm@38150
   238
      val doc_edits = new mutable.ListBuffer[Edit[Command]]
wenzelm@38150
   239
      var nodes = old_doc.nodes
wenzelm@38150
   240
      var former_states = old_doc.assignment.join
immler@34660
   241
wenzelm@38150
   242
      for ((name, text_edits) <- edits) {
wenzelm@38151
   243
        val commands0 = nodes(name).commands
wenzelm@38150
   244
        val commands1 = edit_text(text_edits, commands0)
wenzelm@38220
   245
        val commands2 = parse_spans(commands1)   // FIXME somewhat slow
wenzelm@34863
   246
wenzelm@38150
   247
        val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList
wenzelm@38150
   248
        val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList
wenzelm@38150
   249
wenzelm@38150
   250
        val cmd_edits =
wenzelm@38150
   251
          removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) :::
wenzelm@38150
   252
          inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd)))
immler@34660
   253
wenzelm@38150
   254
        doc_edits += (name -> Some(cmd_edits))
wenzelm@38151
   255
        nodes += (name -> new Node(commands2))
wenzelm@38150
   256
        former_states --= removed_commands
wenzelm@38150
   257
      }
wenzelm@38150
   258
      (doc_edits.toList, new Document(new_id, nodes, former_states))
wenzelm@38150
   259
    }
wenzelm@34485
   260
  }
wenzelm@34840
   261
}
wenzelm@34840
   262
wenzelm@34855
   263
wenzelm@34840
   264
class Document(
wenzelm@38150
   265
    val id: Document.Version_ID,
wenzelm@38151
   266
    val nodes: Map[String, Document.Node],
wenzelm@38150
   267
    former_states: Map[Command, Command])  // FIXME !?
wenzelm@34840
   268
{
wenzelm@34855
   269
  /* command state assignment */
wenzelm@34840
   270
wenzelm@34840
   271
  val assignment = Future.promise[Map[Command, Command]]
wenzelm@34853
   272
  def await_assignment { assignment.join }
wenzelm@34840
   273
wenzelm@34859
   274
  @volatile private var tmp_states = former_states
wenzelm@34840
   275
wenzelm@34840
   276
  def assign_states(new_states: List[(Command, Command)])
wenzelm@34840
   277
  {
wenzelm@34840
   278
    assignment.fulfill(tmp_states ++ new_states)
wenzelm@34840
   279
    tmp_states = Map()
wenzelm@34840
   280
  }
wenzelm@34840
   281
wenzelm@36990
   282
  def current_state(cmd: Command): State =
wenzelm@34840
   283
  {
wenzelm@34840
   284
    require(assignment.is_finished)
wenzelm@36990
   285
    (assignment.join).get(cmd) match {
wenzelm@36990
   286
      case Some(cmd_state) => cmd_state.current_state
wenzelm@37186
   287
      case None => new State(cmd, Command.Status.UNDEFINED, 0, Nil, cmd.empty_markup)
wenzelm@36990
   288
    }
wenzelm@34840
   289
  }
wenzelm@34318
   290
}