Skip to content

Commit

Permalink
Use zig-lsp-codegen (#850)
Browse files Browse the repository at this point in the history
* add lsp.zig

* change references from types.zig to lsp.zig

* remove types.zig and requests.zig

* add tres as a submodule

* transition codebase from types.zig to lsp.zig

* update lsp.zig

* completely overhaul message handler

* fix memory errors

* partially transition tests to lsp.zig

* update lsp.zig

* more test fixes

* disable failing tests

* fix message handling bugs

* fix remaining tests

* access correct union in diff.applyTextEdits

* more message handler fixes

* run zig fmt

* update tres submodule

* fix memory access to freed memory

* simplify initialize_msg for testing

* check if publishDiagnostics is supported
  • Loading branch information
Techatrix authored Dec 27, 2022
1 parent 9418823 commit 61c0981
Show file tree
Hide file tree
Showing 28 changed files with 9,003 additions and 1,869 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "src/tracy"]
path = src/tracy
url = https://github.com/wolfpld/tracy
[submodule "src/tres"]
path = src/tres
url = https://github.com/ziglibs/tres.git
3 changes: 3 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ pub fn build(b: *std.build.Builder) !void {
const known_folders_path = b.option([]const u8, "known-folders", "Path to known-folders package (default: " ++ KNOWN_FOLDERS_DEFAULT_PATH ++ ")") orelse KNOWN_FOLDERS_DEFAULT_PATH;
exe.addPackage(.{ .name = "known-folders", .source = .{ .path = known_folders_path } });

exe.addPackage(.{ .name = "tres", .source = .{ .path = "src/tres/tres.zig" } });

if (enable_tracy) {
const client_cpp = "src/tracy/TracyClient.cpp";

Expand Down Expand Up @@ -146,6 +148,7 @@ pub fn build(b: *std.build.Builder) !void {
}

tests.addPackage(.{ .name = "zls", .source = .{ .path = "src/zls.zig" }, .dependencies = exe.packages.items });
tests.addPackage(.{ .name = "tres", .source = .{ .path = "src/tres/tres.zig" } });
tests.setBuildMode(.Debug);
tests.setTarget(target);
test_step.dependOn(&tests.step);
Expand Down
3 changes: 1 addition & 2 deletions src/DocumentStore.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");
const types = @import("types.zig");
const requests = @import("requests.zig");
const types = @import("lsp.zig");
const URI = @import("uri.zig");
const analysis = @import("analysis.zig");
const offsets = @import("offsets.zig");
Expand Down
38 changes: 27 additions & 11 deletions src/header.zig → src/Header.zig
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
const std = @import("std");

const RequestHeader = struct {
content_length: usize,
const Header = @This();

/// null implies "application/vscode-jsonrpc; charset=utf-8"
content_type: ?[]const u8,
content_length: usize,

pub fn deinit(self: @This(), allocator: std.mem.Allocator) void {
if (self.content_type) |ct| allocator.free(ct);
}
};
/// null implies "application/vscode-jsonrpc; charset=utf-8"
content_type: ?[]const u8 = null,

pub fn deinit(self: @This(), allocator: std.mem.Allocator) void {
if (self.content_type) |ct| allocator.free(ct);
}

pub fn readRequestHeader(allocator: std.mem.Allocator, instream: anytype) !RequestHeader {
var r = RequestHeader{
// Caller owns returned memory.
pub fn parse(allocator: std.mem.Allocator, reader: anytype) !Header {
var r = Header{
.content_length = undefined,
.content_type = null,
};
errdefer r.deinit(allocator);

var has_content_length = false;
while (true) {
const header = try instream.readUntilDelimiterAlloc(allocator, '\n', 0x100);
const header = try reader.readUntilDelimiterAlloc(allocator, '\n', 0x100);
defer allocator.free(header);
if (header.len == 0 or header[header.len - 1] != '\r') return error.MissingCarriageReturn;
if (header.len == 1) break;
Expand All @@ -41,3 +42,18 @@ pub fn readRequestHeader(allocator: std.mem.Allocator, instream: anytype) !Reque

return r;
}

pub fn format(
header: Header,
comptime unused_fmt_string: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) @TypeOf(writer).Error!void {
_ = options;
std.debug.assert(unused_fmt_string.len == 0);
try writer.print("Content-Length: {}\r\n", .{header.content_length});
if (header.content_type) |content_type| {
try writer.print("Content-Type: {s}\r\n", .{content_type});
}
try writer.writeAll("\r\n");
}
Loading

0 comments on commit 61c0981

Please sign in to comment.