src/Tools/jEdit/src/proofdocument/document.scala
author wenzelm
Mon Jan 11 13:58:51 2010 +0100 (2010-01-11)
changeset 34862 a1d394600a92
parent 34860 847c00f5535a
child 34863 3e655d0ff936
permissions -rw-r--r--
updated header;
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@34860
    40
  var raw_source: String = null
wenzelm@34859
    41
wenzelm@34859
    42
wenzelm@34859
    43
wenzelm@34859
    44
  /** document edits **/
wenzelm@34859
    45
wenzelm@34853
    46
  type Edit = (Option[Command], Option[Command])
wenzelm@34824
    47
wenzelm@34824
    48
  def text_edits(session: Session, old_doc: Document, new_id: Isar_Document.Document_ID,
wenzelm@34853
    49
    edits: List[Text_Edit]): (List[Edit], Document) =
wenzelm@34824
    50
  {
wenzelm@34840
    51
    require(old_doc.assignment.is_finished)
wenzelm@34840
    52
wenzelm@34859
    53
    phase0 = edits
wenzelm@34859
    54
wenzelm@34859
    55
wenzelm@34859
    56
    /* unparsed dummy commands */
immler@34538
    57
wenzelm@34859
    58
    def unparsed(source: String) =
wenzelm@34860
    59
      new Command(null, List(Outer_Lex.Token(Outer_Lex.Token_Kind.UNPARSED, source)))
wenzelm@34859
    60
wenzelm@34860
    61
    def is_unparsed(command: Command) = command.id == null
wenzelm@34835
    62
wenzelm@34860
    63
    assert(!old_doc.commands.exists(is_unparsed))
wenzelm@34859
    64
wenzelm@34859
    65
wenzelm@34859
    66
    /* phase 1: edit individual command source */
wenzelm@34859
    67
wenzelm@34859
    68
    var commands = old_doc.commands
wenzelm@34485
    69
wenzelm@34859
    70
    def edit_text(eds: List[Text_Edit]): Unit =
wenzelm@34859
    71
    {
wenzelm@34859
    72
      eds match {
wenzelm@34859
    73
        case e :: es =>
wenzelm@34859
    74
          command_starts(commands).find {   // FIXME relative search!
wenzelm@34859
    75
            case (cmd, cmd_start) => e.can_edit(cmd.length, cmd_start)
wenzelm@34860
    76
          } match {  // FIXME try to append after command
wenzelm@34859
    77
            case Some((cmd, cmd_start)) =>
wenzelm@34859
    78
              val (rest, source) = e.edit(cmd.source, cmd_start)
wenzelm@34859
    79
              // FIXME Linear_Set.edit (?)
wenzelm@34859
    80
              commands = commands.insert_after(Some(cmd), unparsed(source))
wenzelm@34859
    81
              commands -= cmd
wenzelm@34859
    82
              edit_text(rest.toList ::: es)
wenzelm@34318
    83
wenzelm@34859
    84
            case None =>
wenzelm@34859
    85
              require(e.isInstanceOf[Text_Edit.Insert] && e.start == 0)
wenzelm@34859
    86
              commands = commands.insert_after(None, unparsed(e.text))
wenzelm@34859
    87
              edit_text(es)
wenzelm@34859
    88
          }
wenzelm@34859
    89
        case Nil =>
wenzelm@34318
    90
      }
wenzelm@34318
    91
    }
wenzelm@34859
    92
    edit_text(edits)
wenzelm@34859
    93
wenzelm@34859
    94
    phase1 = commands
wenzelm@34582
    95
wenzelm@34818
    96
wenzelm@34859
    97
    /* phase 2: command range edits */
wenzelm@34859
    98
wenzelm@34859
    99
    def edit_commands(): Unit =
wenzelm@34859
   100
    {
wenzelm@34859
   101
      // FIXME relative search!
wenzelm@34859
   102
      commands.elements.find(is_unparsed) match {
wenzelm@34859
   103
        case Some(first_unparsed) =>
wenzelm@34859
   104
          val prefix = commands.prev(first_unparsed)
wenzelm@34859
   105
          val body = commands.elements(first_unparsed).takeWhile(is_unparsed).toList
wenzelm@34859
   106
          val suffix = commands.next(body.last)
wenzelm@34859
   107
wenzelm@34859
   108
          val source =
wenzelm@34859
   109
            (prefix.toList ::: body ::: suffix.toList).flatMap(_.span.map(_.source)).mkString
wenzelm@34860
   110
          assert(source != "")
wenzelm@34859
   111
          raw_source = source
wenzelm@34859
   112
          
wenzelm@34859
   113
          val spans0 = Thy_Syntax.parse_spans(session.current_syntax.scan(source))
wenzelm@34485
   114
wenzelm@34859
   115
          val (before_edit, spans1) =
wenzelm@34859
   116
            if (!spans0.isEmpty && Some(spans0.first) == prefix.map(_.span))
wenzelm@34859
   117
              (prefix, spans0.tail)
wenzelm@34859
   118
            else (if (prefix.isDefined) commands.prev(prefix.get) else None, spans0)
immler@34544
   119
wenzelm@34859
   120
          val (after_edit, spans2) =
wenzelm@34859
   121
            if (!spans1.isEmpty && Some(spans1.last) == suffix.map(_.span))
wenzelm@34859
   122
              (suffix, spans1.take(spans1.length - 1))
wenzelm@34859
   123
            else (if (suffix.isDefined) commands.next(suffix.get) else None, spans1)
wenzelm@34859
   124
wenzelm@34859
   125
          val new_commands = spans2.map(span => new Command(session.create_id(), span))
wenzelm@34485
   126
wenzelm@34859
   127
          commands = commands.delete_between(before_edit, after_edit)
wenzelm@34859
   128
          commands = commands.append_after(before_edit, new_commands)
immler@34554
   129
wenzelm@34859
   130
          edit_commands()
wenzelm@34859
   131
wenzelm@34859
   132
        case None =>
wenzelm@34485
   133
      }
wenzelm@34485
   134
    }
wenzelm@34860
   135
    edit_commands()
wenzelm@34860
   136
    
wenzelm@34859
   137
    phase2 = commands
wenzelm@34739
   138
immler@34554
   139
wenzelm@34859
   140
    /* phase 3: resulting document edits */
wenzelm@34859
   141
wenzelm@34859
   142
    val removed_commands = old_doc.commands.elements.filter(!commands.contains(_)).toList
wenzelm@34859
   143
    val inserted_commands = commands.elements.filter(!old_doc.commands.contains(_)).toList
immler@34660
   144
wenzelm@34860
   145
    // FIXME proper order
wenzelm@34859
   146
    val doc_edits =
wenzelm@34860
   147
      removed_commands.reverse.map(cmd => (commands.prev(cmd), None)) :::
wenzelm@34859
   148
      inserted_commands.map(cmd => (commands.prev(cmd), Some(cmd)))
immler@34660
   149
wenzelm@34859
   150
    val former_states = old_doc.assignment.join -- removed_commands
wenzelm@34859
   151
wenzelm@34859
   152
    phase3 = doc_edits
wenzelm@34859
   153
    (doc_edits, new Document(new_id, commands, former_states))
wenzelm@34485
   154
  }
wenzelm@34840
   155
}
wenzelm@34840
   156
wenzelm@34855
   157
wenzelm@34840
   158
class Document(
wenzelm@34840
   159
    val id: Isar_Document.Document_ID,
wenzelm@34840
   160
    val commands: Linear_Set[Command],
wenzelm@34859
   161
    former_states: Map[Command, Command])
wenzelm@34840
   162
{
wenzelm@34859
   163
  /* command ranges */
wenzelm@34855
   164
wenzelm@34859
   165
  def command_starts: Iterator[(Command, Int)] = Document.command_starts(commands)
wenzelm@34855
   166
wenzelm@34855
   167
  def command_start(cmd: Command): Option[Int] =
wenzelm@34855
   168
    command_starts.find(_._1 == cmd).map(_._2)
wenzelm@34855
   169
wenzelm@34855
   170
  def command_range(i: Int): Iterator[(Command, Int)] =
wenzelm@34855
   171
    command_starts dropWhile { case (cmd, start) => start + cmd.length <= i }
wenzelm@34855
   172
wenzelm@34855
   173
  def command_range(i: Int, j: Int): Iterator[(Command, Int)] =
wenzelm@34855
   174
    command_range(i) takeWhile { case (_, start) => start < j }
wenzelm@34855
   175
wenzelm@34855
   176
  def command_at(i: Int): Option[(Command, Int)] =
wenzelm@34855
   177
  {
wenzelm@34855
   178
    val range = command_range(i)
wenzelm@34855
   179
    if (range.hasNext) Some(range.next) else None
wenzelm@34855
   180
  }
wenzelm@34855
   181
wenzelm@34855
   182
wenzelm@34855
   183
  /* command state assignment */
wenzelm@34840
   184
wenzelm@34840
   185
  val assignment = Future.promise[Map[Command, Command]]
wenzelm@34853
   186
  def await_assignment { assignment.join }
wenzelm@34840
   187
wenzelm@34859
   188
  @volatile private var tmp_states = former_states
wenzelm@34840
   189
wenzelm@34840
   190
  def assign_states(new_states: List[(Command, Command)])
wenzelm@34840
   191
  {
wenzelm@34840
   192
    assignment.fulfill(tmp_states ++ new_states)
wenzelm@34840
   193
    tmp_states = Map()
wenzelm@34840
   194
  }
wenzelm@34840
   195
wenzelm@34840
   196
  def current_state(cmd: Command): State =
wenzelm@34840
   197
  {
wenzelm@34840
   198
    require(assignment.is_finished)
wenzelm@34840
   199
    (assignment.join)(cmd).current_state
wenzelm@34840
   200
  }
wenzelm@34318
   201
}