-
Notifications
You must be signed in to change notification settings - Fork 219
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
Rewrite code writer formatting to allow for Runnable evaluation #1104
Merged
+547
−326
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CodeFormatter now parsers templates into "operations" that are later evaluated. This makes the code easier to understand and easier to evolve. Additionally, various characters were removed as valid formatter identifiers for CodeWriter, including '[', ']', '{', '}', '|', '>', '<'
If CodeWriter sees an argument that's a Runnable, it will invoke the Runnable, expect it to make calls to CodeWriter#write, and then use the buffered writes at the value of the argument. This allows larger templates to be defined and broken into different methods.
JordonPhillips
previously requested changes
Feb 23, 2022
smithy-utils/src/main/java/software/amazon/smithy/utils/CodeFormatter.java
Show resolved
Hide resolved
Rather than bake in support for Runnable to every formatter, I'm introducing C instead, meaning "call". This helps ensure that any existing formatter that people have used that are Runnable don't suddenly stop working. I also added support for "static" alignmnet for blocks. If the text on a line leading up to a block interpolation is all spaces and tabs, then that text is used when aligning subsequent lines written in the block expansion. This allows for things like tab based alignment and spaces for aligning things specifically. Otherwise, block expansion continues to work as is, using only spaces.
rcoh
reviewed
Feb 24, 2022
adamthom-amzn
approved these changes
Feb 24, 2022
Comment on lines
+405
to
+411
* <p>Alignment occurs either statically or dynamically based on the characters | ||
* that come before interpolation. If all of the characters in the literal | ||
* template that come before interpolation are spaces and tabs, then those | ||
* characters are used when indenting newlines. Otherwise, the number of | ||
* characters written as the template result that come before interpolation | ||
* are used when indenting (this takes into account any interpolation that | ||
* may precede block interpolation). |
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 struggled to understand this for a while. I don't have any suggestions to make it clearer, so it may just be a problem with the reader; maybe more examples would help.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
CodeWriter's formatter was rewritten to make it easier to understand, maintain, and evolve. As part of this evolution, this change adds the ability to pass a Runnable into a CodeWriter 'C' formatter (meaning "call"). When encountered, CodeWriter will invoke the Runnable and expect it to make calls to mutate the writer. Any text written is captured and used as the parameter value. This allows larger templates to be defined but still delegate functionality to separate methods. When coupled with text blocks in newer versions of Java, this largely makes things like openBlock/closeBlock irrelevant.
This change also is technically backward incompatible because it removes the ability to register a CodeWriter formatter using '[',']', '{', '}', '|', '<', '>'. Registering a formatter with these characters would either not work at all (e.g., '{'), or would be highly unreadable. By reserving these characters, we can also evolve CodeWriter's functionality as needed with additional sigils.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.