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: include entire code on line in source #19301

Closed
wants to merge 1 commit into from

Conversation

sno2
Copy link
Contributor

@sno2 sno2 commented Mar 14, 2024

Closes #19293

I took the easy route and just add the entire line's contents. This fixes the noted issue:

image

However, care should be taken to make sure it does not introduce bugs if there is non-whitespace on that same line.

I am unsure if I introduced what you were warning against. Do you want

const a = 1; fn add(a: u32, b: u32) u32 {
    return a + b;
}

to render as itself, or

fn add(a: u32, b: u32) u32 {
    return a + b;
}

or

             fn add(a: u32, b: u32) u32 {
    return a + b;
}

This implementation assumes you want the first option.

@@ -540,7 +542,9 @@ export fn decl_doctest_html(decl_index: Decl.Index) String {
return String.init("");

string_result.clearRetainingCapacity();
file_source_html(decl.file, &string_result, doctest_ast_node, .{}) catch |err| {
file_source_html(decl.file, &string_result, doctest_ast_node, .{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds the same behavior to rendering doctests.

@nektro
Copy link
Contributor

nektro commented Mar 14, 2024

imo it should print option 2. just the decl and whitespace trimmed while preserving indentation.

@sno2 sno2 changed the title fix(autodoc): include entire code on line in source autodoc: include entire code on line in source Mar 15, 2024
@sno2
Copy link
Contributor Author

sno2 commented Mar 15, 2024

imo it should print option 2. just the decl and whitespace trimmed while preserving indentation.

Hmm, I think we disagree on what would be the actual "Source Code." I think it would be weird to not include all of the original lines because we may make snippets show up like in the Zig guides with line numbers.

Although, the current way doesn't show any of the tokens on the last line after the terminator which would have to be changed to follow that definition.

@sno2 sno2 closed this Mar 15, 2024
@sno2 sno2 force-pushed the autodoc/better-source-indent branch from badf3fd to 778ab76 Compare March 15, 2024 20:10
@sno2 sno2 reopened this Mar 15, 2024
@sno2 sno2 force-pushed the autodoc/better-source-indent branch 2 times, most recently from 9d8895f to 75c94f7 Compare March 15, 2024 20:17
Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch, @sno2. This discussion has convinced me that simply including the entire line is not good enough, and the original required behavior I specified in #19293 is the required behavior.

Case 1

Input:

    fn add(a: u32, b: u32) u32 {
        return a + b;
    }

Expected Output:

fn add(a: u32, b: u32) u32 {
    return a + b;
}

Case 2

Input:

    const a = 1; fn add(a: u32, b: u32) u32 {
        return a + b;
    }

Expected Output:

fn add(a: u32, b: u32) u32 {
    return a + b;
}

Case 3

Input:

    fn add(a: u32, b: u32) u32 {
return a + b;
    }

Expected Output:

fn add(a: u32, b: u32) u32 {
return a + b;
}

Case 4

Input:

    const a = 1; fn add(a: u32, b: u32) u32 {
return a + b;
    }

Expected Output:

fn add(a: u32, b: u32) u32 {
return a + b;
}

Note that those last two cases are planned to become syntax errors:

@nektro
Copy link
Contributor

nektro commented Mar 16, 2024

imo the existence of zig fmt's api in the stdlib should mean that with:

    const a = 1; fn add(a: u32, b: u32) u32 {
return a + b;
    }

when viewing the source of add is still rendered as:

fn add(a: u32, b: u32) u32 {
    return a + b;
}

@sno2
Copy link
Contributor Author

sno2 commented Mar 20, 2024

If anyone else would like to work on this- be my guest. I have exams to study for until halfway through next week so I will be busy until then. Will re-open next week.

@sno2 sno2 closed this Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

better handle indentation when rendering source code
3 participants