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>
This commit is contained in:
Andrew Kelley 2026-02-10 01:43:12 +01:00
commit 6c7814fabd
4 changed files with 50 additions and 1 deletions

View file

@ -40,6 +40,15 @@
code a {
color: #000000;
}
.source-code {
display: grid;
grid-template-columns: auto 1fr;
align-items: start;
}
.source-line-numbers pre {
text-align: right;
color: #666;
}
#listFields > div, #listParams > div {
margin-bottom: 1em;
}
@ -429,7 +438,14 @@
</div>
<div id="sectSource" class="hidden">
<h2>Source Code</h2>
<pre><code id="sourceText"></code></pre>
<div class="source-code">
<div class="source-line-numbers">
<pre><code id="sourceLineNumbers"></code></pre>
</div>
<div class="source-text">
<pre><code id="sourceText"></code></pre>
</div>
</div>
</div>
</section>
<div id="helpDialog" class="hidden">

View file

@ -50,6 +50,7 @@
const domSectTypes = document.getElementById("sectTypes");
const domSectValues = document.getElementById("sectValues");
const domSourceText = document.getElementById("sourceText");
const domSourceLineNumbers = document.getElementById("sourceLineNumbers");
const domStatus = document.getElementById("status");
const domTableFnErrors = document.getElementById("tableFnErrors");
const domTldDocs = document.getElementById("tldDocs");
@ -238,6 +239,7 @@
href: location.hash,
}]);
domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
domSourceText.innerHTML = declSourceHtml(decl_index);
domSectSource.classList.remove("hidden");
@ -389,6 +391,7 @@
if (members.length !== 0 || fields.length !== 0) {
renderNamespace(decl_index, members, fields);
} else {
domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
domSourceText.innerHTML = declSourceHtml(decl_index);
domSectSource.classList.remove("hidden");
}
@ -419,6 +422,7 @@
renderErrorSet(base_decl, errorSetNodeList(decl_index, errorSetNode));
}
domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
domSourceText.innerHTML = declSourceHtml(decl_index);
domSectSource.classList.remove("hidden");
}
@ -433,6 +437,7 @@
domTldDocs.classList.remove("hidden");
}
domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
domSourceText.innerHTML = declSourceHtml(decl_index);
domSectSource.classList.remove("hidden");
}
@ -918,6 +923,10 @@
return unwrapString(wasm_exports.decl_source_html(decl_index));
}
function declLineNumbersHtml(decl_index) {
return unwrapString(wasm_exports.decl_line_numbers_html(decl_index));
}
function declDoctestHtml(decl_index) {
return unwrapString(wasm_exports.decl_doctest_html(decl_index));
}

View file

@ -30,6 +30,19 @@ pub const Annotation = struct {
dom_id: u32,
};
pub fn fileSourceLineNumbersHtml(
file_index: Walk.File.Index,
out: *std.ArrayListUnmanaged(u8),
root_node: Ast.Node.Index,
) !void {
const ast = file_index.get_ast();
const first_token_line = ast.tokenLocation(0, ast.firstToken(root_node)).line;
const last_token_line = ast.tokenLocation(0, ast.lastToken(root_node)).line;
for (first_token_line..last_token_line + 1) |i| {
try out.print(gpa, "<span>{d}</span>\n", .{i + 1});
}
}
pub fn fileSourceHtml(
file_index: Walk.File.Index,
out: *ArrayList(u8),

View file

@ -9,6 +9,7 @@ const ArrayList = std.ArrayList;
const Writer = std.Io.Writer;
const fileSourceHtml = @import("html_render.zig").fileSourceHtml;
const fileSourceLineNumbersHtml = @import("html_render.zig").fileSourceLineNumbersHtml;
const appendEscaped = @import("html_render.zig").appendEscaped;
const resolveDeclLink = @import("html_render.zig").resolveDeclLink;
const 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
return String.init(string_result.items);
}
export fn decl_line_numbers_html(decl_index: Decl.Index) String {
const decl = decl_index.get();
string_result.clearRetainingCapacity();
fileSourceLineNumbersHtml(decl.file, &string_result, decl.ast_node) catch |err| {
std.debug.panic("unable to render source line numbers: {s}", .{@errorName(err)});
};
return String.init(string_result.items);
}
export fn decl_source_html(decl_index: Decl.Index) String {
const decl = decl_index.get();