src/Tools/jEdit/src/proofdocument/document.scala
author wenzelm
Mon Jan 11 16:49:11 2010 +0100 (2010-01-11)
changeset 34863 3e655d0ff936
parent 34862 a1d394600a92
child 34866 a4fe43df58b3
permissions -rw-r--r--
eliminated strange mutable var commands;
removed_commands: refer to old commands;
misc tuning;
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@34859
    70
            case (cmd, cmd_start) => e.can_edit(cmd.length, cmd_start)
wenzelm@34863
    71
          } match {
wenzelm@34863
    72
            // FIXME try to append after command
wenzelm@34859
    73
            case Some((cmd, cmd_start)) =>
wenzelm@34859
    74
              val (rest, source) = e.edit(cmd.source, cmd_start)
wenzelm@34863
    75
              val new_commands = commands.insert_after(Some(cmd), unparsed(source)) - cmd
wenzelm@34863
    76
              edit_text(rest.toList ::: es, new_commands)
wenzelm@34318
    77
wenzelm@34859
    78
            case None =>
wenzelm@34859
    79
              require(e.isInstanceOf[Text_Edit.Insert] && e.start == 0)
wenzelm@34863
    80
              edit_text(es, commands.insert_after(None, unparsed(e.text)))
wenzelm@34859
    81
          }
wenzelm@34863
    82
        case Nil => commands
wenzelm@34318
    83
      }
wenzelm@34318
    84
    }
wenzelm@34582
    85
wenzelm@34818
    86
wenzelm@34859
    87
    /* phase 2: command range edits */
wenzelm@34859
    88
wenzelm@34863
    89
    def edit_commands(commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@34859
    90
    {
wenzelm@34859
    91
      // FIXME relative search!
wenzelm@34859
    92
      commands.elements.find(is_unparsed) match {
wenzelm@34859
    93
        case Some(first_unparsed) =>
wenzelm@34859
    94
          val prefix = commands.prev(first_unparsed)
wenzelm@34859
    95
          val body = commands.elements(first_unparsed).takeWhile(is_unparsed).toList
wenzelm@34859
    96
          val suffix = commands.next(body.last)
wenzelm@34859
    97
wenzelm@34863
    98
          val sources = (prefix.toList ::: body ::: suffix.toList).flatMap(_.span.map(_.source))
wenzelm@34863
    99
          val span = Thy_Syntax.parse_spans(session.current_syntax.scan(sources.mkString))
wenzelm@34485
   100
wenzelm@34859
   101
          val (before_edit, spans1) =
wenzelm@34863
   102
            if (!span.isEmpty && Some(span.first) == prefix.map(_.span))
wenzelm@34863
   103
              (prefix, span.tail)
wenzelm@34863
   104
            else (if (prefix.isDefined) commands.prev(prefix.get) else None, span)
immler@34544
   105
wenzelm@34859
   106
          val (after_edit, spans2) =
wenzelm@34859
   107
            if (!spans1.isEmpty && Some(spans1.last) == suffix.map(_.span))
wenzelm@34859
   108
              (suffix, spans1.take(spans1.length - 1))
wenzelm@34859
   109
            else (if (suffix.isDefined) commands.next(suffix.get) else None, spans1)
wenzelm@34859
   110
wenzelm@34863
   111
          val inserted = spans2.map(span => new Command(session.create_id(), span))
wenzelm@34863
   112
          val new_commands =
wenzelm@34863
   113
            commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted)
wenzelm@34863
   114
          edit_commands(new_commands)
wenzelm@34485
   115
wenzelm@34863
   116
        case None => commands
wenzelm@34485
   117
      }
wenzelm@34485
   118
    }
wenzelm@34739
   119
immler@34554
   120
wenzelm@34859
   121
    /* phase 3: resulting document edits */
wenzelm@34859
   122
wenzelm@34863
   123
    val commands0 = old_doc.commands
wenzelm@34863
   124
    val commands1 = edit_text(edits, commands0)
wenzelm@34863
   125
    val commands2 = edit_commands(commands1)
immler@34660
   126
wenzelm@34863
   127
    val removed_commands = commands0.elements.filter(!commands2.contains(_)).toList
wenzelm@34863
   128
    val inserted_commands = commands2.elements.filter(!commands0.contains(_)).toList
wenzelm@34863
   129
wenzelm@34863
   130
    // FIXME tune
wenzelm@34859
   131
    val doc_edits =
wenzelm@34863
   132
      removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) :::
wenzelm@34863
   133
      inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd)))
immler@34660
   134
wenzelm@34859
   135
    val former_states = old_doc.assignment.join -- removed_commands
wenzelm@34859
   136
wenzelm@34863
   137
    phase0 = edits
wenzelm@34863
   138
    phase1 = commands1
wenzelm@34863
   139
    phase2 = commands2
wenzelm@34859
   140
    phase3 = doc_edits
wenzelm@34863
   141
wenzelm@34863
   142
    (doc_edits, new Document(new_id, commands2, former_states))
wenzelm@34485
   143
  }
wenzelm@34840
   144
}
wenzelm@34840
   145
wenzelm@34855
   146
wenzelm@34840
   147
class Document(
wenzelm@34840
   148
    val id: Isar_Document.Document_ID,
wenzelm@34840
   149
    val commands: Linear_Set[Command],
wenzelm@34859
   150
    former_states: Map[Command, Command])
wenzelm@34840
   151
{
wenzelm@34859
   152
  /* command ranges */
wenzelm@34855
   153
wenzelm@34859
   154
  def command_starts: Iterator[(Command, Int)] = Document.command_starts(commands)
wenzelm@34855
   155
wenzelm@34855
   156
  def command_start(cmd: Command): Option[Int] =
wenzelm@34855
   157
    command_starts.find(_._1 == cmd).map(_._2)
wenzelm@34855
   158
wenzelm@34855
   159
  def command_range(i: Int): Iterator[(Command, Int)] =
wenzelm@34855
   160
    command_starts dropWhile { case (cmd, start) => start + cmd.length <= i }
wenzelm@34855
   161
wenzelm@34855
   162
  def command_range(i: Int, j: Int): Iterator[(Command, Int)] =
wenzelm@34855
   163
    command_range(i) takeWhile { case (_, start) => start < j }
wenzelm@34855
   164
wenzelm@34855
   165
  def command_at(i: Int): Option[(Command, Int)] =
wenzelm@34855
   166
  {
wenzelm@34855
   167
    val range = command_range(i)
wenzelm@34855
   168
    if (range.hasNext) Some(range.next) else None
wenzelm@34855
   169
  }
wenzelm@34855
   170
wenzelm@34855
   171
wenzelm@34855
   172
  /* command state assignment */
wenzelm@34840
   173
wenzelm@34840
   174
  val assignment = Future.promise[Map[Command, Command]]
wenzelm@34853
   175
  def await_assignment { assignment.join }
wenzelm@34840
   176
wenzelm@34859
   177
  @volatile private var tmp_states = former_states
wenzelm@34840
   178
wenzelm@34840
   179
  def assign_states(new_states: List[(Command, Command)])
wenzelm@34840
   180
  {
wenzelm@34840
   181
    assignment.fulfill(tmp_states ++ new_states)
wenzelm@34840
   182
    tmp_states = Map()
wenzelm@34840
   183
  }
wenzelm@34840
   184
wenzelm@34840
   185
  def current_state(cmd: Command): State =
wenzelm@34840
   186
  {
wenzelm@34840
   187
    require(assignment.is_finished)
wenzelm@34840
   188
    (assignment.join)(cmd).current_state
wenzelm@34840
   189
  }
wenzelm@34318
   190
}