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