src/Pure/PIDE/query_operation.scala
author wenzelm
Mon Oct 07 12:28:19 2013 +0200 (2013-10-07 ago)
changeset 54310 6ddeb83eb67a
parent 53872 6e69f9ca8f1c
child 54325 2c4155003352
permissions -rw-r--r--
clarified remove_overlay: always flush in order to make sure that apply_query can make a fresh start with the same arguments (see also 6e69f9ca8f1c) -- NB: print functions are idempotent;
wenzelm@52981
     1
/*  Title:      Pure/PIDE/query_operation.scala
wenzelm@52865
     2
    Author:     Makarius
wenzelm@52865
     3
wenzelm@52865
     4
One-shot query operations via asynchronous print functions and temporary
wenzelm@52932
     5
document overlays.
wenzelm@52865
     6
*/
wenzelm@52865
     7
wenzelm@52981
     8
package isabelle
wenzelm@52865
     9
wenzelm@52865
    10
wenzelm@52865
    11
import scala.actors.Actor._
wenzelm@52865
    12
wenzelm@52865
    13
wenzelm@52865
    14
object Query_Operation
wenzelm@52865
    15
{
wenzelm@52935
    16
  object Status extends Enumeration
wenzelm@52935
    17
  {
wenzelm@52935
    18
    val WAITING = Value("waiting")
wenzelm@52935
    19
    val RUNNING = Value("running")
wenzelm@52935
    20
    val FINISHED = Value("finished")
wenzelm@53872
    21
    val REMOVED = Value("removed")
wenzelm@52935
    22
  }
wenzelm@52865
    23
}
wenzelm@52865
    24
wenzelm@52980
    25
class Query_Operation[Editor_Context](
wenzelm@52980
    26
  editor: Editor[Editor_Context],
wenzelm@52980
    27
  editor_context: Editor_Context,
wenzelm@52865
    28
  operation_name: String,
wenzelm@52935
    29
  consume_status: Query_Operation.Status.Value => Unit,
wenzelm@52932
    30
  consume_output: (Document.Snapshot, Command.Results, XML.Body) => Unit)
wenzelm@52865
    31
{
wenzelm@52865
    32
  private val instance = Document_ID.make().toString
wenzelm@52865
    33
wenzelm@52865
    34
wenzelm@52877
    35
  /* implicit state -- owned by Swing thread */
wenzelm@52874
    36
wenzelm@52865
    37
  private var current_location: Option[Command] = None
wenzelm@52865
    38
  private var current_query: List[String] = Nil
wenzelm@52876
    39
  private var current_update_pending = false
wenzelm@52865
    40
  private var current_output: List[XML.Tree] = Nil
wenzelm@53872
    41
  private var current_status = Query_Operation.Status.REMOVED
wenzelm@52931
    42
  private var current_exec_id = Document_ID.none
wenzelm@52876
    43
wenzelm@52876
    44
  private def reset_state()
wenzelm@52876
    45
  {
wenzelm@52876
    46
    current_location = None
wenzelm@52876
    47
    current_query = Nil
wenzelm@52876
    48
    current_update_pending = false
wenzelm@52876
    49
    current_output = Nil
wenzelm@53872
    50
    current_status = Query_Operation.Status.REMOVED
wenzelm@52931
    51
    current_exec_id = Document_ID.none
wenzelm@52876
    52
  }
wenzelm@52865
    53
wenzelm@52865
    54
  private def remove_overlay()
wenzelm@52865
    55
  {
wenzelm@54310
    56
    current_location match {
wenzelm@54310
    57
      case None =>
wenzelm@54310
    58
      case Some(command) =>
wenzelm@54310
    59
        editor.remove_overlay(command, operation_name, instance :: current_query)
wenzelm@54310
    60
        editor.flush()
wenzelm@54310
    61
    }
wenzelm@52865
    62
  }
wenzelm@52865
    63
wenzelm@52877
    64
wenzelm@52877
    65
  /* content update */
wenzelm@52877
    66
wenzelm@52877
    67
  private def content_update()
wenzelm@52865
    68
  {
wenzelm@52865
    69
    Swing_Thread.require()
wenzelm@52865
    70
wenzelm@52876
    71
wenzelm@52876
    72
    /* snapshot */
wenzelm@52876
    73
wenzelm@52932
    74
    val (snapshot, state, removed) =
wenzelm@52876
    75
      current_location match {
wenzelm@52876
    76
        case Some(cmd) =>
wenzelm@52974
    77
          val snapshot = editor.node_snapshot(cmd.node_name)
wenzelm@52876
    78
          val state = snapshot.state.command_state(snapshot.version, cmd)
wenzelm@52932
    79
          val removed = !snapshot.version.nodes(cmd.node_name).commands.contains(cmd)
wenzelm@52932
    80
          (snapshot, state, removed)
wenzelm@52876
    81
        case None =>
wenzelm@52972
    82
          (Document.Snapshot.init, Command.empty.init_state, true)
wenzelm@52865
    83
      }
wenzelm@52865
    84
wenzelm@52876
    85
    val results =
wenzelm@52876
    86
      (for {
wenzelm@52931
    87
        (_, elem @ XML.Elem(Markup(Markup.RESULT, props), _)) <- state.results.entries
wenzelm@52876
    88
        if props.contains((Markup.INSTANCE, instance))
wenzelm@52931
    89
      } yield elem).toList
wenzelm@52876
    90
wenzelm@52876
    91
wenzelm@52876
    92
    /* output */
wenzelm@52876
    93
wenzelm@52865
    94
    val new_output =
wenzelm@52876
    95
      for {
wenzelm@52931
    96
        XML.Elem(_, List(XML.Elem(markup, body))) <- results
wenzelm@52876
    97
        if Markup.messages.contains(markup.name)
wenzelm@52931
    98
      } yield XML.Elem(Markup(Markup.message(markup.name), markup.properties), body)
wenzelm@52876
    99
wenzelm@52876
   100
wenzelm@52876
   101
    /* status */
wenzelm@52865
   102
wenzelm@52935
   103
    def get_status(name: String, status: Query_Operation.Status.Value)
wenzelm@52935
   104
        : Option[Query_Operation.Status.Value] =
wenzelm@52931
   105
      results.collectFirst({ case XML.Elem(_, List(elem: XML.Elem)) if elem.name == name => status })
wenzelm@52876
   106
wenzelm@52876
   107
    val new_status =
wenzelm@53872
   108
      if (removed) Query_Operation.Status.REMOVED
wenzelm@52932
   109
      else
wenzelm@52935
   110
        get_status(Markup.FINISHED, Query_Operation.Status.FINISHED) orElse
wenzelm@52935
   111
        get_status(Markup.RUNNING, Query_Operation.Status.RUNNING) getOrElse
wenzelm@52935
   112
        Query_Operation.Status.WAITING
wenzelm@52876
   113
wenzelm@52935
   114
    if (new_status == Query_Operation.Status.RUNNING)
wenzelm@52931
   115
      results.collectFirst(
wenzelm@52931
   116
      {
wenzelm@52931
   117
        case XML.Elem(Markup(_, Position.Id(id)), List(elem: XML.Elem))
wenzelm@52931
   118
        if elem.name == Markup.RUNNING => id
wenzelm@52931
   119
      }).foreach(id => current_exec_id = id)
wenzelm@52931
   120
wenzelm@52876
   121
wenzelm@52876
   122
    /* state update */
wenzelm@52865
   123
wenzelm@52876
   124
    if (current_output != new_output || current_status != new_status) {
wenzelm@52876
   125
      if (snapshot.is_outdated)
wenzelm@52876
   126
        current_update_pending = true
wenzelm@52876
   127
      else {
wenzelm@52877
   128
        current_update_pending = false
wenzelm@52932
   129
        if (current_output != new_output && !removed) {
wenzelm@52877
   130
          current_output = new_output
wenzelm@52932
   131
          consume_output(snapshot, state.results, new_output)
wenzelm@52877
   132
        }
wenzelm@52877
   133
        if (current_status != new_status) {
wenzelm@52877
   134
          current_status = new_status
wenzelm@52935
   135
          consume_status(new_status)
wenzelm@54310
   136
          if (new_status == Query_Operation.Status.REMOVED)
wenzelm@52877
   137
            remove_overlay()
wenzelm@52877
   138
        }
wenzelm@52876
   139
      }
wenzelm@52865
   140
    }
wenzelm@52865
   141
  }
wenzelm@52865
   142
wenzelm@52877
   143
wenzelm@52931
   144
  /* query operations */
wenzelm@52931
   145
wenzelm@52931
   146
  def cancel_query(): Unit =
wenzelm@52971
   147
    Swing_Thread.require { editor.session.cancel_exec(current_exec_id) }
wenzelm@52877
   148
wenzelm@52865
   149
  def apply_query(query: List[String])
wenzelm@52865
   150
  {
wenzelm@52865
   151
    Swing_Thread.require()
wenzelm@52865
   152
wenzelm@52980
   153
    editor.current_node_snapshot(editor_context) match {
wenzelm@52978
   154
      case Some(snapshot) =>
wenzelm@52865
   155
        remove_overlay()
wenzelm@52876
   156
        reset_state()
wenzelm@52972
   157
        consume_output(Document.Snapshot.init, Command.Results.empty, Nil)
wenzelm@52980
   158
        editor.current_command(editor_context, snapshot) match {
wenzelm@53844
   159
          case Some(command) =>
wenzelm@52865
   160
            current_location = Some(command)
wenzelm@52865
   161
            current_query = query
wenzelm@52935
   162
            current_status = Query_Operation.Status.WAITING
wenzelm@52977
   163
            editor.insert_overlay(command, operation_name, instance :: query)
wenzelm@52865
   164
          case None =>
wenzelm@52865
   165
        }
wenzelm@52935
   166
        consume_status(current_status)
wenzelm@52974
   167
        editor.flush()
wenzelm@52865
   168
      case None =>
wenzelm@52865
   169
    }
wenzelm@52865
   170
  }
wenzelm@52865
   171
wenzelm@52865
   172
  def locate_query()
wenzelm@52865
   173
  {
wenzelm@52865
   174
    Swing_Thread.require()
wenzelm@52865
   175
wenzelm@52980
   176
    for {
wenzelm@52980
   177
      command <- current_location
wenzelm@52980
   178
      snapshot = editor.node_snapshot(command.node_name)
wenzelm@52980
   179
      link <- editor.hyperlink_command(snapshot, command)
wenzelm@52980
   180
    } link.follow(editor_context)
wenzelm@52865
   181
  }
wenzelm@52865
   182
wenzelm@52865
   183
wenzelm@52865
   184
  /* main actor */
wenzelm@52865
   185
wenzelm@52865
   186
  private val main_actor = actor {
wenzelm@52865
   187
    loop {
wenzelm@52865
   188
      react {
wenzelm@52865
   189
        case changed: Session.Commands_Changed =>
wenzelm@52865
   190
          current_location match {
wenzelm@52876
   191
            case Some(command)
wenzelm@52876
   192
            if current_update_pending ||
wenzelm@53872
   193
              (current_status != Query_Operation.Status.REMOVED &&
wenzelm@52935
   194
                changed.commands.contains(command)) =>
wenzelm@52877
   195
              Swing_Thread.later { content_update() }
wenzelm@52865
   196
            case _ =>
wenzelm@52865
   197
          }
wenzelm@52865
   198
        case bad =>
wenzelm@52865
   199
          java.lang.System.err.println("Query_Operation: ignoring bad message " + bad)
wenzelm@52865
   200
      }
wenzelm@52865
   201
    }
wenzelm@52865
   202
  }
wenzelm@52865
   203
wenzelm@53840
   204
  def activate() {
wenzelm@53840
   205
    editor.session.commands_changed += main_actor
wenzelm@53840
   206
  }
wenzelm@53000
   207
wenzelm@53000
   208
  def deactivate() {
wenzelm@53000
   209
    editor.session.commands_changed -= main_actor
wenzelm@53000
   210
    remove_overlay()
wenzelm@53000
   211
  }
wenzelm@52865
   212
}