From 75c94f7ab88ade31c10c209fd5995a3e692ca1f6 Mon Sep 17 00:00:00 2001 From: Carter Snook Date: Thu, 14 Mar 2024 00:42:00 -0500 Subject: [PATCH] fix(autodoc): include entire code on line in source --- lib/docs/wasm/main.zig | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/docs/wasm/main.zig b/lib/docs/wasm/main.zig index 496fd618d5f7..ffb2f8b61d15 100644 --- a/lib/docs/wasm/main.zig +++ b/lib/docs/wasm/main.zig @@ -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); @@ -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, .{ + .include_entire_line = true, + }) catch |err| { fatal("unable to render source: {s}", .{@errorName(err)}); }; return String.init(string_result.items); @@ -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( @@ -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 (