Skip to content

Commit

Permalink
respect hover support of client when creating signature help (#1732)
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix authored Jan 30, 2024
1 parent 17befd1 commit 63f5ad1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
20 changes: 20 additions & 0 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const ClientCapabilities = struct {
supports_publish_diagnostics: bool = false,
supports_code_action_fixall: bool = false,
hover_supports_md: bool = false,
signature_help_supports_md: bool = false,
completion_doc_supports_md: bool = false,
label_details_support: bool = false,
supports_configuration: bool = false,
Expand Down Expand Up @@ -469,6 +470,21 @@ 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 (textDocument.signatureHelp) |signature_help_capabilities| {
if (signature_help_capabilities.signatureInformation) |signature_information| {
if (signature_information.documentationFormat) |content_format| {
for (content_format) |format| {
if (format == .plaintext) {
break;
}
if (format == .markdown) {
server.client_capabilities.signature_help_supports_md = true;
break;
}
}
}
}
}
}

if (request.capabilities.workspace) |workspace| {
Expand Down Expand Up @@ -1303,6 +1319,8 @@ fn signatureHelpHandler(server: *Server, arena: std.mem.Allocator, request: type

const source_index = offsets.positionToIndex(handle.tree.source, request.position, server.offset_encoding);

const markup_kind: types.MarkupKind = if (server.client_capabilities.signature_help_supports_md) .markdown else .plaintext;

var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip, handle);
defer analyser.deinit();

Expand All @@ -1311,6 +1329,7 @@ fn signatureHelpHandler(server: *Server, arena: std.mem.Allocator, request: type
arena,
handle,
source_index,
markup_kind,
)) orelse return null;

var signatures = try arena.alloc(types.SignatureInformation, 1);
Expand Down Expand Up @@ -1435,6 +1454,7 @@ fn inlayHintHandler(server: *Server, arena: std.mem.Allocator, request: types.In

const handle = server.document_store.getHandle(request.textDocument.uri) orelse return null;

// The Language Server Specification does not provide a client capabilities that allows the client to specify the MarkupKind of inlay hints.
const hover_kind: types.MarkupKind = if (server.client_capabilities.hover_supports_md) .markdown else .plaintext;
const loc = offsets.rangeToLoc(handle.tree.source, request.range, server.offset_encoding);

Expand Down
31 changes: 20 additions & 11 deletions src/features/signature_help.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn fnProtoToSignatureInfo(
commas: u32,
skip_self_param: bool,
func_type: Analyser.Type,
markup_kind: types.MarkupKind,
) !types.SignatureInformation {
const fn_node_handle = func_type.data.other; // this assumes that function types can only be Ast nodes
const fn_node = fn_node_handle.node;
Expand All @@ -25,7 +26,7 @@ fn fnProtoToSignatureInfo(
const proto = tree.fullFnProto(&buffer, fn_node).?;

const label = Analyser.getFunctionSignature(tree, proto);
const proto_comments = (try Analyser.getDocComments(arena, tree, fn_node)) orelse "";
const proto_comments = try Analyser.getDocComments(arena, tree, fn_node);

const arg_idx = if (skip_self_param) blk: {
const has_self_param = try analyser.hasSelfParam(fn_handle, proto);
Expand All @@ -38,28 +39,34 @@ fn fnProtoToSignatureInfo(
const param_comments = if (param.first_doc_comment) |dc|
try Analyser.collectDocComments(arena, tree, dc, false)
else
"";
null;

try params.append(arena, .{
.label = .{ .string = ast.paramSlice(tree, param) },
.documentation = .{ .MarkupContent = .{
.kind = .markdown,
.value = param_comments,
} },
.documentation = if (param_comments) |comment| .{ .MarkupContent = .{
.kind = markup_kind,
.value = comment,
} } else null,
});
}
return types.SignatureInformation{
.label = label,
.documentation = .{ .MarkupContent = .{
.kind = .markdown,
.value = proto_comments,
} },
.documentation = if (proto_comments) |comment| .{ .MarkupContent = .{
.kind = markup_kind,
.value = comment,
} } else null,
.parameters = params.items,
.activeParameter = if (arg_idx < params.items.len) arg_idx else null,
};
}

pub fn getSignatureInfo(analyser: *Analyser, arena: std.mem.Allocator, handle: *DocumentStore.Handle, absolute_index: usize) !?types.SignatureInformation {
pub fn getSignatureInfo(
analyser: *Analyser,
arena: std.mem.Allocator,
handle: *DocumentStore.Handle,
absolute_index: usize,
markup_kind: types.MarkupKind,
) !?types.SignatureInformation {
const document_scope = try handle.getDocumentScope();
const innermost_block = Analyser.innermostBlockScope(document_scope, absolute_index);
const tree = handle.tree;
Expand Down Expand Up @@ -244,6 +251,7 @@ pub fn getSignatureInfo(analyser: *Analyser, arena: std.mem.Allocator, handle: *
paren_commas,
false,
func_type,
markup_kind,
);
}

Expand All @@ -266,6 +274,7 @@ pub fn getSignatureInfo(analyser: *Analyser, arena: std.mem.Allocator, handle: *
paren_commas,
skip_self_param,
func_type,
markup_kind,
);
}
},
Expand Down

0 comments on commit 63f5ad1

Please sign in to comment.