aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoris Cro <kappaloris@gmail.com>2023-04-13 17:53:13 +0200
committerGitHub <noreply@github.com>2023-04-13 17:53:13 +0200
commitfbcf1c0006dc6656deb3f04e2f52d1da4a599952 (patch)
tree92543a4474cf2ef150c2941f52e932fad16e618e
parent83d1f6b15a84864232eceafb24b95b8d16282a3f (diff)
parentaea886e2f3df6dd7bd89e16d42a953d379b0550a (diff)
downloadzig-fbcf1c0006dc6656deb3f04e2f52d1da4a599952.tar.gz
zig-fbcf1c0006dc6656deb3f04e2f52d1da4a599952.zip
Merge pull request #15261 from der-teufel-programming/autodoc-defaults
Autodoc: default values for fields in structs, explicit values for tags in enums
-rw-r--r--lib/docs/main.js30
-rw-r--r--src/Autodoc.zig35
2 files changed, 50 insertions, 15 deletions
diff --git a/lib/docs/main.js b/lib/docs/main.js
index 534ddb5ef7..8d9a3b08a6 100644
--- a/lib/docs/main.js
+++ b/lib/docs/main.js
@@ -623,7 +623,7 @@ const NAV_MODES = {
function typeIsStructWithNoFields(typeIndex) {
let typeObj = getType(typeIndex);
if (typeObj.kind !== typeKinds.Struct) return false;
- return typeObj.fields.length == 0;
+ return typeObj.field_types.length == 0;
}
function typeIsGenericFn(typeIndex) {
@@ -2705,6 +2705,7 @@ const NAV_MODES = {
let declValue = resolveValue(uns.value);
if (!("type" in declValue.expr)) continue;
let uns_container = getType(declValue.expr.type);
+ if (!isContainerType(uns_container)) continue;
categorizeDecls(
uns_container.pubDecls,
typesList,
@@ -2858,9 +2859,12 @@ const NAV_MODES = {
escapeHtml(fieldName);
if (container.kind === typeKinds.Enum) {
- html += ' = <span class="tok-number">' + fieldName + "</span>";
+ let value = container.values[i];
+ if (value !== null) {
+ html += " = " + exprName(value, { wantHtml: true, wantLink: true });
+ }
} else {
- let fieldTypeExpr = container.fields[i];
+ let fieldTypeExpr = container.field_types[i];
if (container.kind !== typeKinds.Struct || !container.is_tuple) {
html += ": ";
}
@@ -2869,6 +2873,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 +4067,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
@@ -4099,7 +4110,8 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {
privDecls: ty[3],
pubDecls: ty[4],
tag: ty[5],
- nonexhaustive: ty[6],
+ values: ty[6],
+ nonexhaustive: ty[7],
};
case 20: // Union
return {
@@ -4108,7 +4120,7 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) {
src: ty[2],
privDecls: ty[3],
pubDecls: ty[4],
- fields: ty[5],
+ field_types: ty[5],
tag: ty[6],
auto_tag: ty[7],
};
diff --git a/src/Autodoc.zig b/src/Autodoc.zig
index b89e74be07..23731338fb 100644
--- a/src/Autodoc.zig
+++ b/src/Autodoc.zig
@@ -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 = &.{}, // (use src->fields to find names)
+ field_defaults: []?Expr = &.{}, // default values is specified
is_tuple: bool,
line_number: usize,
outer_decl: usize,
@@ -614,6 +615,7 @@ const DocData = struct {
pubDecls: []usize = &.{}, // index into decls
// (use src->fields to find field names)
tag: ?Expr = null, // tag type if specified
+ values: []?Expr = &.{}, // tag values if specified
nonexhaustive: bool,
},
Union: struct {
@@ -2705,6 +2707,7 @@ fn walkInstruction(
extra_index += body_len;
var field_name_indexes: std.ArrayListUnmanaged(usize) = .{};
+ var field_values: std.ArrayListUnmanaged(?DocData.Expr) = .{};
{
var bit_bag_idx = extra_index;
var cur_bit_bag: u32 = undefined;
@@ -2726,12 +2729,13 @@ fn walkInstruction(
const doc_comment_index = file.zir.extra[extra_index];
extra_index += 1;
- const value_ref: ?Ref = if (has_value) blk: {
+ const value_expr: ?DocData.Expr = if (has_value) blk: {
const value_ref = file.zir.extra[extra_index];
extra_index += 1;
- break :blk @intToEnum(Ref, value_ref);
+ const value = try self.walkRef(file, &scope, src_info, @intToEnum(Ref, value_ref), false);
+ break :blk value.expr;
} else null;
- _ = value_ref;
+ try field_values.append(self.arena, value_expr);
const field_name = file.zir.nullTerminatedString(field_name_index);
@@ -2756,6 +2760,7 @@ fn walkInstruction(
.privDecls = priv_decl_indexes.items,
.pubDecls = decl_indexes.items,
.tag = tag_type,
+ .values = field_values.items,
.nonexhaustive = small.nonexhaustive,
},
};
@@ -2831,6 +2836,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 +2844,7 @@ fn walkInstruction(
src_info,
fields_len,
&field_type_refs,
+ &field_default_refs,
&field_name_indexes,
extra_index,
small.is_tuple,
@@ -2851,7 +2858,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 +4317,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 +4415,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
{