From 5bfff2a4b9ee01a7bab5fc26fad6e174f289c28d Mon Sep 17 00:00:00 2001 From: Dave Date: Mon, 7 Aug 2023 16:20:59 +0330 Subject: [PATCH] check `linkSupport` when handling goto requests (#1389) 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 Co-authored-by: nullptrdevs <16590917+nullptrdevs@users.noreply.github.com> Co-authored-by: xphoniex --- src/Server.zig | 33 +++++++++++++++++++++++++++++---- tests/context.zig | 1 + 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Server.zig b/src/Server.zig index 9d72cf8ce..b376e431f 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -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{ @@ -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| { @@ -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 }, }; } @@ -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") { @@ -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") { @@ -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 { diff --git a/tests/context.zig b/tests/context.zig index 6b18857d0..63229d195 100644 --- a/tests/context.zig +++ b/tests/context.zig @@ -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; }