Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
exe.addModule("zls", b.dependency("zls", .{}).module("zls"));
exe.install();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be replaced with b.installArtifact(exe);


const run_cmd = exe.run();

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());

if (b.args) |args| {
Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
.version = "1.0.0",
.dependencies = .{
.zls = .{
.url = "https://github.com/zigtools/zls/archive/build-runner-param-plus-library.tar.gz",
.hash = "1220d72a4bc8e8c934d3854b29e065cbe7d09328c83368b0e658d3f6c0acd7424d92",
.url = "https://github.com/zigtools/zls/archive/refs/tags/0.11.0.tar.gz",
.hash = "1220ef39b9be17640457ba1a737ebbc664d7b728a84b3e088d89258a115816bf28c9",
},
},
}
12 changes: 7 additions & 5 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn main() !void {
// Create a server, then set its encoding
// to utf-8 so we can use the same indices
// as a normal zig []const u8
var server = try zls.Server.create(allocator, &config, null, false, false);
var server = try zls.Server.create(allocator, &config, null);
server.offset_encoding = .@"utf-8";
defer server.destroy();

Expand All @@ -48,7 +48,7 @@ pub fn main() !void {
while (true) {
// Free the server arena if it's past
// a certain threshold
defer server.maybeFreeArena();
//defer server.maybeFreeArena();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no equivalent function for maybeFreeArena. Instead you should create your own ArenaAllocator here and use it when sending request to the zls.


var stdio = std.io.getStdIn().reader();
const input = stdio.readUntilDelimiterOrEof(&input_buf, '\n') catch |err| switch (err) {
Expand All @@ -71,15 +71,17 @@ pub fn main() !void {
, .{input}));

// We request completions from zls
const completions: []const zls.types.CompletionItem = (try server.completionHandler(.{

const emptyList: zls.Server.ResultType("textDocument/completion") = .{ .CompletionList = zls.types.CompletionList{ .isIncomplete = true, .items = &.{} } };
const completions: []const zls.types.CompletionItem = (try server.completionHandler(allocator, .{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't intend for the completionHandler to be directly accesible here. Could you please call server.sendRequestSync(arena, "textDocument/completion", params) here.

You are also leaking memory here because completionHandler expects a arena-like allocator but you are passing std.heap.page_allocator.

.textDocument = .{
.uri = "file:///C:/Programming/Zig/zls-as-lib-demo/src/bs.zig",
},
.position = .{
.line = 4,
.character = @intCast(u32, 4 + input.len),
.character = @intCast(4 + input.len),
},
}) orelse zls.types.CompletionList{ .isIncomplete = true, .items = &.{} }).items;
}) orelse emptyList).?.CompletionList.items;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the unwrap is needed here because zls.Server.ResultType("textDocument/completion") is the following type:
https://github.com/zigtools/zls/blob/5bfff2a4b9ee01a7bab5fc26fad6e174f289c28d/src/lsp.zig#L7441-L7445

?union(enum) {
    array_of_CompletionItem: []const CompletionItem,
    CompletionList: CompletionList,
    pub usingnamespace UnionParser(@This());
}

This means that ((try server.completionHandler(...)) orelse emptyList) resolves to an optional because emptyList is optional.


// We print out the completions
for (completions) |comp| {
Expand Down