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

Rewrite code writer formatting to allow for Runnable evaluation #1104

Merged
merged 4 commits into from
Feb 24, 2022

Conversation

mtdowling
Copy link
Member

@mtdowling mtdowling commented Feb 23, 2022

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.

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.
@mtdowling mtdowling requested a review from a team as a code owner February 23, 2022 07:25
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.
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).
Copy link
Contributor

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.

@mtdowling mtdowling merged commit 9815555 into main Feb 24, 2022
@mtdowling mtdowling deleted the codewriter-formatter-rewrite branch April 8, 2022 05: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.

4 participants