src/Tools/jEdit/src/query_operation.scala
author wenzelm
Fri Aug 09 11:18:36 2013 +0200 (2013-08-09 ago)
changeset 52931 ac6648c0c0fb
parent 52890 36e2c0c308eb
child 52932 eb7d7c0034b5
permissions -rw-r--r--
cancel_query via direct access to the exec_id of the running query process;
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@52865
     5
document overlay.
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@52874
    14
import scala.swing.Label
wenzelm@52865
    15
wenzelm@52865
    16
import org.gjt.sp.jedit.View
wenzelm@52874
    17
import org.gjt.sp.jedit.gui.AnimatedIcon
wenzelm@52865
    18
wenzelm@52865
    19
wenzelm@52865
    20
object Query_Operation
wenzelm@52865
    21
{
wenzelm@52865
    22
  def apply(
wenzelm@52865
    23
      view: View,
wenzelm@52865
    24
      name: String,
wenzelm@52865
    25
      consume: (Document.Snapshot, Command.Results, XML.Body) => Unit) =
wenzelm@52865
    26
    new Query_Operation(view, name, consume)
wenzelm@52865
    27
}
wenzelm@52865
    28
wenzelm@52865
    29
final class Query_Operation private(
wenzelm@52865
    30
  view: View,
wenzelm@52865
    31
  operation_name: String,
wenzelm@52865
    32
  consume_result: (Document.Snapshot, Command.Results, XML.Body) => Unit)
wenzelm@52865
    33
{
wenzelm@52865
    34
  private val instance = Document_ID.make().toString
wenzelm@52865
    35
wenzelm@52865
    36
wenzelm@52877
    37
  /* implicit state -- owned by Swing thread */
wenzelm@52874
    38
wenzelm@52877
    39
  private object Status extends Enumeration
wenzelm@52875
    40
  {
wenzelm@52877
    41
    val WAITING = Value("waiting")
wenzelm@52877
    42
    val RUNNING = Value("running")
wenzelm@52877
    43
    val FINISHED = Value("finished")
wenzelm@52875
    44
  }
wenzelm@52875
    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@52877
    50
  private var current_status = 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@52877
    59
    current_status = 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
  /* animation */
wenzelm@52877
    76
wenzelm@52877
    77
  val animation = new Label
wenzelm@52877
    78
wenzelm@52877
    79
  private val passive_icon =
wenzelm@52877
    80
    JEdit_Lib.load_image_icon(PIDE.options.string("process_passive_icon")).getImage
wenzelm@52877
    81
  private val active_icons =
wenzelm@52877
    82
    space_explode(':', PIDE.options.string("process_active_icons")).map(name =>
wenzelm@52877
    83
      JEdit_Lib.load_image_icon(name).getImage)
wenzelm@52877
    84
wenzelm@52877
    85
  private val animation_icon =
wenzelm@52877
    86
    new AnimatedIcon(passive_icon, active_icons.toArray, 5, animation.peer)
wenzelm@52877
    87
  animation.icon = animation_icon
wenzelm@52877
    88
wenzelm@52877
    89
  private def animation_update()
wenzelm@52877
    90
  {
wenzelm@52877
    91
    animation_icon.stop
wenzelm@52878
    92
    current_status match {
wenzelm@52878
    93
      case Status.WAITING =>
wenzelm@52878
    94
        animation.tooltip = "Waiting for evaluation of query context ..."
wenzelm@52878
    95
        animation_icon.setRate(5)
wenzelm@52878
    96
        animation_icon.start
wenzelm@52878
    97
      case Status.RUNNING =>
wenzelm@52878
    98
        animation.tooltip = "Running query operation ..."
wenzelm@52878
    99
        animation_icon.setRate(15)
wenzelm@52878
   100
        animation_icon.start
wenzelm@52878
   101
      case Status.FINISHED =>
wenzelm@52878
   102
        animation.tooltip = null
wenzelm@52877
   103
    }
wenzelm@52877
   104
  }
wenzelm@52877
   105
wenzelm@52877
   106
wenzelm@52877
   107
  /* content update */
wenzelm@52877
   108
wenzelm@52877
   109
  private def content_update()
wenzelm@52865
   110
  {
wenzelm@52865
   111
    Swing_Thread.require()
wenzelm@52865
   112
wenzelm@52876
   113
wenzelm@52876
   114
    /* snapshot */
wenzelm@52876
   115
wenzelm@52876
   116
    val (snapshot, state) =
wenzelm@52876
   117
      current_location match {
wenzelm@52876
   118
        case Some(cmd) =>
wenzelm@52876
   119
          val snapshot = PIDE.document_snapshot(cmd.node_name)
wenzelm@52876
   120
          val state = snapshot.state.command_state(snapshot.version, cmd)
wenzelm@52876
   121
          (snapshot, state)
wenzelm@52876
   122
        case None =>
wenzelm@52876
   123
          (Document.State.init.snapshot(), Command.empty.init_state)
wenzelm@52865
   124
      }
wenzelm@52865
   125
wenzelm@52876
   126
    val results =
wenzelm@52876
   127
      (for {
wenzelm@52931
   128
        (_, elem @ XML.Elem(Markup(Markup.RESULT, props), _)) <- state.results.entries
wenzelm@52876
   129
        if props.contains((Markup.INSTANCE, instance))
wenzelm@52931
   130
      } yield elem).toList
wenzelm@52876
   131
wenzelm@52876
   132
wenzelm@52876
   133
    /* output */
wenzelm@52876
   134
wenzelm@52865
   135
    val new_output =
wenzelm@52876
   136
      for {
wenzelm@52931
   137
        XML.Elem(_, List(XML.Elem(markup, body))) <- results
wenzelm@52876
   138
        if Markup.messages.contains(markup.name)
wenzelm@52931
   139
      } yield XML.Elem(Markup(Markup.message(markup.name), markup.properties), body)
wenzelm@52876
   140
wenzelm@52876
   141
wenzelm@52876
   142
    /* status */
wenzelm@52865
   143
wenzelm@52877
   144
    def get_status(name: String, status: Status.Value): Option[Status.Value] =
wenzelm@52931
   145
      results.collectFirst({ case XML.Elem(_, List(elem: XML.Elem)) if elem.name == name => status })
wenzelm@52876
   146
wenzelm@52876
   147
    val new_status =
wenzelm@52877
   148
      get_status(Markup.FINISHED, Status.FINISHED) orElse
wenzelm@52877
   149
      get_status(Markup.RUNNING, Status.RUNNING) getOrElse
wenzelm@52877
   150
      Status.WAITING
wenzelm@52876
   151
wenzelm@52931
   152
    if (new_status == Status.RUNNING)
wenzelm@52931
   153
      results.collectFirst(
wenzelm@52931
   154
      {
wenzelm@52931
   155
        case XML.Elem(Markup(_, Position.Id(id)), List(elem: XML.Elem))
wenzelm@52931
   156
        if elem.name == Markup.RUNNING => id
wenzelm@52931
   157
      }).foreach(id => current_exec_id = id)
wenzelm@52931
   158
wenzelm@52876
   159
wenzelm@52876
   160
    /* state update */
wenzelm@52865
   161
wenzelm@52876
   162
    if (current_output != new_output || current_status != new_status) {
wenzelm@52876
   163
      if (snapshot.is_outdated)
wenzelm@52876
   164
        current_update_pending = true
wenzelm@52876
   165
      else {
wenzelm@52877
   166
        current_update_pending = false
wenzelm@52877
   167
        if (current_output != new_output) {
wenzelm@52877
   168
          current_output = new_output
wenzelm@52876
   169
          consume_result(snapshot, state.results, new_output)
wenzelm@52877
   170
        }
wenzelm@52877
   171
        if (current_status != new_status) {
wenzelm@52877
   172
          current_status = new_status
wenzelm@52877
   173
          animation_update()
wenzelm@52877
   174
          if (new_status == Status.FINISHED) {
wenzelm@52877
   175
            remove_overlay()
wenzelm@52877
   176
            PIDE.flush_buffers()
wenzelm@52876
   177
          }
wenzelm@52877
   178
        }
wenzelm@52876
   179
      }
wenzelm@52865
   180
    }
wenzelm@52865
   181
  }
wenzelm@52865
   182
wenzelm@52877
   183
wenzelm@52931
   184
  /* query operations */
wenzelm@52931
   185
wenzelm@52931
   186
  def cancel_query(): Unit =
wenzelm@52931
   187
    Swing_Thread.require { PIDE.session.cancel_exec(current_exec_id) }
wenzelm@52877
   188
wenzelm@52865
   189
  def apply_query(query: List[String])
wenzelm@52865
   190
  {
wenzelm@52865
   191
    Swing_Thread.require()
wenzelm@52865
   192
wenzelm@52865
   193
    Document_View(view.getTextArea) match {
wenzelm@52865
   194
      case Some(doc_view) =>
wenzelm@52865
   195
        val snapshot = doc_view.model.snapshot()
wenzelm@52865
   196
        remove_overlay()
wenzelm@52876
   197
        reset_state()
wenzelm@52865
   198
        snapshot.node.command_at(doc_view.text_area.getCaretPosition).map(_._1) match {
wenzelm@52865
   199
          case Some(command) =>
wenzelm@52865
   200
            current_location = Some(command)
wenzelm@52865
   201
            current_query = query
wenzelm@52877
   202
            current_status = Status.WAITING
wenzelm@52865
   203
            doc_view.model.insert_overlay(command, operation_name, instance :: query)
wenzelm@52865
   204
          case None =>
wenzelm@52865
   205
        }
wenzelm@52877
   206
        animation_update()
wenzelm@52865
   207
        PIDE.flush_buffers()
wenzelm@52865
   208
      case None =>
wenzelm@52865
   209
    }
wenzelm@52865
   210
  }
wenzelm@52865
   211
wenzelm@52865
   212
  def locate_query()
wenzelm@52865
   213
  {
wenzelm@52865
   214
    Swing_Thread.require()
wenzelm@52865
   215
wenzelm@52865
   216
    current_location match {
wenzelm@52865
   217
      case Some(command) =>
wenzelm@52876
   218
        val snapshot = PIDE.document_snapshot(command.node_name)
wenzelm@52865
   219
        val commands = snapshot.node.commands
wenzelm@52865
   220
        if (commands.contains(command)) {
wenzelm@52876
   221
          // FIXME revert offset (!?)
wenzelm@52865
   222
          val sources = commands.iterator.takeWhile(_ != command).map(_.source)
wenzelm@52865
   223
          val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
wenzelm@52865
   224
          Hyperlink(command.node_name.node, line, column).follow(view)
wenzelm@52865
   225
        }
wenzelm@52865
   226
      case None =>
wenzelm@52865
   227
    }
wenzelm@52865
   228
  }
wenzelm@52865
   229
wenzelm@52865
   230
wenzelm@52865
   231
  /* main actor */
wenzelm@52865
   232
wenzelm@52865
   233
  private val main_actor = actor {
wenzelm@52865
   234
    loop {
wenzelm@52865
   235
      react {
wenzelm@52865
   236
        case changed: Session.Commands_Changed =>
wenzelm@52865
   237
          current_location match {
wenzelm@52876
   238
            case Some(command)
wenzelm@52876
   239
            if current_update_pending ||
wenzelm@52877
   240
              (current_status != Status.FINISHED && changed.commands.contains(command)) =>
wenzelm@52877
   241
              Swing_Thread.later { content_update() }
wenzelm@52865
   242
            case _ =>
wenzelm@52865
   243
          }
wenzelm@52865
   244
        case bad =>
wenzelm@52865
   245
          java.lang.System.err.println("Query_Operation: ignoring bad message " + bad)
wenzelm@52865
   246
      }
wenzelm@52865
   247
    }
wenzelm@52865
   248
  }
wenzelm@52865
   249
wenzelm@52865
   250
  def activate() { PIDE.session.commands_changed += main_actor }
wenzelm@52865
   251
  def deactivate() { PIDE.session.commands_changed -= main_actor }
wenzelm@52865
   252
}