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");...@@ -7,6 +7,7 @@ const Compilation = @import("Compilation.zig");
7const Module = @import("Module.zig");7const Module = @import("Module.zig");
8const File = Module.File;8const File = Module.File;
9const Package = @import("Package.zig");9const Package = @import("Package.zig");
10const Tokenizer = std.zig.Tokenizer;
10const Zir = @import("Zir.zig");11const Zir = @import("Zir.zig");
11const Ref = Zir.Inst.Ref;12const Ref = Zir.Inst.Ref;
12const log = std.log.scoped(.autodoc);13const log = std.log.scoped(.autodoc);
...@@ -214,8 +215,13 @@ pub fn generateZirData(self: *Autodoc) !void {...@@ -214,8 +215,13 @@ pub fn generateZirData(self: *Autodoc) !void {
214 .enclosing_type = main_type_index,215 .enclosing_type = main_type_index,
215 };216 };
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 });
218 try self.files.put(self.arena, file, main_type_index);223 try self.files.put(self.arena, file, main_type_index);
224
219 _ = try self.walkInstruction(file, &root_scope, .{}, Zir.main_struct_inst, false);225 _ = try self.walkInstruction(file, &root_scope, .{}, Zir.main_struct_inst, false);
220226
221 if (self.ref_paths_pending_on_decls.count() > 0) {227 if (self.ref_paths_pending_on_decls.count() > 0) {
...@@ -247,21 +253,14 @@ pub fn generateZirData(self: *Autodoc) !void {...@@ -247,21 +253,14 @@ pub fn generateZirData(self: *Autodoc) !void {
247 .comptimeExprs = self.comptime_exprs.items,253 .comptimeExprs = self.comptime_exprs.items,
248 };254 };
249255
250 if (self.doc_location.directory) |d| {256 const base_dir = self.doc_location.directory orelse
251 d.handle.makeDir(257 self.module.zig_cache_artifact_directory;
252 self.doc_location.basename,258
253 ) catch |e| switch (e) {259 base_dir.handle.makeDir(self.doc_location.basename) catch |e| switch (e) {
254 error.PathAlreadyExists => {},260 error.PathAlreadyExists => {},
255 else => |err| return err,261 else => |err| return err,
256 };262 };
257 } else {263
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 }
265 const output_dir = if (self.doc_location.directory) |d|264 const output_dir = if (self.doc_location.directory) |d|
266 try d.handle.openDir(self.doc_location.basename, .{})265 try d.handle.openDir(self.doc_location.basename, .{})
267 else266 else
...@@ -914,7 +913,11 @@ fn walkInstruction(...@@ -914,7 +913,11 @@ fn walkInstruction(
914 .parent = null,913 .parent = null,
915 .enclosing_type = main_type_index,914 .enclosing_type = main_type_index,
916 };915 };
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 });
918 try self.files.put(self.arena, new_file, main_type_index);921 try self.files.put(self.arena, new_file, main_type_index);
919 return self.walkInstruction(922 return self.walkInstruction(
920 new_file,923 new_file,
...@@ -4412,3 +4415,15 @@ fn declIsVar(...@@ -4412,3 +4415,15 @@ fn declIsVar(
4412 // tags[tok_idx] is the token called 'mut token' in AstGen4415 // tags[tok_idx] is the token called 'mut token' in AstGen
4413 return (tags[tok_idx] == .keyword_var);4416 return (tags[tok_idx] == .keyword_var);
4414}4417}
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}