Added doc comments on hover
This commit is contained in:
parent
046ff97216
commit
ef95c76e45
@ -20,39 +20,29 @@ pub fn getFunctionByName(tree: *ast.Tree, name: []const u8) ?*ast.Node.FnProto {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a function's doc comments, caller must free memory when a value is returned
|
/// Get a declaration's doc comment node
|
||||||
|
fn getDocCommentNode(tree: *ast.Tree, node: *ast.Node) ?*ast.Node.DocComment {
|
||||||
|
if (node.cast(ast.Node.FnProto)) |func| {
|
||||||
|
return func.doc_comments;
|
||||||
|
} else if (node.cast(ast.Node.VarDecl)) |var_decl| {
|
||||||
|
return var_decl.doc_comments;
|
||||||
|
} else if (node.cast(ast.Node.ContainerField)) |field| {
|
||||||
|
return field.doc_comments;
|
||||||
|
} else if (node.cast(ast.Node.ErrorTag)) |tag| {
|
||||||
|
return tag.doc_comments;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets a declaration's doc comments, caller must free memory when a value is returned
|
||||||
/// Like:
|
/// Like:
|
||||||
///```zig
|
///```zig
|
||||||
///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) !?[]const u8 {
|
||||||
switch (node.id) {
|
if (getDocCommentNode(tree, node)) |doc_comment_node| {
|
||||||
.FnProto => {
|
return try collectDocComments(allocator, tree, doc_comment_node);
|
||||||
const func = node.cast(ast.Node.FnProto).?;
|
|
||||||
if (func.doc_comments) |doc_comments| {
|
|
||||||
return try collectDocComments(allocator, tree, doc_comments);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
.VarDecl => {
|
|
||||||
const var_decl = node.cast(ast.Node.VarDecl).?;
|
|
||||||
if (var_decl.doc_comments) |doc_comments| {
|
|
||||||
return try collectDocComments(allocator, tree, doc_comments);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
.ContainerField => {
|
|
||||||
const field = node.cast(ast.Node.ContainerField).?;
|
|
||||||
if (field.doc_comments) |doc_comments| {
|
|
||||||
return try collectDocComments(allocator, tree, doc_comments);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
.ErrorTag => {
|
|
||||||
const tag = node.cast(ast.Node.ErrorTag).?;
|
|
||||||
if (tag.doc_comments) |doc_comments| {
|
|
||||||
return try collectDocComments(allocator, tree, doc_comments);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
else => {},
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
12
src/main.zig
12
src/main.zig
@ -231,7 +231,7 @@ fn nodeToCompletion(
|
|||||||
node: *std.zig.ast.Node,
|
node: *std.zig.ast.Node,
|
||||||
config: Config,
|
config: Config,
|
||||||
) error{OutOfMemory}!void {
|
) error{OutOfMemory}!void {
|
||||||
var doc = if (try analysis.getDocComments(list.allocator, analysis_ctx.tree(), node)) |doc_comments|
|
const doc = if (try analysis.getDocComments(list.allocator, analysis_ctx.tree(), node)) |doc_comments|
|
||||||
types.MarkupContent{
|
types.MarkupContent{
|
||||||
.kind = .Markdown,
|
.kind = .Markdown,
|
||||||
.value = doc_comments,
|
.value = doc_comments,
|
||||||
@ -359,7 +359,12 @@ fn gotoDefinitionSymbol(id: i64, analysis_ctx: *DocumentStore.AnalysisContext, d
|
|||||||
fn hoverSymbol(id: i64, analysis_ctx: *DocumentStore.AnalysisContext, decl: *std.zig.ast.Node) !void {
|
fn hoverSymbol(id: i64, analysis_ctx: *DocumentStore.AnalysisContext, decl: *std.zig.ast.Node) !void {
|
||||||
const result = try resolveVarDeclFnAlias(analysis_ctx, decl);
|
const result = try resolveVarDeclFnAlias(analysis_ctx, decl);
|
||||||
|
|
||||||
const str_value = switch (result.decl.id) {
|
const doc_str = if (try analysis.getDocComments(&analysis_ctx.arena.allocator, result.analysis_ctx.tree(), result.decl)) |str|
|
||||||
|
str
|
||||||
|
else
|
||||||
|
"";
|
||||||
|
|
||||||
|
const signature_str = switch (result.decl.id) {
|
||||||
.VarDecl => blk: {
|
.VarDecl => blk: {
|
||||||
const var_decl = result.decl.cast(std.zig.ast.Node.VarDecl).?;
|
const var_decl = result.decl.cast(std.zig.ast.Node.VarDecl).?;
|
||||||
break :blk analysis.getVariableSignature(result.analysis_ctx.tree(), var_decl);
|
break :blk analysis.getVariableSignature(result.analysis_ctx.tree(), var_decl);
|
||||||
@ -371,11 +376,12 @@ fn hoverSymbol(id: i64, analysis_ctx: *DocumentStore.AnalysisContext, decl: *std
|
|||||||
else => analysis.nodeToString(result.analysis_ctx.tree(), result.decl) orelse return try respondGeneric(id, null_result_response),
|
else => analysis.nodeToString(result.analysis_ctx.tree(), result.decl) orelse return try respondGeneric(id, null_result_response),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const md_string = try std.fmt.allocPrint(&analysis_ctx.arena.allocator, "```zig\n{}\n```\n```markdown\n{}\n```", .{ signature_str, doc_str });
|
||||||
try send(types.Response{
|
try send(types.Response{
|
||||||
.id = .{ .Integer = id },
|
.id = .{ .Integer = id },
|
||||||
.result = .{
|
.result = .{
|
||||||
.Hover = .{
|
.Hover = .{
|
||||||
.contents = .{ .value = str_value },
|
.contents = .{ .value = md_string },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -47,14 +47,9 @@ pub const NotificationParams = union(enum) {
|
|||||||
PublishDiagnosticsParams: PublishDiagnosticsParams
|
PublishDiagnosticsParams: PublishDiagnosticsParams
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const MarkedString = struct {
|
|
||||||
language: String = "zig",
|
|
||||||
value: String,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Hover response
|
/// Hover response
|
||||||
pub const Hover = struct {
|
pub const Hover = struct {
|
||||||
contents: MarkedString,
|
contents: MarkupContent,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Params of a response (result)
|
/// Params of a response (result)
|
||||||
|
Loading…
Reference in New Issue
Block a user