src/Tools/jEdit/src/query_operation.scala
author wenzelm
Mon Aug 05 17:14:02 2013 +0200 (2013-08-05 ago)
changeset 52865 02a7e7180ee5
child 52874 91032244e4ca
permissions -rw-r--r--
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
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@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@52865
    20
  def apply(
wenzelm@52865
    21
      view: View,
wenzelm@52865
    22
      name: String,
wenzelm@52865
    23
      consume: (Document.Snapshot, Command.Results, XML.Body) => Unit) =
wenzelm@52865
    24
    new Query_Operation(view, name, consume)
wenzelm@52865
    25
}
wenzelm@52865
    26
wenzelm@52865
    27
final class Query_Operation private(
wenzelm@52865
    28
  view: View,
wenzelm@52865
    29
  operation_name: String,
wenzelm@52865
    30
  consume_result: (Document.Snapshot, Command.Results, XML.Body) => Unit)
wenzelm@52865
    31
{
wenzelm@52865
    32
  private val instance = Document_ID.make().toString
wenzelm@52865
    33
wenzelm@52865
    34
wenzelm@52865
    35
  /* implicit state -- owned by Swing thread */
wenzelm@52865
    36
wenzelm@52865
    37
  private var current_location: Option[Command] = None
wenzelm@52865
    38
  private var current_query: List[String] = Nil
wenzelm@52865
    39
  private var current_result = false
wenzelm@52865
    40
  private var current_snapshot = Document.State.init.snapshot()
wenzelm@52865
    41
  private var current_state = Command.empty.init_state
wenzelm@52865
    42
  private var current_output: List[XML.Tree] = Nil
wenzelm@52865
    43
wenzelm@52865
    44
  private def remove_overlay()
wenzelm@52865
    45
  {
wenzelm@52865
    46
    Swing_Thread.require()
wenzelm@52865
    47
wenzelm@52865
    48
    for {
wenzelm@52865
    49
      command <- current_location
wenzelm@52865
    50
      buffer <- JEdit_Lib.jedit_buffer(command.node_name.node)
wenzelm@52865
    51
      model <- PIDE.document_model(buffer)
wenzelm@52865
    52
    } model.remove_overlay(command, operation_name, instance :: current_query)
wenzelm@52865
    53
  }
wenzelm@52865
    54
wenzelm@52865
    55
  private def handle_result()
wenzelm@52865
    56
  {
wenzelm@52865
    57
    Swing_Thread.require()
wenzelm@52865
    58
wenzelm@52865
    59
    val (new_snapshot, new_state) =
wenzelm@52865
    60
      Document_View(view.getTextArea) match {
wenzelm@52865
    61
        case Some(doc_view) =>
wenzelm@52865
    62
          val snapshot = doc_view.model.snapshot()
wenzelm@52865
    63
          current_location match {
wenzelm@52865
    64
            case Some(cmd) =>
wenzelm@52865
    65
              (snapshot, snapshot.state.command_state(snapshot.version, cmd))
wenzelm@52865
    66
            case None =>
wenzelm@52865
    67
              (Document.State.init.snapshot(), Command.empty.init_state)
wenzelm@52865
    68
          }
wenzelm@52865
    69
        case None => (current_snapshot, current_state)
wenzelm@52865
    70
      }
wenzelm@52865
    71
wenzelm@52865
    72
    val new_output =
wenzelm@52865
    73
      (for {
wenzelm@52865
    74
        (_, XML.Elem(Markup(Markup.RESULT, props), List(XML.Elem(markup, body)))) <-
wenzelm@52865
    75
          new_state.results.entries
wenzelm@52865
    76
        if props.contains((Markup.INSTANCE, instance))
wenzelm@52865
    77
      } yield XML.Elem(Markup(Markup.message(markup.name), markup.properties), body)).toList
wenzelm@52865
    78
wenzelm@52865
    79
    if (new_output != current_output)
wenzelm@52865
    80
      consume_result(new_snapshot, new_state.results, new_output)
wenzelm@52865
    81
wenzelm@52865
    82
    if (!new_output.isEmpty) {
wenzelm@52865
    83
      current_result = true
wenzelm@52865
    84
      remove_overlay()
wenzelm@52865
    85
      PIDE.flush_buffers()
wenzelm@52865
    86
    }
wenzelm@52865
    87
wenzelm@52865
    88
    current_snapshot = new_snapshot
wenzelm@52865
    89
    current_state = new_state
wenzelm@52865
    90
    current_output = new_output
wenzelm@52865
    91
  }
wenzelm@52865
    92
wenzelm@52865
    93
  def apply_query(query: List[String])
wenzelm@52865
    94
  {
wenzelm@52865
    95
    Swing_Thread.require()
wenzelm@52865
    96
wenzelm@52865
    97
    Document_View(view.getTextArea) match {
wenzelm@52865
    98
      case Some(doc_view) =>
wenzelm@52865
    99
        val snapshot = doc_view.model.snapshot()
wenzelm@52865
   100
        remove_overlay()
wenzelm@52865
   101
        current_location = None
wenzelm@52865
   102
        current_query = Nil
wenzelm@52865
   103
        current_result = false
wenzelm@52865
   104
        snapshot.node.command_at(doc_view.text_area.getCaretPosition).map(_._1) match {
wenzelm@52865
   105
          case Some(command) =>
wenzelm@52865
   106
            current_location = Some(command)
wenzelm@52865
   107
            current_query = query
wenzelm@52865
   108
            doc_view.model.insert_overlay(command, operation_name, instance :: query)
wenzelm@52865
   109
          case None =>
wenzelm@52865
   110
        }
wenzelm@52865
   111
        PIDE.flush_buffers()
wenzelm@52865
   112
      case None =>
wenzelm@52865
   113
    }
wenzelm@52865
   114
  }
wenzelm@52865
   115
wenzelm@52865
   116
  def locate_query()
wenzelm@52865
   117
  {
wenzelm@52865
   118
    Swing_Thread.require()
wenzelm@52865
   119
wenzelm@52865
   120
    current_location match {
wenzelm@52865
   121
      case Some(command) =>
wenzelm@52865
   122
        val snapshot = PIDE.session.snapshot(command.node_name)
wenzelm@52865
   123
        val commands = snapshot.node.commands
wenzelm@52865
   124
        if (commands.contains(command)) {
wenzelm@52865
   125
          val sources = commands.iterator.takeWhile(_ != command).map(_.source)
wenzelm@52865
   126
          val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
wenzelm@52865
   127
          Hyperlink(command.node_name.node, line, column).follow(view)
wenzelm@52865
   128
        }
wenzelm@52865
   129
      case None =>
wenzelm@52865
   130
    }
wenzelm@52865
   131
  }
wenzelm@52865
   132
wenzelm@52865
   133
wenzelm@52865
   134
  /* main actor */
wenzelm@52865
   135
wenzelm@52865
   136
  private val main_actor = actor {
wenzelm@52865
   137
    loop {
wenzelm@52865
   138
      react {
wenzelm@52865
   139
        case changed: Session.Commands_Changed =>
wenzelm@52865
   140
          current_location match {
wenzelm@52865
   141
            case Some(command) if !current_result && changed.commands.contains(command) =>
wenzelm@52865
   142
              Swing_Thread.later { handle_result() }
wenzelm@52865
   143
            case _ =>
wenzelm@52865
   144
          }
wenzelm@52865
   145
        case bad =>
wenzelm@52865
   146
          java.lang.System.err.println("Query_Operation: ignoring bad message " + bad)
wenzelm@52865
   147
      }
wenzelm@52865
   148
    }
wenzelm@52865
   149
  }
wenzelm@52865
   150
wenzelm@52865
   151
  def activate() { PIDE.session.commands_changed += main_actor }
wenzelm@52865
   152
  def deactivate() { PIDE.session.commands_changed -= main_actor }
wenzelm@52865
   153
}