3 public class LanguageClientVala : LanguageClient {
4 protected bool initialized = false;
5 bool sent_shutdown = false;
6 uint change_queue_id = 0;
9 private bool _closed = false;
11 get { return this._closed ; }
13 GLib.debug("closed has been set? to %s" , value ? "TRUE" : "FALSE" );
17 private GLib.SubprocessLauncher launcher = null;
18 private GLib.Subprocess? subprocess = null;
19 private IOStream? subprocess_stream = null;
20 public Jsonrpc.Client? jsonrpc_client = null;
23 Gee.ArrayList<JsRender.JsRender> open_files;
24 private JsRender.JsRender? _change_queue_file = null;
25 int doc_countdown = 0;
26 private string change_queue_file_source = "";
27 private JsRender.JsRender? doc_queue_file = null;
30 JsRender.JsRender? change_queue_file {
32 this.change_queue_file_source = value == null ? "" : value.toSource();
33 this._change_queue_file = value;
36 return this._change_queue_file;
44 var exe = GLib.Environment.find_program_in_path( "vala-language-server");
46 GLib.warning("could not find vala-language-server");
50 this.initProcess(exe);
54 public LanguageClientVala(Project.Project project)
56 // extend versions will proably call initialize to start and connect to server.
59 if (this.change_queue_id == 0 ) {
60 this.change_queue_id = GLib.Timeout.add(500, () => {
61 this.run_change_queue();
71 void run_change_queue()
74 if (this.change_queue_file == null) {
77 if (this.countdown < -1) {
80 if (this.getting_diagnostics) {
86 if (this.countdown < 0){
87 this.document_change_force.begin(this.change_queue_file, this.change_queue_file_source, (o, res) => {
88 this.document_change_force.end(res);
90 this.change_queue_file = null;
100 if (this.doc_queue_file == null) {
103 if (this.doc_countdown < -1) {
106 this.doc_countdown--;
108 if (this.doc_countdown < 0){
109 var sendfile = this.doc_queue_file;
110 this.documentSymbols.begin(this.doc_queue_file, (o, res) => {
111 var ret = this.documentSymbols.end(res);
112 sendfile.navigation_tree_updated(ret);
114 this.doc_queue_file = null;
119 public bool initProcess(string process_path)
122 this.log(LanguageClientAction.LAUNCH, process_path);
123 GLib.debug("Launching %s", process_path);
124 this.launcher = new GLib.SubprocessLauncher (SubprocessFlags.STDIN_PIPE | SubprocessFlags.STDOUT_PIPE);
125 var env = GLib.Environ.get();
126 env += "G_MESSAGES_DEBUG=all";
128 this.launcher.set_environ(env);
129 var logpath = GLib.Environment.get_home_dir() + "/.cache/vala-language-server";
131 if (!GLib.FileUtils.test(logpath, GLib.FileTest.IS_DIR)) {
132 Posix.mkdir(logpath, 0700);
134 // not very reliable..
135 //this.launcher.set_stderr_file_path(
137 // (new GLib.DateTime.now_local()).format("%Y-%m-%d") + ".log"
139 //GLib.debug("log lang server to %s", logpath + "/" +
140 // (new GLib.DateTime.now_local()).format("%Y-%m-%d") + ".log");
145 this.subprocess = launcher.spawnv ({ process_path , "2>" , "/tmp/vala-language-server.log" });
147 this.subprocess.wait_async.begin( null, ( obj,res ) => {
149 this.subprocess.wait_async.end(res);
150 } catch (GLib.Error e) {
151 this.log(LanguageClientAction.ERROR_START, e.message);
152 GLib.debug("subprocess startup error %s", e.message);
154 this.log(LanguageClientAction.EXIT, "process ended");
155 GLib.debug("Subprocess ended %s", process_path);
159 var input_stream = this.subprocess.get_stdout_pipe ();
160 var output_stream = this.subprocess.get_stdin_pipe ();
162 if (input_stream is GLib.UnixInputStream && output_stream is GLib.UnixOutputStream) {
164 if (!GLib.Unix.set_fd_nonblocking(((GLib.UnixInputStream)input_stream).fd, true)
165 || !GLib.Unix.set_fd_nonblocking (((GLib.UnixOutputStream)output_stream).fd, true))
167 GLib.debug("could not set pipes to nonblocking");
172 this.subprocess_stream = new GLib.SimpleIOStream (input_stream, output_stream);
173 this.accept_io_stream ( this.subprocess_stream);
174 } catch (GLib.Error e) {
175 this.log(LanguageClientAction.ERROR_START, e.message);
176 GLib.debug("subprocess startup error %s", e.message);
182 bool in_close = false;
183 public override void client_accepted (Jsonrpc.Client client)
185 if (this.jsonrpc_client == null) {
186 this.jsonrpc_client = client;
188 GLib.debug("client accepted connection - calling init server");
189 this.log(LanguageClientAction.ACCEPT, "client accepted");
191 this.jsonrpc_client.notification.connect((method, paramz) => {
192 this.onNotification(method, paramz);
195 this.jsonrpc_client.failed.connect(() => {
196 this.log(LanguageClientAction.ERROR_RPC, "client failed");
197 GLib.debug("language server server has failed");
203 this.initialize_server ();
210 public override void initialize_server() {
212 Variant? return_value;
213 this.jsonrpc_client.call (
216 processId: new Variant.int32 ((int32) Posix.getpid ()),
217 rootPath: new Variant.string (this.project.path),
218 rootUri: new Variant.string (File.new_for_path (this.project.path).get_uri ()),
219 capabilities : this.buildDict (
220 textDocument: this.buildDict (
221 documentSymbol : this.buildDict (
222 hierarchicalDocumentSymbolSupport : new Variant.boolean (true)
230 GLib.debug ("LS replied with %s", Json.to_string (Json.gvariant_serialize (return_value), true));
231 this.open_files = new Gee.ArrayList<JsRender.JsRender>((a,b) => {
232 return a.path == b.path;
234 this.initialized = true;
235 this.getting_diagnostics = false;
237 } catch (GLib.Error e) {
238 GLib.debug ("LS replied with error %s", e.message);
248 if (this.launcher == null) {
251 this.getting_diagnostics = false;
252 this.in_close = true;
253 GLib.debug("onClose called");
255 if (this.jsonrpc_client != null) {
257 this.jsonrpc_client.close();
258 } catch (GLib.Error e) {
259 GLib.debug("rpc Error close error %s", e.message);
262 if (this.subprocess_stream != null) {
264 this.subprocess_stream.close();
265 } catch (GLib.Error e) {
266 GLib.debug("stream Error close %s", e.message);
269 if (this.subprocess != null) {
270 this.subprocess.force_exit();
272 if (this.launcher != null) {
273 this.launcher.close();
276 this.launcher = null;
277 this.subprocess = null;
278 this.jsonrpc_client = null;
280 this.in_close = false;
283 public async void restartServer()
289 public bool isReady()
292 this.log(LanguageClientAction.RESTART,"closed is set - restarting");
293 GLib.debug("server stopped = restarting");
294 this.initialized = false;
296 GLib.MainLoop loop = new GLib.MainLoop ();
297 this.restartServer.begin ((obj, async_res) => {
298 this.restartServer.end(async_res);
301 return false; // can't do an operation yet?
305 if (!this.initialized) {
306 GLib.debug("Server has not been initialized");
309 if (this.sent_shutdown) {
310 GLib.debug("Server has been started its shutting down process");
320 public void onNotification(string method, Variant? return_value)
323 case "textDocument/publishDiagnostics":
324 //GLib.debug("got notification %s : %s", method , Json.to_string (Json.gvariant_serialize (return_value), true));
326 GLib.Idle.add(() => {
327 this.onDiagnostic(return_value);
335 GLib.debug("got notification %s : %s", method , Json.to_string (Json.gvariant_serialize (return_value), true));
339 bool getting_diagnostics = false;
343 public void onDiagnostic(Variant? return_value)
345 //GLib.debug ("LS replied with %s", Json.to_string (Json.gvariant_serialize (return_value), true));
346 var dg = Json.gobject_deserialize (typeof (Lsp.Diagnostics), Json.gvariant_serialize (return_value)) as Lsp.Diagnostics;
347 GLib.debug("got diag for %s", dg.filename);
348 this.log(LanguageClientAction.DIAG, dg.filename);
349 if (this.project.path == dg.filename) {
350 this.getting_diagnostics = false;
351 this.log(LanguageClientAction.DIAG_END, "diagnostics done");
355 this.getting_diagnostics =true;
356 var f = this.project.getByPath(dg.filename);
358 //GLib.debug("no file %s", dg.uri);
359 //this.project.updateErrorsforFile(null);
362 //GLib.debug("got Diagnostics for %s", f.path);
363 f.updateErrors( dg.diagnostics );
368 public override void document_open (JsRender.JsRender file)
370 if (!this.isReady()) {
373 if (this.open_files.contains(file)) {
376 this.open_files.add(file);
379 GLib.debug ("LS sent open");
381 this.jsonrpc_client.send_notification (
382 "textDocument/didOpen",
384 textDocument : this.buildDict (
385 uri: new Variant.string (file.to_url()),
386 languageId : new Variant.string (file.language_id()),
387 version : new GLib.Variant.uint64 ( (uint64) file.version),
388 text : new Variant.string (file.toSource())
393 this.log(LanguageClientAction.OPEN, file.path);
394 } catch( GLib.Error e) {
395 this.log(LanguageClientAction.ERROR_RPC, e.message);
397 GLib.debug ("LS sent open err %s", e.message);
402 public override async void document_save (JsRender.JsRender file)
404 if (!this.isReady()) {
407 // save only really flags the file on the server - to actually force a change update - we need to
408 // flag it as changed.
409 yield this.document_change_force(file, file.toSource());
411 this.change_queue_file = null;
412 GLib.debug ("LS send save");
415 var args = this.buildDict (
416 textDocument : this.buildDict ( ///TextDocumentItem;
417 uri: new GLib.Variant.string (file.to_url()),
418 version : new GLib.Variant.uint64 ( (uint64) file.version)
422 //GLib.debug ("textDocument/save send with %s", Json.to_string (Json.gvariant_serialize (args), true));
426 yield this.jsonrpc_client.send_notification_async (
427 "textDocument/didSave",
431 this.log(LanguageClientAction.SAVE, file.path);
432 } catch( GLib.Error e) {
433 this.log(LanguageClientAction.ERROR_RPC, e.message);
434 GLib.debug ("LS save err %s", e.message);
440 public override void document_close (JsRender.JsRender file)
442 if (!this.isReady()) {
445 this.change_queue_file = null;
447 if (this.open_files.contains(file)) {
448 this.open_files.remove(file);
450 this.log(LanguageClientAction.CLOSE, file.path);
451 GLib.debug ("LS send close");
453 this.jsonrpc_client.send_notification (
454 "textDocument/didChange",
456 textDocument : this.buildDict ( ///TextDocumentItem;
457 uri: new GLib.Variant.string (file.to_url())
463 } catch( GLib.Error e) {
464 this.log(LanguageClientAction.ERROR_RPC, e.message);
465 GLib.debug ("LS close err %s", e.message);
473 public override void document_change (JsRender.JsRender file )
475 if (this.change_queue_file != null && this.change_queue_file.path != file.path) {
476 this.document_change_force.begin(this.change_queue_file, this.change_queue_file_source, (o, res) => {
477 this.document_change_force.end(res);
482 this.change_queue_file = file;
489 public override async void document_change_force (JsRender.JsRender file, string contents)
493 if (!this.isReady()) {
496 this.countdown = -2; // not really relivant..
497 this.change_queue_file = null; // this is more important..
499 if (!this.open_files.contains(file)) {
500 this.document_open(file);
503 GLib.debug ("LS send change %s rev %d", file.path, file.version);
504 var ar = new Json.Array();
505 var obj = new Json.Object();
506 obj.set_string_member("text", contents);
507 ar.add_object_element(obj);
508 var node = new Json.Node(Json.NodeType.ARRAY);
510 this.log(LanguageClientAction.CHANGE, file.path);
512 yield this.jsonrpc_client.send_notification_async (
513 "textDocument/didChange",
515 textDocument : this.buildDict ( ///TextDocumentItem;
516 uri: new GLib.Variant.string (file.to_url()),
517 version : new GLib.Variant.uint64 ( (uint64) file.version)
519 contentChanges : Json.gvariant_deserialize (node, null)
524 } catch( GLib.Error e) {
525 this.log(LanguageClientAction.ERROR_RPC, e.message);
526 GLib.debug ("LS change err %s", e.message);
532 // called by close window (on last window)...
533 public override void exit () throws GLib.Error
535 if (!this.isReady()) {
539 this.log(LanguageClientAction.TERM, "SEND exit");
541 this.jsonrpc_client.send_notification (
549 // not used currently..
550 public override async void shutdown () throws GLib.Error
552 if (!this.isReady()) {
555 this.log(LanguageClientAction.TERM, "SEND shutodwn");
556 this.sent_shutdown = true;
557 Variant? return_value;
558 yield this.jsonrpc_client.call_async (
564 GLib.debug ("LS replied with %s", Json.to_string (Json.gvariant_serialize (return_value), true));
566 //public async ??/symbol (string symbol) throws GLib.Error {
568 // and now for the important styff..
572 @triggerType 1 = typing or ctl-spac, 2 = tiggercharactres? 3= inside completion?
574 public override async Lsp.CompletionList? completion(JsRender.JsRender file, int line, int offset , int triggerType = 1) throws GLib.Error
576 /* partial_result_token , work_done_token context = null) */
577 GLib.debug("%s get completion %s @ %d:%d", this.get_type().name(), file.relpath, line, offset);
579 var ret = new Lsp.CompletionList();
581 if (!this.isReady()) {
582 GLib.debug("completion - language server not ready");
585 // make sure completion has the latest info..
586 //if (this.change_queue_file != null && this.change_queue_file.path != file.path) {
587 // this.document_change_real(this.change_queue_file, this.change_queue_file_source);
588 // this.change_queue_file != null;
590 this.log(LanguageClientAction.COMPLETE, "SEND complete %s @ %d:%d".printf(file.relpath, line, offset) );
592 Variant? return_value;
594 var args = this.buildDict (
595 context : this.buildDict ( ///CompletionContext;
596 triggerKind: new GLib.Variant.int32 (triggerType)
597 // triggerCharacter : new GLib.Variant.string ("")
599 textDocument : this.buildDict ( ///TextDocumentItem;
600 uri: new GLib.Variant.string (file.to_url()),
601 version : new GLib.Variant.uint64 ( (uint64) file.version)
603 position : this.buildDict (
604 line : new GLib.Variant.uint64 ( (uint) line) ,
605 character : new GLib.Variant.uint64 ( uint.max(0, (offset -1)))
609 GLib.debug ("textDocument/completion send with %s", Json.to_string (Json.gvariant_serialize (args), true));
611 yield this.jsonrpc_client.call_async (
612 "textDocument/completion",
619 //GLib.debug ("LS replied with %s", Json.to_string (Json.gvariant_serialize (return_value), true));
620 var json = Json.gvariant_serialize (return_value);
623 if (json.get_node_type() == Json.NodeType.OBJECT) {
624 ret = Json.gobject_deserialize (typeof (Lsp.CompletionList), json) as Lsp.CompletionList;
625 this.log(LanguageClientAction.COMPLETE_REPLY, "GOT complete %d items".printf(ret.items.size) );
626 GLib.debug ("LS replied with Object");
630 if (json.get_node_type() != Json.NodeType.ARRAY) {
631 GLib.debug ("LS replied with %s", Json.to_string (Json.gvariant_serialize (return_value), true));
632 this.log(LanguageClientAction.ERROR_REPLY, "GOT something else??");
636 var ar = json.get_array();
638 for(var i = 0; i < ar.get_length(); i++ ) {
639 var add= Json.gobject_deserialize ( typeof (Lsp.CompletionItem), ar.get_element(i)) as Lsp.CompletionItem;
643 this.log(LanguageClientAction.COMPLETE_REPLY, "GOT array %d items".printf(ret.items.size) );
644 GLib.debug ("LS replied with Array");
651 //CompletionListInfo.itmems.parse_varient or CompletionListInfo.parsevarient
652 public override async Lsp.Hover hover (JsRender.JsRender file, int line, int offset) throws GLib.Error
654 /* partial_result_token , work_done_token context = null) */
655 GLib.debug("get syntax %s", file.relpath);
656 var ret = new Lsp.Hover();
658 if (!this.isReady()) {
661 Variant? return_value;
662 yield this.jsonrpc_client.call_async (
663 "textDocument/hover",
666 textDocument : this.buildDict ( ///TextDocumentItem;
667 uri: new GLib.Variant.string (file.to_url()),
668 version : new GLib.Variant.uint64 ( (uint64) file.version)
670 position : this.buildDict (
671 line : new GLib.Variant.uint64 ( (uint) line) ,
672 character : new GLib.Variant.uint64 ( uint.max(0, (offset -1)))
681 GLib.debug ("LS replied with %s", Json.to_string (Json.gvariant_serialize (return_value), true));
682 var json = Json.gvariant_serialize (return_value);
683 ret = Json.gobject_deserialize ( typeof (Lsp.Hover), json) as Lsp.Hover;
691 public override void queueDocumentSymbols (JsRender.JsRender file)
693 if (this.doc_queue_file != null && this.doc_queue_file.path != file.path) {
694 var sendfile = this.doc_queue_file;
695 this.documentSymbols.begin(this.doc_queue_file, (o, res) => {
696 var ret = documentSymbols.end(res);
697 sendfile.navigation_tree_updated(ret);
701 this.doc_countdown = 4;
702 this.doc_queue_file = file;
706 public override async Gee.ArrayList<Lsp.DocumentSymbol> documentSymbols (JsRender.JsRender file) throws GLib.Error {
707 /* partial_result_token , work_done_token context = null) */
708 GLib.debug("get documentSymbols %s", file.relpath);
709 var ret = new Gee.ArrayList<Lsp.DocumentSymbol>();
711 if (!this.isReady()) {
714 Variant? return_value;
715 yield this.jsonrpc_client.call_async (
716 "textDocument/documentSymbol",
719 textDocument : this.buildDict ( ///TextDocumentItem;
720 uri: new GLib.Variant.string (file.to_url()),
721 version : new GLib.Variant.uint64 ( (uint64) file.version)
730 GLib.debug ("LS replied with %s", Json.to_string (Json.gvariant_serialize (return_value), true));
731 var json = Json.gvariant_serialize (return_value);
735 var ar = json.get_array();
736 GLib.debug ("LS replied with %D items", ar.get_length());
737 for(var i = 0; i < ar.get_length(); i++ ) {
738 var add= Json.gobject_deserialize ( typeof (Lsp.DocumentSymbol), ar.get_element(i)) as Lsp.DocumentSymbol;