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