Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lsp_hover: format function signatures if needed #2025

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,37 @@ pub fn getFunctionSignature(tree: Ast, func: Ast.full.FnProto) []const u8 {
return offsets.tokensToSlice(tree, first_token, last_token);
}

/// Formats a function signature in markdown style with 4 space indent for cases where it's a nested
/// decl and is more than a single line.
///
/// Otherwise it will write the signature as it's provided but in markdown style.
pub fn formatFunctionSignatureMarkdown(signature: []const u8, writer: anytype) @TypeOf(writer).Error!void {
var iter = std.mem.tokenizeScalar(u8, signature, '\n');

try writer.writeAll("```zig\n");

const first = iter.next() orelse unreachable; // Expected at least one.
try writer.writeAll(first);

while (iter.next()) |slice| {
const trimmed = std.mem.trimLeft(u8, slice, " ");

switch (trimmed[0]) {
')', '}' => {
try writer.writeAll("\n");
try writer.writeAll(trimmed);
},
else => {
try writer.writeAll("\n");
try writer.writeAll(" ");
try writer.writeAll(trimmed);
},
}
}

try writer.writeAll("\n```");
}

fn formatSnippetPlaceholder(
data: []const u8,
comptime fmt: []const u8,
Expand Down
2 changes: 1 addition & 1 deletion src/features/hover.zig
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn hoverSymbolRecursive(
for (doc_strings.items) |doc|
try writer.print("{s}\n\n", .{doc});
if (is_fn) {
try writer.print("```zig\n{s}\n```", .{def_str});
try Analyser.formatFunctionSignatureMarkdown(def_str, writer);
} else {
try writer.print("```zig\n{s}\n```\n```zig\n({s})\n```", .{ def_str, resolved_type_str });
}
Expand Down
27 changes: 27 additions & 0 deletions tests/lsp_features/hover.zig
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,33 @@ test "function parameter" {
);
}

test "nested" {
try testHover(
\\const Error = error{InvalidBaz};
\\pub fn FooResponse(comptime T: type) type {
\\ return struct {baz: T};
\\}
\\
\\pub fn Foo(comptime T: type) type {
\\ return struct {
\\ pub fn fooAnd<cursor>Bar(self: *T, baz: u8) (Error || error{
\\ InvalidFoo,
\\ InvalidBar,
\\ })!FooResponse(T) {}
\\ };
\\}
,
\\```zig
\\fn fooAndBar(self: *T, baz: u8) (Error || error{
\\ InvalidFoo,
\\ InvalidBar,
\\})!FooResponse(T)
\\```
\\
\\Go to [FooResponse](file:///test.zig#L2)
);
}

test "optional" {
try testHover(
\\const S = struct { a: i32 };
Expand Down