src/Tools/jEdit/src/query_operation.scala
author wenzelm
Tue Aug 06 21:34:58 2013 +0200 (2013-08-06 ago)
changeset 52877 9a26ec5739dd
parent 52876 78989972d5b8
child 52878 8069c8b9335e
permissions -rw-r--r--
tuned -- more explicit type Status.Value;
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@52876
    51
wenzelm@52876
    52
  private def reset_state()
wenzelm@52876
    53
  {
wenzelm@52876
    54
    current_location = None
wenzelm@52876
    55
    current_query = Nil
wenzelm@52876
    56
    current_update_pending = false
wenzelm@52876
    57
    current_output = Nil
wenzelm@52877
    58
    current_status = Status.FINISHED
wenzelm@52876
    59
  }
wenzelm@52865
    60
wenzelm@52865
    61
  private def remove_overlay()
wenzelm@52865
    62
  {
wenzelm@52865
    63
    Swing_Thread.require()
wenzelm@52865
    64
wenzelm@52865
    65
    for {
wenzelm@52865
    66
      command <- current_location
wenzelm@52865
    67
      buffer <- JEdit_Lib.jedit_buffer(command.node_name.node)
wenzelm@52865
    68
      model <- PIDE.document_model(buffer)
wenzelm@52865
    69
    } model.remove_overlay(command, operation_name, instance :: current_query)
wenzelm@52865
    70
  }
wenzelm@52865
    71
wenzelm@52877
    72
wenzelm@52877
    73
  /* animation */
wenzelm@52877
    74
wenzelm@52877
    75
  val animation = new Label
wenzelm@52877
    76
wenzelm@52877
    77
  private val passive_icon =
wenzelm@52877
    78
    JEdit_Lib.load_image_icon(PIDE.options.string("process_passive_icon")).getImage
wenzelm@52877
    79
  private val active_icons =
wenzelm@52877
    80
    space_explode(':', PIDE.options.string("process_active_icons")).map(name =>
wenzelm@52877
    81
      JEdit_Lib.load_image_icon(name).getImage)
wenzelm@52877
    82
wenzelm@52877
    83
  private val animation_icon =
wenzelm@52877
    84
    new AnimatedIcon(passive_icon, active_icons.toArray, 5, animation.peer)
wenzelm@52877
    85
  animation.icon = animation_icon
wenzelm@52877
    86
wenzelm@52877
    87
  private def animation_update()
wenzelm@52877
    88
  {
wenzelm@52877
    89
    animation_icon.stop
wenzelm@52877
    90
    if (current_status != Status.FINISHED) {
wenzelm@52877
    91
      animation_icon.setRate(if (current_status == Status.RUNNING) 15 else 5)
wenzelm@52877
    92
      animation_icon.start
wenzelm@52877
    93
    }
wenzelm@52877
    94
  }
wenzelm@52877
    95
wenzelm@52877
    96
wenzelm@52877
    97
  /* content update */
wenzelm@52877
    98
wenzelm@52877
    99
  private def content_update()
wenzelm@52865
   100
  {
wenzelm@52865
   101
    Swing_Thread.require()
wenzelm@52865
   102
wenzelm@52876
   103
wenzelm@52876
   104
    /* snapshot */
wenzelm@52876
   105
wenzelm@52876
   106
    val (snapshot, state) =
wenzelm@52876
   107
      current_location match {
wenzelm@52876
   108
        case Some(cmd) =>
wenzelm@52876
   109
          val snapshot = PIDE.document_snapshot(cmd.node_name)
wenzelm@52876
   110
          val state = snapshot.state.command_state(snapshot.version, cmd)
wenzelm@52876
   111
          (snapshot, state)
wenzelm@52876
   112
        case None =>
wenzelm@52876
   113
          (Document.State.init.snapshot(), Command.empty.init_state)
wenzelm@52865
   114
      }
wenzelm@52865
   115
wenzelm@52876
   116
    val results =
wenzelm@52876
   117
      (for {
wenzelm@52876
   118
        (_, XML.Elem(Markup(Markup.RESULT, props), body)) <- state.results.entries
wenzelm@52876
   119
        if props.contains((Markup.INSTANCE, instance))
wenzelm@52876
   120
      } yield body).toList
wenzelm@52876
   121
wenzelm@52876
   122
wenzelm@52876
   123
    /* output */
wenzelm@52876
   124
wenzelm@52865
   125
    val new_output =
wenzelm@52876
   126
      for {
wenzelm@52876
   127
        List(XML.Elem(markup, body)) <- results
wenzelm@52876
   128
        if Markup.messages.contains(markup.name)
wenzelm@52876
   129
      }
wenzelm@52876
   130
      yield XML.Elem(Markup(Markup.message(markup.name), markup.properties), body)
wenzelm@52876
   131
wenzelm@52876
   132
wenzelm@52876
   133
    /* status */
wenzelm@52865
   134
wenzelm@52877
   135
    def get_status(name: String, status: Status.Value): Option[Status.Value] =
wenzelm@52877
   136
      results.collectFirst({ case List(XML.Elem(m, _)) if m.name == name => status })
wenzelm@52876
   137
wenzelm@52876
   138
    val new_status =
wenzelm@52877
   139
      get_status(Markup.FINISHED, Status.FINISHED) orElse
wenzelm@52877
   140
      get_status(Markup.RUNNING, Status.RUNNING) getOrElse
wenzelm@52877
   141
      Status.WAITING
wenzelm@52876
   142
wenzelm@52876
   143
wenzelm@52876
   144
    /* state update */
wenzelm@52865
   145
wenzelm@52876
   146
    if (current_output != new_output || current_status != new_status) {
wenzelm@52876
   147
      if (snapshot.is_outdated)
wenzelm@52876
   148
        current_update_pending = true
wenzelm@52876
   149
      else {
wenzelm@52877
   150
        current_update_pending = false
wenzelm@52877
   151
        if (current_output != new_output) {
wenzelm@52877
   152
          current_output = new_output
wenzelm@52876
   153
          consume_result(snapshot, state.results, new_output)
wenzelm@52877
   154
        }
wenzelm@52877
   155
        if (current_status != new_status) {
wenzelm@52877
   156
          current_status = new_status
wenzelm@52877
   157
          animation_update()
wenzelm@52877
   158
          if (new_status == Status.FINISHED) {
wenzelm@52877
   159
            remove_overlay()
wenzelm@52877
   160
            PIDE.flush_buffers()
wenzelm@52876
   161
          }
wenzelm@52877
   162
        }
wenzelm@52876
   163
      }
wenzelm@52865
   164
    }
wenzelm@52865
   165
  }
wenzelm@52865
   166
wenzelm@52877
   167
wenzelm@52877
   168
  /* apply query */
wenzelm@52877
   169
wenzelm@52865
   170
  def apply_query(query: List[String])
wenzelm@52865
   171
  {
wenzelm@52865
   172
    Swing_Thread.require()
wenzelm@52865
   173
wenzelm@52865
   174
    Document_View(view.getTextArea) match {
wenzelm@52865
   175
      case Some(doc_view) =>
wenzelm@52865
   176
        val snapshot = doc_view.model.snapshot()
wenzelm@52865
   177
        remove_overlay()
wenzelm@52876
   178
        reset_state()
wenzelm@52865
   179
        snapshot.node.command_at(doc_view.text_area.getCaretPosition).map(_._1) match {
wenzelm@52865
   180
          case Some(command) =>
wenzelm@52865
   181
            current_location = Some(command)
wenzelm@52865
   182
            current_query = query
wenzelm@52877
   183
            current_status = Status.WAITING
wenzelm@52865
   184
            doc_view.model.insert_overlay(command, operation_name, instance :: query)
wenzelm@52865
   185
          case None =>
wenzelm@52865
   186
        }
wenzelm@52877
   187
        animation_update()
wenzelm@52865
   188
        PIDE.flush_buffers()
wenzelm@52865
   189
      case None =>
wenzelm@52865
   190
    }
wenzelm@52865
   191
  }
wenzelm@52865
   192
wenzelm@52877
   193
wenzelm@52877
   194
  /* locate query */
wenzelm@52877
   195
wenzelm@52865
   196
  def locate_query()
wenzelm@52865
   197
  {
wenzelm@52865
   198
    Swing_Thread.require()
wenzelm@52865
   199
wenzelm@52865
   200
    current_location match {
wenzelm@52865
   201
      case Some(command) =>
wenzelm@52876
   202
        val snapshot = PIDE.document_snapshot(command.node_name)
wenzelm@52865
   203
        val commands = snapshot.node.commands
wenzelm@52865
   204
        if (commands.contains(command)) {
wenzelm@52876
   205
          // FIXME revert offset (!?)
wenzelm@52865
   206
          val sources = commands.iterator.takeWhile(_ != command).map(_.source)
wenzelm@52865
   207
          val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
wenzelm@52865
   208
          Hyperlink(command.node_name.node, line, column).follow(view)
wenzelm@52865
   209
        }
wenzelm@52865
   210
      case None =>
wenzelm@52865
   211
    }
wenzelm@52865
   212
  }
wenzelm@52865
   213
wenzelm@52865
   214
wenzelm@52865
   215
  /* main actor */
wenzelm@52865
   216
wenzelm@52865
   217
  private val main_actor = actor {
wenzelm@52865
   218
    loop {
wenzelm@52865
   219
      react {
wenzelm@52865
   220
        case changed: Session.Commands_Changed =>
wenzelm@52865
   221
          current_location match {
wenzelm@52876
   222
            case Some(command)
wenzelm@52876
   223
            if current_update_pending ||
wenzelm@52877
   224
              (current_status != Status.FINISHED && changed.commands.contains(command)) =>
wenzelm@52877
   225
              Swing_Thread.later { content_update() }
wenzelm@52865
   226
            case _ =>
wenzelm@52865
   227
          }
wenzelm@52865
   228
        case bad =>
wenzelm@52865
   229
          java.lang.System.err.println("Query_Operation: ignoring bad message " + bad)
wenzelm@52865
   230
      }
wenzelm@52865
   231
    }
wenzelm@52865
   232
  }
wenzelm@52865
   233
wenzelm@52865
   234
  def activate() { PIDE.session.commands_changed += main_actor }
wenzelm@52865
   235
  def deactivate() { PIDE.session.commands_changed -= main_actor }
wenzelm@52865
   236
}