mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
autodoc: better indentation handling when rendering source code (#19422)
Closes #19293
This commit is contained in:
parent
31a7f22b80
commit
ef31d8f48f
1 changed files with 39 additions and 2 deletions
|
|
@ -938,6 +938,17 @@ fn file_source_html(
|
|||
|
||||
var cursor: usize = token_starts[start_token];
|
||||
|
||||
var indent: usize = 0;
|
||||
if (std.mem.lastIndexOf(u8, ast.source[0..cursor], "\n")) |newline_index| {
|
||||
for (ast.source[newline_index + 1 .. cursor]) |c| {
|
||||
if (c == ' ') {
|
||||
indent += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (
|
||||
token_tags[start_token..end_token],
|
||||
token_starts[start_token..end_token],
|
||||
|
|
@ -947,7 +958,7 @@ fn file_source_html(
|
|||
if (std.mem.trim(u8, between, " \t\r\n").len > 0) {
|
||||
if (!options.skip_comments) {
|
||||
try out.appendSlice(gpa, "<span class=\"tok-comment\">");
|
||||
try appendEscaped(out, between);
|
||||
try appendUnindented(out, between, indent);
|
||||
try out.appendSlice(gpa, "</span>");
|
||||
}
|
||||
} else if (between.len > 0) {
|
||||
|
|
@ -955,7 +966,7 @@ fn file_source_html(
|
|||
if (out.items.len > 0 and out.items[out.items.len - 1] != ' ')
|
||||
try out.append(gpa, ' ');
|
||||
} else {
|
||||
try out.appendSlice(gpa, between);
|
||||
try appendUnindented(out, between, indent);
|
||||
}
|
||||
}
|
||||
if (tag == .eof) break;
|
||||
|
|
@ -1188,6 +1199,32 @@ fn file_source_html(
|
|||
}
|
||||
}
|
||||
|
||||
fn unindent(s: []const u8, indent: usize) []const u8 {
|
||||
var indent_idx: usize = 0;
|
||||
for (s) |c| {
|
||||
if (c == ' ' and indent_idx < indent) {
|
||||
indent_idx += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return s[indent_idx..];
|
||||
}
|
||||
|
||||
fn appendUnindented(out: *std.ArrayListUnmanaged(u8), s: []const u8, indent: usize) !void {
|
||||
var it = std.mem.split(u8, s, "\n");
|
||||
var is_first_line = true;
|
||||
while (it.next()) |line| {
|
||||
if (is_first_line) {
|
||||
try appendEscaped(out, line);
|
||||
is_first_line = false;
|
||||
} else {
|
||||
try out.appendSlice(gpa, "\n");
|
||||
try appendEscaped(out, unindent(line, indent));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_ident_link(
|
||||
file_index: Walk.File.Index,
|
||||
out: *std.ArrayListUnmanaged(u8),
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue