Skip to content

Commit 93117da

Browse files
dansanduleacGitHub Enterprise
authored andcommitted
Baasic restructuring for palantir style (#1)
Changed "GOOGLE" style (to be renamed later) * 120 character limit * 4 character indent (i.e. internal indent multiplier of 2 instead of the default 1) * IntelliJ run configuration that runs all tests * Ability to regenerate test cases by running `FormatterIntegrationTest` with `-Drecreate=true` Also, added a test case using internal code, that is currently handled badly by both GJF and eclipse (`palantir-sandor-lambdas`).
1 parent 05df9d3 commit 93117da

166 files changed

Lines changed: 4612 additions & 4623 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ target/
1313
bin/
1414
out/
1515
eclipse_plugin/lib/
16+
17+
!.idea/runConfigurations

‎.idea/runConfigurations/Test.xml‎

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/src/main/java/com/google/googlejavaformat/java/Formatter.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
@Immutable
8888
public final class Formatter {
8989

90-
public static final int MAX_LINE_LENGTH = 100;
90+
public static final int MAX_LINE_LENGTH = 120;
9191

9292
static final Range<Integer> EMPTY_RANGE = Range.closedOpen(-1, -1);
9393

‎core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class JavaFormatterOptions {
3131

3232
public enum Style {
3333
/** The default Google Java Style configuration. */
34-
GOOGLE(1),
34+
GOOGLE(2),
3535

3636
/** The AOSP-compliant configuration. */
3737
AOSP(2);

‎core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@
2525
import com.google.common.reflect.ClassPath;
2626
import com.google.common.reflect.ClassPath.ResourceInfo;
2727
import com.google.googlejavaformat.Newlines;
28+
import java.io.BufferedWriter;
2829
import java.io.IOException;
2930
import java.io.InputStream;
3031
import java.io.InputStreamReader;
32+
import java.nio.file.Files;
3133
import java.nio.file.Path;
3234
import java.nio.file.Paths;
3335
import java.util.ArrayList;
3436
import java.util.List;
3537
import java.util.Map;
3638
import java.util.TreeMap;
39+
import org.junit.Assume;
3740
import org.junit.Test;
3841
import org.junit.runner.RunWith;
3942
import org.junit.runners.Parameterized;
@@ -43,17 +46,20 @@
4346
@RunWith(Parameterized.class)
4447
public class FormatterIntegrationTest {
4548

49+
private static final Path TEST_DATA_PATH = Paths.get("com/google/googlejavaformat/java/testdata");
50+
/** Where to output test outputs when recreating. */
51+
private static final Path OUTPUT_TEST_PATH = Paths.get("src/test/resources").resolve(TEST_DATA_PATH);
52+
4653
@Parameters(name = "{index}: {0}")
4754
public static Iterable<Object[]> data() throws IOException {
48-
Path testDataPath = Paths.get("com/google/googlejavaformat/java/testdata");
4955
ClassLoader classLoader = FormatterIntegrationTest.class.getClassLoader();
5056
Map<String, String> inputs = new TreeMap<>();
5157
Map<String, String> outputs = new TreeMap<>();
5258
for (ResourceInfo resourceInfo : ClassPath.from(classLoader).getResources()) {
5359
String resourceName = resourceInfo.getResourceName();
5460
Path resourceNamePath = Paths.get(resourceName);
55-
if (resourceNamePath.startsWith(testDataPath)) {
56-
Path subPath = testDataPath.relativize(resourceNamePath);
61+
if (resourceNamePath.startsWith(TEST_DATA_PATH)) {
62+
Path subPath = TEST_DATA_PATH.relativize(resourceNamePath);
5763
assertEquals("bad testdata file names", 1, subPath.getNameCount());
5864
String baseName = getNameWithoutExtension(subPath.getFileName().toString());
5965
String extension = getFileExtension(subPath.getFileName().toString());
@@ -74,12 +80,20 @@ public static Iterable<Object[]> data() throws IOException {
7480
}
7581
}
7682
List<Object[]> testInputs = new ArrayList<>();
77-
assertEquals("unmatched inputs and outputs", inputs.size(), outputs.size());
83+
if (!isRecreate()) {
84+
assertEquals("unmatched inputs and outputs", inputs.size(), outputs.size());
85+
}
7886
for (Map.Entry<String, String> entry : inputs.entrySet()) {
7987
String fileName = entry.getKey();
8088
String input = inputs.get(fileName);
81-
assertTrue("unmatched input", outputs.containsKey(fileName));
82-
String expectedOutput = outputs.get(fileName);
89+
90+
String expectedOutput;
91+
if (isRecreate()) {
92+
expectedOutput = null;
93+
} else {
94+
assertTrue("unmatched input", outputs.containsKey(fileName));
95+
expectedOutput = outputs.get(fileName);
96+
}
8397
testInputs.add(new Object[] {fileName, input, expectedOutput});
8498
}
8599
return testInputs;
@@ -94,13 +108,17 @@ public FormatterIntegrationTest(String name, String input, String expected) {
94108
this.name = name;
95109
this.input = input;
96110
this.expected = expected;
97-
this.separator = Newlines.getLineEnding(expected);
111+
this.separator = isRecreate() ? null : Newlines.getLineEnding(expected);
98112
}
99113

100114
@Test
101115
public void format() {
102116
try {
103117
String output = new Formatter().formatSource(input);
118+
if (isRecreate()) {
119+
writeFormatterOutput(output);
120+
return;
121+
}
104122
assertEquals("bad output for " + name, expected, output);
105123
} catch (FormatterException e) {
106124
fail(String.format("Formatter crashed on %s: %s", name, e.getMessage()));
@@ -109,6 +127,7 @@ public void format() {
109127

110128
@Test
111129
public void idempotentLF() {
130+
Assume.assumeFalse("Not running when recreating test outputs", isRecreate());
112131
try {
113132
String mangled = expected.replace(separator, "\n");
114133
String output = new Formatter().formatSource(mangled);
@@ -120,6 +139,7 @@ public void idempotentLF() {
120139

121140
@Test
122141
public void idempotentCR() throws IOException {
142+
Assume.assumeFalse("Not running when recreating test outputs", isRecreate());
123143
try {
124144
String mangled = expected.replace(separator, "\r");
125145
String output = new Formatter().formatSource(mangled);
@@ -131,6 +151,7 @@ public void idempotentCR() throws IOException {
131151

132152
@Test
133153
public void idempotentCRLF() {
154+
Assume.assumeFalse("Not running when recreating test outputs", isRecreate());
134155
try {
135156
String mangled = expected.replace(separator, "\r\n");
136157
String output = new Formatter().formatSource(mangled);
@@ -139,4 +160,20 @@ public void idempotentCRLF() {
139160
fail(String.format("Formatter crashed on %s: %s", name, e.getMessage()));
140161
}
141162
}
163+
164+
private static boolean isRecreate() {
165+
return Boolean.getBoolean("recreate");
166+
}
167+
168+
private Path getOutputTestPath() {
169+
return OUTPUT_TEST_PATH.resolve(name + ".output");
170+
}
171+
172+
private void writeFormatterOutput(String output) {
173+
try (BufferedWriter writer = Files.newBufferedWriter(getOutputTestPath())) {
174+
writer.append(output);
175+
} catch (IOException e) {
176+
throw new RuntimeException(e);
177+
}
178+
}
142179
}

‎core/src/test/resources/com/google/googlejavaformat/java/testdata/A.output‎

Lines changed: 65 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,77 @@ package com.google.googlejavaformat.java.test;
66
* ArrayTypes, AssertStatements, and Assignments.
77
*/
88
class A {
9-
public @interface X {
10-
int x();
9+
public @interface X {
10+
int x();
1111

12-
int y() default
13-
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
14-
+ 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;
15-
}
12+
int y() default
13+
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
14+
+ 1 + 1 + 1 + 1 + 1 + 1;
15+
}
1616

17-
@X(x = 1)
18-
private @interface Y {}
17+
@X(x = 1)
18+
private @interface Y {}
1919

20-
// TODO(jdd): Add annotation declaration with empty body.
20+
// TODO(jdd): Add annotation declaration with empty body.
2121

22-
@X(x = 1)
23-
@Y
24-
protected @interface Z {}
22+
@X(x = 1)
23+
@Y
24+
protected @interface Z {}
2525

26-
// TODO(jdd): Include type annotations once we can include a higher language level.
26+
// TODO(jdd): Include type annotations once we can include a higher language level.
2727

28-
int[] array1 = new int[5];
29-
int[] array2 =
30-
new int[] {
31-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
32-
25, 26, 27, 28, 29, 30, 31
33-
};
34-
int[] array3 = {
35-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
36-
26, 27, 28, 29, 30, 31
37-
};
38-
int[][] array4 = {
39-
{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}, {10, 11}, {12, 13}, {14, 15}, {16, 17}, {18, 19},
40-
{20, 21}, {22, 23}
41-
};
42-
int[][][] arrayWithLongName =
43-
new int
44-
[0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0
45-
+ 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0]
46-
[0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0
47-
+ 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0]
48-
[];
28+
int[] array1 = new int[5];
29+
int[] array2 =
30+
new int[] {
31+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
32+
28, 29, 30, 31
33+
};
34+
int[] array3 = {
35+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
36+
30, 31
37+
};
38+
int[][] array4 = {
39+
{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}, {10, 11}, {12, 13}, {14, 15}, {16, 17}, {18, 19}, {20, 21}, {22, 23}
40+
};
41+
int[][][] arrayWithLongName =
42+
new int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 + 0 + 0 + 0
54+
+ 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0]
55+
+ array2[
56+
0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0
57+
+ 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0]
58+
+ array3[
59+
0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0
60+
+ 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0]
61+
+ array4[
62+
0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0
63+
+ 0][
64+
0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0
65+
+ 0];
66+
};
6867

69-
void f(int something) {
70-
assert 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
71-
== 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;
72-
assert 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
73-
== 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
74-
: "that was certainly unexpected!";
75-
arrayWithLongName[
76-
0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0] =
77-
arrayWithLongName[
78-
0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 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 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
81+
}
8482
}

‎core/src/test/resources/com/google/googlejavaformat/java/testdata/B.output‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package com.google.googlejavaformat.java.test;
22

33
/** Tests for Blocks, BodyDeclarations, BooleanLiterals, and BreakStatements. */
44
class B {
5-
int x;
6-
private int y;
7-
public int z;
5+
int x;
6+
private int y;
7+
public int z;
88

9-
void f() {
10-
LABEL:
11-
while (true != false) {
12-
if (false == true) break;
13-
if (false == false) break LABEL;
9+
void f() {
10+
LABEL:
11+
while (true != false) {
12+
if (false == true) break;
13+
if (false == false) break LABEL;
14+
}
1415
}
15-
}
1616
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class B112853497 {
2-
{
3-
XxxxxxxxxxXxxx xxxXxxxx =
4-
xxxXxxxx.xxxxXxXxxxx(xxxxXxxx, XXXXXX_XXXX_XXXXXXXX_XX_XXXXXXXX).toBuilder()
5-
.xxxXxxxxXxxxXx(xxxxxXx.xxxXxxxxXxxxXx())
6-
.xxxXxxxxXxxxXx(xxxxxXx.xxxXxxxxXxxxXx())
7-
.xxxXxxxxx(xxxxxx.xxxXxxxxxXxxx())
8-
.xxxxx();
9-
}
2+
{
3+
XxxxxxxxxxXxxx xxxXxxxx =
4+
xxxXxxxx.xxxxXxXxxxx(xxxxXxxx, XXXXXX_XXXX_XXXXXXXX_XX_XXXXXXXX).toBuilder()
5+
.xxxXxxxxXxxxXx(xxxxxXx.xxxXxxxxXxxxXx())
6+
.xxxXxxxxXxxxXx(xxxxxXx.xxxXxxxxXxxxXx())
7+
.xxxXxxxxx(xxxxxx.xxxXxxxxxXxxx())
8+
.xxxxx();
9+
}
1010
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class B124394008 {
2-
{
3-
LocalReviewProto.Builder newReview =
4-
readProfileResponse
5-
.flatMap(authorExtractor::extract)
6-
.map(luReviewsExtractor::toLocalReviewProto)
7-
.orElse(LocalReviewProto.getDefaultInstance())
8-
.toBuilder();
9-
}
2+
{
3+
LocalReviewProto.Builder newReview =
4+
readProfileResponse
5+
.flatMap(authorExtractor::extract)
6+
.map(luReviewsExtractor::toLocalReviewProto)
7+
.orElse(LocalReviewProto.getDefaultInstance())
8+
.toBuilder();
9+
}
1010
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class B126411718 {
2-
{
3-
logger.atDebug().log(
4-
"Scratch Session Cleaner exiting. Number of deleted sessions: %d, names: %s",
5-
deletedPersistentNames.size(), deletedPersistentNames);
6-
}
2+
{
3+
logger.atDebug().log(
4+
"Scratch Session Cleaner exiting. Number of deleted sessions: %d, names: %s",
5+
deletedPersistentNames.size(), deletedPersistentNames);
6+
}
77
}

0 commit comments

Comments
 (0)