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

Format guard clauses in switches. #1383

Merged
merged 1 commit into from
Feb 15, 2024
Merged

Format guard clauses in switches. #1383

merged 1 commit into from
Feb 15, 2024

Conversation

munificent
Copy link
Member

There were enough tweaks in the formatting specific to the two contexts where the guards can appear that I ultimately decided to make new Piece classes for switch expression cases and switch statement cases. I was hoping for more code reuse, but I think it would have been harder to maintain if I jammed all of this into a single Piece class.

I also slightly tweaked the indentation style of switch expression cases. When I'd first added support for switch expressions in the new formatter, I changed the style from what the old formatter had, for reasons that aren't entirely clear. Maybe I thought it made it more consistent to indent +4?

Either way, once guard clauses came into play, it became clear that the old style made more sense because it gives a clearer indentation level for the when clause when it splits. So this change also makes switch expression formatting more similar to the old style.

There were enough tweaks in the formatting specific to the two contexts
where the guards can appear that I ultimately decided to make new Piece
classes for switch expression cases and switch statement cases. I was
hoping for more code reuse, but I think it would have been harder to
maintain if I jammed all of this into a single Piece class.

I also slightly tweaked the indentation style of switch expression
cases. When I'd first added support for switch expressions in the new
formatter, I changed the style from what the old formatter had, for
reasons that aren't entirely clear. Maybe I thought it made it more
consistent to indent +4?

Either way, once guard clauses came into play, it became clear that the
old style made more sense because it gives a clearer indentation level
for the `when` clause when it splits. So this change also makes switch
expression formatting more similar to the old style.
lib/src/front_end/ast_node_visitor.dart Show resolved Hide resolved
@@ -28,7 +28,7 @@ e = switch (c) {
e = switch (c) {
first => a,
second =>
veryLongExpression + thatSplits,
veryLongExpression + thatSplits,
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the style change. By indenting bodies +2 instead of +4, it makes it more natural to indent the when clause +4 when it splits. If we indent the body +4, then you have to do like +8 for the when clause which looks pretty extreme.

Copy link
Member

@kallentu kallentu left a comment

Choose a reason for hiding this comment

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

Generally, LGTM!

@munificent munificent merged commit d37c9e2 into main Feb 15, 2024
7 checks passed
@munificent munificent deleted the format-switch-guard branch February 15, 2024 17:40
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.

3 participants