-
Notifications
You must be signed in to change notification settings - Fork 11
Improve List/Map Formatting #478
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
base: main
Are you sure you want to change the base?
Improve List/Map Formatting #478
Conversation
|
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.
big improvement for lists and maps 🔥
discussed a bit offline to look at whether we can do without the reference check, will re-review
@@ -148,23 +158,34 @@ function validateFinalState(state: State) { | |||
} | |||
} | |||
|
|||
function applySpecialBreak(state: State, chunk: Chunk, nextChunk: Chunk) { |
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.
could this function clarify what all the booleans mean conceptually? I don't really understand why we have to check for no comments, and whether the next groups are going to break.
Also, not a huge fan of doing the overflowing calculation in here, I feel like it is risky to introduce that logic in more than one place (based on what we found previously)
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.
I'll add comments to explain them.
The overflowing calculation was redundant, now removed.
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.
Added comment, re-review is required
Description
This PR improves how List:s, Map:s and Map Projections are being formatted in certain cases. This was a tech debt from #460.
The general idea of this PR is to fix the following:
There are a few caveats to this. This type of formatting only occurs if the List/Map is "alone". Meaning if two lists would occur in the return, the regular formatting applies. Which is similar to how prettier does it:
Another case is if a comment appear, then the regular style is also applied. This is because otherwise the comment will end up inside the list, which may cause the comment to refer to something else.
This is how we do it:
This PR covers the cases where we know so far should have the special split. There is a chance that later on more places should have special split.
Special Split
These things can special split:
In these places:
So to allow a special split, two things needs to be fullfilled. A parent should only have one child, and that child should want to special split. This means that if a return has two lists as return expression, the first part is not fulfilled of only having one child. Similarly, if a return expression contains a node expression for example, the child is not of "special split" kind, and therefore not fulfilling the second part.
Indentation
The most challening thing about this PR was how to handle indentation. In the regular case, both the return statement and the list adds one level of indentation. However, with the "special" formatting, only one should be applied. If you just ignore adding of the the indentation levels, the formatter throws an error. This is by design to not allow removing indentation levels which has not been applied. The solution to this was to let the add indentation part include a reference to the remove indentation modifier, if the add indentation is not applied, a flag is set on the remove indentation.
Tests