Take more client capabilities into account

This commit is contained in:
Alexandros Naskos 2020-06-12 17:42:41 +03:00
parent 612e93be6a
commit 98ebac6d64
3 changed files with 76 additions and 35 deletions

View File

@ -23,14 +23,24 @@ fn getDocCommentNode(tree: *ast.Tree, node: *ast.Node) ?*ast.Node.DocComment {
///var comments = getFunctionDocComments(allocator, tree, func); ///var comments = getFunctionDocComments(allocator, tree, func);
///defer if (comments) |comments_pointer| allocator.free(comments_pointer); ///defer if (comments) |comments_pointer| allocator.free(comments_pointer);
///``` ///```
pub fn getDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, node: *ast.Node) !?[]const u8 { pub fn getDocComments(
allocator: *std.mem.Allocator,
tree: *ast.Tree,
node: *ast.Node,
format: types.MarkupKind,
) !?[]const u8 {
if (getDocCommentNode(tree, node)) |doc_comment_node| { if (getDocCommentNode(tree, node)) |doc_comment_node| {
return try collectDocComments(allocator, tree, doc_comment_node); return try collectDocComments(allocator, tree, doc_comment_node, format);
} }
return null; return null;
} }
pub fn collectDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, doc_comments: *ast.Node.DocComment) ![]const u8 { pub fn collectDocComments(
allocator: *std.mem.Allocator,
tree: *ast.Tree,
doc_comments: *ast.Node.DocComment,
format: types.MarkupKind,
) ![]const u8 {
var lines = std.ArrayList([]const u8).init(allocator); var lines = std.ArrayList([]const u8).init(allocator);
defer lines.deinit(); defer lines.deinit();
@ -45,7 +55,7 @@ pub fn collectDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, doc_co
} }
} }
return try std.mem.join(allocator, "\\\n", lines.items); return try std.mem.join(allocator, if (format == .Markdown) "\\\n" else "\n", lines.items);
} }
/// Gets a function signature (keywords, name, return value) /// Gets a function signature (keywords, name, return value)

View File

@ -59,7 +59,7 @@ pub const TagStore = struct {
const item = types.CompletionItem{ const item = types.CompletionItem{
.label = try std.mem.dupe(alloc, u8, name), .label = try std.mem.dupe(alloc, u8, name),
.kind = .Constant, .kind = .Constant,
.documentation = if (try analysis.getDocComments(alloc, tree, tag)) |docs| .documentation = if (try analysis.getDocComments(alloc, tree, tag, .Markdown)) |docs|
.{ .{
.kind = .Markdown, .kind = .Markdown,
.value = docs, .value = docs,

View File

@ -20,6 +20,9 @@ var workspace_folder_configs: std.StringHashMap(?Config) = undefined;
const ClientCapabilities = struct { const ClientCapabilities = struct {
supports_snippets: bool = false, supports_snippets: bool = false,
supports_semantic_tokens: bool = false,
hover_supports_md: bool = false,
completion_doc_supports_md: bool = false,
}; };
var client_capabilities = ClientCapabilities{}; var client_capabilities = ClientCapabilities{};
@ -233,9 +236,15 @@ fn nodeToCompletion(
const node = node_handle.node; const node = node_handle.node;
const handle = node_handle.handle; const handle = node_handle.handle;
const doc = if (try analysis.getDocComments(list.allocator, handle.tree, node)) |doc_comments| const doc_kind: types.MarkupKind = if (client_capabilities.completion_doc_supports_md) .Markdown else .PlainText;
const doc = if (try analysis.getDocComments(
list.allocator,
handle.tree,
node,
doc_kind,
)) |doc_comments|
types.MarkupContent{ types.MarkupContent{
.kind = .Markdown, .kind = doc_kind,
.value = doc_comments, .value = doc_comments,
} }
else else
@ -418,11 +427,12 @@ fn gotoDefinitionSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, de
fn hoverSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, decl_handle: analysis.DeclWithHandle) !void { fn hoverSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, decl_handle: analysis.DeclWithHandle) !void {
const handle = decl_handle.handle; const handle = decl_handle.handle;
const hover_kind: types.MarkupKind = if (client_capabilities.hover_supports_md) .Markdown else .PlainText;
const md_string = switch (decl_handle.decl.*) { const md_string = switch (decl_handle.decl.*) {
.ast_node => |node| ast_node: { .ast_node => |node| ast_node: {
const result = try resolveVarDeclFnAlias(arena, .{ .node = node, .handle = handle }); const result = try resolveVarDeclFnAlias(arena, .{ .node = node, .handle = handle });
const doc_str = if (try analysis.getDocComments(&arena.allocator, result.handle.tree, result.node)) |str| const doc_str = if (try analysis.getDocComments(&arena.allocator, result.handle.tree, result.node, hover_kind)) |str|
str str
else else
""; "";
@ -443,38 +453,35 @@ fn hoverSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, decl_handle
else => analysis.nodeToString(result.handle.tree, result.node) orelse return try respondGeneric(id, null_result_response), else => analysis.nodeToString(result.handle.tree, result.node) orelse return try respondGeneric(id, null_result_response),
}; };
break :ast_node try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```\n{}", .{ signature_str, doc_str }); break :ast_node if (hover_kind == .Markdown)
try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```\n{}", .{ signature_str, doc_str })
else
try std.fmt.allocPrint(&arena.allocator, "{}\n{}", .{ signature_str, doc_str });
}, },
.param_decl => |param| param_decl: { .param_decl => |param| param_decl: {
const doc_str = if (param.doc_comments) |doc_comments| const doc_str = if (param.doc_comments) |doc_comments|
try analysis.collectDocComments(&arena.allocator, handle.tree, doc_comments) try analysis.collectDocComments(&arena.allocator, handle.tree, doc_comments, hover_kind)
else else
""; "";
break :param_decl try std.fmt.allocPrint( const signature_str = handle.tree.source[handle.tree.token_locs[param.firstToken()].start..handle.tree.token_locs[param.lastToken()].end];
&arena.allocator, break :param_decl if (hover_kind == .Markdown)
"```zig\n{}\n```\n{}", try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```\n{}", .{ signature_str, doc_str })
.{ else
handle.tree.source[handle.tree.token_locs[param.firstToken()].start..handle.tree.token_locs[param.lastToken()].end], try std.fmt.allocPrint(&arena.allocator, "{}\n{}", .{ signature_str, doc_str });
doc_str,
}, },
); .pointer_payload => |payload| if (hover_kind == .Markdown)
}, try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```", .{handle.tree.tokenSlice(payload.node.value_symbol.firstToken())})
.pointer_payload => |payload| try std.fmt.allocPrint( else
&arena.allocator, try std.fmt.allocPrint(&arena.allocator, "{}", .{handle.tree.tokenSlice(payload.node.value_symbol.firstToken())}),
"```zig\n{}\n```", .array_payload => |payload| if (hover_kind == .Markdown)
.{handle.tree.tokenSlice(payload.node.value_symbol.firstToken())}, try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```", .{handle.tree.tokenSlice(payload.identifier.firstToken())})
), else
.array_payload => |payload| try std.fmt.allocPrint( try std.fmt.allocPrint(&arena.allocator, "{}", .{handle.tree.tokenSlice(payload.identifier.firstToken())}),
&arena.allocator, .switch_payload => |payload| if (hover_kind == .Markdown)
"```zig\n{}\n```", try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```", .{handle.tree.tokenSlice(payload.node.value_symbol.firstToken())})
.{handle.tree.tokenSlice(payload.identifier.firstToken())}, else
), try std.fmt.allocPrint(&arena.allocator, "{}", .{handle.tree.tokenSlice(payload.node.value_symbol.firstToken())}),
.switch_payload => |payload| try std.fmt.allocPrint(
&arena.allocator,
"```zig\n{}\n```",
.{handle.tree.tokenSlice(payload.node.value_symbol.firstToken())},
),
}; };
try send(types.Response{ try send(types.Response{
@ -598,10 +605,11 @@ fn declToCompletion(context: DeclToCompletionContext, decl_handle: analysis.Decl
switch (decl_handle.decl.*) { switch (decl_handle.decl.*) {
.ast_node => |node| try nodeToCompletion(context.arena, context.completions, .{ .node = node, .handle = decl_handle.handle }, context.orig_handle, context.config.*), .ast_node => |node| try nodeToCompletion(context.arena, context.completions, .{ .node = node, .handle = decl_handle.handle }, context.orig_handle, context.config.*),
.param_decl => |param| { .param_decl => |param| {
const doc_kind: types.MarkupKind = if (client_capabilities.completion_doc_supports_md) .Markdown else .PlainText;
const doc = if (param.doc_comments) |doc_comments| const doc = if (param.doc_comments) |doc_comments|
types.MarkupContent{ types.MarkupContent{
.kind = .Markdown, .kind = doc_kind,
.value = try analysis.collectDocComments(&context.arena.allocator, tree, doc_comments), .value = try analysis.collectDocComments(&context.arena.allocator, tree, doc_comments, doc_kind),
} }
else else
null; null;
@ -823,14 +831,37 @@ fn processJsonRpc(parser: *std.json.Parser, json: []const u8, config: Config) !v
if (std.mem.eql(u8, method, "initialize")) { if (std.mem.eql(u8, method, "initialize")) {
const client_capabs = params.getValue("capabilities").?.Object; const client_capabs = params.getValue("capabilities").?.Object;
if (client_capabs.getValue("textDocument")) |text_doc_capabs| { if (client_capabs.getValue("textDocument")) |text_doc_capabs| {
if (text_doc_capabs.Object.getValue("completion")) |completion_capabs| { if (text_doc_capabs.Object.getValue("semanticTokens")) |_| {
if (completion_capabs.Object.getValue("completionItem")) |item_capabs| { client_capabilities.supports_semantic_tokens = true;
const maybe_support_snippet = item_capabs.Object.getValue("snippetSupport"); }
client_capabilities.supports_snippets = maybe_support_snippet != null and maybe_support_snippet.?.Bool;
if (text_doc_capabs.Object.getValue("hover")) |hover_capabs| {
if (hover_capabs.Object.getValue("contentFormat")) |content_formats| {
for (content_formats.Array.items) |format| {
if (std.mem.eql(u8, "markdown", format.String)) {
client_capabilities.hover_supports_md = true;
}
} }
} }
} }
if (text_doc_capabs.Object.getValue("completion")) |completion_capabs| {
if (completion_capabs.Object.getValue("completionItem")) |item_capabs| {
const maybe_support_snippet = item_capabs.Object.getValue("snippetSupport");
client_capabilities.supports_snippets = maybe_support_snippet != null and maybe_support_snippet.?.Bool;
if (item_capabs.Object.getValue("documentationFormat")) |content_formats| {
for (content_formats.Array.items) |format| {
if (std.mem.eql(u8, "markdown", format.String)) {
client_capabilities.completion_doc_supports_md = true;
}
}
}
}
}
}
std.debug.warn("{}\n", .{client_capabilities});
try respondGeneric(id, initialize_response); try respondGeneric(id, initialize_response);
} else if (std.mem.eql(u8, method, "initialized")) { } else if (std.mem.eql(u8, method, "initialized")) {
// Send the workspaceFolders request // Send the workspaceFolders request