src/Pure/PIDE/document.scala
author wenzelm
Thu Sep 01 13:34:45 2011 +0200 (2011-09-01 ago)
changeset 44615 a4ff8a787202
parent 44613 a3255c85327b
child 44616 4beeaf2a226d
permissions -rw-r--r--
more abstract Document.Node.Name;
tuned signature;
     1 /*  Title:      Pure/PIDE/document.scala
     2     Author:     Makarius
     3 
     4 Document as collection of named nodes, each consisting of an editable
     5 list of commands, associated with asynchronous execution process.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 import scala.collection.mutable
    12 
    13 
    14 object Document
    15 {
    16   /* formal identifiers */
    17 
    18   type ID = Long
    19   val ID = Properties.Value.Long
    20 
    21   type Version_ID = ID
    22   type Command_ID = ID
    23   type Exec_ID = ID
    24 
    25   val no_id: ID = 0
    26   val new_id = new Counter
    27 
    28 
    29 
    30   /** document structure **/
    31 
    32   /* named nodes -- development graph */
    33 
    34   type Edit[A, B] = (Node.Name, Node.Edit[A, B])
    35   type Edit_Text = Edit[Text.Edit, Text.Perspective]
    36   type Edit_Command = Edit[(Option[Command], Option[Command]), Command.Perspective]
    37 
    38   type Node_Header = Exn.Result[Thy_Header]
    39 
    40   object Node
    41   {
    42     sealed case class Name(node: String, master_dir: String, theory: String)
    43     {
    44       override def hashCode: Int = node.hashCode
    45       override def equals(that: Any): Boolean =
    46         that match {
    47           case other: Name => node == other.node
    48           case _ => false
    49         }
    50     }
    51 
    52     sealed abstract class Edit[A, B]
    53     {
    54       def foreach(f: A => Unit)
    55       {
    56         this match {
    57           case Edits(es) => es.foreach(f)
    58           case _ =>
    59         }
    60       }
    61     }
    62     case class Clear[A, B]() extends Edit[A, B]
    63     case class Edits[A, B](edits: List[A]) extends Edit[A, B]
    64     case class Header[A, B](header: Node_Header) extends Edit[A, B]
    65     case class Perspective[A, B](perspective: B) extends Edit[A, B]
    66 
    67     def norm_header(f: String => String, g: String => String, header: Node_Header): Node_Header =
    68       header match {
    69         case Exn.Res(h) => Exn.capture { h.norm_deps(f, g) }
    70         case exn => exn
    71       }
    72 
    73     val empty: Node =
    74       Node(Exn.Exn(ERROR("Bad theory header")), Command.Perspective.empty, Map(), Linear_Set())
    75 
    76     def command_starts(commands: Iterator[Command], offset: Text.Offset = 0)
    77       : Iterator[(Command, Text.Offset)] =
    78     {
    79       var i = offset
    80       for (command <- commands) yield {
    81         val start = i
    82         i += command.length
    83         (command, start)
    84       }
    85     }
    86   }
    87 
    88   private val block_size = 1024
    89 
    90   sealed case class Node(
    91     val header: Node_Header,
    92     val perspective: Command.Perspective,
    93     val blobs: Map[String, Blob],
    94     val commands: Linear_Set[Command])
    95   {
    96     def clear: Node = Node.empty.copy(header = header)
    97 
    98 
    99     /* commands */
   100 
   101     private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
   102     {
   103       val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
   104       var next_block = 0
   105       var last_stop = 0
   106       for ((command, start) <- Node.command_starts(commands.iterator)) {
   107         last_stop = start + command.length
   108         while (last_stop + 1 > next_block) {
   109           blocks += (command -> start)
   110           next_block += block_size
   111         }
   112       }
   113       (blocks.toArray, Text.Range(0, last_stop))
   114     }
   115 
   116     def full_range: Text.Range = full_index._2
   117 
   118     def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
   119     {
   120       if (!commands.isEmpty && full_range.contains(i)) {
   121         val (cmd0, start0) = full_index._1(i / block_size)
   122         Node.command_starts(commands.iterator(cmd0), start0) dropWhile {
   123           case (cmd, start) => start + cmd.length <= i }
   124       }
   125       else Iterator.empty
   126     }
   127 
   128     def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] =
   129       command_range(range.start) takeWhile { case (_, start) => start < range.stop }
   130 
   131     def command_at(i: Text.Offset): Option[(Command, Text.Offset)] =
   132     {
   133       val range = command_range(i)
   134       if (range.hasNext) Some(range.next) else None
   135     }
   136 
   137     def proper_command_at(i: Text.Offset): Option[Command] =
   138       command_at(i) match {
   139         case Some((command, _)) =>
   140           commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
   141         case None => None
   142       }
   143 
   144     def command_start(cmd: Command): Option[Text.Offset] =
   145       command_starts.find(_._1 == cmd).map(_._2)
   146 
   147     def command_starts: Iterator[(Command, Text.Offset)] =
   148       Node.command_starts(commands.iterator)
   149   }
   150 
   151 
   152 
   153   /** versioning **/
   154 
   155   /* particular document versions */
   156 
   157   object Version
   158   {
   159     val init: Version = Version(no_id, Map().withDefaultValue(Node.empty))
   160   }
   161 
   162   type Nodes = Map[Node.Name, Node]
   163   sealed case class Version(val id: Version_ID, val nodes: Nodes)
   164 
   165 
   166   /* changes of plain text, eventually resulting in document edits */
   167 
   168   object Change
   169   {
   170     val init: Change = Change(Future.value(Version.init), Nil, Future.value(Version.init))
   171   }
   172 
   173   sealed case class Change(
   174     val previous: Future[Version],
   175     val edits: List[Edit_Text],
   176     val version: Future[Version])
   177   {
   178     def is_finished: Boolean = previous.is_finished && version.is_finished
   179   }
   180 
   181 
   182   /* history navigation */
   183 
   184   object History
   185   {
   186     val init: History = new History(List(Change.init))
   187   }
   188 
   189   // FIXME pruning, purging of state
   190   class History(val undo_list: List[Change])
   191   {
   192     require(!undo_list.isEmpty)
   193 
   194     def tip: Change = undo_list.head
   195     def +(change: Change): History = new History(change :: undo_list)
   196   }
   197 
   198 
   199 
   200   /** global state -- document structure, execution process, editing history **/
   201 
   202   abstract class Snapshot
   203   {
   204     val state: State
   205     val version: Version
   206     val node: Node
   207     val is_outdated: Boolean
   208     def command_state(command: Command): Command.State
   209     def convert(i: Text.Offset): Text.Offset
   210     def convert(range: Text.Range): Text.Range
   211     def revert(i: Text.Offset): Text.Offset
   212     def revert(range: Text.Range): Text.Range
   213     def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A])
   214       : Stream[Text.Info[Option[A]]]
   215   }
   216 
   217   type Assign =
   218    (List[(Document.Command_ID, Option[Document.Exec_ID])],  // exec state assignment
   219     List[(String, Option[Document.Command_ID])])  // last exec
   220 
   221   val no_assign: Assign = (Nil, Nil)
   222 
   223   object State
   224   {
   225     class Fail(state: State) extends Exception
   226 
   227     val init: State =
   228       State().define_version(Version.init, Assignment.init).assign(Version.init.id, no_assign)._2
   229 
   230     object Assignment
   231     {
   232       val init: Assignment = Assignment(Map.empty, Map.empty, false)
   233     }
   234 
   235     case class Assignment(
   236       val command_execs: Map[Command_ID, Exec_ID],
   237       val last_execs: Map[String, Option[Command_ID]],
   238       val is_finished: Boolean)
   239     {
   240       def check_finished: Assignment = { require(is_finished); this }
   241       def unfinished: Assignment = copy(is_finished = false)
   242 
   243       def assign(add_command_execs: List[(Command_ID, Option[Exec_ID])],
   244         add_last_execs: List[(String, Option[Command_ID])]): Assignment =
   245       {
   246         require(!is_finished)
   247         val command_execs1 =
   248           (command_execs /: add_command_execs) {
   249             case (res, (command_id, None)) => res - command_id
   250             case (res, (command_id, Some(exec_id))) => res + (command_id -> exec_id)
   251           }
   252         Assignment(command_execs1, last_execs ++ add_last_execs, true)
   253       }
   254     }
   255   }
   256 
   257   sealed case class State(
   258     val versions: Map[Version_ID, Version] = Map(),
   259     val commands: Map[Command_ID, Command.State] = Map(),  // static markup from define_command
   260     val execs: Map[Exec_ID, Command.State] = Map(),  // dynamic markup from execution
   261     val assignments: Map[Version_ID, State.Assignment] = Map(),
   262     val disposed: Set[ID] = Set(),  // FIXME unused!?
   263     val history: History = History.init)
   264   {
   265     private def fail[A]: A = throw new State.Fail(this)
   266 
   267     def define_version(version: Version, assignment: State.Assignment): State =
   268     {
   269       val id = version.id
   270       if (versions.isDefinedAt(id) || disposed(id)) fail
   271       copy(versions = versions + (id -> version),
   272         assignments = assignments + (id -> assignment.unfinished))
   273     }
   274 
   275     def define_command(command: Command): State =
   276     {
   277       val id = command.id
   278       if (commands.isDefinedAt(id) || disposed(id)) fail
   279       copy(commands = commands + (id -> command.empty_state))
   280     }
   281 
   282     def defined_command(id: Command_ID): Boolean = commands.isDefinedAt(id)
   283 
   284     def find_command(version: Version, id: ID): Option[(Node, Command)] =
   285       commands.get(id) orElse execs.get(id) match {
   286         case None => None
   287         case Some(st) =>
   288           val command = st.command
   289           version.nodes.get(command.node_name).map((_, command))
   290       }
   291 
   292     def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
   293     def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)  // FIXME rename
   294     def the_exec(id: Exec_ID): Command.State = execs.getOrElse(id, fail)
   295     def the_assignment(version: Version): State.Assignment =
   296       assignments.getOrElse(version.id, fail)
   297 
   298     def accumulate(id: ID, message: XML.Elem): (Command.State, State) =
   299       execs.get(id) match {
   300         case Some(st) =>
   301           val new_st = st.accumulate(message)
   302           (new_st, copy(execs = execs + (id -> new_st)))
   303         case None =>
   304           commands.get(id) match {
   305             case Some(st) =>
   306               val new_st = st.accumulate(message)
   307               (new_st, copy(commands = commands + (id -> new_st)))
   308             case None => fail
   309           }
   310       }
   311 
   312     def assign(id: Version_ID, arg: Assign): (List[Command], State) =
   313     {
   314       val version = the_version(id)
   315       val (command_execs, last_execs) = arg
   316 
   317       val (changed_commands, new_execs) =
   318         ((Nil: List[Command], execs) /: command_execs) {
   319           case ((commands1, execs1), (cmd_id, exec)) =>
   320             val st = the_command(cmd_id)
   321             val commands2 = st.command :: commands1
   322             val execs2 =
   323               exec match {
   324                 case None => execs1
   325                 case Some(exec_id) => execs1 + (exec_id -> st)
   326               }
   327             (commands2, execs2)
   328         }
   329       val new_assignment = the_assignment(version).assign(command_execs, last_execs)
   330       val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
   331 
   332       (changed_commands, new_state)
   333     }
   334 
   335     def is_assigned(version: Version): Boolean =
   336       assignments.get(version.id) match {
   337         case Some(assgn) => assgn.is_finished
   338         case None => false
   339       }
   340 
   341     def is_stable(change: Change): Boolean =
   342       change.is_finished && is_assigned(change.version.get_finished)
   343 
   344     def recent_stable: Option[Change] = history.undo_list.find(is_stable)
   345     def tip_stable: Boolean = is_stable(history.tip)
   346     def tip_version: Version = history.tip.version.get_finished
   347 
   348     def last_exec_offset(name: Node.Name): Text.Offset =
   349     {
   350       val version = tip_version
   351       the_assignment(version).last_execs.get(name.node) match {
   352         case Some(Some(id)) =>
   353           val node = version.nodes(name)
   354           val cmd = the_command(id).command
   355           node.command_start(cmd) match {
   356             case None => 0
   357             case Some(start) => start + cmd.length
   358           }
   359         case _ => 0
   360       }
   361     }
   362 
   363     def continue_history(
   364         previous: Future[Version],
   365         edits: List[Edit_Text],
   366         version: Future[Version]): (Change, State) =
   367     {
   368       val change = Change(previous, edits, version)
   369       (change, copy(history = history + change))
   370     }
   371 
   372     def command_state(version: Version, command: Command): Command.State =
   373     {
   374       require(is_assigned(version))
   375       try {
   376         the_exec(the_assignment(version).check_finished.command_execs
   377           .getOrElse(command.id, fail))
   378       }
   379       catch { case _: State.Fail => command.empty_state }
   380     }
   381 
   382 
   383     // persistent user-view
   384     def snapshot(name: Node.Name, pending_edits: List[Text.Edit]): Snapshot =
   385     {
   386       val stable = recent_stable.get
   387       val latest = history.tip
   388 
   389       // FIXME proper treatment of removed nodes
   390       val edits =
   391         (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
   392             (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
   393       lazy val reverse_edits = edits.reverse
   394 
   395       new Snapshot
   396       {
   397         val state = State.this
   398         val version = stable.version.get_finished
   399         val node = version.nodes(name)
   400         val is_outdated = !(pending_edits.isEmpty && latest == stable)
   401         def command_state(command: Command): Command.State = state.command_state(version, command)
   402 
   403         def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
   404         def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
   405         def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
   406         def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
   407 
   408         def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A])
   409           : Stream[Text.Info[Option[A]]] =
   410         {
   411           val former_range = revert(range)
   412           for {
   413             (command, command_start) <- node.command_range(former_range).toStream
   414             Text.Info(r0, x) <- command_state(command).markup.
   415               select((former_range - command_start).restrict(command.range)) {
   416                 case Text.Info(r0, info)
   417                 if result.isDefinedAt(Text.Info(convert(r0 + command_start), info)) =>
   418                   result(Text.Info(convert(r0 + command_start), info))
   419               }
   420           } yield Text.Info(convert(r0 + command_start), x)
   421         }
   422       }
   423     }
   424   }
   425 }
   426