diff --git a/src/analysis.zig b/src/analysis.zig index f4fbc41..af6d6b5 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -20,39 +20,29 @@ pub fn getFunctionByName(tree: *ast.Tree, name: []const u8) ?*ast.Node.FnProto { 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: ///```zig ///var comments = getFunctionDocComments(allocator, tree, func); ///defer if (comments) |comments_pointer| allocator.free(comments_pointer); ///``` pub fn getDocComments(allocator: *std.mem.Allocator, tree: *ast.Tree, node: *ast.Node) !?[]const u8 { - switch (node.id) { - .FnProto => { - 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 => {}, + if (getDocCommentNode(tree, node)) |doc_comment_node| { + return try collectDocComments(allocator, tree, doc_comment_node); } return null; } diff --git a/src/main.zig b/src/main.zig index f986dcb..d74f50e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -231,7 +231,7 @@ fn nodeToCompletion( node: *std.zig.ast.Node, config: Config, ) 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{ .kind = .Markdown, .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 { 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: { const var_decl = result.decl.cast(std.zig.ast.Node.VarDecl).?; 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), }; + 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{ .id = .{ .Integer = id }, .result = .{ .Hover = .{ - .contents = .{ .value = str_value }, + .contents = .{ .value = md_string }, }, }, }); diff --git a/src/types.zig b/src/types.zig index 0ed9471..7730dd7 100644 --- a/src/types.zig +++ b/src/types.zig @@ -47,14 +47,9 @@ pub const NotificationParams = union(enum) { PublishDiagnosticsParams: PublishDiagnosticsParams }; -pub const MarkedString = struct { - language: String = "zig", - value: String, -}; - /// Hover response pub const Hover = struct { - contents: MarkedString, + contents: MarkupContent, }; /// Params of a response (result)