Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autodoc: code block is rendered incorrectly #21358

Closed
prajwalch opened this issue Sep 9, 2024 · 2 comments · Fixed by #21371
Closed

Autodoc: code block is rendered incorrectly #21358

prajwalch opened this issue Sep 9, 2024 · 2 comments · Fixed by #21371
Labels
autodoc The web application for interactive documentation and generation of its assets. bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@prajwalch
Copy link

Zig Version

0.14.0-dev.1510+fb0028a0d

Steps to Reproduce and Observed Behavior

I have a function like this:

/// Appends the new argument to the list of arguments.
///
/// **NOTE:** It returns an `error.DuplicatePositionalArgIndex` when attempting
/// to append two positional arguments with the same index. See the examples below.
///
/// ## Examples
///
/// ```zig
/// var app = App.init(allocator, "myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
/// try root.addArg(Arg.booleanOption("version", 'v', "Show version number"));
///
/// var test = app.createCommand("test", "Run test");
/// try test.addArg(Arg.positional("FILE", null, null));
/// ```
///
/// Appending two positional arguments with the same index.
///
/// ```zig
/// var app = App.init(allocator, "myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
/// try root.addArg(Arg.positional("FIRST", null, 1));
/// // Returns `error.DuplicatePositionalArgIndex`.
/// try root.addArg(Arg.positional("SECOND", null, 1));
/// ```
pub fn addArg(self: *Command, arg: Arg) !void { ... }

When I generate and look at the documentation page, I got:

Screenshot 2024-09-09 at 12-30-24 test Command - Zig Documentation

Another example

/// Appends multiple arguments to the list of arguments.
///
/// ## Examples
///
/// ```zig
/// var app = App.init(allocator, "myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
/// try root.addArgs(&[_]Arg {
///     Arg.singleValueOption("firstname", 'f', "First name"),
///     Arg.singleValueOption("lastname", 'l', "Last name"),
/// });
///
/// var address = app.createCommand("address", "Address");
/// try address.addArgs(&[_]Arg {
///     Arg.singleValueOption("street", 's', "Street name"),
///     Arg.singleValueOption("postal", 'p', "Postal code"),
/// });
/// ```
pub fn addArgs(self: *Command, args: []const Arg) !void {
    for (args) |arg| try self.addArg(arg);
}

Screenshot 2024-09-09 at 12-36-20 test Command - Zig Documentation

Expected Behavior

I expected correct rendering.

@prajwalch prajwalch added the bug Observed behavior contradicts documented or intended behavior label Sep 9, 2024
@Vexu Vexu added the autodoc The web application for interactive documentation and generation of its assets. label Sep 9, 2024
@Vexu Vexu added this to the 0.15.0 milestone Sep 9, 2024
@rohlem
Copy link
Contributor

rohlem commented Sep 9, 2024

Right now I couldn't find it, but I remember a comment from andrew that doctests should be preferred to code blocks in doc comments, since they also appear in documentation but are additionally run as tests making them easier to stay in sync with changes in the implementation.
This also generates an "Example Usage" section designed specifically for this, f.e. example in std.Build.dirnameAllowEmpty.

@prajwalch
Copy link
Author

prajwalch commented Sep 9, 2024

I am ok with the doctests but embedding the code block in the docstring is common and should be supported.

ianprime0509 added a commit to ianprime0509/zig that referenced this issue Sep 10, 2024
Closes ziglang#21358
Closes ziglang#21360

This commit modifies the `multiline_string_literal_line`, `doc_comment`,
and `container_doc_comment` tokens to no longer include the line ending
as part of the token. This makes it easier to handle line endings (which
may be LF, CRLF, or in edge cases possibly nonexistent) consistently.

In the two issues linked above, Autodoc was already assuming this for
doc comments, and yielding incorrect results when handling files with
CRLF line endings (both in Markdown parsing and source rendering).

Applying the same simplification for multiline string literals also
brings `zig fmt` into conformance with
ziglang/zig-spec#38 regarding formatting of
multiline strings with CRLF line endings: the spec says that `zig fmt`
should remove the CR from such line endings, but this was not previously
the case.
@Vexu Vexu modified the milestones: 0.15.0, 0.14.0 Sep 10, 2024
@Vexu Vexu closed this as completed in 9007534 Sep 10, 2024
DivergentClouds pushed a commit to DivergentClouds/zig that referenced this issue Sep 24, 2024
Closes ziglang#21358
Closes ziglang#21360

This commit modifies the `multiline_string_literal_line`, `doc_comment`,
and `container_doc_comment` tokens to no longer include the line ending
as part of the token. This makes it easier to handle line endings (which
may be LF, CRLF, or in edge cases possibly nonexistent) consistently.

In the two issues linked above, Autodoc was already assuming this for
doc comments, and yielding incorrect results when handling files with
CRLF line endings (both in Markdown parsing and source rendering).

Applying the same simplification for multiline string literals also
brings `zig fmt` into conformance with
ziglang/zig-spec#38 regarding formatting of
multiline strings with CRLF line endings: the spec says that `zig fmt`
should remove the CR from such line endings, but this was not previously
the case.
richerfu pushed a commit to richerfu/zig that referenced this issue Oct 28, 2024
Closes ziglang#21358
Closes ziglang#21360

This commit modifies the `multiline_string_literal_line`, `doc_comment`,
and `container_doc_comment` tokens to no longer include the line ending
as part of the token. This makes it easier to handle line endings (which
may be LF, CRLF, or in edge cases possibly nonexistent) consistently.

In the two issues linked above, Autodoc was already assuming this for
doc comments, and yielding incorrect results when handling files with
CRLF line endings (both in Markdown parsing and source rendering).

Applying the same simplification for multiline string literals also
brings `zig fmt` into conformance with
ziglang/zig-spec#38 regarding formatting of
multiline strings with CRLF line endings: the spec says that `zig fmt`
should remove the CR from such line endings, but this was not previously
the case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autodoc The web application for interactive documentation and generation of its assets. bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants