27987
|
1 |
/* Title: lib/jedit/plugin/isabelle_plugin.scala
|
|
2 |
ID: $Id$
|
|
3 |
Author: Makarius
|
|
4 |
|
|
5 |
Isabelle/jEdit plugin -- main setup.
|
|
6 |
*/
|
|
7 |
|
|
8 |
package isabelle.jedit
|
|
9 |
|
|
10 |
import org.gjt.sp.jedit.EditPlugin
|
|
11 |
import org.gjt.sp.util.Log
|
|
12 |
|
|
13 |
import errorlist.DefaultErrorSource
|
|
14 |
import errorlist.ErrorSource
|
|
15 |
|
|
16 |
import java.util.Properties
|
|
17 |
import java.lang.NumberFormatException
|
|
18 |
|
|
19 |
import scala.collection.mutable.ListBuffer
|
|
20 |
import scala.io.Source
|
|
21 |
|
|
22 |
|
27991
|
23 |
|
|
24 |
/** global state **/
|
27987
|
25 |
|
|
26 |
object IsabellePlugin {
|
27991
|
27 |
|
|
28 |
/* Isabelle process */
|
|
29 |
|
|
30 |
var isabelle: IsabelleProcess = null
|
|
31 |
|
|
32 |
def result_content(result: IsabelleProcess.Result) =
|
|
33 |
XML.content(isabelle.decode_result(result)).mkString("")
|
|
34 |
|
|
35 |
|
|
36 |
/* unique ids */
|
|
37 |
|
|
38 |
private var id_count: BigInt = 0
|
27987
|
39 |
|
27988
|
40 |
def id() : String = synchronized { id_count += 1; "jedit:" + id_count }
|
27987
|
41 |
|
27988
|
42 |
def id_properties(value: String) : Properties = {
|
27987
|
43 |
val props = new Properties
|
|
44 |
props.setProperty(Markup.ID, value)
|
|
45 |
props
|
|
46 |
}
|
|
47 |
|
27988
|
48 |
def id_properties() : Properties = { id_properties(id()) }
|
27987
|
49 |
|
|
50 |
|
27991
|
51 |
/* result consumers */
|
27987
|
52 |
|
27991
|
53 |
type Consumer = IsabelleProcess.Result => Boolean
|
|
54 |
private var consumers = new ListBuffer[Consumer]
|
27987
|
55 |
|
27991
|
56 |
def add_consumer(consumer: Consumer) = synchronized { consumers += consumer }
|
27987
|
57 |
|
27988
|
58 |
def add_permanent_consumer(consumer: IsabelleProcess.Result => Unit) = {
|
|
59 |
add_consumer(result => { consumer(result); false })
|
27987
|
60 |
}
|
|
61 |
|
27991
|
62 |
def del_consumer(consumer: Consumer) = synchronized { consumers -= consumer }
|
27987
|
63 |
|
27991
|
64 |
private def consume(result: IsabelleProcess.Result) = {
|
27987
|
65 |
synchronized { consumers.elements.toList } foreach (consumer =>
|
|
66 |
{
|
27991
|
67 |
if (result != null && result.is_control) Log.log(Log.DEBUG, result, null)
|
27987
|
68 |
val finished =
|
|
69 |
try { consumer(result) }
|
27991
|
70 |
catch { case e: Throwable => Log.log(Log.ERROR, result, e); true }
|
|
71 |
if (finished || result == null) del_consumer(consumer)
|
27987
|
72 |
})
|
|
73 |
}
|
27991
|
74 |
|
|
75 |
class ConsumerThread extends Thread {
|
|
76 |
override def run = {
|
|
77 |
var finished = false
|
|
78 |
while (!finished) {
|
|
79 |
val result =
|
|
80 |
try { IsabellePlugin.isabelle.get_result() }
|
|
81 |
catch { case _: NullPointerException => null }
|
|
82 |
|
|
83 |
if (result != null) {
|
|
84 |
consume(result)
|
|
85 |
if (result.kind == IsabelleProcess.Kind.EXIT) {
|
|
86 |
consume(null)
|
|
87 |
finished = true
|
|
88 |
}
|
|
89 |
}
|
|
90 |
else finished = true
|
|
91 |
}
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
27987
|
95 |
}
|
|
96 |
|
|
97 |
|
|
98 |
/* Main plugin setup */
|
|
99 |
|
|
100 |
class IsabellePlugin extends EditPlugin {
|
27991
|
101 |
|
|
102 |
import IsabellePlugin._
|
|
103 |
|
|
104 |
val errors = new DefaultErrorSource("isabelle")
|
|
105 |
val consumer_thread = new ConsumerThread
|
|
106 |
|
27987
|
107 |
|
|
108 |
override def start = {
|
27991
|
109 |
|
|
110 |
/* error source */
|
27987
|
111 |
|
27991
|
112 |
ErrorSource.registerErrorSource(errors)
|
|
113 |
|
|
114 |
add_permanent_consumer (result =>
|
|
115 |
if (result != null &&
|
27987
|
116 |
(result.kind == IsabelleProcess.Kind.WARNING ||
|
|
117 |
result.kind == IsabelleProcess.Kind.ERROR)) {
|
|
118 |
(Position.line_of(result.props), Position.file_of(result.props)) match {
|
|
119 |
case (Some(line), Some(file)) =>
|
27991
|
120 |
val typ =
|
|
121 |
if (result.kind == IsabelleProcess.Kind.WARNING) ErrorSource.WARNING
|
|
122 |
else ErrorSource.ERROR
|
|
123 |
val content = result_content(result)
|
27987
|
124 |
if (content.length > 0) {
|
|
125 |
val lines = Source.fromString(content).getLines
|
27991
|
126 |
val err = new DefaultErrorSource.DefaultError(errors,
|
27987
|
127 |
typ, file, line - 1, 0, 0, lines.next)
|
|
128 |
for (msg <- lines) err.addExtraMessage(msg)
|
27991
|
129 |
errors.addError(err)
|
27987
|
130 |
}
|
|
131 |
case _ =>
|
|
132 |
}
|
|
133 |
})
|
|
134 |
|
|
135 |
|
27991
|
136 |
/* Isabelle process */
|
|
137 |
|
|
138 |
isabelle = new IsabelleProcess("-mno_brackets", "-mno_type_brackets", "-mxsymbols")
|
|
139 |
consumer_thread.start
|
|
140 |
|
27987
|
141 |
}
|
|
142 |
|
|
143 |
|
27991
|
144 |
override def stop = {
|
|
145 |
isabelle.kill
|
|
146 |
consumer_thread.join
|
|
147 |
ErrorSource.unregisterErrorSource(errors)
|
27987
|
148 |
}
|
27991
|
149 |
|
27987
|
150 |
}
|