authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2023-01-18 19:45:32+01:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2023-01-18 19:45:32+01:00
log35f5d437688841d1c908cd81f205d7c9b44e3ae6
tree4859d79c68e810f908a0367497e46c1cbcbf8550
parent72c09b7b3b2b804bcc57befb5d82c7e89b6deeac

autodoc: add support for top-level doc comments


1 files changed, 32 insertions(+), 17 deletions(-)

src/Autodoc.zig+32-17
......@@ -7,6 +7,7 @@ const Compilation = @import("Compilation.zig");
77const Module = @import("Module.zig");
88const File = Module.File;
99const Package = @import("Package.zig");
10const Tokenizer = std.zig.Tokenizer;
1011const Zir = @import("Zir.zig");
1112const Ref = Zir.Inst.Ref;
1213const log = std.log.scoped(.autodoc);
......@@ -214,8 +215,13 @@ pub fn generateZirData(self: *Autodoc) !void {
214215 .enclosing_type = main_type_index,
215216 };
216217
217 try self.ast_nodes.append(self.arena, .{ .name = "(root)" });
218 const maybe_tldoc_comment = try self.getTLDocComment(file);
219 try self.ast_nodes.append(self.arena, .{
220 .name = "(root)",
221 .docs = maybe_tldoc_comment,
222 });
218223 try self.files.put(self.arena, file, main_type_index);
224
219225 _ = try self.walkInstruction(file, &root_scope, .{}, Zir.main_struct_inst, false);
220226
221227 if (self.ref_paths_pending_on_decls.count() > 0) {
......@@ -247,21 +253,14 @@ pub fn generateZirData(self: *Autodoc) !void {
247253 .comptimeExprs = self.comptime_exprs.items,
248254 };
249255
250 if (self.doc_location.directory) |d| {
251 d.handle.makeDir(
252 self.doc_location.basename,
253 ) catch |e| switch (e) {
254 error.PathAlreadyExists => {},
255 else => |err| return err,
256 };
257 } else {
258 self.module.zig_cache_artifact_directory.handle.makeDir(
259 self.doc_location.basename,
260 ) catch |e| switch (e) {
261 error.PathAlreadyExists => {},
262 else => |err| return err,
263 };
264 }
256 const base_dir = self.doc_location.directory orelse
257 self.module.zig_cache_artifact_directory;
258
259 base_dir.handle.makeDir(self.doc_location.basename) catch |e| switch (e) {
260 error.PathAlreadyExists => {},
261 else => |err| return err,
262 };
263
265264 const output_dir = if (self.doc_location.directory) |d|
266265 try d.handle.openDir(self.doc_location.basename, .{})
267266 else
......@@ -914,7 +913,11 @@ fn walkInstruction(
914913 .parent = null,
915914 .enclosing_type = main_type_index,
916915 };
917 try self.ast_nodes.append(self.arena, .{ .name = "(root)" });
916 const maybe_tldoc_comment = try self.getTLDocComment(file);
917 try self.ast_nodes.append(self.arena, .{
918 .name = "(root)",
919 .docs = maybe_tldoc_comment,
920 });
918921 try self.files.put(self.arena, new_file, main_type_index);
919922 return self.walkInstruction(
920923 new_file,
......@@ -4412,3 +4415,15 @@ fn declIsVar(
44124415 // tags[tok_idx] is the token called 'mut token' in AstGen
44134416 return (tags[tok_idx] == .keyword_var);
44144417}
4418
4419fn getTLDocComment(self: *Autodoc, file: *File) ![]const u8 {
4420 const source = (try file.getSource(self.module.gpa)).bytes;
4421 var tokenizer = Tokenizer.init(source);
4422 var tok = tokenizer.next();
4423 var comment = std.ArrayList(u8).init(self.arena);
4424 while (tok.tag == .container_doc_comment) : (tok = tokenizer.next()) {
4425 try comment.appendSlice(source[tok.loc.start + 3 .. tok.loc.end + 1]);
4426 }
4427
4428 return comment.items;
4429}