Skip to content

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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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
if the formatter is configured to sort members alphabetically,
the hint should not rearrange record components.
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) 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.

2 participants