Skip to content

Organize Members hint: don't rearrange record components#8220

Merged
mbien merged 1 commit intoapache:masterfrom
mbien:fix-organize-members-for-records
Jul 17, 2025
Merged

Organize Members hint: don't rearrange record components#8220
mbien merged 1 commit intoapache:masterfrom
mbien:fix-organize-members-for-records

Conversation

@mbien
Copy link
Member

@mbien mbien commented Feb 2, 2025

if the formatter is configured to sort members alphabetically, the hint should not rearrange record components.

The fix reuses the existing special case for enums, which holds the enum "members" in place and does the same for synthetic record members.

import java.awt.Color;

// enable formatter setting: ordering -> alphabetical member sort
// this should not influence record components
public record OrderMembersRecord(Color borderColor, double borderSize, int width, int height) {

    public static int c;
    public static int b;
    public static int d;
    public static int a;

}

@mbien mbien added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) hints ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) labels Feb 2, 2025
@mbien mbien added this to the NB26 milestone Feb 2, 2025
@mbien mbien marked this pull request as ready for review March 30, 2025 18:20
@mbien mbien force-pushed the fix-organize-members-for-records branch from 646d9ba to 75199a9 Compare April 7, 2025 22:35
Comment on lines 131 to 136
for (Tree tree : clazz.getMembers()) {
if (copy.getTreeUtilities().isSynthetic(new TreePath(path, tree))) {
// isSynthetic does not consider record components to be synthetic
if (copy.getTreeUtilities().isSynthetic(new TreePath(path, tree))
&& !(tree.getKind() == Kind.VARIABLE && copy.getTreeUtilities().isRecordComponent((VariableTree)tree))) {
continue;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought this is lower risk than updating isSynthetic() although it probably should be updated at some point.

inserting

            if (path.getLeaf().getKind() == Kind.VARIABLE &&
                path.getParentPath() != null &&
                path.getParentPath().getLeaf().getKind() == Kind.RECORD) {
                Set<Modifier> mods = ((VariableTree) path.getLeaf()).getModifiers().getFlags();
                if (!mods.contains(Modifier.STATIC)) {
                    return true; // all non static record fields are synthetic
                }
            }

at

would work

Comment on lines +47 to +51
@Override
protected boolean runInEQ() {
// without it, hint markers are sometimes not found
return true;
}
Copy link
Member Author

@mbien mbien Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't know why this is needed. OrganizeMembers invokes runModificationTask which calls copy.rewrite() at some point.

result.getDifferences(source.getFileObject()) is sometimes null if tests don't run on EDT.

@mbien mbien requested a review from lahodaj April 7, 2025 22:46
@neilcsmith-net neilcsmith-net modified the milestones: NB26, NB27 Apr 16, 2025
@mbien mbien force-pushed the fix-organize-members-for-records branch from 75199a9 to bb696b9 Compare June 23, 2025 17:02
Copy link
Contributor

@lahodaj lahodaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable to me. Thanks!

if the formatter is configured to sort members alphabetically,
the hint should not rearrange record components.
@mbien mbien removed the ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) label Jul 17, 2025
@mbien mbien force-pushed the fix-organize-members-for-records branch from bb696b9 to ba195cc Compare July 17, 2025 19:24
@mbien mbien merged commit 6fb7d9d into apache:master Jul 17, 2025
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hints Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants