author | wenzelm |
Wed, 21 Dec 2016 11:55:59 +0100 | |
changeset 64641 | 7b9196394b32 |
parent 64623 | 83f012ce2567 |
child 64642 | c231206a84c8 |
permissions | -rw-r--r-- |
64605 | 1 |
/* Title: Tools/VSCode/src/server.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Server for VS Code Language Server Protocol 2.0, see also |
|
5 |
https://github.com/Microsoft/language-server-protocol |
|
6 |
https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md |
|
7 |
*/ |
|
8 |
||
9 |
package isabelle.vscode |
|
10 |
||
11 |
||
12 |
import isabelle._ |
|
13 |
||
14 |
import java.io.{PrintStream, OutputStream} |
|
15 |
||
16 |
import scala.annotation.tailrec |
|
17 |
||
18 |
||
19 |
object Server |
|
20 |
{ |
|
21 |
/* Isabelle tool wrapper */ |
|
22 |
||
64618 | 23 |
private val default_text_length = "UTF-16" |
64605 | 24 |
private lazy val default_logic = Isabelle_System.getenv("ISABELLE_LOGIC") |
25 |
||
26 |
val isabelle_tool = Isabelle_Tool("vscode_server", "VSCode Language Server for PIDE", args => |
|
27 |
{ |
|
28 |
var log_file: Option[Path] = None |
|
64618 | 29 |
var text_length = Length.encoding(default_text_length) |
64605 | 30 |
var dirs: List[Path] = Nil |
31 |
var logic = default_logic |
|
32 |
var modes: List[String] = Nil |
|
33 |
var options = Options.init() |
|
34 |
||
64618 | 35 |
def text_length_choice: String = |
36 |
commas(Length.encodings.map( |
|
37 |
{ case (a, _) => if (a == default_text_length) a + " (default)" else a })) |
|
38 |
||
64605 | 39 |
val getopts = Getopts(""" |
40 |
Usage: isabelle vscode_server [OPTIONS] |
|
41 |
||
42 |
Options are: |
|
43 |
-L FILE enable logging on FILE |
|
64618 | 44 |
-T LENGTH text length encoding: """ + text_length_choice + """ |
64605 | 45 |
-d DIR include session directory |
46 |
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """) |
|
47 |
-m MODE add print mode for output |
|
48 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
|
49 |
||
50 |
Run the VSCode Language Server protocol (JSON RPC) over stdin/stdout. |
|
51 |
""", |
|
52 |
"L:" -> (arg => log_file = Some(Path.explode(arg))), |
|
64618 | 53 |
"T:" -> (arg => Length.encoding(arg)), |
64605 | 54 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), |
55 |
"l:" -> (arg => logic = arg), |
|
56 |
"m:" -> (arg => modes = arg :: modes), |
|
57 |
"o:" -> (arg => options = options + arg)) |
|
58 |
||
59 |
val more_args = getopts(args) |
|
60 |
if (more_args.nonEmpty) getopts.usage() |
|
61 |
||
62 |
if (!Build.build(options = options, build_heap = true, no_build = true, |
|
63 |
dirs = dirs, sessions = List(logic)).ok) |
|
64 |
error("Missing logic image " + quote(logic)) |
|
65 |
||
66 |
val channel = new Channel(System.in, System.out, log_file) |
|
64618 | 67 |
val server = new Server(channel, options, text_length, logic, dirs, modes) |
64605 | 68 |
|
69 |
// prevent spurious garbage on the main protocol channel |
|
70 |
val orig_out = System.out |
|
71 |
try { |
|
72 |
System.setOut(new PrintStream(new OutputStream { def write(n: Int) {} })) |
|
73 |
server.start() |
|
74 |
} |
|
75 |
finally { System.setOut(orig_out) } |
|
76 |
}) |
|
77 |
||
78 |
||
79 |
/* server state */ |
|
80 |
||
81 |
sealed case class State( |
|
82 |
session: Option[Session] = None, |
|
83 |
models: Map[String, Document_Model] = Map.empty) |
|
84 |
} |
|
85 |
||
86 |
class Server( |
|
87 |
channel: Channel, |
|
88 |
options: Options, |
|
64618 | 89 |
text_length: Length = Length.encoding(Server.default_text_length), |
64605 | 90 |
session_name: String = Server.default_logic, |
91 |
session_dirs: List[Path] = Nil, |
|
92 |
modes: List[String] = Nil) |
|
93 |
{ |
|
94 |
/* server state */ |
|
95 |
||
96 |
private val state = Synchronized(Server.State()) |
|
97 |
||
98 |
def session: Session = state.value.session getOrElse error("Session inactive") |
|
64623 | 99 |
def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources] |
64605 | 100 |
|
101 |
||
102 |
/* init and exit */ |
|
103 |
||
64641 | 104 |
def init(id: Protocol.Id) |
64605 | 105 |
{ |
64641 | 106 |
def reply(err: String) |
107 |
{ |
|
108 |
channel.write(Protocol.Initialize.reply(id, err)) |
|
109 |
if (err == "") { |
|
110 |
channel.write(Protocol.DisplayMessage(Protocol.MessageType.Info, |
|
111 |
"Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")")) |
|
112 |
} |
|
113 |
else channel.write(Protocol.DisplayMessage(Protocol.MessageType.Error, err)) |
|
114 |
} |
|
115 |
||
116 |
// FIXME handle error |
|
64605 | 117 |
val content = Build.session_content(options, false, session_dirs, session_name) |
118 |
val resources = |
|
64623 | 119 |
new VSCode_Resources(content.loaded_theories, content.known_theories, content.syntax) |
64605 | 120 |
|
121 |
val session = |
|
122 |
new Session(resources) { |
|
123 |
override def output_delay = options.seconds("editor_output_delay") |
|
124 |
override def prune_delay = options.seconds("editor_prune_delay") |
|
125 |
override def syslog_limit = options.int("editor_syslog_limit") |
|
126 |
override def reparse_limit = options.int("editor_reparse_limit") |
|
127 |
} |
|
128 |
||
129 |
var session_phase: Session.Consumer[Session.Phase] = null |
|
130 |
session_phase = |
|
131 |
Session.Consumer(getClass.getName) { |
|
132 |
case Session.Ready => |
|
133 |
session.phase_changed -= session_phase |
|
134 |
session.update_options(options) |
|
135 |
reply("") |
|
136 |
case Session.Failed => |
|
137 |
session.phase_changed -= session_phase |
|
138 |
reply("Prover startup failed") |
|
139 |
case _ => |
|
140 |
} |
|
141 |
session.phase_changed += session_phase |
|
142 |
||
143 |
session.start(receiver => |
|
144 |
Isabelle_Process(options = options, logic = session_name, dirs = session_dirs, |
|
145 |
modes = modes, receiver = receiver)) |
|
146 |
||
147 |
state.change(_.copy(session = Some(session))) |
|
148 |
} |
|
149 |
||
64641 | 150 |
def shutdown(id: Protocol.Id) |
64605 | 151 |
{ |
64641 | 152 |
def reply(err: String): Unit = channel.write(Protocol.Shutdown.reply(id, err)) |
153 |
||
64605 | 154 |
state.change(st => |
155 |
st.session match { |
|
156 |
case None => reply("Prover inactive"); st |
|
157 |
case Some(session) => |
|
158 |
var session_phase: Session.Consumer[Session.Phase] = null |
|
159 |
session_phase = |
|
160 |
Session.Consumer(getClass.getName) { |
|
161 |
case Session.Inactive => |
|
162 |
session.phase_changed -= session_phase |
|
163 |
reply("") |
|
164 |
case _ => |
|
165 |
} |
|
166 |
session.phase_changed += session_phase |
|
167 |
session.stop() |
|
168 |
st.copy(session = None) |
|
169 |
}) |
|
170 |
} |
|
171 |
||
172 |
def exit() { sys.exit(if (state.value.session.isDefined) 1 else 0) } |
|
173 |
||
174 |
||
175 |
/* document management */ |
|
176 |
||
177 |
private val delay_flush = |
|
178 |
Standard_Thread.delay_last(options.seconds("editor_input_delay")) { |
|
179 |
state.change(st => |
|
180 |
{ |
|
181 |
val models = st.models |
|
182 |
val changed = (for { entry <- models.iterator if entry._2.changed } yield entry).toList |
|
183 |
val edits = for { (_, model) <- changed; edit <- model.node_edits } yield edit |
|
184 |
val models1 = |
|
185 |
(models /: changed)({ case (m, (uri, model)) => m + (uri -> model.unchanged) }) |
|
186 |
||
187 |
session.update(Document.Blobs.empty, edits) |
|
188 |
st.copy(models = models1) |
|
189 |
}) |
|
190 |
} |
|
191 |
||
192 |
def update_document(uri: String, text: String) |
|
193 |
{ |
|
194 |
state.change(st => |
|
195 |
{ |
|
196 |
val node_name = resources.node_name(uri) |
|
197 |
val model = Document_Model(session, Line.Document(text), node_name) |
|
198 |
st.copy(models = st.models + (uri -> model)) |
|
199 |
}) |
|
200 |
delay_flush.invoke() |
|
201 |
} |
|
202 |
||
203 |
||
64622
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
64618
diff
changeset
|
204 |
/* hover */ |
64605 | 205 |
|
64641 | 206 |
def hover(id: Protocol.Id, uri: String, pos: Line.Position) |
207 |
{ |
|
208 |
val result = |
|
209 |
for { |
|
210 |
model <- state.value.models.get(uri) |
|
211 |
rendering = model.rendering(options) |
|
212 |
offset <- model.doc.offset(pos, text_length) |
|
213 |
info <- rendering.tooltip(Text.Range(offset, offset + 1)) |
|
214 |
} yield { |
|
215 |
val start = model.doc.position(info.range.start, text_length) |
|
216 |
val stop = model.doc.position(info.range.stop, text_length) |
|
217 |
val s = Pretty.string_of(info.info, margin = rendering.tooltip_margin) |
|
218 |
(Line.Range(start, stop), List("```\n" + s + "\n```")) // FIXME proper content format |
|
219 |
} |
|
220 |
channel.write(Protocol.Hover.reply(id, result)) |
|
221 |
} |
|
64605 | 222 |
|
223 |
||
224 |
/* main loop */ |
|
225 |
||
226 |
def start() |
|
227 |
{ |
|
228 |
channel.log("\nServer started " + Date.now()) |
|
229 |
||
230 |
def handle(json: JSON.T) |
|
231 |
{ |
|
232 |
try { |
|
233 |
json match { |
|
64641 | 234 |
case Protocol.Initialize(id) => init(id) |
235 |
case Protocol.Shutdown(id) => shutdown(id) |
|
236 |
case Protocol.Exit => exit() |
|
64605 | 237 |
case Protocol.DidOpenTextDocument(uri, lang, version, text) => |
238 |
update_document(uri, text) |
|
239 |
case Protocol.DidChangeTextDocument(uri, version, List(Protocol.TextDocumentContent(text))) => |
|
240 |
update_document(uri, text) |
|
241 |
case Protocol.DidCloseTextDocument(uri) => channel.log("CLOSE " + uri) |
|
242 |
case Protocol.DidSaveTextDocument(uri) => channel.log("SAVE " + uri) |
|
64641 | 243 |
case Protocol.Hover(id, uri, pos) => hover(id, uri, pos) |
64605 | 244 |
case _ => channel.log("### IGNORED") |
245 |
} |
|
246 |
} |
|
247 |
catch { case exn: Throwable => channel.log("*** ERROR: " + Exn.message(exn)) } |
|
248 |
} |
|
249 |
||
250 |
@tailrec def loop() |
|
251 |
{ |
|
252 |
channel.read() match { |
|
253 |
case Some(json) => |
|
254 |
json match { |
|
255 |
case bulk: List[_] => bulk.foreach(handle(_)) |
|
256 |
case _ => handle(json) |
|
257 |
} |
|
258 |
loop() |
|
259 |
case None => channel.log("### TERMINATE") |
|
260 |
} |
|
261 |
} |
|
262 |
loop() |
|
263 |
} |
|
264 |
} |