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

autodoc: include entire code on line in source #19301

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 13 additions & 2 deletions lib/docs/wasm/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,9 @@ export fn decl_source_html(decl_index: Decl.Index) String {
const decl = decl_index.get();

string_result.clearRetainingCapacity();
file_source_html(decl.file, &string_result, decl.ast_node, .{}) catch |err| {
file_source_html(decl.file, &string_result, decl.ast_node, .{
.include_entire_line = true,
}) catch |err| {
fatal("unable to render source: {s}", .{@errorName(err)});
};
return String.init(string_result.items);
Expand All @@ -540,7 +542,9 @@ export fn decl_doctest_html(decl_index: Decl.Index) String {
return String.init("");

string_result.clearRetainingCapacity();
file_source_html(decl.file, &string_result, doctest_ast_node, .{}) catch |err| {
file_source_html(decl.file, &string_result, doctest_ast_node, .{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds the same behavior to rendering doctests.

.include_entire_line = true,
}) catch |err| {
fatal("unable to render source: {s}", .{@errorName(err)});
};
return String.init(string_result.items);
Expand Down Expand Up @@ -914,6 +918,7 @@ const RenderSourceOptions = struct {
skip_comments: bool = false,
collapse_whitespace: bool = false,
fn_link: Decl.Index = .none,
include_entire_line: bool = false,
};

fn file_source_html(
Expand All @@ -936,6 +941,12 @@ fn file_source_html(
const start_token = ast.firstToken(root_node);
const end_token = ast.lastToken(root_node) + 1;

if (options.include_entire_line) {
if (std.mem.lastIndexOfAny(u8, ast.source[0..token_starts[start_token]], "\n\r")) |newline_idx| {
try out.appendSlice(gpa, ast.source[newline_idx + 1 .. token_starts[start_token]]);
}
}

var cursor: usize = token_starts[start_token];

for (
Expand Down