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

Split the PrettyPrint class into two pieces. #759

Merged
merged 2 commits into from
Jun 27, 2024

Conversation

shawnhyam
Copy link
Contributor

This change breaks up the PrettyPrint class into one piece that is responsible for tracking the state of the pretty printing algorithm - processing the tokens, tracking the break stack, comma delimited region stack, etc.; and another piece that is responsible for assembling the output and tracking the state of the output - current line, column position, and indentation.

This change breaks up the PrettyPrint class into one piece that is
responsible for tracking the state of the pretty printing algorithm -
processing the tokens, tracking the break stack, comma delimited
region stack, etc.; and another piece that is responsible for assembling
the output and tracking the state of the output - current line, column
position, and indentation.
@shawnhyam
Copy link
Contributor Author

I did this as a learning exercise, to separate out the state that was part of the algorithm vs. the state that was part of the output stream. To me it makes things a bit easier to grok, interested in your thoughts on it.

Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

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

Interesting idea! I think this looks really good; the algorithm has gotten more complex over time as we've needed to add new features to the original, and I like that this moves the much of the "number fudging" (i.e., direct math operations) down to a lower layer and making the core algorithm read a bit more semantically.

Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift Outdated Show resolved Hide resolved

import Foundation

struct PrettyPrintBuffer {
Copy link
Member

Choose a reason for hiding this comment

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

Please add a brief doc comment for this type and for the stored properties below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@allevato allevato merged commit d8b446b into swiftlang:main Jun 27, 2024
@shawnhyam shawnhyam deleted the pretty-print-code-reorg branch August 2, 2024 13:48
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.

2 participants