Skip to content

Commit

Permalink
Autodoc: display line numbers in source code display
Browse files Browse the repository at this point in the history
  • Loading branch information
skk64 committed Jan 7, 2025
1 parent fc28a71 commit 775671c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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;
}
Expand Down Expand Up @@ -400,7 +409,14 @@ <h2>Example Usage</h2>
</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">
Expand Down
9 changes: 9 additions & 0 deletions lib/docs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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");
Expand Down Expand Up @@ -215,6 +216,7 @@
href: location.hash,
}]);

domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
domSourceText.innerHTML = declSourceHtml(decl_index);

domSectSource.classList.remove("hidden");
Expand Down Expand Up @@ -366,6 +368,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");
}
Expand Down Expand Up @@ -396,6 +399,7 @@
renderErrorSet(base_decl, errorSetNodeList(decl_index, errorSetNode));
}

domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
domSourceText.innerHTML = declSourceHtml(decl_index);
domSectSource.classList.remove("hidden");
}
Expand All @@ -410,6 +414,7 @@
domTldDocs.classList.remove("hidden");
}

domSourceLineNumbers.innerHTML = declLineNumbersHtml(decl_index);
domSourceText.innerHTML = declSourceHtml(decl_index);
domSectSource.classList.remove("hidden");
}
Expand Down Expand Up @@ -890,6 +895,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));
}
Expand Down
14 changes: 14 additions & 0 deletions lib/docs/wasm/html_render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ 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;
const writer = out.writer(gpa);
for (first_token_line..last_token_line + 1) |i| {
try std.fmt.format(writer, "<span>{d}</span>\n", .{i + 1});
}
}

pub fn fileSourceHtml(
file_index: Walk.File.Index,
out: *std.ArrayListUnmanaged(u8),
Expand Down
11 changes: 11 additions & 0 deletions lib/docs/wasm/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const markdown = @import("markdown.zig");
const Decl = Walk.Decl;

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;
Expand Down Expand Up @@ -519,6 +520,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| {
fatal("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();

Expand Down

0 comments on commit 775671c

Please sign in to comment.