src/Tools/jEdit/src/query_operation.scala
author wenzelm
Fri Aug 09 13:51:34 2013 +0200 (2013-08-09 ago)
changeset 52935 6fc13c31c775
parent 52934 bfb6873df88e
child 52971 31926d2c04ee
permissions -rw-r--r--
more abstract consume_status operation;
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
import org.gjt.sp.jedit.View
wenzelm@52865
    16
wenzelm@52865
    17
wenzelm@52865
    18
object Query_Operation
wenzelm@52865
    19
{
wenzelm@52935
    20
  object Status extends Enumeration
wenzelm@52935
    21
  {
wenzelm@52935
    22
    val WAITING = Value("waiting")
wenzelm@52935
    23
    val RUNNING = Value("running")
wenzelm@52935
    24
    val FINISHED = Value("finished")
wenzelm@52935
    25
  }
wenzelm@52935
    26
wenzelm@52865
    27
  def apply(
wenzelm@52865
    28
      view: View,
wenzelm@52932
    29
      operation_name: String,
wenzelm@52935
    30
      consume_status: Status.Value => Unit,
wenzelm@52932
    31
      consume_output: (Document.Snapshot, Command.Results, XML.Body) => Unit) =
wenzelm@52935
    32
    new Query_Operation(view, operation_name, consume_status, consume_output)
wenzelm@52865
    33
}
wenzelm@52865
    34
wenzelm@52865
    35
final class Query_Operation private(
wenzelm@52865
    36
  view: View,
wenzelm@52865
    37
  operation_name: String,
wenzelm@52935
    38
  consume_status: Query_Operation.Status.Value => Unit,
wenzelm@52932
    39
  consume_output: (Document.Snapshot, Command.Results, XML.Body) => Unit)
wenzelm@52865
    40
{
wenzelm@52865
    41
  private val instance = Document_ID.make().toString
wenzelm@52865
    42
wenzelm@52865
    43
wenzelm@52877
    44
  /* implicit state -- owned by Swing thread */
wenzelm@52874
    45
wenzelm@52865
    46
  private var current_location: Option[Command] = None
wenzelm@52865
    47
  private var current_query: List[String] = Nil
wenzelm@52876
    48
  private var current_update_pending = false
wenzelm@52865
    49
  private var current_output: List[XML.Tree] = Nil
wenzelm@52935
    50
  private var current_status = Query_Operation.Status.FINISHED
wenzelm@52931
    51
  private var current_exec_id = Document_ID.none
wenzelm@52876
    52
wenzelm@52876
    53
  private def reset_state()
wenzelm@52876
    54
  {
wenzelm@52876
    55
    current_location = None
wenzelm@52876
    56
    current_query = Nil
wenzelm@52876
    57
    current_update_pending = false
wenzelm@52876
    58
    current_output = Nil
wenzelm@52935
    59
    current_status = Query_Operation.Status.FINISHED
wenzelm@52931
    60
    current_exec_id = Document_ID.none
wenzelm@52876
    61
  }
wenzelm@52865
    62
wenzelm@52865
    63
  private def remove_overlay()
wenzelm@52865
    64
  {
wenzelm@52865
    65
    Swing_Thread.require()
wenzelm@52865
    66
wenzelm@52865
    67
    for {
wenzelm@52865
    68
      command <- current_location
wenzelm@52865
    69
      buffer <- JEdit_Lib.jedit_buffer(command.node_name.node)
wenzelm@52865
    70
      model <- PIDE.document_model(buffer)
wenzelm@52865
    71
    } model.remove_overlay(command, operation_name, instance :: current_query)
wenzelm@52865
    72
  }
wenzelm@52865
    73
wenzelm@52877
    74
wenzelm@52877
    75
  /* content update */
wenzelm@52877
    76
wenzelm@52877
    77
  private def content_update()
wenzelm@52865
    78
  {
wenzelm@52865
    79
    Swing_Thread.require()
wenzelm@52865
    80
wenzelm@52876
    81
wenzelm@52876
    82
    /* snapshot */
wenzelm@52876
    83
wenzelm@52932
    84
    val (snapshot, state, removed) =
wenzelm@52876
    85
      current_location match {
wenzelm@52876
    86
        case Some(cmd) =>
wenzelm@52876
    87
          val snapshot = PIDE.document_snapshot(cmd.node_name)
wenzelm@52876
    88
          val state = snapshot.state.command_state(snapshot.version, cmd)
wenzelm@52932
    89
          val removed = !snapshot.version.nodes(cmd.node_name).commands.contains(cmd)
wenzelm@52932
    90
          (snapshot, state, removed)
wenzelm@52876
    91
        case None =>
wenzelm@52932
    92
          (Document.State.init.snapshot(), Command.empty.init_state, true)
wenzelm@52865
    93
      }
wenzelm@52865
    94
wenzelm@52876
    95
    val results =
wenzelm@52876
    96
      (for {
wenzelm@52931
    97
        (_, elem @ XML.Elem(Markup(Markup.RESULT, props), _)) <- state.results.entries
wenzelm@52876
    98
        if props.contains((Markup.INSTANCE, instance))
wenzelm@52931
    99
      } yield elem).toList
wenzelm@52876
   100
wenzelm@52876
   101
wenzelm@52876
   102
    /* output */
wenzelm@52876
   103
wenzelm@52865
   104
    val new_output =
wenzelm@52876
   105
      for {
wenzelm@52931
   106
        XML.Elem(_, List(XML.Elem(markup, body))) <- results
wenzelm@52876
   107
        if Markup.messages.contains(markup.name)
wenzelm@52931
   108
      } yield XML.Elem(Markup(Markup.message(markup.name), markup.properties), body)
wenzelm@52876
   109
wenzelm@52876
   110
wenzelm@52876
   111
    /* status */
wenzelm@52865
   112
wenzelm@52935
   113
    def get_status(name: String, status: Query_Operation.Status.Value)
wenzelm@52935
   114
        : Option[Query_Operation.Status.Value] =
wenzelm@52931
   115
      results.collectFirst({ case XML.Elem(_, List(elem: XML.Elem)) if elem.name == name => status })
wenzelm@52876
   116
wenzelm@52876
   117
    val new_status =
wenzelm@52935
   118
      if (removed) Query_Operation.Status.FINISHED
wenzelm@52932
   119
      else
wenzelm@52935
   120
        get_status(Markup.FINISHED, Query_Operation.Status.FINISHED) orElse
wenzelm@52935
   121
        get_status(Markup.RUNNING, Query_Operation.Status.RUNNING) getOrElse
wenzelm@52935
   122
        Query_Operation.Status.WAITING
wenzelm@52876
   123
wenzelm@52935
   124
    if (new_status == Query_Operation.Status.RUNNING)
wenzelm@52931
   125
      results.collectFirst(
wenzelm@52931
   126
      {
wenzelm@52931
   127
        case XML.Elem(Markup(_, Position.Id(id)), List(elem: XML.Elem))
wenzelm@52931
   128
        if elem.name == Markup.RUNNING => id
wenzelm@52931
   129
      }).foreach(id => current_exec_id = id)
wenzelm@52931
   130
wenzelm@52876
   131
wenzelm@52876
   132
    /* state update */
wenzelm@52865
   133
wenzelm@52876
   134
    if (current_output != new_output || current_status != new_status) {
wenzelm@52876
   135
      if (snapshot.is_outdated)
wenzelm@52876
   136
        current_update_pending = true
wenzelm@52876
   137
      else {
wenzelm@52877
   138
        current_update_pending = false
wenzelm@52932
   139
        if (current_output != new_output && !removed) {
wenzelm@52877
   140
          current_output = new_output
wenzelm@52932
   141
          consume_output(snapshot, state.results, new_output)
wenzelm@52877
   142
        }
wenzelm@52877
   143
        if (current_status != new_status) {
wenzelm@52877
   144
          current_status = new_status
wenzelm@52935
   145
          consume_status(new_status)
wenzelm@52935
   146
          if (new_status == Query_Operation.Status.FINISHED) {
wenzelm@52877
   147
            remove_overlay()
wenzelm@52877
   148
            PIDE.flush_buffers()
wenzelm@52876
   149
          }
wenzelm@52877
   150
        }
wenzelm@52876
   151
      }
wenzelm@52865
   152
    }
wenzelm@52865
   153
  }
wenzelm@52865
   154
wenzelm@52877
   155
wenzelm@52931
   156
  /* query operations */
wenzelm@52931
   157
wenzelm@52931
   158
  def cancel_query(): Unit =
wenzelm@52931
   159
    Swing_Thread.require { PIDE.session.cancel_exec(current_exec_id) }
wenzelm@52877
   160
wenzelm@52865
   161
  def apply_query(query: List[String])
wenzelm@52865
   162
  {
wenzelm@52865
   163
    Swing_Thread.require()
wenzelm@52865
   164
wenzelm@52865
   165
    Document_View(view.getTextArea) match {
wenzelm@52865
   166
      case Some(doc_view) =>
wenzelm@52865
   167
        val snapshot = doc_view.model.snapshot()
wenzelm@52865
   168
        remove_overlay()
wenzelm@52876
   169
        reset_state()
wenzelm@52932
   170
        consume_output(Document.State.init.snapshot(), Command.Results.empty, Nil)
wenzelm@52865
   171
        snapshot.node.command_at(doc_view.text_area.getCaretPosition).map(_._1) match {
wenzelm@52865
   172
          case Some(command) =>
wenzelm@52865
   173
            current_location = Some(command)
wenzelm@52865
   174
            current_query = query
wenzelm@52935
   175
            current_status = Query_Operation.Status.WAITING
wenzelm@52865
   176
            doc_view.model.insert_overlay(command, operation_name, instance :: query)
wenzelm@52865
   177
          case None =>
wenzelm@52865
   178
        }
wenzelm@52935
   179
        consume_status(current_status)
wenzelm@52865
   180
        PIDE.flush_buffers()
wenzelm@52865
   181
      case None =>
wenzelm@52865
   182
    }
wenzelm@52865
   183
  }
wenzelm@52865
   184
wenzelm@52865
   185
  def locate_query()
wenzelm@52865
   186
  {
wenzelm@52865
   187
    Swing_Thread.require()
wenzelm@52865
   188
wenzelm@52865
   189
    current_location match {
wenzelm@52865
   190
      case Some(command) =>
wenzelm@52876
   191
        val snapshot = PIDE.document_snapshot(command.node_name)
wenzelm@52865
   192
        val commands = snapshot.node.commands
wenzelm@52865
   193
        if (commands.contains(command)) {
wenzelm@52876
   194
          // FIXME revert offset (!?)
wenzelm@52865
   195
          val sources = commands.iterator.takeWhile(_ != command).map(_.source)
wenzelm@52865
   196
          val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
wenzelm@52865
   197
          Hyperlink(command.node_name.node, line, column).follow(view)
wenzelm@52865
   198
        }
wenzelm@52865
   199
      case None =>
wenzelm@52865
   200
    }
wenzelm@52865
   201
  }
wenzelm@52865
   202
wenzelm@52865
   203
wenzelm@52865
   204
  /* main actor */
wenzelm@52865
   205
wenzelm@52865
   206
  private val main_actor = actor {
wenzelm@52865
   207
    loop {
wenzelm@52865
   208
      react {
wenzelm@52865
   209
        case changed: Session.Commands_Changed =>
wenzelm@52865
   210
          current_location match {
wenzelm@52876
   211
            case Some(command)
wenzelm@52876
   212
            if current_update_pending ||
wenzelm@52935
   213
              (current_status != Query_Operation.Status.FINISHED &&
wenzelm@52935
   214
                changed.commands.contains(command)) =>
wenzelm@52877
   215
              Swing_Thread.later { content_update() }
wenzelm@52865
   216
            case _ =>
wenzelm@52865
   217
          }
wenzelm@52865
   218
        case bad =>
wenzelm@52865
   219
          java.lang.System.err.println("Query_Operation: ignoring bad message " + bad)
wenzelm@52865
   220
      }
wenzelm@52865
   221
    }
wenzelm@52865
   222
  }
wenzelm@52865
   223
wenzelm@52865
   224
  def activate() { PIDE.session.commands_changed += main_actor }
wenzelm@52865
   225
  def deactivate() { PIDE.session.commands_changed -= main_actor }
wenzelm@52865
   226
}