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