author | wenzelm |
Tue, 29 Aug 2023 12:53:28 +0200 | |
changeset 78592 | fdfe9b91d96e |
parent 76680 | e95b9c9e17ff |
child 78595 | b0abf5a9dada |
permissions | -rw-r--r-- |
52981 | 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 | 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 | 11 |
object Query_Operation { |
12 |
object Status extends Enumeration { |
|
52935 | 13 |
val WAITING = Value("waiting") |
14 |
val RUNNING = Value("running") |
|
15 |
val FINISHED = Value("finished") |
|
16 |
} |
|
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
17 |
|
75393 | 18 |
object State { |
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
19 |
val empty: State = State() |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
20 |
|
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
21 |
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
|
22 |
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
|
23 |
location = Some(command), |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
24 |
query = query, |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
25 |
status = Status.WAITING) |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
26 |
} |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
27 |
|
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
28 |
sealed case class State( |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
29 |
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
|
30 |
location: Option[Command] = None, |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
31 |
query: List[String] = Nil, |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
32 |
update_pending: Boolean = false, |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
33 |
output: List[XML.Tree] = Nil, |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
34 |
status: Status.Value = Status.FINISHED, |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
35 |
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
|
36 |
} |
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
37 |
|
52980 | 38 |
class Query_Operation[Editor_Context]( |
39 |
editor: Editor[Editor_Context], |
|
40 |
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
|
41 |
operation_name: String, |
52935 | 42 |
consume_status: Query_Operation.Status.Value => Unit, |
75393 | 43 |
consume_output: (Document.Snapshot, Command.Results, XML.Body) => Unit |
44 |
) { |
|
61206
d5aeb401111a
more specific name to reduce danger of clash with direct uses of plain Command.print_function;
wenzelm
parents:
60893
diff
changeset
|
45 |
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
|
46 |
|
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
47 |
|
66094 | 48 |
/* implicit state -- owned by editor thread */ |
52874 | 49 |
|
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
50 |
private val current_state = Synchronized(Query_Operation.State.empty) |
61210 | 51 |
|
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
52 |
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
|
53 |
|
75393 | 54 |
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
|
55 |
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
|
56 |
for (command <- state.location) { |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
57 |
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
|
58 |
} |
52865
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
59 |
} |
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
60 |
|
52877 | 61 |
|
62 |
/* content update */ |
|
63 |
||
75393 | 64 |
private def content_update(): Unit = { |
66094 | 65 |
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
|
66 |
|
52876 | 67 |
|
68 |
/* snapshot */ |
|
69 |
||
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
70 |
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
|
71 |
|
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
72 |
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
|
73 |
state0.location match { |
52876 | 74 |
case Some(cmd) => |
52974 | 75 |
val snapshot = editor.node_snapshot(cmd.node_name) |
65195 | 76 |
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
|
77 |
val results = |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
78 |
(for { |
78592 | 79 |
case (_, elem @ XML.Elem(Markup(Markup.RESULT, props), _)) <- command_results.iterator |
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
80 |
if props.contains((Markup.INSTANCE, state0.instance)) |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
81 |
} yield elem).toList |
72823 | 82 |
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
|
83 |
(snapshot, command_results, results, removed) |
52876 | 84 |
case None => |
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
85 |
(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
|
86 |
} |
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
87 |
|
52876 | 88 |
|
89 |
||
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
|
90 |
/* 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
|
91 |
|
75393 | 92 |
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
|
93 |
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
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
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
|
101 |
props.map({ |
63805 | 102 |
case (Markup.ID, Value.Long(id)) if id == state0.exec_id => |
103 |
(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
|
104 |
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
|
105 |
}) |
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 |
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
|
107 |
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
|
108 |
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
|
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 |
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
|
111 |
} |
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
|
112 |
} |
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
|
113 |
|
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
|
114 |
|
52876 | 115 |
/* output */ |
116 |
||
52865
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
117 |
val new_output = |
52876 | 118 |
for { |
78592 | 119 |
case XML.Elem(_, List(XML.Elem(markup, body))) <- results |
52876 | 120 |
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
|
121 |
body1 = resolve_sendback(body) |
76680 | 122 |
} yield Protocol.make_message(body1, markup.name, props = markup.properties) |
52876 | 123 |
|
124 |
||
125 |
/* status */ |
|
52865
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
126 |
|
52935 | 127 |
def get_status(name: String, status: Query_Operation.Status.Value) |
128 |
: Option[Query_Operation.Status.Value] = |
|
52931
ac6648c0c0fb
cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents:
52890
diff
changeset
|
129 |
results.collectFirst({ case XML.Elem(_, List(elem: XML.Elem)) if elem.name == name => status }) |
52876 | 130 |
|
131 |
val new_status = |
|
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
|
132 |
if (removed) Query_Operation.Status.FINISHED |
52932
eb7d7c0034b5
removed command location is considered finished, and its overlay removed eventually;
wenzelm
parents:
52931
diff
changeset
|
133 |
else |
52935 | 134 |
get_status(Markup.FINISHED, Query_Operation.Status.FINISHED) orElse |
135 |
get_status(Markup.RUNNING, Query_Operation.Status.RUNNING) getOrElse |
|
136 |
Query_Operation.Status.WAITING |
|
52876 | 137 |
|
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
138 |
|
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
139 |
/* state update */ |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
140 |
|
52935 | 141 |
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
|
142 |
results.collectFirst( |
ac6648c0c0fb
cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents:
52890
diff
changeset
|
143 |
{ |
ac6648c0c0fb
cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents:
52890
diff
changeset
|
144 |
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
|
145 |
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
|
146 |
}).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
|
147 |
|
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
148 |
if (state0.output != new_output || state0.status != new_status) { |
52876 | 149 |
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
|
150 |
current_state.change(_.copy(update_pending = true)) |
52876 | 151 |
else { |
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
152 |
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
|
153 |
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
|
154 |
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
|
155 |
consume_output(snapshot, command_results, new_output) |
52877 | 156 |
} |
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
157 |
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
|
158 |
current_state.change(_.copy(status = new_status)) |
52935 | 159 |
consume_status(new_status) |
61547
8494a947fa65
avoid premature flushing and thus flashing of text area;
wenzelm
parents:
61545
diff
changeset
|
160 |
if (new_status == Query_Operation.Status.FINISHED) |
52877 | 161 |
remove_overlay() |
162 |
} |
|
52876 | 163 |
} |
52865
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
164 |
} |
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
165 |
} |
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
166 |
|
52877 | 167 |
|
52931
ac6648c0c0fb
cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents:
52890
diff
changeset
|
168 |
/* query operations */ |
ac6648c0c0fb
cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents:
52890
diff
changeset
|
169 |
|
ac6648c0c0fb
cancel_query via direct access to the exec_id of the running query process;
wenzelm
parents:
52890
diff
changeset
|
170 |
def cancel_query(): Unit = |
66094 | 171 |
editor.require_dispatcher { editor.session.cancel_exec(current_state.value.exec_id) } |
52877 | 172 |
|
75393 | 173 |
def apply_query(query: List[String]): Unit = { |
66094 | 174 |
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
|
175 |
|
52980 | 176 |
editor.current_node_snapshot(editor_context) match { |
52978 | 177 |
case Some(snapshot) => |
52865
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
178 |
remove_overlay() |
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
179 |
current_state.change(_ => Query_Operation.State.empty) |
52972 | 180 |
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
|
181 |
|
8b433b6f302f
more permissive: avoid situations where query is silently ignored;
wenzelm
parents:
66094
diff
changeset
|
182 |
editor.current_command(editor_context, snapshot) match { |
8b433b6f302f
more permissive: avoid situations where query is silently ignored;
wenzelm
parents:
66094
diff
changeset
|
183 |
case Some(command) => |
8b433b6f302f
more permissive: avoid situations where query is silently ignored;
wenzelm
parents:
66094
diff
changeset
|
184 |
val state = Query_Operation.State.make(command, query) |
8b433b6f302f
more permissive: avoid situations where query is silently ignored;
wenzelm
parents:
66094
diff
changeset
|
185 |
current_state.change(_ => state) |
8b433b6f302f
more permissive: avoid situations where query is silently ignored;
wenzelm
parents:
66094
diff
changeset
|
186 |
editor.insert_overlay(command, print_function, state.instance :: query) |
8b433b6f302f
more permissive: avoid situations where query is silently ignored;
wenzelm
parents:
66094
diff
changeset
|
187 |
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
|
188 |
} |
66108
8b433b6f302f
more permissive: avoid situations where query is silently ignored;
wenzelm
parents:
66094
diff
changeset
|
189 |
|
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
190 |
consume_status(current_state.value.status) |
52974 | 191 |
editor.flush() |
52865
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
192 |
case None => |
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
193 |
} |
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
194 |
} |
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
195 |
|
75393 | 196 |
def locate_query(): Unit = { |
66094 | 197 |
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
|
198 |
|
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
199 |
val state = current_state.value |
52980 | 200 |
for { |
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
201 |
command <- state.location |
52980 | 202 |
snapshot = editor.node_snapshot(command.node_name) |
64664 | 203 |
link <- editor.hyperlink_command(true, snapshot, command.id) |
52980 | 204 |
} 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
|
205 |
} |
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
206 |
|
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
207 |
|
56715
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents:
56662
diff
changeset
|
208 |
/* main */ |
52865
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
209 |
|
56715
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents:
56662
diff
changeset
|
210 |
private val main = |
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents:
56662
diff
changeset
|
211 |
Session.Consumer[Session.Commands_Changed](getClass.getName) { |
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents:
56662
diff
changeset
|
212 |
case changed => |
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
213 |
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
|
214 |
state.location match { |
56715
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents:
56662
diff
changeset
|
215 |
case Some(command) |
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
216 |
if state.update_pending || |
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
217 |
(state.status != Query_Operation.Status.FINISHED && |
56715
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents:
56662
diff
changeset
|
218 |
changed.commands.contains(command)) => |
66094 | 219 |
editor.send_dispatcher { content_update() } |
56715
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents:
56662
diff
changeset
|
220 |
case _ => |
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents:
56662
diff
changeset
|
221 |
} |
52865
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
222 |
} |
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
223 |
|
75393 | 224 |
def activate(): Unit = { |
56715
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents:
56662
diff
changeset
|
225 |
editor.session.commands_changed += main |
53840 | 226 |
} |
53000 | 227 |
|
75393 | 228 |
def deactivate(): Unit = { |
56715
52125652e82a
clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents:
56662
diff
changeset
|
229 |
editor.session.commands_changed -= main |
53000 | 230 |
remove_overlay() |
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
231 |
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
|
232 |
consume_output(Document.Snapshot.init, Command.Results.empty, Nil) |
61545
57000ac6291f
clarified Query_Operation.State, with separate instance to avoid extra flush (see also 6ddeb83eb67a);
wenzelm
parents:
61210
diff
changeset
|
233 |
consume_status(Query_Operation.Status.FINISHED) |
53000 | 234 |
} |
52865
02a7e7180ee5
slightly more general support for one-shot query operations via asynchronous print functions and temporary document overlay;
wenzelm
parents:
diff
changeset
|
235 |
} |