From f192c9e4ec8e025afd263a8802a3144a96099040 Mon Sep 17 00:00:00 2001 From: Ben Iofel Date: Thu, 23 Nov 2017 00:59:55 -0500 Subject: [PATCH] vala.codecontext --- .editorconfig | 2 +- config.vala.in | 3 + main.vala | 162 ++++++++++++++++++++++++++++++------------------- meson.build | 13 +++- report.vala | 14 +++++ 5 files changed, 130 insertions(+), 64 deletions(-) create mode 100644 config.vala.in create mode 100644 report.vala diff --git a/.editorconfig b/.editorconfig index bb08d8fde..df27be310 100644 --- a/.editorconfig +++ b/.editorconfig @@ -4,6 +4,6 @@ root = true end_of_line = lf insert_final_newline = true -[*.vala] +[*.{vala,vapi}] indent_style = space indent_size = 4 diff --git a/config.vala.in b/config.vala.in new file mode 100644 index 000000000..1105780ce --- /dev/null +++ b/config.vala.in @@ -0,0 +1,3 @@ +namespace Config { + const string libvala_version = "@LIBVALA_VERSION@"; +} diff --git a/main.vala b/main.vala index d4bf38c9f..1f0df173a 100644 --- a/main.vala +++ b/main.vala @@ -1,79 +1,117 @@ using LanguageServer; -class ValaLanguageServer { - Jsonrpc.Server server; - MainLoop loop; - - public ValaLanguageServer (MainLoop loop) { - this.loop = loop; - server = new Jsonrpc.Server (); - var stdin = new UnixInputStream (Posix.STDIN_FILENO, false); - var stdout = new UnixOutputStream (Posix.STDOUT_FILENO, false); - server.accept_io_stream (new SimpleIOStream (stdin, stdout)); - - server.notification.connect ((client, method, @params) => { - stderr.printf (@"Got notification! $method\n"); - if (method == "textDocument/didOpen") - this.textDocumentDidOpen(client, @params); - }); - - server.add_handler ("initialize", this.initialize); - server.add_handler ("exit", this.exit); - } - - // a{sv} only - Variant buildDict (...) { - var builder = new VariantBuilder (new VariantType ("a{sv}")); - var l = va_list (); - while (true) { - string? key = l.arg (); - if (key == null) { - break; - } - Variant val = l.arg (); - builder.add ("{sv}", key, val); +class Vls.Server { + Jsonrpc.Server server; + MainLoop loop; + + Vala.CodeContext ctx; + + public Server (MainLoop loop) { + this.loop = loop; + this.server = new Jsonrpc.Server (); + + var stdin = new UnixInputStream (Posix.STDIN_FILENO, false); + var stdout = new UnixOutputStream (Posix.STDOUT_FILENO, false); + server.accept_io_stream (new SimpleIOStream (stdin, stdout)); + + server.notification.connect ((client, method, @params) => { + stderr.printf (@"Got notification! $method\n"); + if (method == "textDocument/didOpen") + this.textDocumentDidOpen(client, @params); + }); + + server.add_handler ("initialize", this.initialize); + server.add_handler ("exit", this.exit); + + // libvala setup + this.ctx = new Vala.CodeContext (); + Vala.CodeContext.push (ctx); + + string version = Config.libvala_version; + string[] parts = version.split("."); + assert (parts.length == 3); + assert (parts[0] == "0"); + var minor = int.parse (parts[1]); + + ctx.profile = Vala.Profile.GOBJECT; + for (int i = 2; i <= minor; i += 2) { + ctx.add_define ("VALA_0_%d".printf (i)); + } + ctx.target_glib_major = 2; + ctx.target_glib_minor = 44; + for (int i = 16; i <= ctx.target_glib_major; i += 2) { + ctx.add_define ("GLIB_2_%d".printf (i)); + } + ctx.report = new Report (); + ctx.add_external_package ("glib-2.0"); + ctx.add_external_package ("gobject-2.0"); + } + + // a{sv} only + Variant buildDict (...) { + var builder = new VariantBuilder (new VariantType ("a{sv}")); + var l = va_list (); + while (true) { + string? key = l.arg (); + if (key == null) { + break; + } + Variant val = l.arg (); + builder.add ("{sv}", key, val); + } + return builder.end (); } - return builder.end (); - } - void initialize (Jsonrpc.Server self, Jsonrpc.Client client, string method, Variant id, Variant @params) { - var dict = new VariantDict (@params); + void initialize (Jsonrpc.Server self, Jsonrpc.Client client, string method, Variant id, Variant @params) { + var dict = new VariantDict (@params); - int64 pid; - dict.lookup ("processId", "x", out pid); + int64 pid; + dict.lookup ("processId", "x", out pid); - string root_path; - dict.lookup ("rootPath", "s", out root_path); + string root_path; + dict.lookup ("rootPath", "s", out root_path); - //log (@"pid = $pid, root_path = $root_path\n"); + client.reply (id, buildDict( + capabilities: buildDict ( + textDocumentSync: new Variant.int16 (TextDocumentSyncKind.Full) + ) + )); + } - client.reply (id, buildDict( - capabilities: buildDict ( - textDocumentSync: new Variant.int16 (TextDocumentSyncKind.Full) - ) - )); - } + void textDocumentDidOpen (Jsonrpc.Client client, Variant @params) { + var document = @params.lookup_value ("textDocument", VariantType.VARDICT); - void textDocumentDidOpen (Jsonrpc.Client client, Variant @params) { - var document = @params.lookup_value ("textDocument", VariantType.VARDICT); + string uri = (string) document.lookup_value ("uri", VariantType.STRING); + string languageId = (string) document.lookup_value ("languageId", VariantType.STRING); + string fileContents = (string) document.lookup_value ("text", VariantType.STRING); - string uri = (string) document.lookup_value ("uri", VariantType.STRING); - string languageId = (string) document.lookup_value ("languageId", VariantType.STRING); - string fileContents = (string) document.lookup_value ("text", VariantType.STRING); + if (languageId != "vala") { + warning (@"$languageId file sent to vala language server"); + return; + } - if (languageId != "vala") { - warning (@"$languageId file sent to vala language server"); - return; + var type = Vala.SourceFileType.NONE; + if (uri.has_suffix (".vala")) + type = Vala.SourceFileType.SOURCE; + else if (uri.has_suffix (".vapi")) + type = Vala.SourceFileType.PACKAGE; + var filename = Filename.from_uri (uri); + var source = new Vala.SourceFile (ctx, type, filename, fileContents); + + ctx.add_source_file (source); + + if (ctx.report.get_errors () > 0) { + stderr.printf ("got errors !!!! :/\n"); + } } - } - void exit (Jsonrpc.Server self, Jsonrpc.Client client, string method, Variant id, Variant @params) { - loop.quit (); - } + void exit (Jsonrpc.Server self, Jsonrpc.Client client, string method, Variant id, Variant @params) { + loop.quit (); + } } void main () { - var loop = new MainLoop (); - new ValaLanguageServer (loop); - loop.run (); + var loop = new MainLoop (); + new Vls.Server (loop); + loop.run (); } diff --git a/meson.build b/meson.build index 36989d516..bf008a2ce 100644 --- a/meson.build +++ b/meson.build @@ -2,20 +2,31 @@ project('vala-language-server', 'vala', 'c') jsonrpc_sp = subproject('jsonrpc-glib') +libvala = dependency('libvala-0.36') + deps = [ dependency('glib-2.0'), dependency('gobject-2.0'), dependency('gio-unix-2.0'), + libvala, jsonrpc_sp.get_variable('libjsonrpc_glib_dep'), jsonrpc_sp.get_variable('libjsonrpc_glib_vapi'), ] +conf = configuration_data() +conf.set('LIBVALA_VERSION', libvala.version()) + +conf_vapi = configure_file(input: 'config.vala.in', + output: 'config.vala', + configuration: conf) + src = files([ 'main.vala', 'protocol.vala', + 'report.vala', ]) executable('vala-language-server', dependencies: deps, - sources: src, + sources: [src, conf_vapi], vala_args: ['--pkg', 'posix']) diff --git a/report.vala b/report.vala new file mode 100644 index 000000000..37703c713 --- /dev/null +++ b/report.vala @@ -0,0 +1,14 @@ +class Vls.Report : Vala.Report { + public override void depr (Vala.SourceReference? source, string message) { + ++warnings; + } + public override void note (Vala.SourceReference? source, string message) { + ++warnings; + } + public override void warn (Vala.SourceReference? source, string message) { + ++warnings; + } + public override void err (Vala.SourceReference? source, string message) { + ++errors; + } +}