Improved support for indentation formatting using records and single arg method declarations#6158
Improved support for indentation formatting using records and single arg method declarations#6158
Conversation
…t + to the commit keeping Markup markers present
…t + to the commit keeping Markup markers present
Updated copyright year from 2024 to 2025.
…arg method declarations.
rewrite-java/src/main/java/org/openrewrite/java/format/MinimumViableSpacingVisitor.java
Outdated
Show resolved
Hide resolved
…ViableSpacingVisitor.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
rewrite-java/src/main/java/org/openrewrite/java/format/TabsAndIndentsVisitor.java
Outdated
Show resolved
Hide resolved
| if (elements.size() > 1) { | ||
| try { | ||
| if ((loc == JRightPadded.Location.METHOD_DECLARATION_PARAMETER && style.getMethodDeclarationParameters().getAlignWhenMultiple()) || | ||
| ((loc == JRightPadded.Location.RECORD_STATE_VECTOR && style.getRecordComponents() != null && style.getRecordComponents().getAlignWhenMultiple()))) { |
There was a problem hiding this comment.
style.getRecordComponents() is not marked as @Nullable; should it be? Or can we remove this guard since we seem to rely on the NoSuchMethodError below.
There was a problem hiding this comment.
as with the try/catch, we had NPE's, No SuchMethodErrors last time when introducing new style classes. So added like this to be safe. We should then later be able to remove the try/catch's and the if != null statements
There was a problem hiding this comment.
I think it makes more sense for these temporary backwards compatibility null checks to exist within the relevant style class. You can always provide an implementation for getRecordComponents() which returns the default value when the underlying field is null. Then when the null check is no longer relevant there is only one place to remove it from, rather than from every call site.
There was a problem hiding this comment.
That makes sense in this occasion as we will introduce both the style and the default getter in a single go. It will hence be either a NoSuchMethod or a defaulted getter. Last time we also had the possibilitiy of an old (non-defaulted) getter being parent loaded.
Adapted!
…IndentsVisitor.java Co-authored-by: Tim te Beek <tim@moderne.io>
rewrite-java/src/main/java/org/openrewrite/java/format/TabsAndIndentsVisitor.java
Outdated
Show resolved
Hide resolved
|
ran with cli against apache org -> no errors datatable |
Summary
This PR improves indentation formatting support for Java records and method declarations, particularly for cases with single or multiple parameters that span multiple lines. The changes ensure consistent continuation indentation and proper alignment of parameters in both record components and method declarations.
Changes
Core Formatting Improvements
TabsAndIndentsVisitor.javacomputeFirstParameterColumn()to handle bothJ.MethodDeclarationandJ.ClassDeclaration(for records)IndentTypemessage passing to maintain proper indentation contextdeclPrefixLength + continuationIndentwhen first parameter is on a new lineRECORD_STATE_VECTORandMETHOD_DECLARATION_PARAMETERto argument alignment casesMinimumViableSpacingVisitor.javaStyle Configuration
TabsAndIndentsStyle.javaRecordComponentsconfiguration class withalignWhenMultiplesettingMethodDeclarationParametersstructure for consistencyAutodetect.javaRecordComponentsconfigurationIntelliJ.javaRecordComponentswith alignment enabledTest Coverage
AutoFormatTest.javaAdded comprehensive test coverage for:
StyleHelperTest.javaRecordComponentsstyle merging works correctlyExample Transformations
Record with single parameter
Record with multiple parameters
Method with single parameter
Technical Notes
NoSuchMethodErrorcatch block for backward compatibility whenRecordComponentsis not available in older runtime environments/lsts