#How do I write a JSON-RPC request to ZLS?

1 messages · Page 1 of 1 (latest)

patent sequoia
#

Hi, I'm writing a text editor as a hobby project, and I want to hook it up to ZLS so whenever I save my file, it sends the file buffer to ZLS, which can then return the errors, correct syntax et c. ```pub fn runZlsOnSave(self: *ZlsIntegration, filename: []const u8, filedata: *abuf) !void {
const zls_args = [_][]const u8{"zls"};
var child = std.process.Child.init(&zls_args, self.allocator);
defer _ = child.kill() catch anyerror;

    child.stdin_behavior = .Pipe;
    child.stdout_behavior = .Pipe;
    child.stderr_behavior = .Pipe;

    try child.spawn();

    if (child.stdin) |stdin| {
        const stdin_writer = stdin.writer();
        const initialize_request = try self.initRequest(filename);
        try stdin_writer.writeAll(initialize_request);
        const did_open_notification = try self.openRequest(filename, filedata.b.?);
        defer self.allocator.free(did_open_notification);
        try stdin_writer.writeAll(did_open_notification);
        try setStatusMessage("Wrote JSON", 1);
    }``` This is the write code which should make the JSON request, and there's similar read code. However I can't find much on the JSON formats, and when I asked Claude it gave me:        ``` return std.fmt.allocPrint(self.allocator, "Content-Length: {d}\r\n\r\n{{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"initialize\", \"params\": {{\"processId\": null, \"rootUri\": \"file://{s}\", \"capabilities\": {{}}}}}}", .{ filename.len + 100, filename });``` which returns std_error ParseError. Can someone help me figure out how do i do the read/writes to ZLS?
cursive vector
#

You can find documentation about the LSP protocol in the official Language Server Protcol Specification. (See the 'base protocol' section)

I believe that your code is not working because the content-length is incorrect.

You may be interested in checking out the zig-lsp-codegen library. It offers some of the necessary utilities to implement the LSP Protocol.

cursive vector
patent sequoia
#

thank you my friend i will look into this

patent sequoia
#

hello, i am stuck again. this might be a really dumb question, but i cannot seem to figure out a way to send and read message. could you help me understand how to structure a sendMessage call, say to check a particular file's syntax or one string? Thanks in advance

cursive vector
#

Is your problem that you do not know how to send and receive messages or that you don't know what messages should be send to achieve what you want? Some clarification on what you are stuck at would be appreciated.

If your issue is the latter then the following may help you:
If you want to check a particular file for syntax errors, you will need to send a textDocument/didOpen notification to send the document to the server. The server (ZLS) can then send a textDocument/publishDiagnostics notification that contains diagnostics like syntax errors for a given file.
I've modified the previous example to do this: https://zigbin.io/e45fe8

Keep in mind that a client like a text editor will usually keep the Server (ZLS) continuously running in the background instead of starting the Server whenever it need some information.

patent sequoia
#

hi, thank you so much for replying. i understand from the previous file you sent me how the general structure of sending and receiving works, a la converting the message to a JSON and then decoding it. what i dont understand specifically is the structure to send and receive messages, especially with params. your example looks quite helpful i will peruse it and get back to you with further questions, i hope thats okay