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