Skip to content

Commit

Permalink
check linkSupport when handling goto requests (#1389)
Browse files Browse the repository at this point in the history
Starting from LSP 3.14.0, an optional flag `linkSupport` is
introduced which changes the result type to LocationLink[]

This patch makes `zls` backwards-compatible

Signed-off-by: xphoniex <[email protected]>
Co-authored-by: nullptrdevs <[email protected]>
Co-authored-by: xphoniex <[email protected]>
  • Loading branch information
3 people committed Aug 7, 2023
1 parent c29ff3c commit 5bfff2a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const ClientCapabilities = packed struct {
label_details_support: bool = false,
supports_configuration: bool = false,
supports_workspace_did_change_configuration_dynamic_registration: bool = false,
supports_textDocument_definition_linkSupport: bool = false,
};

pub const Error = error{
Expand Down Expand Up @@ -414,6 +415,9 @@ fn initializeHandler(server: *Server, _: std.mem.Allocator, request: types.Initi
}
}
}
if (textDocument.definition) |definition| {
server.client_capabilities.supports_textDocument_definition_linkSupport = definition.linkSupport orelse false;
}
}

if (request.capabilities.workspace) |workspace| {
Expand Down Expand Up @@ -900,8 +904,20 @@ fn gotoHandler(
var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip);
defer analyser.deinit();

const response = try goto.goto(&analyser, &server.document_store, arena, handle, source_index, kind, server.offset_encoding) orelse return null;
if (server.client_capabilities.supports_textDocument_definition_linkSupport) {
return .{
.array_of_DefinitionLink = response,
};
}

var aol = try arena.alloc(types.Location, response.len);
for (0..response.len) |index| {
aol[index].uri = response[index].targetUri;
aol[index].range = response[index].targetSelectionRange;
}
return .{
.array_of_DefinitionLink = try goto.goto(&analyser, &server.document_store, arena, handle, source_index, kind, server.offset_encoding) orelse return null,
.Definition = .{ .array_of_Location = aol },
};
}

Expand All @@ -912,7 +928,10 @@ fn gotoTypeDefinitionHandler(server: *Server, arena: std.mem.Allocator, request:
.workDoneToken = request.workDoneToken,
.partialResultToken = request.partialResultToken,
})) orelse return null;
return .{ .array_of_DefinitionLink = response.array_of_DefinitionLink };
return switch (response) {
.array_of_DefinitionLink => |adl| .{ .array_of_DefinitionLink = adl },
.Definition => |def| .{ .Definition = def },
};
}

fn gotoImplementationHandler(server: *Server, arena: std.mem.Allocator, request: types.ImplementationParams) Error!ResultType("textDocument/implementation") {
Expand All @@ -922,7 +941,10 @@ fn gotoImplementationHandler(server: *Server, arena: std.mem.Allocator, request:
.workDoneToken = request.workDoneToken,
.partialResultToken = request.partialResultToken,
})) orelse return null;
return .{ .array_of_DefinitionLink = response.array_of_DefinitionLink };
return switch (response) {
.array_of_DefinitionLink => |adl| .{ .array_of_DefinitionLink = adl },
.Definition => |def| .{ .Definition = def },
};
}

fn gotoDeclarationHandler(server: *Server, arena: std.mem.Allocator, request: types.DeclarationParams) Error!ResultType("textDocument/declaration") {
Expand All @@ -932,7 +954,10 @@ fn gotoDeclarationHandler(server: *Server, arena: std.mem.Allocator, request: ty
.workDoneToken = request.workDoneToken,
.partialResultToken = request.partialResultToken,
})) orelse return null;
return .{ .array_of_DeclarationLink = response.array_of_DefinitionLink };
return switch (response) {
.array_of_DefinitionLink => |adl| .{ .array_of_DeclarationLink = adl },
.Definition => |def| .{ .Declaration = .{ .array_of_Location = def.array_of_Location } },
};
}

pub fn hoverHandler(server: *Server, arena: std.mem.Allocator, request: types.HoverParams) Error!?types.Hover {
Expand Down
1 change: 1 addition & 0 deletions tests/context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub const Context = struct {

// TODO this line shouldn't be needed
context.server.client_capabilities.label_details_support = false;
context.server.client_capabilities.supports_textDocument_definition_linkSupport = true;

return context;
}
Expand Down

0 comments on commit 5bfff2a

Please sign in to comment.