src/Pure/PIDE/document.scala
author wenzelm
Thu Aug 05 16:58:18 2010 +0200 (2010-08-05)
changeset 38151 2837c952ca31
parent 38150 67fc24df3721
child 38220 b30aa2dbedca
permissions -rw-r--r--
explicit Change.Snapshot and Document.Node;
misc tuning and clarification;
Document_View: explicitly highlight outdated command status;
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@38150
    17
  /* unique identifiers */
wenzelm@38150
    18
wenzelm@38150
    19
  type State_ID = String
wenzelm@38150
    20
  type Command_ID = String
wenzelm@38150
    21
  type Version_ID = String
wenzelm@38150
    22
wenzelm@38150
    23
  val NO_ID = ""
wenzelm@38150
    24
wenzelm@38150
    25
wenzelm@34859
    26
  /* command start positions */
wenzelm@34818
    27
wenzelm@37685
    28
  def command_starts(commands: Iterator[Command], offset: Int = 0): Iterator[(Command, Int)] =
wenzelm@34859
    29
  {
wenzelm@37685
    30
    var i = offset
wenzelm@37685
    31
    for (command <- commands) yield {
wenzelm@37685
    32
      val start = i
wenzelm@37685
    33
      i += command.length
wenzelm@37685
    34
      (command, start)
wenzelm@34859
    35
    }
wenzelm@34859
    36
  }
wenzelm@34859
    37
wenzelm@34859
    38
wenzelm@38151
    39
  /* named document nodes */
wenzelm@38151
    40
wenzelm@38151
    41
  object Node
wenzelm@38151
    42
  {
wenzelm@38151
    43
    val empty: Node = new Node(Linear_Set())
wenzelm@38151
    44
  }
wenzelm@38151
    45
wenzelm@38151
    46
  class Node(val commands: Linear_Set[Command])
wenzelm@38151
    47
  {
wenzelm@38151
    48
    /* command ranges */
wenzelm@38151
    49
wenzelm@38151
    50
    def command_starts: Iterator[(Command, Int)] =
wenzelm@38151
    51
      Document.command_starts(commands.iterator)
wenzelm@38151
    52
wenzelm@38151
    53
    def command_start(cmd: Command): Option[Int] =
wenzelm@38151
    54
      command_starts.find(_._1 == cmd).map(_._2)
wenzelm@38151
    55
wenzelm@38151
    56
    def command_range(i: Int): Iterator[(Command, Int)] =
wenzelm@38151
    57
      command_starts dropWhile { case (cmd, start) => start + cmd.length <= i }
wenzelm@38151
    58
wenzelm@38151
    59
    def command_range(i: Int, j: Int): Iterator[(Command, Int)] =
wenzelm@38151
    60
      command_range(i) takeWhile { case (_, start) => start < j }
wenzelm@38151
    61
wenzelm@38151
    62
    def command_at(i: Int): Option[(Command, Int)] =
wenzelm@38151
    63
    {
wenzelm@38151
    64
      val range = command_range(i)
wenzelm@38151
    65
      if (range.hasNext) Some(range.next) else None
wenzelm@38151
    66
    }
wenzelm@38151
    67
wenzelm@38151
    68
    def proper_command_at(i: Int): Option[Command] =
wenzelm@38151
    69
      command_at(i) match {
wenzelm@38151
    70
        case Some((command, _)) =>
wenzelm@38151
    71
          commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
wenzelm@38151
    72
        case None => None
wenzelm@38151
    73
      }
wenzelm@38151
    74
  }
wenzelm@38151
    75
wenzelm@38151
    76
wenzelm@38150
    77
  /* initial document */
wenzelm@34485
    78
wenzelm@38150
    79
  val init: Document =
wenzelm@34835
    80
  {
wenzelm@38151
    81
    val doc = new Document(NO_ID, Map().withDefaultValue(Node.empty), Map())
wenzelm@34835
    82
    doc.assign_states(Nil)
wenzelm@34835
    83
    doc
wenzelm@34835
    84
  }
immler@34660
    85
wenzelm@34859
    86
wenzelm@34859
    87
wenzelm@38151
    88
  /** editing **/
wenzelm@38151
    89
wenzelm@38151
    90
  type Node_Text_Edit = (String, List[Text_Edit])  // FIXME None: remove
wenzelm@38151
    91
wenzelm@38151
    92
  type Edit[C] =
wenzelm@38151
    93
   (String,  // node name
wenzelm@38151
    94
    Option[List[(Option[C], Option[C])]])  // None: remove, Some: insert/remove commands
wenzelm@34859
    95
wenzelm@38150
    96
  def text_edits(session: Session, old_doc: Document, new_id: Version_ID,
wenzelm@38151
    97
      edits: List[Node_Text_Edit]): (List[Edit[Command]], Document) =
wenzelm@34824
    98
  {
wenzelm@34840
    99
    require(old_doc.assignment.is_finished)
wenzelm@34840
   100
wenzelm@34859
   101
wenzelm@34859
   102
    /* unparsed dummy commands */
immler@34538
   103
wenzelm@34859
   104
    def unparsed(source: String) =
wenzelm@36956
   105
      new Command(null, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@34859
   106
wenzelm@34860
   107
    def is_unparsed(command: Command) = command.id == null
wenzelm@34835
   108
wenzelm@34859
   109
wenzelm@34859
   110
    /* phase 1: edit individual command source */
wenzelm@34859
   111
wenzelm@38150
   112
    @tailrec def edit_text(eds: List[Text_Edit],
wenzelm@38150
   113
        commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@34859
   114
    {
wenzelm@34859
   115
      eds match {
wenzelm@34859
   116
        case e :: es =>
wenzelm@37685
   117
          command_starts(commands.iterator).find {
wenzelm@34866
   118
            case (cmd, cmd_start) =>
wenzelm@37685
   119
              e.can_edit(cmd.source, cmd_start) ||
wenzelm@37685
   120
                e.is_insert && e.start == cmd_start + cmd.length
wenzelm@34863
   121
          } match {
wenzelm@34866
   122
            case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@34866
   123
              val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@34866
   124
              val new_commands = commands.insert_after(Some(cmd), unparsed(text)) - cmd
wenzelm@34863
   125
              edit_text(rest.toList ::: es, new_commands)
wenzelm@34318
   126
wenzelm@34866
   127
            case Some((cmd, cmd_start)) =>
wenzelm@34866
   128
              edit_text(es, commands.insert_after(Some(cmd), unparsed(e.text)))
wenzelm@34866
   129
wenzelm@34859
   130
            case None =>
wenzelm@34866
   131
              require(e.is_insert && e.start == 0)
wenzelm@34863
   132
              edit_text(es, commands.insert_after(None, unparsed(e.text)))
wenzelm@34859
   133
          }
wenzelm@34863
   134
        case Nil => commands
wenzelm@34318
   135
      }
wenzelm@34318
   136
    }
wenzelm@34582
   137
wenzelm@34818
   138
wenzelm@34866
   139
    /* phase 2: recover command spans */
wenzelm@34859
   140
wenzelm@37073
   141
    @tailrec def parse_spans(commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@34859
   142
    {
wenzelm@36011
   143
      commands.iterator.find(is_unparsed) match {
wenzelm@34859
   144
        case Some(first_unparsed) =>
wenzelm@37071
   145
          val first =
wenzelm@37072
   146
            commands.reverse_iterator(first_unparsed).find(_.is_command) getOrElse commands.head
wenzelm@37071
   147
          val last =
wenzelm@37071
   148
            commands.iterator(first_unparsed).find(_.is_command) getOrElse commands.last
wenzelm@37071
   149
          val range =
wenzelm@37071
   150
            commands.iterator(first).takeWhile(_ != last).toList ::: List(last)
wenzelm@34859
   151
wenzelm@37071
   152
          val sources = range.flatMap(_.span.map(_.source))
wenzelm@34866
   153
          val spans0 = Thy_Syntax.parse_spans(session.current_syntax.scan(sources.mkString))
wenzelm@34485
   154
wenzelm@34859
   155
          val (before_edit, spans1) =
wenzelm@37071
   156
            if (!spans0.isEmpty && first.is_command && first.span == spans0.head)
wenzelm@37071
   157
              (Some(first), spans0.tail)
wenzelm@37071
   158
            else (commands.prev(first), spans0)
immler@34544
   159
wenzelm@34859
   160
          val (after_edit, spans2) =
wenzelm@37071
   161
            if (!spans1.isEmpty && last.is_command && last.span == spans1.last)
wenzelm@37071
   162
              (Some(last), spans1.take(spans1.length - 1))
wenzelm@37071
   163
            else (commands.next(last), spans1)
wenzelm@34859
   164
wenzelm@34863
   165
          val inserted = spans2.map(span => new Command(session.create_id(), span))
wenzelm@34863
   166
          val new_commands =
wenzelm@34863
   167
            commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted)
wenzelm@34866
   168
          parse_spans(new_commands)
wenzelm@34485
   169
wenzelm@34863
   170
        case None => commands
wenzelm@34485
   171
      }
wenzelm@34485
   172
    }
wenzelm@34739
   173
immler@34554
   174
wenzelm@34859
   175
    /* phase 3: resulting document edits */
wenzelm@34859
   176
wenzelm@38150
   177
    {
wenzelm@38150
   178
      val doc_edits = new mutable.ListBuffer[Edit[Command]]
wenzelm@38150
   179
      var nodes = old_doc.nodes
wenzelm@38150
   180
      var former_states = old_doc.assignment.join
immler@34660
   181
wenzelm@38150
   182
      for ((name, text_edits) <- edits) {
wenzelm@38151
   183
        val commands0 = nodes(name).commands
wenzelm@38150
   184
        val commands1 = edit_text(text_edits, commands0)
wenzelm@38150
   185
        val commands2 = parse_spans(commands1)
wenzelm@34863
   186
wenzelm@38150
   187
        val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList
wenzelm@38150
   188
        val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList
wenzelm@38150
   189
wenzelm@38150
   190
        val cmd_edits =
wenzelm@38150
   191
          removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) :::
wenzelm@38150
   192
          inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd)))
immler@34660
   193
wenzelm@38150
   194
        doc_edits += (name -> Some(cmd_edits))
wenzelm@38151
   195
        nodes += (name -> new Node(commands2))
wenzelm@38150
   196
        former_states --= removed_commands
wenzelm@38150
   197
      }
wenzelm@38150
   198
      (doc_edits.toList, new Document(new_id, nodes, former_states))
wenzelm@38150
   199
    }
wenzelm@34485
   200
  }
wenzelm@34840
   201
}
wenzelm@34840
   202
wenzelm@34855
   203
wenzelm@34840
   204
class Document(
wenzelm@38150
   205
    val id: Document.Version_ID,
wenzelm@38151
   206
    val nodes: Map[String, Document.Node],
wenzelm@38150
   207
    former_states: Map[Command, Command])  // FIXME !?
wenzelm@34840
   208
{
wenzelm@34855
   209
  /* command state assignment */
wenzelm@34840
   210
wenzelm@34840
   211
  val assignment = Future.promise[Map[Command, Command]]
wenzelm@34853
   212
  def await_assignment { assignment.join }
wenzelm@34840
   213
wenzelm@34859
   214
  @volatile private var tmp_states = former_states
wenzelm@34840
   215
wenzelm@34840
   216
  def assign_states(new_states: List[(Command, Command)])
wenzelm@34840
   217
  {
wenzelm@34840
   218
    assignment.fulfill(tmp_states ++ new_states)
wenzelm@34840
   219
    tmp_states = Map()
wenzelm@34840
   220
  }
wenzelm@34840
   221
wenzelm@36990
   222
  def current_state(cmd: Command): State =
wenzelm@34840
   223
  {
wenzelm@34840
   224
    require(assignment.is_finished)
wenzelm@36990
   225
    (assignment.join).get(cmd) match {
wenzelm@36990
   226
      case Some(cmd_state) => cmd_state.current_state
wenzelm@37186
   227
      case None => new State(cmd, Command.Status.UNDEFINED, 0, Nil, cmd.empty_markup)
wenzelm@36990
   228
    }
wenzelm@34840
   229
  }
wenzelm@34318
   230
}