mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
autodoc: Handling of default values for struct fields
This commit is contained in:
parent
83d1f6b15a
commit
97b3b36c65
2 changed files with 34 additions and 8 deletions
|
|
@ -2860,7 +2860,7 @@ const NAV_MODES = {
|
|||
if (container.kind === typeKinds.Enum) {
|
||||
html += ' = <span class="tok-number">' + fieldName + "</span>";
|
||||
} else {
|
||||
let fieldTypeExpr = container.fields[i];
|
||||
let fieldTypeExpr = container.field_types[i];
|
||||
if (container.kind !== typeKinds.Struct || !container.is_tuple) {
|
||||
html += ": ";
|
||||
}
|
||||
|
|
@ -2869,6 +2869,12 @@ const NAV_MODES = {
|
|||
if (tsn) {
|
||||
html += "<span> (" + tsn + ")</span>";
|
||||
}
|
||||
if (container.kind === typeKinds.Struct && !container.is_tuple) {
|
||||
let defaultInitExpr = container.field_defaults[i];
|
||||
if (defaultInitExpr !== null) {
|
||||
html += " = " + exprName(defaultInitExpr, { wantHtml: true, wantLink: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
html += ",</pre></div>";
|
||||
|
|
@ -4057,10 +4063,11 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {
|
|||
src: ty[2],
|
||||
privDecls: ty[3],
|
||||
pubDecls: ty[4],
|
||||
fields: ty[5],
|
||||
is_tuple: ty[6],
|
||||
line_number: ty[7],
|
||||
outer_decl: ty[8],
|
||||
field_types: ty[5],
|
||||
field_defaults: ty[6],
|
||||
is_tuple: ty[7],
|
||||
line_number: ty[8],
|
||||
outer_decl: ty[9],
|
||||
};
|
||||
case 10: // ComptimeExpr
|
||||
case 11: // ComptimeFloat
|
||||
|
|
|
|||
|
|
@ -586,7 +586,8 @@ const DocData = struct {
|
|||
src: usize, // index into astNodes
|
||||
privDecls: []usize = &.{}, // index into decls
|
||||
pubDecls: []usize = &.{}, // index into decls
|
||||
fields: ?[]Expr = null, // (use src->fields to find names)
|
||||
field_types: ?[]Expr = null, // (use src->fields to find names)
|
||||
field_defaults: ?[]?Expr = null,
|
||||
is_tuple: bool,
|
||||
line_number: usize,
|
||||
outer_decl: usize,
|
||||
|
|
@ -2831,6 +2832,7 @@ fn walkInstruction(
|
|||
);
|
||||
|
||||
var field_type_refs: std.ArrayListUnmanaged(DocData.Expr) = .{};
|
||||
var field_default_refs: std.ArrayListUnmanaged(?DocData.Expr) = .{};
|
||||
var field_name_indexes: std.ArrayListUnmanaged(usize) = .{};
|
||||
try self.collectStructFieldInfo(
|
||||
file,
|
||||
|
|
@ -2838,6 +2840,7 @@ fn walkInstruction(
|
|||
src_info,
|
||||
fields_len,
|
||||
&field_type_refs,
|
||||
&field_default_refs,
|
||||
&field_name_indexes,
|
||||
extra_index,
|
||||
small.is_tuple,
|
||||
|
|
@ -2851,7 +2854,8 @@ fn walkInstruction(
|
|||
.src = self_ast_node_index,
|
||||
.privDecls = priv_decl_indexes.items,
|
||||
.pubDecls = decl_indexes.items,
|
||||
.fields = field_type_refs.items,
|
||||
.field_types = field_type_refs.items,
|
||||
.field_defaults = field_default_refs.items,
|
||||
.is_tuple = small.is_tuple,
|
||||
.line_number = self.ast_nodes.items[self_ast_node_index].line,
|
||||
.outer_decl = type_slot_index - 1,
|
||||
|
|
@ -4309,6 +4313,7 @@ fn collectStructFieldInfo(
|
|||
parent_src: SrcLocInfo,
|
||||
fields_len: usize,
|
||||
field_type_refs: *std.ArrayListUnmanaged(DocData.Expr),
|
||||
field_default_refs: *std.ArrayListUnmanaged(?DocData.Expr),
|
||||
field_name_indexes: *std.ArrayListUnmanaged(usize),
|
||||
ei: usize,
|
||||
is_tuple: bool,
|
||||
|
|
@ -4406,9 +4411,23 @@ fn collectStructFieldInfo(
|
|||
};
|
||||
|
||||
extra_index += field.align_body_len;
|
||||
extra_index += field.init_body_len;
|
||||
|
||||
const default_expr: ?DocData.Expr = def: {
|
||||
if (field.init_body_len == 0) {
|
||||
break :def null;
|
||||
}
|
||||
|
||||
const body = file.zir.extra[extra_index..][0..field.init_body_len];
|
||||
extra_index += body.len;
|
||||
|
||||
const break_inst = body[body.len - 1];
|
||||
const operand = data[break_inst].@"break".operand;
|
||||
const walk_result = try self.walkRef(file, scope, parent_src, operand, false);
|
||||
break :def walk_result.expr;
|
||||
};
|
||||
|
||||
try field_type_refs.append(self.arena, type_expr);
|
||||
try field_default_refs.append(self.arena, default_expr);
|
||||
|
||||
// ast node
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue