authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-10 01:43:12+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-10 01:43:12+01:00
log6c7814fabd5cc94dce13ed02e5313069be9af482
treee524e1558a6ab228ec93ce4832a7087c231d1063
parentb607b0c27af71a541811955d3012f5346c997b09
parentc99b43e3da014f943018f3baabf590122cd216a6

Merge pull request 'Autodoc: display line numbers in source code display' (#31155) from nektro/fork-zig:nektro-patch-54643 into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31155 Reviewed-by: Andrew Kelley <andrew@ziglang.org>

4 files changed, 50 insertions(+), 1 deletions(-)

lib/docs/index.html+17-1
......@@ -40,6 +40,15 @@
4040 code a {
4141 color: #000000;
4242 }
43 .source-code {
44 display: grid;
45 grid-template-columns: auto 1fr;
46 align-items: start;
47 }
48 .source-line-numbers pre {
49 text-align: right;
50 color: #666;
51 }
4352 #listFields > div, #listParams > div {
4453 margin-bottom: 1em;
4554 }
......@@ -429,7 +438,14 @@
429438 </div>
430439 <div id="sectSource" class="hidden">
431440 <h2>Source Code</h2>
432 <pre><code id="sourceText"></code></pre>
441 <div class="source-code">
442 <div class="source-line-numbers">
443 <pre><code id="sourceLineNumbers"></code></pre>
444 </div>
445 <div class="source-text">
446 <pre><code id="sourceText"></code></pre>
447 </div>
448 </div>
433449 </div>
434450 </section>
435451 <div id="helpDialog" class="hidden">
lib/docs/main.js+9
......@@ -50,6 +50,7 @@
5050 const domSectTypes = document.getElementById("sectTypes");
5151 const domSectValues = document.getElementById("sectValues");
5252 const domSourceText = document.getElementById("sourceText");
53 const domSourceLineNumbers = document.getElementById("sourceLineNumbers");
5354 const domStatus = document.getElementById("status");
5455 const domTableFnErrors = document.getElementById("tableFnErrors");
5556 const domTldDocs = document.getElementById("tldDocs");
......@@ -238,6 +239,7 @@
238239 href: location.hash,
239240 }]);
240241
242 domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
241243 domSourceText.innerHTML = declSourceHtml(decl_index);
242244
243245 domSectSource.classList.remove("hidden");
......@@ -389,6 +391,7 @@
389391 if (members.length !== 0 || fields.length !== 0) {
390392 renderNamespace(decl_index, members, fields);
391393 } else {
394 domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
392395 domSourceText.innerHTML = declSourceHtml(decl_index);
393396 domSectSource.classList.remove("hidden");
394397 }
......@@ -419,6 +422,7 @@
419422 renderErrorSet(base_decl, errorSetNodeList(decl_index, errorSetNode));
420423 }
421424
425 domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
422426 domSourceText.innerHTML = declSourceHtml(decl_index);
423427 domSectSource.classList.remove("hidden");
424428 }
......@@ -433,6 +437,7 @@
433437 domTldDocs.classList.remove("hidden");
434438 }
435439
440 domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
436441 domSourceText.innerHTML = declSourceHtml(decl_index);
437442 domSectSource.classList.remove("hidden");
438443 }
......@@ -918,6 +923,10 @@
918923 return unwrapString(wasm_exports.decl_source_html(decl_index));
919924 }
920925
926 function declLineNumbersHtml(decl_index) {
927 return unwrapString(wasm_exports.decl_line_numbers_html(decl_index));
928 }
929
921930 function declDoctestHtml(decl_index) {
922931 return unwrapString(wasm_exports.decl_doctest_html(decl_index));
923932 }
lib/docs/wasm/html_render.zig+13
......@@ -30,6 +30,19 @@ pub const Annotation = struct {
3030 dom_id: u32,
3131};
3232
33pub fn fileSourceLineNumbersHtml(
34 file_index: Walk.File.Index,
35 out: *std.ArrayListUnmanaged(u8),
36 root_node: Ast.Node.Index,
37) !void {
38 const ast = file_index.get_ast();
39 const first_token_line = ast.tokenLocation(0, ast.firstToken(root_node)).line;
40 const last_token_line = ast.tokenLocation(0, ast.lastToken(root_node)).line;
41 for (first_token_line..last_token_line + 1) |i| {
42 try out.print(gpa, "<span>{d}</span>\n", .{i + 1});
43 }
44}
45
3346pub fn fileSourceHtml(
3447 file_index: Walk.File.Index,
3548 out: *ArrayList(u8),
lib/docs/wasm/main.zig+11
......@@ -9,6 +9,7 @@ const ArrayList = std.ArrayList;
99const Writer = std.Io.Writer;
1010
1111const fileSourceHtml = @import("html_render.zig").fileSourceHtml;
12const fileSourceLineNumbersHtml = @import("html_render.zig").fileSourceLineNumbersHtml;
1213const appendEscaped = @import("html_render.zig").appendEscaped;
1314const resolveDeclLink = @import("html_render.zig").resolveDeclLink;
1415const missing_feature_url_escape = @import("html_render.zig").missing_feature_url_escape;
......@@ -543,6 +544,16 @@ export fn decl_fn_proto_html(decl_index: Decl.Index, linkify_fn_name: bool) Stri
543544 return String.init(string_result.items);
544545}
545546
547export fn decl_line_numbers_html(decl_index: Decl.Index) String {
548 const decl = decl_index.get();
549
550 string_result.clearRetainingCapacity();
551 fileSourceLineNumbersHtml(decl.file, &string_result, decl.ast_node) catch |err| {
552 std.debug.panic("unable to render source line numbers: {s}", .{@errorName(err)});
553 };
554 return String.init(string_result.items);
555}
556
546557export fn decl_source_html(decl_index: Decl.Index) String {
547558 const decl = decl_index.get();
548559