Skip to content

Commit b1652c4

Browse files
cushonronshapiro
authored andcommitted
Ensure a single blank line is present before and after non-empty import blocks
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=155446104
1 parent 9204cf5 commit b1652c4

File tree

4 files changed

+63
-20
lines changed

4 files changed

+63
-20
lines changed

core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import static com.google.common.collect.Iterables.getLast;
1717

18+
import com.google.common.base.CharMatcher;
1819
import com.google.common.base.Optional;
1920
import com.google.common.collect.ImmutableList;
2021
import com.google.common.collect.ImmutableSet;
@@ -124,10 +125,18 @@ private String reorderImports() throws FormatterException {
124125
tail = text.substring(tailStart);
125126
}
126127

127-
return tokString(0, unindentedFirstImportStart)
128-
+ reorderedImportsString(imports.imports)
129-
+ tokString(afterLastImport, toks.size())
130-
+ tail;
128+
StringBuilder result = new StringBuilder();
129+
result.append(
130+
CharMatcher.whitespace().trimTrailingFrom(tokString(0, unindentedFirstImportStart)));
131+
if (result.length() > 0) {
132+
result.append(lineSeparator).append(lineSeparator);
133+
}
134+
result.append(reorderedImportsString(imports.imports));
135+
result.append(lineSeparator);
136+
result.append(
137+
CharMatcher.whitespace().trimLeadingFrom(tokString(afterLastImport, toks.size())));
138+
result.append(tail);
139+
return result.toString();
131140
}
132141

133142
private String tokString(int start, int end) {

core/src/test/java/com/google/googlejavaformat/java/ImportOrdererTest.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import org.junit.runners.Parameterized;
2626
import org.junit.runners.Parameterized.Parameters;
2727

28-
/**
29-
* Tests for {@link ImportOrderer}.
30-
*/
28+
/** Tests for {@link ImportOrderer}. */
3129
@RunWith(Parameterized.class)
3230
public class ImportOrdererTest {
3331
@Parameters(name = "{index}: {0}")
@@ -42,7 +40,7 @@ public static Collection<Object[]> parameters() {
4240
String[][][] inputsOutputs = {
4341
{
4442
// Empty input produces empty output.
45-
{},
43+
{}, //
4644
{}
4745
},
4846
{
@@ -55,14 +53,14 @@ public static Collection<Object[]> parameters() {
5553
},
5654
{
5755
{
58-
"package foo;",
56+
"package foo;", //
5957
"",
6058
"import com.google.first.Bar;",
6159
"",
6260
"public class Blim {}",
6361
},
6462
{
65-
"package foo;",
63+
"package foo;", //
6664
"",
6765
"import com.google.first.Bar;",
6866
"",
@@ -204,29 +202,32 @@ public static Collection<Object[]> parameters() {
204202
// we unindent imports, if we reorder them
205203
{
206204
{
207-
" import com.foo.Second;",
205+
" import com.foo.Second;", //
208206
" import com.foo.First;",
209207
" public class Foo {}",
210208
},
211209
{
212-
"import com.foo.First;",
210+
"import com.foo.First;", //
213211
"import com.foo.Second;",
214-
" public class Foo {}",
212+
"",
213+
"public class Foo {}",
215214
}
216215
},
217216

218217
// Error cases
219218
{
220-
{"package com.google.example;",
219+
{
220+
"package com.google.example;", //
221221
"",
222-
"import\\", // \\ means there is no newline here.
222+
"import\\", // \\ means there is no newline here.
223223
},
224224
{
225225
"!!Unexpected token after import: ",
226226
}
227227
},
228228
{
229-
{"package com.google.example;",
229+
{
230+
"package com.google.example;", //
230231
"",
231232
"import",
232233
},
@@ -235,7 +236,8 @@ public static Collection<Object[]> parameters() {
235236
}
236237
},
237238
{
238-
{"package com.google.example;",
239+
{
240+
"package com.google.example;", //
239241
"",
240242
"import foo\\",
241243
},
@@ -244,7 +246,8 @@ public static Collection<Object[]> parameters() {
244246
}
245247
},
246248
{
247-
{"package com.google.example;",
249+
{
250+
"package com.google.example;", //
248251
"",
249252
"import foo.\\",
250253
},
@@ -279,7 +282,7 @@ public static Collection<Object[]> parameters() {
279282
},
280283
{
281284
{
282-
"import com.foo.Second; /* no block comments after imports */",
285+
"import com.foo.Second; /* no block comments after imports */", //
283286
"import com.foo.First;",
284287
},
285288
{
@@ -297,6 +300,7 @@ public static Collection<Object[]> parameters() {
297300
{
298301
"import com.foo.First;",
299302
"import com.foo.Second;",
303+
"",
300304
"/* but we're not fooled by comments that look like imports:",
301305
"import com.foo.Third;",
302306
"*/",
@@ -313,7 +317,7 @@ public static Collection<Object[]> parameters() {
313317
},
314318
{
315319
{
316-
"import com.abc.@;",
320+
"import com.abc.@;", //
317321
"import com.abc.@@;",
318322
},
319323
{

core/src/test/java/com/google/googlejavaformat/java/MainTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public void imports() throws Exception {
173173
String[] expected = {
174174
"import java.util.ArrayList;",
175175
"import java.util.List;",
176+
"",
176177
"class Test {",
177178
" /**",
178179
" * May be an {@link ArrayList}.",
@@ -189,6 +190,7 @@ public void imports() throws Exception {
189190
{
190191
String[] expected = {
191192
"import java.util.List;",
193+
"",
192194
"class Test {",
193195
" /** May be an {@link java.util.ArrayList}. */",
194196
" public static List<String> names;",
@@ -217,6 +219,7 @@ public void importRemovalLines() throws Exception {
217219
};
218220
String[] expected = {
219221
"import java.util.ArrayList;",
222+
"",
220223
"class Test {",
221224
" ArrayList<String> a = new ArrayList<>();",
222225
"ArrayList<String> b = new ArrayList<>();",

core/src/test/java/com/google/googlejavaformat/java/PartialFormattingTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,4 +1701,31 @@ private Range<Integer> rangeOf(String input, String needle) {
17011701
int idx = input.indexOf(needle);
17021702
return Range.closedOpen(idx, idx + needle.length());
17031703
}
1704+
1705+
@Test
1706+
public void importJavadocNewlines() throws Exception {
1707+
String input =
1708+
lines(
1709+
"package p;",
1710+
"import java.util.ArrayList;",
1711+
"/** */",
1712+
"class Foo {",
1713+
" ArrayList<String> xs = new ArrayList<>();",
1714+
"}",
1715+
"");
1716+
String expectedOutput =
1717+
lines(
1718+
"package p;",
1719+
"",
1720+
"import java.util.ArrayList;",
1721+
"",
1722+
"/** */",
1723+
"class Foo {",
1724+
" ArrayList<String> xs = new ArrayList<>();",
1725+
"}",
1726+
"");
1727+
1728+
String output = runFormatter(input, new String[] {"-lines", "2"});
1729+
assertThat(output).isEqualTo(expectedOutput);
1730+
}
17041731
}

0 commit comments

Comments
 (0)