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 @@...@@ -40,6 +40,15 @@
40 code a {40 code a {
41 color: #000000;41 color: #000000;
42 }42 }
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 }
43 #listFields > div, #listParams > div {52 #listFields > div, #listParams > div {
44 margin-bottom: 1em;53 margin-bottom: 1em;
45 }54 }
...@@ -429,7 +438,14 @@...@@ -429,7 +438,14 @@
429 </div>438 </div>
430 <div id="sectSource" class="hidden">439 <div id="sectSource" class="hidden">
431 <h2>Source Code</h2>440 <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>
433 </div>449 </div>
434 </section>450 </section>
435 <div id="helpDialog" class="hidden">451 <div id="helpDialog" class="hidden">
lib/docs/main.js+9
...@@ -50,6 +50,7 @@...@@ -50,6 +50,7 @@
50 const domSectTypes = document.getElementById("sectTypes");50 const domSectTypes = document.getElementById("sectTypes");
51 const domSectValues = document.getElementById("sectValues");51 const domSectValues = document.getElementById("sectValues");
52 const domSourceText = document.getElementById("sourceText");52 const domSourceText = document.getElementById("sourceText");
53 const domSourceLineNumbers = document.getElementById("sourceLineNumbers");
53 const domStatus = document.getElementById("status");54 const domStatus = document.getElementById("status");
54 const domTableFnErrors = document.getElementById("tableFnErrors");55 const domTableFnErrors = document.getElementById("tableFnErrors");
55 const domTldDocs = document.getElementById("tldDocs");56 const domTldDocs = document.getElementById("tldDocs");
...@@ -238,6 +239,7 @@...@@ -238,6 +239,7 @@
238 href: location.hash,239 href: location.hash,
239 }]);240 }]);
240241
242 domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
241 domSourceText.innerHTML = declSourceHtml(decl_index);243 domSourceText.innerHTML = declSourceHtml(decl_index);
242244
243 domSectSource.classList.remove("hidden");245 domSectSource.classList.remove("hidden");
...@@ -389,6 +391,7 @@...@@ -389,6 +391,7 @@
389 if (members.length !== 0 || fields.length !== 0) {391 if (members.length !== 0 || fields.length !== 0) {
390 renderNamespace(decl_index, members, fields);392 renderNamespace(decl_index, members, fields);
391 } else {393 } else {
394 domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
392 domSourceText.innerHTML = declSourceHtml(decl_index);395 domSourceText.innerHTML = declSourceHtml(decl_index);
393 domSectSource.classList.remove("hidden");396 domSectSource.classList.remove("hidden");
394 }397 }
...@@ -419,6 +422,7 @@...@@ -419,6 +422,7 @@
419 renderErrorSet(base_decl, errorSetNodeList(decl_index, errorSetNode));422 renderErrorSet(base_decl, errorSetNodeList(decl_index, errorSetNode));
420 }423 }
421424
425 domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
422 domSourceText.innerHTML = declSourceHtml(decl_index);426 domSourceText.innerHTML = declSourceHtml(decl_index);
423 domSectSource.classList.remove("hidden");427 domSectSource.classList.remove("hidden");
424 }428 }
...@@ -433,6 +437,7 @@...@@ -433,6 +437,7 @@
433 domTldDocs.classList.remove("hidden");437 domTldDocs.classList.remove("hidden");
434 }438 }
435439
440 domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
436 domSourceText.innerHTML = declSourceHtml(decl_index);441 domSourceText.innerHTML = declSourceHtml(decl_index);
437 domSectSource.classList.remove("hidden");442 domSectSource.classList.remove("hidden");
438 }443 }
...@@ -918,6 +923,10 @@...@@ -918,6 +923,10 @@
918 return unwrapString(wasm_exports.decl_source_html(decl_index));923 return unwrapString(wasm_exports.decl_source_html(decl_index));
919 }924 }
920925
926 function declLineNumbersHtml(decl_index) {
927 return unwrapString(wasm_exports.decl_line_numbers_html(decl_index));
928 }
929
921 function declDoctestHtml(decl_index) {930 function declDoctestHtml(decl_index) {
922 return unwrapString(wasm_exports.decl_doctest_html(decl_index));931 return unwrapString(wasm_exports.decl_doctest_html(decl_index));
923 }932 }
lib/docs/wasm/html_render.zig+13
...@@ -30,6 +30,19 @@ pub const Annotation = struct {...@@ -30,6 +30,19 @@ pub const Annotation = struct {
30 dom_id: u32,30 dom_id: u32,
31};31};
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
33pub fn fileSourceHtml(46pub fn fileSourceHtml(
34 file_index: Walk.File.Index,47 file_index: Walk.File.Index,
35 out: *ArrayList(u8),48 out: *ArrayList(u8),
lib/docs/wasm/main.zig+11
...@@ -9,6 +9,7 @@ const ArrayList = std.ArrayList;...@@ -9,6 +9,7 @@ const ArrayList = std.ArrayList;
9const Writer = std.Io.Writer;9const Writer = std.Io.Writer;
1010
11const fileSourceHtml = @import("html_render.zig").fileSourceHtml;11const fileSourceHtml = @import("html_render.zig").fileSourceHtml;
12const fileSourceLineNumbersHtml = @import("html_render.zig").fileSourceLineNumbersHtml;
12const appendEscaped = @import("html_render.zig").appendEscaped;13const appendEscaped = @import("html_render.zig").appendEscaped;
13const resolveDeclLink = @import("html_render.zig").resolveDeclLink;14const resolveDeclLink = @import("html_render.zig").resolveDeclLink;
14const missing_feature_url_escape = @import("html_render.zig").missing_feature_url_escape;15const 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...@@ -543,6 +544,16 @@ export fn decl_fn_proto_html(decl_index: Decl.Index, linkify_fn_name: bool) Stri
543 return String.init(string_result.items);544 return String.init(string_result.items);
544}545}
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
546export fn decl_source_html(decl_index: Decl.Index) String {557export fn decl_source_html(decl_index: Decl.Index) String {
547 const decl = decl_index.get();558 const decl = decl_index.get();
548559