src/Pure/PIDE/document.scala
author wenzelm
Mon May 24 23:19:40 2010 +0200 (2010-05-24 ago)
changeset 37073 5e42e36a6693
parent 37072 9105c8237c7a
child 37186 349e9223c685
permissions -rw-r--r--
@tailrec annotation;
wenzelm@36676
     1
/*  Title:      Pure/PIDE/document.scala
wenzelm@36676
     2
    Author:     Makarius
wenzelm@36676
     3
wenzelm@36676
     4
Document as editable list of commands.
wenzelm@36676
     5
*/
wenzelm@34407
     6
wenzelm@34871
     7
package isabelle
wenzelm@34318
     8
wenzelm@34760
     9
wenzelm@37073
    10
import scala.annotation.tailrec
wenzelm@37073
    11
wenzelm@37073
    12
wenzelm@34823
    13
object Document
wenzelm@34483
    14
{
wenzelm@34859
    15
  /* command start positions */
wenzelm@34818
    16
wenzelm@34859
    17
  def command_starts(commands: Linear_Set[Command]): Iterator[(Command, Int)] =
wenzelm@34859
    18
  {
wenzelm@34859
    19
    var offset = 0
wenzelm@36011
    20
    for (cmd <- commands.iterator) yield {
wenzelm@34859
    21
      val start = offset
wenzelm@34859
    22
      offset += cmd.length
wenzelm@34859
    23
      (cmd, start)
wenzelm@34859
    24
    }
wenzelm@34859
    25
  }
wenzelm@34859
    26
wenzelm@34859
    27
wenzelm@34859
    28
  /* empty document */
wenzelm@34485
    29
wenzelm@34823
    30
  def empty(id: Isar_Document.Document_ID): Document =
wenzelm@34835
    31
  {
wenzelm@34859
    32
    val doc = new Document(id, Linear_Set(), Map())
wenzelm@34835
    33
    doc.assign_states(Nil)
wenzelm@34835
    34
    doc
wenzelm@34835
    35
  }
immler@34660
    36
wenzelm@34859
    37
wenzelm@34859
    38
wenzelm@34859
    39
  /** document edits **/
wenzelm@34859
    40
wenzelm@34853
    41
  type Edit = (Option[Command], Option[Command])
wenzelm@34824
    42
wenzelm@34824
    43
  def text_edits(session: Session, old_doc: Document, new_id: Isar_Document.Document_ID,
wenzelm@34853
    44
    edits: List[Text_Edit]): (List[Edit], Document) =
wenzelm@34824
    45
  {
wenzelm@34840
    46
    require(old_doc.assignment.is_finished)
wenzelm@34840
    47
wenzelm@34859
    48
wenzelm@34859
    49
    /* unparsed dummy commands */
immler@34538
    50
wenzelm@34859
    51
    def unparsed(source: String) =
wenzelm@36956
    52
      new Command(null, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@34859
    53
wenzelm@34860
    54
    def is_unparsed(command: Command) = command.id == null
wenzelm@34835
    55
wenzelm@34859
    56
wenzelm@34859
    57
    /* phase 1: edit individual command source */
wenzelm@34859
    58
wenzelm@34863
    59
    def edit_text(eds: List[Text_Edit], commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@34859
    60
    {
wenzelm@34859
    61
      eds match {
wenzelm@34859
    62
        case e :: es =>
wenzelm@34859
    63
          command_starts(commands).find {   // FIXME relative search!
wenzelm@34866
    64
            case (cmd, cmd_start) =>
wenzelm@34866
    65
              e.can_edit(cmd.source, cmd_start) || e.is_insert && e.start == cmd_start + cmd.length
wenzelm@34863
    66
          } match {
wenzelm@34866
    67
            case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@34866
    68
              val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@34866
    69
              val new_commands = commands.insert_after(Some(cmd), unparsed(text)) - cmd
wenzelm@34863
    70
              edit_text(rest.toList ::: es, new_commands)
wenzelm@34318
    71
wenzelm@34866
    72
            case Some((cmd, cmd_start)) =>
wenzelm@34866
    73
              edit_text(es, commands.insert_after(Some(cmd), unparsed(e.text)))
wenzelm@34866
    74
wenzelm@34859
    75
            case None =>
wenzelm@34866
    76
              require(e.is_insert && e.start == 0)
wenzelm@34863
    77
              edit_text(es, commands.insert_after(None, unparsed(e.text)))
wenzelm@34859
    78
          }
wenzelm@34863
    79
        case Nil => commands
wenzelm@34318
    80
      }
wenzelm@34318
    81
    }
wenzelm@34582
    82
wenzelm@34818
    83
wenzelm@34866
    84
    /* phase 2: recover command spans */
wenzelm@34859
    85
wenzelm@37073
    86
    @tailrec def parse_spans(commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@34859
    87
    {
wenzelm@36011
    88
      commands.iterator.find(is_unparsed) match {
wenzelm@34859
    89
        case Some(first_unparsed) =>
wenzelm@37071
    90
          val first =
wenzelm@37072
    91
            commands.reverse_iterator(first_unparsed).find(_.is_command) getOrElse commands.head
wenzelm@37071
    92
          val last =
wenzelm@37071
    93
            commands.iterator(first_unparsed).find(_.is_command) getOrElse commands.last
wenzelm@37071
    94
          val range =
wenzelm@37071
    95
            commands.iterator(first).takeWhile(_ != last).toList ::: List(last)
wenzelm@34859
    96
wenzelm@37071
    97
          val sources = range.flatMap(_.span.map(_.source))
wenzelm@34866
    98
          val spans0 = Thy_Syntax.parse_spans(session.current_syntax.scan(sources.mkString))
wenzelm@34485
    99
wenzelm@34859
   100
          val (before_edit, spans1) =
wenzelm@37071
   101
            if (!spans0.isEmpty && first.is_command && first.span == spans0.head)
wenzelm@37071
   102
              (Some(first), spans0.tail)
wenzelm@37071
   103
            else (commands.prev(first), spans0)
immler@34544
   104
wenzelm@34859
   105
          val (after_edit, spans2) =
wenzelm@37071
   106
            if (!spans1.isEmpty && last.is_command && last.span == spans1.last)
wenzelm@37071
   107
              (Some(last), spans1.take(spans1.length - 1))
wenzelm@37071
   108
            else (commands.next(last), spans1)
wenzelm@34859
   109
wenzelm@34863
   110
          val inserted = spans2.map(span => new Command(session.create_id(), span))
wenzelm@34863
   111
          val new_commands =
wenzelm@34863
   112
            commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted)
wenzelm@34866
   113
          parse_spans(new_commands)
wenzelm@34485
   114
wenzelm@34863
   115
        case None => commands
wenzelm@34485
   116
      }
wenzelm@34485
   117
    }
wenzelm@34739
   118
immler@34554
   119
wenzelm@34859
   120
    /* phase 3: resulting document edits */
wenzelm@34859
   121
wenzelm@37059
   122
    val commands0 = old_doc.commands
wenzelm@37059
   123
    val commands1 = edit_text(edits, commands0)
wenzelm@37059
   124
    val commands2 = parse_spans(commands1)
immler@34660
   125
wenzelm@37059
   126
    val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList
wenzelm@37059
   127
    val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList
wenzelm@34863
   128
wenzelm@37059
   129
    val doc_edits =
wenzelm@37059
   130
      removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) :::
wenzelm@37059
   131
      inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd)))
immler@34660
   132
wenzelm@37059
   133
    val former_states = old_doc.assignment.join -- removed_commands
wenzelm@34859
   134
wenzelm@37059
   135
    (doc_edits, new Document(new_id, commands2, former_states))
wenzelm@34485
   136
  }
wenzelm@34840
   137
}
wenzelm@34840
   138
wenzelm@34855
   139
wenzelm@34840
   140
class Document(
wenzelm@34840
   141
    val id: Isar_Document.Document_ID,
wenzelm@34840
   142
    val commands: Linear_Set[Command],
wenzelm@34859
   143
    former_states: Map[Command, Command])
wenzelm@34840
   144
{
wenzelm@34859
   145
  /* command ranges */
wenzelm@34855
   146
wenzelm@34859
   147
  def command_starts: Iterator[(Command, Int)] = Document.command_starts(commands)
wenzelm@34855
   148
wenzelm@34855
   149
  def command_start(cmd: Command): Option[Int] =
wenzelm@34855
   150
    command_starts.find(_._1 == cmd).map(_._2)
wenzelm@34855
   151
wenzelm@34855
   152
  def command_range(i: Int): Iterator[(Command, Int)] =
wenzelm@34855
   153
    command_starts dropWhile { case (cmd, start) => start + cmd.length <= i }
wenzelm@34855
   154
wenzelm@34855
   155
  def command_range(i: Int, j: Int): Iterator[(Command, Int)] =
wenzelm@34855
   156
    command_range(i) takeWhile { case (_, start) => start < j }
wenzelm@34855
   157
wenzelm@34855
   158
  def command_at(i: Int): Option[(Command, Int)] =
wenzelm@34855
   159
  {
wenzelm@34855
   160
    val range = command_range(i)
wenzelm@34855
   161
    if (range.hasNext) Some(range.next) else None
wenzelm@34855
   162
  }
wenzelm@34855
   163
wenzelm@34855
   164
wenzelm@34855
   165
  /* command state assignment */
wenzelm@34840
   166
wenzelm@34840
   167
  val assignment = Future.promise[Map[Command, Command]]
wenzelm@34853
   168
  def await_assignment { assignment.join }
wenzelm@34840
   169
wenzelm@34859
   170
  @volatile private var tmp_states = former_states
wenzelm@34840
   171
wenzelm@34840
   172
  def assign_states(new_states: List[(Command, Command)])
wenzelm@34840
   173
  {
wenzelm@34840
   174
    assignment.fulfill(tmp_states ++ new_states)
wenzelm@34840
   175
    tmp_states = Map()
wenzelm@34840
   176
  }
wenzelm@34840
   177
wenzelm@36990
   178
  def current_state(cmd: Command): State =
wenzelm@34840
   179
  {
wenzelm@34840
   180
    require(assignment.is_finished)
wenzelm@36990
   181
    (assignment.join).get(cmd) match {
wenzelm@36990
   182
      case Some(cmd_state) => cmd_state.current_state
wenzelm@36990
   183
      case None => new State(cmd, Command.Status.UNDEFINED, Nil, cmd.empty_markup)
wenzelm@36990
   184
    }
wenzelm@34840
   185
  }
wenzelm@34318
   186
}