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