author | wenzelm |
Thu, 01 Mar 2012 14:12:18 +0100 | |
changeset 46740 | 852baa599351 |
parent 46739 | 6024353549ca |
child 46748 | 8f3ae4d04a2d |
permissions | -rw-r--r-- |
43282
5d294220ca43
moved sources -- eliminated Netbeans artifact of jedit package directory;
wenzelm
parents:
40851
diff
changeset
|
1 |
/* Title: Tools/jEdit/src/document_model.scala |
36760 | 2 |
Author: Fabian Immler, TU Munich |
3 |
Author: Makarius |
|
4 |
||
38222
dac5fa0ac971
replaced individual Document_Model history by all-inclusive one in Session;
wenzelm
parents:
38158
diff
changeset
|
5 |
Document model connected to jEdit buffer -- single node in theory graph. |
36760 | 6 |
*/ |
34407 | 7 |
|
34784
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
8 |
package isabelle.jedit |
34760 | 9 |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
10 |
|
36015 | 11 |
import isabelle._ |
12 |
||
34693 | 13 |
import scala.collection.mutable |
34446 | 14 |
|
34784
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
15 |
import org.gjt.sp.jedit.Buffer |
34783
cb95d6bbf5f1
clarified BufferListener: use adapter, listen to contentInserted instead of preContentInserted;
wenzelm
parents:
34778
diff
changeset
|
16 |
import org.gjt.sp.jedit.buffer.{BufferAdapter, BufferListener, JEditBuffer} |
38158 | 17 |
import org.gjt.sp.jedit.textarea.TextArea |
18 |
||
19 |
import java.awt.font.TextAttribute |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
20 |
|
34760 | 21 |
|
34784
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
22 |
object Document_Model |
34588 | 23 |
{ |
34784
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
24 |
/* document model of buffer */ |
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
25 |
|
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
26 |
private val key = "isabelle.document_model" |
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
27 |
|
34788
3779c54a2d21
direct apply for Document_Model and Document_View;
wenzelm
parents:
34784
diff
changeset
|
28 |
def apply(buffer: Buffer): Option[Document_Model] = |
34784
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
29 |
{ |
38223 | 30 |
Swing_Thread.require() |
34784
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
31 |
buffer.getProperty(key) match { |
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
32 |
case model: Document_Model => Some(model) |
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
33 |
case _ => None |
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
34 |
} |
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
35 |
} |
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
36 |
|
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
37 |
def exit(buffer: Buffer) |
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
38 |
{ |
38223 | 39 |
Swing_Thread.require() |
34788
3779c54a2d21
direct apply for Document_Model and Document_View;
wenzelm
parents:
34784
diff
changeset
|
40 |
apply(buffer) match { |
39636 | 41 |
case None => |
34784
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
42 |
case Some(model) => |
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
43 |
model.deactivate() |
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
44 |
buffer.unsetProperty(key) |
34653 | 45 |
} |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
46 |
} |
43397 | 47 |
|
44615 | 48 |
def init(session: Session, buffer: Buffer, name: Document.Node.Name): Document_Model = |
43397 | 49 |
{ |
50 |
exit(buffer) |
|
44615 | 51 |
val model = new Document_Model(session, buffer, name) |
43397 | 52 |
buffer.setProperty(key, model) |
53 |
model.activate() |
|
54 |
model |
|
55 |
} |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
56 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
57 |
|
38151 | 58 |
|
44615 | 59 |
class Document_Model(val session: Session, val buffer: Buffer, val name: Document.Node.Name) |
34588 | 60 |
{ |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
61 |
/* header */ |
38222
dac5fa0ac971
replaced individual Document_Model history by all-inclusive one in Session;
wenzelm
parents:
38158
diff
changeset
|
62 |
|
46737 | 63 |
def node_header(): Document.Node_Header = |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
64 |
{ |
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
65 |
Swing_Thread.require() |
46738 | 66 |
if (Isabelle.jedit_buffer(name.node) == Some(buffer)) |
46739
6024353549ca
clarified document nodes (full import graph) vs. node_status (non-preloaded theories);
wenzelm
parents:
46738
diff
changeset
|
67 |
Exn.capture { Isabelle.thy_load.check_thy(name) } |
46738 | 68 |
else Exn.Exn(ERROR("Bad theory header")) // FIXME odd race condition!? |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
69 |
} |
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
70 |
|
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
71 |
|
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
72 |
/* perspective */ |
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
73 |
|
44776
47e8c8daccae
added "check" button: adhoc change to full buffer perspective;
wenzelm
parents:
44615
diff
changeset
|
74 |
def buffer_range(): Text.Range = Text.Range(0, (buffer.getLength - 1) max 0) |
47e8c8daccae
added "check" button: adhoc change to full buffer perspective;
wenzelm
parents:
44615
diff
changeset
|
75 |
|
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
76 |
def perspective(): Text.Perspective = |
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
77 |
{ |
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
78 |
Swing_Thread.require() |
44473 | 79 |
Text.Perspective( |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
80 |
for { |
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
81 |
doc_view <- Isabelle.document_views(buffer) |
44473 | 82 |
range <- doc_view.perspective().ranges |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
83 |
} yield range) |
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
84 |
} |
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
85 |
|
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
86 |
|
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44379
diff
changeset
|
87 |
/* pending text edits */ |
43648 | 88 |
|
43644 | 89 |
private object pending_edits // owned by Swing thread |
38224 | 90 |
{ |
38425 | 91 |
private val pending = new mutable.ListBuffer[Text.Edit] |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
92 |
private var pending_perspective = false |
44473 | 93 |
private var last_perspective: Text.Perspective = Text.Perspective.empty |
44438 | 94 |
|
38425 | 95 |
def snapshot(): List[Text.Edit] = pending.toList |
38224 | 96 |
|
43648 | 97 |
def flush() |
38224 | 98 |
{ |
99 |
Swing_Thread.require() |
|
44438 | 100 |
|
101 |
val new_perspective = |
|
102 |
if (pending_perspective) { pending_perspective = false; perspective() } |
|
103 |
else last_perspective |
|
104 |
||
43648 | 105 |
snapshot() match { |
44479
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents:
44473
diff
changeset
|
106 |
case Nil if last_perspective == new_perspective => |
43648 | 107 |
case edits => |
108 |
pending.clear |
|
44438 | 109 |
last_perspective = new_perspective |
44615 | 110 |
session.edit_node(name, node_header(), new_perspective, edits) |
43648 | 111 |
} |
38224 | 112 |
} |
113 |
||
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40474
diff
changeset
|
114 |
private val delay_flush = |
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40474
diff
changeset
|
115 |
Swing_Thread.delay_last(session.input_delay) { flush() } |
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40474
diff
changeset
|
116 |
|
38425 | 117 |
def +=(edit: Text.Edit) |
38224 | 118 |
{ |
119 |
Swing_Thread.require() |
|
120 |
pending += edit |
|
46740
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
121 |
delay_flush(true) |
38222
dac5fa0ac971
replaced individual Document_Model history by all-inclusive one in Session;
wenzelm
parents:
38158
diff
changeset
|
122 |
} |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
123 |
|
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
124 |
def update_perspective() |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
125 |
{ |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
126 |
pending_perspective = true |
46740
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
127 |
delay_flush(true) |
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
128 |
} |
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
129 |
|
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
130 |
def init() |
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
131 |
{ |
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
132 |
flush() |
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
133 |
session.init_node(name, node_header(), perspective(), Isabelle.buffer_text(buffer)) |
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
134 |
} |
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
135 |
|
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
136 |
def exit() |
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
137 |
{ |
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
138 |
delay_flush(false) |
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
139 |
flush() |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
140 |
} |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
141 |
} |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
142 |
|
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
143 |
def update_perspective() |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
144 |
{ |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
145 |
Swing_Thread.require() |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
146 |
pending_edits.update_perspective() |
38222
dac5fa0ac971
replaced individual Document_Model history by all-inclusive one in Session;
wenzelm
parents:
38158
diff
changeset
|
147 |
} |
38152
eab0d1c2e46e
Change.Snapshot: include from_current/to_current here, with precomputed changes;
wenzelm
parents:
38151
diff
changeset
|
148 |
|
44776
47e8c8daccae
added "check" button: adhoc change to full buffer perspective;
wenzelm
parents:
44615
diff
changeset
|
149 |
def full_perspective() |
47e8c8daccae
added "check" button: adhoc change to full buffer perspective;
wenzelm
parents:
44615
diff
changeset
|
150 |
{ |
47e8c8daccae
added "check" button: adhoc change to full buffer perspective;
wenzelm
parents:
44615
diff
changeset
|
151 |
pending_edits.flush() |
47e8c8daccae
added "check" button: adhoc change to full buffer perspective;
wenzelm
parents:
44615
diff
changeset
|
152 |
session.edit_node(name, node_header(), Text.Perspective(List(buffer_range())), Nil) |
47e8c8daccae
added "check" button: adhoc change to full buffer perspective;
wenzelm
parents:
44615
diff
changeset
|
153 |
} |
47e8c8daccae
added "check" button: adhoc change to full buffer perspective;
wenzelm
parents:
44615
diff
changeset
|
154 |
|
38152
eab0d1c2e46e
Change.Snapshot: include from_current/to_current here, with precomputed changes;
wenzelm
parents:
38151
diff
changeset
|
155 |
|
eab0d1c2e46e
Change.Snapshot: include from_current/to_current here, with precomputed changes;
wenzelm
parents:
38151
diff
changeset
|
156 |
/* snapshot */ |
34731
c0cb6bd10eec
keep BufferListener and TextAreaExtension private;
wenzelm
parents:
34724
diff
changeset
|
157 |
|
38417 | 158 |
def snapshot(): Document.Snapshot = |
159 |
{ |
|
38223 | 160 |
Swing_Thread.require() |
44615 | 161 |
session.snapshot(name, pending_edits.snapshot()) |
38223 | 162 |
} |
34828 | 163 |
|
164 |
||
165 |
/* buffer listener */ |
|
166 |
||
167 |
private val buffer_listener: BufferListener = new BufferAdapter |
|
168 |
{ |
|
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40474
diff
changeset
|
169 |
override def bufferLoaded(buffer: JEditBuffer) |
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40474
diff
changeset
|
170 |
{ |
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40474
diff
changeset
|
171 |
pending_edits.init() |
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40474
diff
changeset
|
172 |
} |
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40474
diff
changeset
|
173 |
|
34828 | 174 |
override def contentInserted(buffer: JEditBuffer, |
175 |
start_line: Int, offset: Int, num_lines: Int, length: Int) |
|
176 |
{ |
|
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40474
diff
changeset
|
177 |
if (!buffer.isLoading) |
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40474
diff
changeset
|
178 |
pending_edits += Text.Edit.insert(offset, buffer.getText(offset, length)) |
34828 | 179 |
} |
180 |
||
181 |
override def preContentRemoved(buffer: JEditBuffer, |
|
38426 | 182 |
start_line: Int, offset: Int, num_lines: Int, removed_length: Int) |
34828 | 183 |
{ |
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40474
diff
changeset
|
184 |
if (!buffer.isLoading) |
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40474
diff
changeset
|
185 |
pending_edits += Text.Edit.remove(offset, buffer.getText(offset, removed_length)) |
34828 | 186 |
} |
187 |
} |
|
188 |
||
189 |
||
38158 | 190 |
/* activation */ |
37557
1ae272fd4082
refresh Isabelle token marker after buffer properties changed, e.g. when fold mode is switched;
wenzelm
parents:
37555
diff
changeset
|
191 |
|
43512 | 192 |
private def activate() |
34784
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
193 |
{ |
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
194 |
buffer.addBufferListener(buffer_listener) |
43512 | 195 |
pending_edits.init() |
44358
2a2df4de1bbe
more robust initialization of token marker and line context wrt. session startup;
wenzelm
parents:
44182
diff
changeset
|
196 |
Token_Markup.refresh_buffer(buffer) |
34680 | 197 |
} |
198 |
||
43512 | 199 |
private def deactivate() |
34680 | 200 |
{ |
46740
852baa599351
explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents:
46739
diff
changeset
|
201 |
pending_edits.exit() |
34784
02959dcea756
split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents:
34783
diff
changeset
|
202 |
buffer.removeBufferListener(buffer_listener) |
44358
2a2df4de1bbe
more robust initialization of token marker and line context wrt. session startup;
wenzelm
parents:
44182
diff
changeset
|
203 |
Token_Markup.refresh_buffer(buffer) |
34680 | 204 |
} |
34447 | 205 |
} |