src/Pure/PIDE/query_operation.scala
author wenzelm
Mon, 27 Oct 2025 10:33:53 +0100
changeset 83411 182728f4e18b
parent 83365 675ec7e6b233
child 83412 b1a472adb667
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52981
c7afd884dfb2 moved generic module to its proper place;
wenzelm
parents: 52980
diff changeset
     1
/*  Title:      Pure/PIDE/query_operation.scala
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     3
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     4
One-shot query operations via asynchronous print functions and temporary
52932
eb7d7c0034b5 removed command location is considered finished, and its overlay removed eventually;
wenzelm
parents: 52931
diff changeset
     5
document overlays.
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     6
*/
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     7
52981
c7afd884dfb2 moved generic module to its proper place;
wenzelm
parents: 52980
diff changeset
     8
package isabelle
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
     9
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    10
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    11
object Query_Operation {
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
    12
  enum Status { case waiting, running, finished }
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    13
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    14
  object State {
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    15
    val empty: State = State()
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    16
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    17
    def make(command: Command, query: List[String]): State =
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    18
      State(instance = Document_ID.make().toString,
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    19
        location = Some(command),
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    20
        query = query,
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
    21
        status = Status.waiting)
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    22
  }
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    23
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    24
  sealed case class State(
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    25
    instance: String = Document_ID.none.toString,
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    26
    location: Option[Command] = None,
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    27
    query: List[String] = Nil,
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    28
    update_pending: Boolean = false,
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    29
    output: List[XML.Tree] = Nil,
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
    30
    status: Status = Status.finished,
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    31
    exec_id: Document_ID.Exec = Document_ID.none)
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    32
}
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    33
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    34
class Query_Operation[Editor_Context](
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    35
  editor: Editor[Editor_Context],
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
    36
  editor_context: Editor_Context,
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    37
  operation_name: String,
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
    38
  consume_status: Query_Operation.Status => Unit,
81394
95b53559af80 clarified signature;
wenzelm
parents: 78595
diff changeset
    39
  consume_output: (Document.Snapshot, Command.Results, List[XML.Elem]) => Unit
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    40
) {
61206
d5aeb401111a more specific name to reduce danger of clash with direct uses of plain Command.print_function;
wenzelm
parents: 60893
diff changeset
    41
  private val print_function = operation_name + "_query"
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    42
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    43
66094
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 65195
diff changeset
    44
  /* implicit state -- owned by editor thread */
52874
91032244e4ca query process animation;
wenzelm
parents: 52865
diff changeset
    45
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    46
  private val current_state = Synchronized(Query_Operation.State.empty)
61210
a3a05d734858 support for auto update via caret focus;
wenzelm
parents: 61206
diff changeset
    47
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    48
  def get_location: Option[Command] = current_state.value.location
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    49
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    50
  private def remove_overlay(): Unit = {
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    51
    val state = current_state.value
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    52
    for (command <- state.location) {
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    53
      editor.remove_overlay(command, print_function, state.instance :: state.query)
54310
6ddeb83eb67a clarified remove_overlay: always flush in order to make sure that apply_query can make a fresh start with the same arguments (see also 6e69f9ca8f1c) -- NB: print functions are idempotent;
wenzelm
parents: 53872
diff changeset
    54
    }
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    55
  }
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    56
52877
9a26ec5739dd tuned -- more explicit type Status.Value;
wenzelm
parents: 52876
diff changeset
    57
9a26ec5739dd tuned -- more explicit type Status.Value;
wenzelm
parents: 52876
diff changeset
    58
  /* content update */
9a26ec5739dd tuned -- more explicit type Status.Value;
wenzelm
parents: 52876
diff changeset
    59
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    60
  private def content_update(): Unit = {
66094
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 65195
diff changeset
    61
    editor.require_dispatcher {}
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    62
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
    63
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
    64
    /* snapshot */
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
    65
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    66
    val state0 = current_state.value
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    67
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    68
    val (snapshot, command_results, results, removed) =
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    69
      state0.location match {
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
    70
        case Some(cmd) =>
52974
908e8a36e975 tuned signature;
wenzelm
parents: 52972
diff changeset
    71
          val snapshot = editor.node_snapshot(cmd.node_name)
65195
wenzelm
parents: 64664
diff changeset
    72
          val command_results = snapshot.command_results(cmd)
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    73
          val results =
83411
wenzelm
parents: 83365
diff changeset
    74
            List.from(
wenzelm
parents: 83365
diff changeset
    75
              for {
wenzelm
parents: 83365
diff changeset
    76
                case (_, elem@XML.Elem(Markup(Markup.RESULT, props), _)) <- command_results.iterator
wenzelm
parents: 83365
diff changeset
    77
                if props.contains((Markup.INSTANCE, state0.instance))
wenzelm
parents: 83365
diff changeset
    78
              } yield elem)
72823
ab1a49ac456b tuned signature;
wenzelm
parents: 66108
diff changeset
    79
          val removed = !snapshot.get_node(cmd.node_name).commands.contains(cmd)
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    80
          (snapshot, command_results, results, removed)
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
    81
        case None =>
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    82
          (Document.Snapshot.init, Command.Results.empty, Nil, true)
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    83
      }
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
    84
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
    85
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
    86
54640
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
    87
    /* resolve sendback: static command id */
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
    88
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    89
    def resolve_sendback(body: XML.Body): XML.Body = {
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
    90
      state0.location match {
54640
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
    91
        case None => body
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
    92
        case Some(command) =>
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
    93
          def resolve(body: XML.Body): XML.Body =
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
    94
            body map {
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
    95
              case XML.Wrapped_Elem(m, b1, b2) => XML.Wrapped_Elem(m, resolve(b1), resolve(b2))
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
    96
              case XML.Elem(Markup(Markup.SENDBACK, props), b) =>
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
    97
                val props1 =
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
    98
                  props.map({
63805
c272680df665 clarified modules;
wenzelm
parents: 61547
diff changeset
    99
                    case (Markup.ID, Value.Long(id)) if id == state0.exec_id =>
c272680df665 clarified modules;
wenzelm
parents: 61547
diff changeset
   100
                      (Markup.ID, Value.Long(command.id))
54640
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
   101
                    case p => p
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
   102
                  })
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
   103
                XML.Elem(Markup(Markup.SENDBACK, props1), resolve(b))
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
   104
              case XML.Elem(m, b) => XML.Elem(m, resolve(b))
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
   105
              case t => t
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
   106
            }
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
   107
          resolve(body)
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
   108
      }
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
   109
    }
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
   110
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
   111
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
   112
    /* output */
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
   113
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   114
    val new_output =
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
   115
      for {
78592
fdfe9b91d96e misc tuning: support "scalac -source 3.3";
wenzelm
parents: 76680
diff changeset
   116
        case XML.Elem(_, List(XML.Elem(markup, body))) <- results
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
   117
        if Markup.messages.contains(markup.name)
54640
bbd2fa353809 back to Status.FINISHED and immediate remove_overlay (reverting 6e69f9ca8f1c), which is important to avoid restart of print function after edits + re-assignment of located command;
wenzelm
parents: 54327
diff changeset
   118
        body1 = resolve_sendback(body)
76680
e95b9c9e17ff clarified signature;
wenzelm
parents: 76022
diff changeset
   119
      } yield Protocol.make_message(body1, markup.name, props = markup.properties)
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
   120
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
   121
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
   122
    /* status */
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   123
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
   124
    def get_status(name: String, status: Query_Operation.Status): Option[Query_Operation.Status] =
52931
ac6648c0c0fb cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents: 52890
diff changeset
   125
      results.collectFirst({ case XML.Elem(_, List(elem: XML.Elem)) if elem.name == name => status })
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
   126
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
   127
    val new_status =
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
   128
      if (removed) Query_Operation.Status.finished
52932
eb7d7c0034b5 removed command location is considered finished, and its overlay removed eventually;
wenzelm
parents: 52931
diff changeset
   129
      else
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
   130
        get_status(Markup.FINISHED, Query_Operation.Status.finished) orElse
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
   131
        get_status(Markup.RUNNING, Query_Operation.Status.running) getOrElse
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
   132
        Query_Operation.Status.waiting
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
   133
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   134
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   135
    /* state update */
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   136
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
   137
    if (new_status == Query_Operation.Status.running)
52931
ac6648c0c0fb cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents: 52890
diff changeset
   138
      results.collectFirst(
ac6648c0c0fb cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents: 52890
diff changeset
   139
      {
ac6648c0c0fb cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents: 52890
diff changeset
   140
        case XML.Elem(Markup(_, Position.Id(id)), List(elem: XML.Elem))
ac6648c0c0fb cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents: 52890
diff changeset
   141
        if elem.name == Markup.RUNNING => id
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   142
      }).foreach(id => current_state.change(_.copy(exec_id = id)))
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   143
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   144
    if (state0.output != new_output || state0.status != new_status) {
83411
wenzelm
parents: 83365
diff changeset
   145
      if (snapshot.is_outdated) {
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   146
        current_state.change(_.copy(update_pending = true))
83411
wenzelm
parents: 83365
diff changeset
   147
      }
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
   148
      else {
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   149
        current_state.change(_.copy(update_pending = false))
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   150
        if (state0.output != new_output && !removed) {
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   151
          current_state.change(_.copy(output = new_output))
56299
8201790fdeb9 more careful treatment of multiple command states (eval + prints): merge content that is actually required;
wenzelm
parents: 55618
diff changeset
   152
          consume_output(snapshot, command_results, new_output)
52877
9a26ec5739dd tuned -- more explicit type Status.Value;
wenzelm
parents: 52876
diff changeset
   153
        }
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   154
        if (state0.status != new_status) {
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   155
          current_state.change(_.copy(status = new_status))
52935
6fc13c31c775 more abstract consume_status operation;
wenzelm
parents: 52934
diff changeset
   156
          consume_status(new_status)
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
   157
          if (new_status == Query_Operation.Status.finished)
52877
9a26ec5739dd tuned -- more explicit type Status.Value;
wenzelm
parents: 52876
diff changeset
   158
            remove_overlay()
9a26ec5739dd tuned -- more explicit type Status.Value;
wenzelm
parents: 52876
diff changeset
   159
        }
52876
78989972d5b8 more explicit status for query operation;
wenzelm
parents: 52875
diff changeset
   160
      }
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   161
    }
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   162
  }
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   163
52877
9a26ec5739dd tuned -- more explicit type Status.Value;
wenzelm
parents: 52876
diff changeset
   164
52931
ac6648c0c0fb cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents: 52890
diff changeset
   165
  /* query operations */
ac6648c0c0fb cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents: 52890
diff changeset
   166
ac6648c0c0fb cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents: 52890
diff changeset
   167
  def cancel_query(): Unit =
66094
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 65195
diff changeset
   168
    editor.require_dispatcher { editor.session.cancel_exec(current_state.value.exec_id) }
52877
9a26ec5739dd tuned -- more explicit type Status.Value;
wenzelm
parents: 52876
diff changeset
   169
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   170
  def apply_query(query: List[String]): Unit = {
83365
675ec7e6b233 avoid somewhat adhoc change to query_operation.scala (see also 486e094b676c);
wenzelm
parents: 83363
diff changeset
   171
    editor.require_dispatcher {}
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   172
83365
675ec7e6b233 avoid somewhat adhoc change to query_operation.scala (see also 486e094b676c);
wenzelm
parents: 83363
diff changeset
   173
    editor.current_node_snapshot(editor_context) match {
675ec7e6b233 avoid somewhat adhoc change to query_operation.scala (see also 486e094b676c);
wenzelm
parents: 83363
diff changeset
   174
      case Some(snapshot) =>
675ec7e6b233 avoid somewhat adhoc change to query_operation.scala (see also 486e094b676c);
wenzelm
parents: 83363
diff changeset
   175
        remove_overlay()
675ec7e6b233 avoid somewhat adhoc change to query_operation.scala (see also 486e094b676c);
wenzelm
parents: 83363
diff changeset
   176
        current_state.change(_ => Query_Operation.State.empty)
675ec7e6b233 avoid somewhat adhoc change to query_operation.scala (see also 486e094b676c);
wenzelm
parents: 83363
diff changeset
   177
        consume_output(Document.Snapshot.init, Command.Results.empty, Nil)
66108
8b433b6f302f more permissive: avoid situations where query is silently ignored;
wenzelm
parents: 66094
diff changeset
   178
8b433b6f302f more permissive: avoid situations where query is silently ignored;
wenzelm
parents: 66094
diff changeset
   179
        editor.current_command(editor_context, snapshot) match {
8b433b6f302f more permissive: avoid situations where query is silently ignored;
wenzelm
parents: 66094
diff changeset
   180
          case Some(command) =>
8b433b6f302f more permissive: avoid situations where query is silently ignored;
wenzelm
parents: 66094
diff changeset
   181
            val state = Query_Operation.State.make(command, query)
8b433b6f302f more permissive: avoid situations where query is silently ignored;
wenzelm
parents: 66094
diff changeset
   182
            current_state.change(_ => state)
8b433b6f302f more permissive: avoid situations where query is silently ignored;
wenzelm
parents: 66094
diff changeset
   183
            editor.insert_overlay(command, print_function, state.instance :: query)
8b433b6f302f more permissive: avoid situations where query is silently ignored;
wenzelm
parents: 66094
diff changeset
   184
          case None =>
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   185
        }
66108
8b433b6f302f more permissive: avoid situations where query is silently ignored;
wenzelm
parents: 66094
diff changeset
   186
83365
675ec7e6b233 avoid somewhat adhoc change to query_operation.scala (see also 486e094b676c);
wenzelm
parents: 83363
diff changeset
   187
        consume_status(current_state.value.status)
675ec7e6b233 avoid somewhat adhoc change to query_operation.scala (see also 486e094b676c);
wenzelm
parents: 83363
diff changeset
   188
        editor.flush()
675ec7e6b233 avoid somewhat adhoc change to query_operation.scala (see also 486e094b676c);
wenzelm
parents: 83363
diff changeset
   189
      case None =>
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   190
    }
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   191
  }
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   192
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   193
  def locate_query(): Unit = {
66094
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 65195
diff changeset
   194
    editor.require_dispatcher {}
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   195
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   196
    val state = current_state.value
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   197
    for {
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   198
      command <- state.location
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   199
      snapshot = editor.node_snapshot(command.node_name)
64664
wenzelm
parents: 63805
diff changeset
   200
      link <- editor.hyperlink_command(true, snapshot, command.id)
52980
28f59ca8ce78 manage hyperlinks via PIDE editor interface;
wenzelm
parents: 52978
diff changeset
   201
    } link.follow(editor_context)
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   202
  }
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   203
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   204
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   205
  /* main */
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   206
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   207
  private val main =
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   208
    Session.Consumer[Session.Commands_Changed](getClass.getName) {
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   209
      case changed =>
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   210
        val state = current_state.value
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   211
        state.location match {
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   212
          case Some(command)
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   213
          if state.update_pending ||
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
   214
            (state.status != Query_Operation.Status.finished &&
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   215
              changed.commands.contains(command)) =>
66094
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 65195
diff changeset
   216
            editor.send_dispatcher { content_update() }
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   217
          case _ =>
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   218
        }
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   219
    }
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   220
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   221
  def activate(): Unit = {
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   222
    editor.session.commands_changed += main
53840
wenzelm
parents: 53000
diff changeset
   223
  }
53000
50d06647cf24 more cleanup;
wenzelm
parents: 52981
diff changeset
   224
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
   225
  def deactivate(): Unit = {
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   226
    editor.session.commands_changed -= main
53000
50d06647cf24 more cleanup;
wenzelm
parents: 52981
diff changeset
   227
    remove_overlay()
61545
57000ac6291f clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents: 61210
diff changeset
   228
    current_state.change(_ => Query_Operation.State.empty)
54327
148903e47b26 more consistent state and GUI update, e.g. relevant for full-screen mode switch with its exit/init side-effect;
wenzelm
parents: 54325
diff changeset
   229
    consume_output(Document.Snapshot.init, Command.Results.empty, Nil)
78595
b0abf5a9dada clarified signature: prefer enum types;
wenzelm
parents: 78592
diff changeset
   230
    consume_status(Query_Operation.Status.finished)
53000
50d06647cf24 more cleanup;
wenzelm
parents: 52981
diff changeset
   231
  }
52865
02a7e7180ee5 slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff changeset
   232
}