src/Pure/PIDE/document.scala
author wenzelm
Wed Aug 24 13:03:39 2011 +0200 (2011-08-24 ago)
changeset 44436 546adfa8a6fc
parent 44385 e7fdb008aa7d
child 44442 cb18e4f09053
permissions -rw-r--r--
update_perspective without actual edits, bypassing the full state assignment protocol;
edit_nodes/Perspective: do not touch_descendants here;
propagate editor scroll events via update_perspective;
misc tuning;
     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] = (String, 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 abstract class Edit[A, B]
    43     {
    44       def foreach(f: A => Unit)
    45       {
    46         this match {
    47           case Edits(es) => es.foreach(f)
    48           case _ =>
    49         }
    50       }
    51     }
    52     case class Clear[A, B]() extends Edit[A, B]
    53     case class Edits[A, B](edits: List[A]) extends Edit[A, B]
    54     case class Header[A, B](header: Node_Header) extends Edit[A, B]
    55     case class Perspective[A, B](perspective: B) extends Edit[A, B]
    56 
    57     def norm_header[A, B](f: String => String, g: String => String, header: Node_Header)
    58         : Header[A, B] =
    59       header match {
    60         case Exn.Res(h) => Header[A, B](Exn.capture { h.norm_deps(f, g) })
    61         case exn => Header[A, B](exn)
    62       }
    63 
    64     val empty: Node = Node(Exn.Exn(ERROR("Bad theory header")), Nil, Map(), Linear_Set())
    65 
    66     def command_starts(commands: Iterator[Command], offset: Text.Offset = 0)
    67       : Iterator[(Command, Text.Offset)] =
    68     {
    69       var i = offset
    70       for (command <- commands) yield {
    71         val start = i
    72         i += command.length
    73         (command, start)
    74       }
    75     }
    76   }
    77 
    78   private val block_size = 1024
    79 
    80   sealed case class Node(
    81     val header: Node_Header,
    82     val perspective: Command.Perspective,
    83     val blobs: Map[String, Blob],
    84     val commands: Linear_Set[Command])
    85   {
    86     /* commands */
    87 
    88     private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) =
    89     {
    90       val blocks = new mutable.ListBuffer[(Command, Text.Offset)]
    91       var next_block = 0
    92       var last_stop = 0
    93       for ((command, start) <- Node.command_starts(commands.iterator)) {
    94         last_stop = start + command.length
    95         while (last_stop + 1 > next_block) {
    96           blocks += (command -> start)
    97           next_block += block_size
    98         }
    99       }
   100       (blocks.toArray, Text.Range(0, last_stop))
   101     }
   102 
   103     def full_range: Text.Range = full_index._2
   104 
   105     def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] =
   106     {
   107       if (!commands.isEmpty && full_range.contains(i)) {
   108         val (cmd0, start0) = full_index._1(i / block_size)
   109         Node.command_starts(commands.iterator(cmd0), start0) dropWhile {
   110           case (cmd, start) => start + cmd.length <= i }
   111       }
   112       else Iterator.empty
   113     }
   114 
   115     def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] =
   116       command_range(range.start) takeWhile { case (_, start) => start < range.stop }
   117 
   118     def command_at(i: Text.Offset): Option[(Command, Text.Offset)] =
   119     {
   120       val range = command_range(i)
   121       if (range.hasNext) Some(range.next) else None
   122     }
   123 
   124     def proper_command_at(i: Text.Offset): Option[Command] =
   125       command_at(i) match {
   126         case Some((command, _)) =>
   127           commands.reverse_iterator(command).find(cmd => !cmd.is_ignored)
   128         case None => None
   129       }
   130 
   131     def command_start(cmd: Command): Option[Text.Offset] =
   132       command_starts.find(_._1 == cmd).map(_._2)
   133 
   134     def command_starts: Iterator[(Command, Text.Offset)] =
   135       Node.command_starts(commands.iterator)
   136   }
   137 
   138 
   139 
   140   /** versioning **/
   141 
   142   /* particular document versions */
   143 
   144   object Version
   145   {
   146     val init: Version = Version(no_id, Map().withDefaultValue(Node.empty))
   147   }
   148 
   149   type Nodes = Map[String, Node]
   150   sealed case class Version(val id: Version_ID, val nodes: Nodes)
   151 
   152 
   153   /* changes of plain text, eventually resulting in document edits */
   154 
   155   object Change
   156   {
   157     val init = Change(Future.value(Version.init), Nil, Future.value(Version.init))
   158   }
   159 
   160   sealed case class Change(
   161     val previous: Future[Version],
   162     val edits: List[Edit_Text],
   163     val version: Future[Version])
   164   {
   165     def is_finished: Boolean = previous.is_finished && version.is_finished
   166   }
   167 
   168 
   169   /* history navigation */
   170 
   171   object History
   172   {
   173     val init = new History(List(Change.init))
   174   }
   175 
   176   // FIXME pruning, purging of state
   177   class History(val undo_list: List[Change])
   178   {
   179     require(!undo_list.isEmpty)
   180 
   181     def tip: Change = undo_list.head
   182     def +(change: Change): History = new History(change :: undo_list)
   183   }
   184 
   185 
   186 
   187   /** global state -- document structure, execution process, editing history **/
   188 
   189   abstract class Snapshot
   190   {
   191     val version: Version
   192     val node: Node
   193     val is_outdated: Boolean
   194     def lookup_command(id: Command_ID): Option[Command]
   195     def state(command: Command): Command.State
   196     def convert(i: Text.Offset): Text.Offset
   197     def convert(range: Text.Range): Text.Range
   198     def revert(i: Text.Offset): Text.Offset
   199     def revert(range: Text.Range): Text.Range
   200     def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A])
   201       : Stream[Text.Info[Option[A]]]
   202   }
   203 
   204   object State
   205   {
   206     class Fail(state: State) extends Exception
   207 
   208     val init = State().define_version(Version.init, Map()).assign(Version.init.id, Nil)._2
   209 
   210     case class Assignment(
   211       val assignment: Map[Command, Exec_ID],
   212       val is_finished: Boolean = false)
   213     {
   214       def get_finished: Map[Command, Exec_ID] = { require(is_finished); assignment }
   215       def assign(command_execs: List[(Command, Exec_ID)]): Assignment =
   216       {
   217         require(!is_finished)
   218         Assignment(assignment ++ command_execs, true)
   219       }
   220     }
   221   }
   222 
   223   sealed case class State(
   224     val versions: Map[Version_ID, Version] = Map(),
   225     val commands: Map[Command_ID, Command.State] = Map(),
   226     val execs: Map[Exec_ID, (Command.State, Set[Version])] = Map(),
   227     val assignments: Map[Version_ID, State.Assignment] = Map(),
   228     val disposed: Set[ID] = Set(),  // FIXME unused!?
   229     val history: History = History.init)
   230   {
   231     private def fail[A]: A = throw new State.Fail(this)
   232 
   233     def define_version(version: Version, former_assignment: Map[Command, Exec_ID]): State =
   234     {
   235       val id = version.id
   236       if (versions.isDefinedAt(id) || disposed(id)) fail
   237       copy(versions = versions + (id -> version),
   238         assignments = assignments + (id -> State.Assignment(former_assignment)))
   239     }
   240 
   241     def define_command(command: Command): State =
   242     {
   243       val id = command.id
   244       if (commands.isDefinedAt(id) || disposed(id)) fail
   245       copy(commands = commands + (id -> command.empty_state))
   246     }
   247 
   248     def lookup_command(id: Command_ID): Option[Command] = commands.get(id).map(_.command)
   249 
   250     def the_version(id: Version_ID): Version = versions.getOrElse(id, fail)
   251     def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail)
   252     def the_exec_state(id: Exec_ID): Command.State = execs.getOrElse(id, fail)._1
   253     def the_assignment(version: Version): State.Assignment =
   254       assignments.getOrElse(version.id, fail)
   255 
   256     def accumulate(id: ID, message: XML.Elem): (Command.State, State) =
   257       execs.get(id) match {
   258         case Some((st, occs)) =>
   259           val new_st = st.accumulate(message)
   260           (new_st, copy(execs = execs + (id -> (new_st, occs))))
   261         case None =>
   262           commands.get(id) match {
   263             case Some(st) =>
   264               val new_st = st.accumulate(message)
   265               (new_st, copy(commands = commands + (id -> new_st)))
   266             case None => fail
   267           }
   268       }
   269 
   270     def assign(id: Version_ID, edits: List[(Command_ID, Exec_ID)]): (List[Command], State) =
   271     {
   272       val version = the_version(id)
   273       val occs = Set(version)  // FIXME unused (!?)
   274 
   275       var new_execs = execs
   276       val assigned_execs =
   277         for ((cmd_id, exec_id) <- edits) yield {
   278           val st = the_command(cmd_id)
   279           if (new_execs.isDefinedAt(exec_id) || disposed(exec_id)) fail
   280           new_execs += (exec_id -> (st, occs))
   281           (st.command, exec_id)
   282         }
   283       val new_assignment = the_assignment(version).assign(assigned_execs)
   284       val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs)
   285       (assigned_execs.map(_._1), new_state)
   286     }
   287 
   288     def is_assigned(version: Version): Boolean =
   289       assignments.get(version.id) match {
   290         case Some(assgn) => assgn.is_finished
   291         case None => false
   292       }
   293 
   294     def is_stable(change: Change): Boolean =
   295       change.is_finished && is_assigned(change.version.get_finished)
   296 
   297     def tip_stable: Boolean = is_stable(history.tip)
   298     def recent_stable: Option[Change] = history.undo_list.find(is_stable)
   299 
   300     def extend_history(previous: Future[Version],
   301         edits: List[Edit_Text],
   302         version: Future[Version]): (Change, State) =
   303     {
   304       val change = Change(previous, edits, version)
   305       (change, copy(history = history + change))
   306     }
   307 
   308 
   309     // persistent user-view
   310     def snapshot(name: String, pending_edits: List[Text.Edit]): Snapshot =
   311     {
   312       val stable = recent_stable.get
   313       val latest = history.tip
   314 
   315       // FIXME proper treatment of removed nodes
   316       val edits =
   317         (pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) =>
   318             (for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits)
   319       lazy val reverse_edits = edits.reverse
   320 
   321       new Snapshot
   322       {
   323         val version = stable.version.get_finished
   324         val node = version.nodes(name)
   325         val is_outdated = !(pending_edits.isEmpty && latest == stable)
   326 
   327         def lookup_command(id: Command_ID): Option[Command] = State.this.lookup_command(id)
   328 
   329         def state(command: Command): Command.State =
   330           try { the_exec_state(the_assignment(version).get_finished.getOrElse(command, fail)) }
   331           catch { case _: State.Fail => command.empty_state }
   332 
   333         def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i))
   334         def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i))
   335         def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r))
   336         def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r))
   337 
   338         def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A])
   339           : Stream[Text.Info[Option[A]]] =
   340         {
   341           val former_range = revert(range)
   342           for {
   343             (command, command_start) <- node.command_range(former_range).toStream
   344             Text.Info(r0, x) <- state(command).markup.
   345               select((former_range - command_start).restrict(command.range)) {
   346                 case Text.Info(r0, info)
   347                 if result.isDefinedAt(Text.Info(convert(r0 + command_start), info)) =>
   348                   result(Text.Info(convert(r0 + command_start), info))
   349               }
   350           } yield Text.Info(convert(r0 + command_start), x)
   351         }
   352       }
   353     }
   354   }
   355 }
   356