-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
std/zig/render: Rewrite indentation #21727
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
Conversation
|
CC @castholm |
1e5b8cd to
f972539
Compare
(Just so there's no confusion, I have no affiliation with the Zig compiler team and am just a random sporadic contributor.) If you can find a way to fix the failing I quickly copy/pasted your code into my local copy of comptime {
// if blah blah blah then we should ...
const result = if (foo)
one()
// otherwise, if blah blah then we need to ...
else if (bar)
two()
// otherwise, fall back to ...
else
three();
}
// becomes
comptime {
// if blah blah blah then we should ...
const result = if (foo)
one()
// otherwise, if blah blah then we need to ...
else if (bar)
two()
// otherwise, fall back to ...
else
three();
}The comments one the lines before |
|
I'm not sure if I would expect the former to be rendered in that example, in the way one can imagine invisible braces: comptime {
// if blah blah blah then we should ...
const result = if (foo) {
one()
// otherwise, if blah blah then we need to ...
} else if (bar) {
two()
// otherwise, fall back to ...
} else {
three();
}
} |
|
I would consider the behavior @castholm suggests to be unexpected and unintuitive, and this PR's behavior to therefore be correct in this instance. |
|
Fixed tests locally. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice work! This approach seems obviously superior to the status quo in my opinion. I've read through render.zig pretty carefully and nothing jumped out at me as obvious code smell.
I think it would be good to add examples from your PR description as test cases in parser_test.zig to ensure that the new behavior does not regress.
Also, it would be nice to include the nice high level design notes in your PR description as doc comments in AutoIndentingStream to help readers orient themselves. I found the examples in the PR description particularly helpful to understand the approach taken.
e56d5e8 to
aec19ed
Compare
Both excellent suggestions and will do so. |
|
Merging this will require a little bit of strategy since it will cause just about every other open PR to need a rebase. I apologize in advance but I will save it for a few weeks from now when I will try to get the PR count lower, and then choose that time to land it. |
|
@andrewrk Understandable! Happy to rebase whenever you're ready. |
c1ac292 to
4a20da1
Compare
4a20da1 to
90135c8
Compare
|
rebased onto latest master |
|
Thanks. Planning to land this before the release for sure. |
|
This probably closes #5777 as well. |
90135c8 to
eb3c7f5
Compare
|
Thank you for your patience on this one. I merged it by checking out your branch, deleting the FYI I have made many changes to render.zig in an active branch however I will take responsibility for dealing with those merge conflicts when I resume working on it. |
|
Many thanks! |
I checked just now. Issue remains |
Closes #7363. Closes #21102.
This PR rewrites how indentation is handed in
std.zig.render, which results in better indentation forzig fmt.PR is intended to be reviewed one commit at time. It will likely be easier to review without the final commit.
Formatting changes
Please see f972539 for the result of running
zig fmt srcfor a more complete demonstration.Some examples:
Design
indent_stackIndentation should only ever increment by one from one line to the next, no matter how many new indentation scopes are introduced.
This is implemented by keeping track of indentation scopes in a stack (
indent_stack), and only realizing the latest one when a newline occurs.As an example:
The body of
whileintroduces a new indentation scope, but the body ofifalso introduces a new indentation scope. When the newline is seen, only the indentation scope of theifis realized, and thewhileis not.This is a lot more straightforward to reason about, and removes the necessity of "oneshot"s.
space_stackIn order to ensure the correct indentation of comments, keeping track of appropriate indentation levels of comments after a semicolon or a comma is required in this new scheme.
space_stackdoes this by keeping track of the indentation level whenever a context that ends in a.semicolonor a.commais introduced..after_equalsand.binopindentation collapseThis was implemented in order to keep the prettiness of a particular indentation style:
If you remove this commit, the above would instead be formatted like this:
which i feel is less desirable.