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