-
Notifications
You must be signed in to change notification settings - Fork 664
fix(rome_formatter): measure first variant of BestFitting in flat mode #2675
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.
This may fix the test bit I don't think it works as expected because any group inside of the second or later variant always get printed in expanded mode (same as for rest elements). My understanding is that the printing of a variant should work the same as any other content outside of the group where the first group changes the print mode to flat to all its enclosing group and the mode only changes back for the groups from the rest queue
Ah, yeah that makes sense. |
Deploying with Cloudflare Pages
|
This seems to be the case, unless I'm misunderstanding what you're saying here. I added a test where the best fitting second variant is printed the same as it would be if it were printed directly. Inner groups are still printed flat when possible. Do you have an example where the current implementation doesn't work as intended? Do you think the problem is with how it measures, how it prints, or both? Also, I'm not sure about this line:
What's the purpose of setting that as true when measuring in expanded mode? |
It could be but let's say we have the following example: // variant2:
token("("), group_elements(
token("["), soft_line_break(),
indent(token("abcdef")),
soft_line_break(),
token("]"))
) My understanding is that the printer calls
Which will ultimately cause the You can change the second variant of the
This should not fit because the printer should try to print the inner group as flat and not as expanded but the printer breaks at the
The purpose on that line is to cache the fact that everything following now up to the first line break will fit on the line. So that the printer doesn't need to re-measure if the picked group contains an inner group:
There's no point in re-calculating if the group containing the array fits because the printer already ensured that this is the case when measuring the variant content. The boolean gets set to false when printing the next line break and any group following then must be re-measured. |
They won't necessarily break during printing if that group fits on one line. The second variant will be selected if it fits in expanded mode, but a group that's enqueued for printing in expanded mode will still be printed flat if it fits. If we have:
Formatting that at width 80 prints the first variant.
Formatting that at width 8 prints the second variant as:
Formatting that at width 1 defaults to printing the most expanded variant. Does that address your concern or am I still misunderstanding?
That variant would be selected because it fits in expanded mode, but inner groups would still be printed flat if they fit. |
That makes sense. I think where this solution differs from what I thought it does is that inner groups "fit" if the content up to the first line break fits (including soft) whereas what prettier does (I think) is that the group only fits if the whole group in flat mode fits. We can go forward with your solution and see if it suits our needs. Please add some documentation explaining how this behaviour differs from prettiers |
Yeah, that's a good point. In this implementation, there's no way to enforce that a specific group inside a variant must be flat when measuring if that variant fits. If that ends up being important, we can follow up with a new approach. I think we'd need to add something like |
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.
Thank you for addressing my nitpicks! Let's 🚢 it!!
Summary
This is a minimal fix for the
BestFitting
element that was broken by #2645. It now attempts to fit the first variant in flat mode on a single line. If that doesn't fit, it continues attempting the remaining variants inPrintMode::Expanded
before defaulting to themost_expanded
variant. This seems to match the original intention based on the doc comments for thebest_fitting
macro. @MichaReiser?I don't know if this will be sufficient for our needs, but I believe that matching the exact behavior of Prettier's
conditionalGroup
could involve heavy enough changes to our "fits" measurement and printing that it may be worth trying this out first.Happy to close this, though, if we'd rather go with a different approach.
Test Plan
Removed the
ignore
from the brokenbest_fitting
doc test.