Skip to content

Feature/3184 sort members by name - #3200

Open
Pankraz76 wants to merge 4 commits into
diffplug:mainfrom
Pankraz76:feature/3184-sort-members-by-name
Open

Pankraz76 wants to merge 4 commits into
diffplug:mainfrom
Pankraz76:feature/3184-sort-members-by-name

Conversation

@Pankraz76

Copy link
Copy Markdown

Closes #3184

Problem

With the Eclipse JDT step's Sort Members feature enabled, members of the same category (and the same visibility, if sortMembersVisibilityOrderEnabled is on) are always sorted alphabetically by name. There is no way to keep their original relative order — e.g. a JUnit setup method ends up between test methods.

Root cause

DefaultJavaElementComparator.compare(...) always falls back to a name comparison (methods: name, then parameter types; fields/types/enum constants/annotation members: name) once category and visibility are equal. Only initializers and members preserved by sortMembersDoNotSortFields keep their relative order.

Change

  • New opt-in option sortMembersByName (default true = current behaviour, unchanged). If false, members that are equal on category (and visibility) keep their original relative order — for all member kinds (methods incl. overloads, constructors, fields when sorted, nested types, enum constants, annotation type members).
    • lib: EclipseJdtFormatterStep.Builder#sortMembersByName(boolean) (Spotless-specific key spotless.sort_members.by_name, no Eclipse equivalent)
    • Gradle: eclipse().sortMembersByName(false)
    • Maven: <sortMembersByName>false</sortMembersByName>
  • File-level override // @SortMembers:sortByName=false|true, same mechanism as the existing overrides.
  • READMEs (Gradle + Maven) and all three CHANGES.md updated.

Tests

New cases in EclipseJdtFormatterStepSpecialCaseTest (all via StepHarness.testResource, which also asserts idempotence), expected outputs written by hand:

  • by-name disabled → categories still grouped, relative order kept (incl. overloads foo(String)/foo(int) and constructors)
  • by-name disabled + visibility order → grouped by visibility, relative order kept inside each group
  • by-name disabled + sortMembersDoNotSortFields(false) → fields and enum constants keep their order
  • local override sortByName=false with global default, and sortByName=true with global false

All existing sort tests and resources are unchanged and green.

  • PR allows edits from maintainers
  • CHANGES.md, plugin-gradle/CHANGES.md, plugin-maven/CHANGES.md updated (linking the issue)

🤖 Generated with Claude Code

https://claude.ai/code/session_01MTr23xDiCMWvQrEoZQSNHQ

When disabled, members of the same category (and visibility, if enabled)
keep their original relative order instead of being sorted alphabetically.
Can be overridden per file with // @SortMembers:sortByName=false|true.
Default (true) keeps the current behaviour.

Refs diffplug#3184

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MTr23xDiCMWvQrEoZQSNHQ
Refs diffplug#3184

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MTr23xDiCMWvQrEoZQSNHQ
Refs diffplug#3184

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MTr23xDiCMWvQrEoZQSNHQ

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Disable Alphabetical order when sortMembersEnabled is enabled

2 participants