Skip to content

Conversation

@josevalim
Copy link
Member

Before we had doc_fits and doc_group which could be nested arbitrarily and complicated the formatting and fitting logic. By merging them into optimistic/pessimistic groups, we can simplify the API and logic.

@typep mode :: :flat | :flat_no_break | :break | :break_no_flat

@spec fits(
@spec fits?(
Copy link
Member Author

Choose a reason for hiding this comment

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

It can now be a boolean because format injects a fake :group_over document that allows us to track where the parent groups stops. And by making next_break_fits, we can avoid look aheads.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this changes the contract between the formatter and the algebra, is there a risk for this to be a breaking change for libraries relying on algebra? (e.g. now they need to add a fake |> group() too?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have added doc_fits back and I confirmed that the formatter without the changes in this PR still works, except for the next_break_fits bug you fixed (which caused this function to change). So if they want the fix, they need to move to the new API. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that's fair, it was quite the edge-case bug 🙂


defp size(%Date.Range{first_in_iso_days: first_days, last_in_iso_days: last_days, step: step}),
do: abs(div(last_days - first_days, step)) + 1
defp size(%Date.Range{
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 was going over the line limit.

@typep mode :: :flat | :flat_no_break | :break | :break_no_flat

@spec fits(
@spec fits?(
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this changes the contract between the formatter and the algebra, is there a risk for this to be a breaking change for libraries relying on algebra? (e.g. now they need to add a fake |> group() too?

will convert breaks into newlines, hoping the child group fits.
However, if the child group is optimistic, then the parent can
assume that it will fit, leaving the overall fitting decision
to the child
Copy link
Member Author

Choose a reason for hiding this comment

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

@sabiwara I pushed more docs here. Another way to call these options would be to call them "tie_breaker":

  • parent
  • child
  • force_parent

But I think I like normal/optimistic/pessimistic better.

Copy link
Member Author

Choose a reason for hiding this comment

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

Claude suggested normal/deferred/???, but could not come up with good names for the third option.

Copy link
Contributor

Choose a reason for hiding this comment

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

Naming is hard...
Since it's "leaving the overall fitting decision to the child", something around the lines of delegate?
:normal | :delegate | :cancel_delegate? (just spitballing, I don't really like it)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah. I think the current one is my favorite. The optimistic also explains why force_unfit is ignored by the parent.

@josevalim josevalim merged commit f628d95 into main Apr 7, 2025
20 checks passed
@josevalim josevalim deleted the jv-algebra branch April 7, 2025 13:07
@josevalim
Copy link
Member Author

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants