-
Notifications
You must be signed in to change notification settings - Fork 7
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
Formatting in @AfterTemplate
is ignored, leading to huge single-line statements after the migration
#92
Comments
Thanks for the detailed look & report! I'm not sure if the RefasterTemplateProcessor would even have access to any formatting or comments (which came up before), but we can certainly strive to do a better job at formatting than placing everything on a single line. Perhaps leveraging auto format in the generated recipes could be of help, but I'm not sure what that would do for performance, or even have the desired effect. |
Yes, we could certainly add formatting as a default embedding option. That should be easy to implement. Alternatively, maintain the formatting from the template might also be possible. But that would be more work. |
This is actually what i'm asking for here. The template string in the generated JavaTemplate.builder should equal what is defined in the IMHO a formatting run should be a separate step. However if that's the way to go it'd be good to have some determinism as a recipe developer, so one can anticipate the result to put into the tests. |
@knutwannheden There already seems to be some kind of auto-formatting at work, i.e. when using statement lambdas instead of expression lambdas – and the mileage of it varies. Here is an example where it is really helpful as there is literally no way define that kind of formatting as part of the template: @AfterTemplate void after(Class<? extends Throwable> throwableClass, Executable executable) {
Assertions.assertThrows(throwableClass, executable);
} … will lead to this migration: @Test void runnableWithNoExpectedType() {
// language=java
rewriteRun(java(
"""
import org.testng.Assert;
class MyTest {
void testMethod() {
Assert.assertThrows(() -> { throw new RuntimeException(); });
}
}
""",
"""
import org.junit.jupiter.api.Assertions;
class MyTest {
void testMethod() {
Assertions.assertThrows(Throwable.class, () -> {
throw new RuntimeException();
});
}
}
"""
));
} Neat. But over here is an example where it very much gets into the way. In that example it becomes really hard to tell that there are two arguments passed to the method as the end of the first one and the beginning of the second one are put on the same line. So again, my suggestion would be to aim at preserving comments and formatting from the I was indeed already contemplating if i should post a new issue with an RFC to implement a new annotation for Refaster templates that would enable to configure exactly that, which i would want to add to the refaster template in the first example here. |
What version of OpenRewrite are you using?
I am using
I have the following refaster recipe:
(which is already breaking the 100 character barrier, but gives a better trade-off regarding cognitive complexity)
What did you expect to see?
The migrated code should keep the formatting.
What did you see instead?
It creates this monster:
I can see that the generated code has ignored the formatting, the JavaTemplate string has no line breaks or indents whatsoever:
The text was updated successfully, but these errors were encountered: