-
Notifications
You must be signed in to change notification settings - Fork 226
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
CodeWriter rehaul to allow typed sections #1110
Merged
Merged
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
bf4e901
to
5459c0c
Compare
JordonPhillips
approved these changes
Feb 28, 2022
5459c0c
to
6d66e90
Compare
This commit is a major refactor to CodeWriter to introduce a recursively typed AbstractCodeWriter that allows fluent interfaces to work with subtypes and allows the introduction of strongly typed section interceptors to CodeWriter.
6d66e90
to
21255d0
Compare
These methods are extremely rarely used and have been obsoleted by onSection with a custom interceptor. Any existing usage based on CodeWriter will continue to work using the deprecated methods that were moved to CodeWriter.
CodeWriter newlines now allow more explicit control when intercepting sections. The value of a section is intercepted as-is and can be written back to the section verbatim using writeInlineWithNoFormatting. However, the contents of $C and inline sections still trim a trailing newline since newlines for these features are controlled by the call to write/writeInline/etc that contained the formatter or inline section definition.
The Smithy Kotlin generator uses the call method which was removed during the refactor. Adding that back, and renaming the newly added call method that just statically types a `Consumer` to a method named `consumer`.
This commit updates onSection to preserve the previous newline behavior of `onSection(String, Consumer<String>)` while still providing explicit control over newlines in `onSection(CodeInterceptor)`. This ensures that existing users of CodeWriter don't break, while allowing new integration to be added that use CodeInterceptors (usually CodeInterceptor.Append or CodeInterceptor.Prepend to reduce errors in how newlines are handled).
JordonPhillips
approved these changes
Mar 2, 2022
kstich
approved these changes
Mar 2, 2022
smithy-utils/src/main/java/software/amazon/smithy/utils/CodeInterceptor.java
Show resolved
Hide resolved
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.
This commit is a major refactor to CodeWriter to introduce a recursively typed
AbstractCodeWriter
that allows fluent interfaces to work with subtypes and allows the introduction of strongly typed section interceptors to CodeWriter.This adds a major new feature to CodeWriter: typed sections and interceptors. This provides a much safer plugin system that can be used in code generators. Because the binding of an "interceptor" to a "section" is an interface in and of itself, code generation plugin systems don't need to actually hand off CodeWriters to plugins but rather just ask plugins for interceptors.
You can create a strongly typed section by passing a CodeSection to
injectSection
orpushState
. A CodeSection is just an interface with asectionName()
method that returns the name of the class by default.You can create
CodeInterceptors
by implementingCodeInterceptor
, or even better,CodeInterceptor.Appender
/CodeInterceptor.Prepender
that perform writing the previously emitted content correctly (only if non-empty and using writeInlineWithNoFormatting).By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.