From e6cab917e15a064f5247a742e028bcb628bfc014 Mon Sep 17 00:00:00 2001 From: Der Teufel Date: Wed, 25 Jan 2023 15:28:17 +0100 Subject: [PATCH] autodoc: added support for tuple structs --- lib/docs/main.js | 30 +++++++++++++++++++++++------- src/Autodoc.zig | 21 +++++++++++++++++---- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/lib/docs/main.js b/lib/docs/main.js index 6710dd964f..d488d018a6 100644 --- a/lib/docs/main.js +++ b/lib/docs/main.js @@ -1772,9 +1772,17 @@ const NAV_MODES = { let structObj = typeObj; let name = ""; if (opts.wantHtml) { - name = "struct { "; + if (structObj.is_tuple) { + name = "tuple { "; + } else { + name = "struct { "; + } } else { - name = "struct { "; + if (structObj.is_tuple) { + name = "tuple { "; + } else { + name = "struct { "; + } } if (structObj.fields.length > 1 && opts.wantHtml) {name += "
";} let indent = ""; @@ -1795,10 +1803,15 @@ const NAV_MODES = { for(let i = 0; i < structObj.fields.length; i += 1) { let fieldNode = getAstNode(structNode.fields[i]); let fieldName = fieldNode.name; - let html = indent + escapeHtml(fieldName); + let html = indent; + if (!structObj.is_tuple) { + html += escapeHtml(fieldName); + } let fieldTypeExpr = structObj.fields[i]; - html += ": "; + if(!structObj.is_tuple) { + html += ": "; + } html += exprName(fieldTypeExpr, {...opts, indent: indent}); @@ -2797,7 +2810,9 @@ const NAV_MODES = { html += ' = ' + fieldName + ""; } else { let fieldTypeExpr = container.fields[i]; - html += ": "; + if(container.kind ==! typeKinds.Struct || !container.is_tuple) { + html += ": "; + } html += exprName(fieldTypeExpr, {wantHtml:true, wantLink:true}); let tsn = typeShorthandName(fieldTypeExpr); if (tsn) { @@ -3945,8 +3960,9 @@ const NAV_MODES = { privDecls: ty[3], pubDecls: ty[4], fields: ty[5], - line_number: ty[6], - outer_decl: ty[7], + is_tuple: ty[6], + line_number: ty[7], + outer_decl: ty[8], }; case 10: // ComptimeExpr case 11: // ComptimeFloat diff --git a/src/Autodoc.zig b/src/Autodoc.zig index fab20bff85..8afc9c859b 100644 --- a/src/Autodoc.zig +++ b/src/Autodoc.zig @@ -561,6 +561,7 @@ const DocData = struct { privDecls: []usize = &.{}, // index into decls pubDecls: []usize = &.{}, // index into decls fields: ?[]Expr = null, // (use src->fields to find names) + is_tuple: bool, line_number: usize, outer_decl: usize, }, @@ -2942,6 +2943,7 @@ fn walkInstruction( &field_type_refs, &field_name_indexes, extra_index, + small.is_tuple, ); self.ast_nodes.items[self_ast_node_index].fields = field_name_indexes.items; @@ -2953,6 +2955,7 @@ fn walkInstruction( .privDecls = priv_decl_indexes.items, .pubDecls = decl_indexes.items, .fields = field_type_refs.items, + .is_tuple = small.is_tuple, .line_number = self.ast_nodes.items[self_ast_node_index].line, .outer_decl = type_slot_index - 1, }, @@ -4116,6 +4119,7 @@ fn collectStructFieldInfo( field_type_refs: *std.ArrayListUnmanaged(DocData.Expr), field_name_indexes: *std.ArrayListUnmanaged(usize), ei: usize, + is_tuple: bool, ) !void { if (fields_len == 0) return; var extra_index = ei; @@ -4125,7 +4129,7 @@ fn collectStructFieldInfo( const bit_bags_count = std.math.divCeil(usize, fields_len, fields_per_u32) catch unreachable; const Field = struct { - field_name: u32, + field_name: ?u32, doc_comment_index: u32, type_body_len: u32 = 0, align_body_len: u32 = 0, @@ -4153,8 +4157,12 @@ fn collectStructFieldInfo( const has_type_body = @truncate(u1, cur_bit_bag) != 0; cur_bit_bag >>= 1; - const field_name = file.zir.extra[extra_index]; - extra_index += 1; + const field_name: ?u32 = if (!is_tuple) blk: { + const fname = file.zir.extra[extra_index]; + extra_index += 1; + break :blk fname; + } else null; + const doc_comment_index = file.zir.extra[extra_index]; extra_index += 1; @@ -4217,8 +4225,13 @@ fn collectStructFieldInfo( file.zir.nullTerminatedString(field.doc_comment_index) else null; + const field_name: []const u8 = if (field.field_name) |f_name| + file.zir.nullTerminatedString(f_name) + else + ""; + try self.ast_nodes.append(self.arena, .{ - .name = file.zir.nullTerminatedString(field.field_name), + .name = field_name, .docs = doc_comment, }); }