Skip to content

Commit

Permalink
vala.codecontext
Browse files Browse the repository at this point in the history
  • Loading branch information
benwaffle committed Nov 23, 2017
1 parent 25dcf74 commit f192c9e
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ root = true
end_of_line = lf
insert_final_newline = true

[*.vala]
[*.{vala,vapi}]
indent_style = space
indent_size = 4
3 changes: 3 additions & 0 deletions config.vala.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Config {
const string libvala_version = "@LIBVALA_VERSION@";
}
162 changes: 100 additions & 62 deletions main.vala
Original file line number Diff line number Diff line change
@@ -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 ();
}
13 changes: 12 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
14 changes: 14 additions & 0 deletions report.vala
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit f192c9e

Please sign in to comment.