Skip to content

Commit 8b8ae6e

Browse files
committed
Add local properties to enable/disable certain features of the Sort Members functionality
1 parent 9171810 commit 8b8ae6e

File tree

11 files changed

+443
-12
lines changed

11 files changed

+443
-12
lines changed

lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtSortMembers.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import java.util.Comparator;
1919
import java.util.Map;
20+
import java.util.Optional;
21+
import java.util.regex.Matcher;
22+
import java.util.regex.Pattern;
2023

2124
import org.eclipse.core.resources.IResource;
2225
import org.eclipse.core.runtime.CoreException;
@@ -32,18 +35,41 @@
3235

3336
public class EclipseJdtSortMembers {
3437

35-
static String sortMember(String code, SortProperties properties) {
36-
if (!properties.enabled) {
38+
private static final Pattern PATTERN_DO_NOT_SORT_FIELDS = Pattern.compile("@SortMembers:doNotSortFields\\s*=\\s*(false|true)");
39+
private static final Pattern PATTERN_ENABLED = Pattern.compile("@SortMembers:enabled\\s*=\\s*(false|true)");
40+
private static final Pattern PATTERN_SORT_BY_VISIBILITY = Pattern.compile("@SortMembers:sortByVisibility\\s*=\\s*(false|true)");
41+
42+
static SortProperties localProperties(SortProperties globalProperties, String code) {
43+
Optional<Boolean> localDoNotSortFields = testOverwriteProperty(PATTERN_DO_NOT_SORT_FIELDS, code);
44+
Optional<Boolean> localEnabled = testOverwriteProperty(PATTERN_ENABLED, code);
45+
Optional<Boolean> localSortByVisibility = testOverwriteProperty(PATTERN_SORT_BY_VISIBILITY, code);
46+
if (localDoNotSortFields.isEmpty() && localEnabled.isEmpty() && localSortByVisibility.isEmpty()) {
47+
return globalProperties;
48+
}
49+
boolean doNotSortFields = localDoNotSortFields.orElse(globalProperties.doNotSortFields);
50+
boolean enabled = localEnabled.orElse(globalProperties.enabled);
51+
boolean sortByVisibility = localSortByVisibility.orElse(globalProperties.sortByVisibility);
52+
return new SortProperties(
53+
enabled,
54+
globalProperties.membersOrder,
55+
doNotSortFields,
56+
sortByVisibility,
57+
globalProperties.visibilityOrder);
58+
}
59+
60+
static String sortMember(String code, SortProperties globalProperties) {
61+
SortProperties localProperties = localProperties(globalProperties, code);
62+
if (!localProperties.enabled) {
3763
return code;
3864
}
3965

4066
try {
4167
CompilationUnit compilationUnit = new CompilationUnit(code);
4268
DefaultJavaElementComparator comparator = DefaultJavaElementComparator.of(
43-
properties.doNotSortFields,
44-
properties.membersOrder,
45-
properties.sortByVisibility,
46-
properties.visibilityOrder);
69+
localProperties.doNotSortFields,
70+
localProperties.membersOrder,
71+
localProperties.sortByVisibility,
72+
localProperties.visibilityOrder);
4773
new Sorter(AST.getJLSLatest(), compilationUnit, null, comparator).sort();
4874
String content = compilationUnit.getBuffer().getContents();
4975
if (content != null) {
@@ -55,6 +81,15 @@ static String sortMember(String code, SortProperties properties) {
5581
return code;
5682
}
5783

84+
static Optional<Boolean> testOverwriteProperty(Pattern pattern, String code) {
85+
Matcher matcher = pattern.matcher(code);
86+
if (matcher.find()) {
87+
String flag = matcher.group(1);
88+
return Optional.of(Boolean.valueOf(flag));
89+
}
90+
return Optional.empty();
91+
}
92+
5893
private static class Buffer implements IBuffer {
5994

6095
private String contents;

lib-extra/src/test/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStepSpecialCaseTest.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,60 @@ void issue_1638() {
3737
}
3838

3939
@Test
40-
void sort_members_no_fields() {
40+
void sort_members_global_by_visibility() {
4141
EclipseJdtFormatterStep.Builder builder = EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral());
42-
builder.setMembersOrdering("SF,SI,SM,F,I,C,M,T", true);
42+
builder.setMembersOrdering("SF,SI,SM,F,I,C,M,T", false);
43+
builder.setVisibilityOrdering("B,R,D,V");
4344
StepHarness.forStep(builder.build())
44-
.testResource("java/eclipse/SortExample.test", "java/eclipse/SortExample.sortMembersNoFields.clean");
45+
.testResource("java/eclipse/SortExample.test", "java/eclipse/SortExample.sortMembersByVisibility.clean");
4546
}
4647

4748
@Test
48-
void sort_members() {
49+
void sort_members_global_enabled() {
4950
EclipseJdtFormatterStep.Builder builder = EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral());
5051
builder.setMembersOrdering("SF,SI,SM,F,I,C,M,T", false);
5152
StepHarness.forStep(builder.build())
5253
.testResource("java/eclipse/SortExample.test", "java/eclipse/SortExample.sortMembers.clean");
5354
}
5455

5556
@Test
56-
void sort_members_by_visibility() {
57+
void sort_members_global_no_fields() {
58+
EclipseJdtFormatterStep.Builder builder = EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral());
59+
builder.setMembersOrdering("SF,SI,SM,F,I,C,M,T", true);
60+
StepHarness.forStep(builder.build())
61+
.testResource("java/eclipse/SortExample.test", "java/eclipse/SortExample.sortMembersNoFields.clean");
62+
}
63+
64+
@Test
65+
void sort_members_local_by_visibility() {
5766
EclipseJdtFormatterStep.Builder builder = EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral());
5867
builder.setMembersOrdering("SF,SI,SM,F,I,C,M,T", false);
5968
builder.setVisibilityOrdering("B,R,D,V");
6069
StepHarness.forStep(builder.build())
61-
.testResource("java/eclipse/SortExample.test", "java/eclipse/SortExample.sortMembersByVisibility.clean");
70+
.testResource("java/eclipse/SortExample.localSortByVisibility.test", "java/eclipse/SortExample.localSortByVisibility.clean");
71+
}
72+
73+
@Test
74+
void sort_members_local_enabled_false() {
75+
EclipseJdtFormatterStep.Builder builder = EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral());
76+
builder.setMembersOrdering("SF,SI,SM,F,I,C,M,T", false);
77+
StepHarness.forStep(builder.build())
78+
.testResource("java/eclipse/SortExample.localEnabledFalse.test", "java/eclipse/SortExample.localEnabledFalse.clean");
79+
}
80+
81+
@Test
82+
void sort_members_local_no_fields() {
83+
EclipseJdtFormatterStep.Builder builder = EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral());
84+
builder.setMembersOrdering("SF,SI,SM,F,I,C,M,T", false);
85+
StepHarness.forStep(builder.build())
86+
.testResource("java/eclipse/SortExample.localDoNotSortFields.test", "java/eclipse/SortExample.localDoNotSortFields.clean");
87+
}
88+
89+
90+
@Test
91+
void sort_members_local_enabled_true() {
92+
EclipseJdtFormatterStep.Builder builder = EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral());
93+
StepHarness.forStep(builder.build())
94+
.testResource("java/eclipse/SortExample.localEnabledTrue.test", "java/eclipse/SortExample.localEnabledTrue.clean");
6295
}
6396
}

plugin-gradle/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ spotless {
267267
eclipse().withP2Mirrors(['https://download.eclipse.org/eclipse/updates/4.29/':'https://some.internal.mirror/4-29-updates-p2/'])
268268
```
269269

270+
#### Sort Members
271+
270272
Not only can you format your code with Eclipse JDT, but you can also sort the members as you know it from Eclipse IDE.
271273
This ensures that the methods are always in sorted order (and thus reduces the likelihood of collisions in a version
272274
control system). It is turned off by default, but you might want to consider enabling it when setting coding standards
@@ -276,6 +278,8 @@ The format to specify the sort order follows the `outlinesortoption` and `org.ec
276278
properties that can be found in the workspace folder of your Eclipse IDE. Look up the
277279
file `.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs` in your workspace directory.
278280

281+
###### Define Sort Members settings on project level
282+
279283
```gradle
280284
spotless {
281285
java {
@@ -290,6 +294,13 @@ spotless {
290294
eclipse().sortMembers(memberCategoryOrder, doNotSortFields, visibilityOrder)
291295
```
292296

297+
###### Overwrite Sort Members settings on file level
298+
299+
You can enable/disable the sort properties on file level by adding the following comments:
300+
- `// @SortMembers:enabled=false` - disable the Sort Members feature for this file
301+
- `// @SortMembers:doNotSortFields=true` - disable the sorting of static and instance fields
302+
- `// @SortMembers:sortByVisibility=false` - don't sort members by its visibility modifier
303+
293304
### formatAnnotations
294305

295306
Type annotations should be on the same line as the type that they qualify.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.diffplug.spotless.extra.glue.jdt;
2+
3+
// @SortMembers:doNotSortFields = true
4+
public class SortExample {
5+
public static final int Z;
6+
private static final int A;
7+
static final int B = 1;
8+
protected static final int C = 2;
9+
static {
10+
Z = 9;
11+
A = 0;
12+
}
13+
static void s1() {
14+
}
15+
private static void s2() {
16+
}
17+
public static void s3() {
18+
}
19+
protected static void s4() {
20+
}
21+
final int z = 1;
22+
final int a = 1;
23+
private final int c = 1;
24+
protected final int e = 1;
25+
protected final int d = 1;
26+
public final int f = 1;
27+
public final int b = 1;
28+
final boolean _1 = false;
29+
SortExample() {
30+
}
31+
SortExample(int a) {
32+
}
33+
SortExample(int b, int c) {
34+
}
35+
class Nested {
36+
private static final String C = "C";
37+
protected static final String D = "D";
38+
public static final String B = "B";
39+
void a() {
40+
}
41+
public void b() {
42+
}
43+
private void c() {
44+
}
45+
protected void d() {
46+
}
47+
void z() {
48+
}
49+
}
50+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.diffplug.spotless.extra.glue.jdt;
2+
3+
// @SortMembers:doNotSortFields = true
4+
public class SortExample {
5+
final int z = 1;
6+
final int a = 1;
7+
private final int c = 1;
8+
protected final int e = 1;
9+
protected final int d = 1;
10+
public final int f = 1;
11+
public final int b = 1;
12+
final boolean _1 = false;
13+
static void s1() {}
14+
private static void s2() {}
15+
public static void s3() {}
16+
protected static void s4() {}
17+
SortExample(int b, int c) {}
18+
SortExample(int a) {}
19+
SortExample() {}
20+
class Nested {
21+
void z() {}
22+
private static final String C = "C";
23+
protected static final String D = "D";
24+
public static final String B = "B";
25+
void a() {}
26+
private void c() {}
27+
protected void d() {}
28+
public void b() {}
29+
}
30+
public static final int Z;
31+
private static final int A;
32+
static final int B =1;
33+
protected static final int C= 2;
34+
static {
35+
Z = 9;
36+
A = 0;
37+
}
38+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.diffplug.spotless.extra.glue.jdt;
2+
3+
// @SortMembers:enabled = false
4+
public class SortExample {
5+
final int z = 1;
6+
final int a = 1;
7+
private final int c = 1;
8+
protected final int e = 1;
9+
protected final int d = 1;
10+
public final int f = 1;
11+
public final int b = 1;
12+
final boolean _1 = false;
13+
static void s1() {
14+
}
15+
private static void s2() {
16+
}
17+
public static void s3() {
18+
}
19+
protected static void s4() {
20+
}
21+
SortExample(int b, int c) {
22+
}
23+
SortExample(int a) {
24+
}
25+
SortExample() {
26+
}
27+
class Nested {
28+
void z() {
29+
}
30+
private static final String C = "C";
31+
protected static final String D = "D";
32+
public static final String B = "B";
33+
void a() {
34+
}
35+
private void c() {
36+
}
37+
protected void d() {
38+
}
39+
public void b() {
40+
}
41+
}
42+
public static final int Z;
43+
private static final int A;
44+
static final int B = 1;
45+
protected static final int C = 2;
46+
static {
47+
Z = 9;
48+
A = 0;
49+
}
50+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.diffplug.spotless.extra.glue.jdt;
2+
3+
// @SortMembers:enabled = false
4+
public class SortExample {
5+
final int z = 1;
6+
final int a = 1;
7+
private final int c = 1;
8+
protected final int e = 1;
9+
protected final int d = 1;
10+
public final int f = 1;
11+
public final int b = 1;
12+
final boolean _1 = false;
13+
static void s1() {}
14+
private static void s2() {}
15+
public static void s3() {}
16+
protected static void s4() {}
17+
SortExample(int b, int c) {}
18+
SortExample(int a) {}
19+
SortExample() {}
20+
class Nested {
21+
void z() {}
22+
private static final String C = "C";
23+
protected static final String D = "D";
24+
public static final String B = "B";
25+
void a() {}
26+
private void c() {}
27+
protected void d() {}
28+
public void b() {}
29+
}
30+
public static final int Z;
31+
private static final int A;
32+
static final int B =1;
33+
protected static final int C= 2;
34+
static {
35+
Z = 9;
36+
A = 0;
37+
}
38+
}

0 commit comments

Comments
 (0)