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 if elements. #1344

Merged
merged 5 commits into from
Jan 3, 2024
Merged

Format if elements. #1344

merged 5 commits into from
Jan 3, 2024

Conversation

munificent
Copy link
Member

I was hoping to be able to reuse more of the createIf() code in PieceFactory for both if elements and if statements, but there are enough differences between the two that that didn't work out, so I just inlined the code for both of them directly in AstNodeVisitor.

I was able to reuse the existing IfPiece, though, which is nice.

When I added support for constructors, I introduced a little API to let
pieces enforce constraints between their state and the states of other
child pieces. It worked but felt a little bolted on. In particular,
enforcing the constraints during CodeWriter formatting meant we didn't
prune invalid solutions as efficiently as we could.

I'm working on if elements now and I needed a better way to handle
constraints between pieces for that, so I went ahead and refactored the
way this works to be, I think, generally better:

- Enforce constraints between pieces during solution expansion instead
  of during formatting. This means that we eagerly discard any solution
  (and the whole tree of solutions that could have come from it) if it
  doesn't meet the constraints.

- Remove the `_pieces` fields in PieceStateSet. With the previous
  optimization to track the next unsolved piece in Solution, that list
  wasn't doing much of anything. It's only use (aside from debug output)
  was for determining the order that pieces are compared when comparing
  two solutions. But we can get that from iterating over the
  _pieceStates map since maps in Dart track their key insertion order.

- Update ConstructorPiece to use this new API.
I was hoping to be able to reuse more of the createIf() code in
PieceFactory for both if elements and if statements, but there are
enough differences between the two that that didn't work out, so I just
inlined the code for both of them directly in AstNodeVisitor.

I was able to reuse the existing IfPiece, though, which is nice.
return switch (node.expression) {
ListLiteral(:var elements, :var rightBracket) ||
SetOrMapLiteral(:var elements, :var rightBracket)
when elements.canSplit(rightBracket) =>
Copy link
Member

Choose a reason for hiding this comment

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

Consider adding a comment explaining why element.canSplit indicates a non-empty collection.

(Forgot to save this change before committing. :( )
Base automatically changed from piece-constraints to main January 2, 2024 21:57
@munificent munificent merged commit 885f1ec into main Jan 3, 2024
7 checks passed
@munificent munificent deleted the format-if-elements branch January 3, 2024 00:31
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