Skip to content

Commit adf8837

Browse files
sormurascushon
authored andcommitted
Added format and optimize imports entry point.
Fixes #92 MOE_MIGRATED_REVID=171984272
1 parent c63984a commit adf8837

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ public void formatSource(CharSource input, CharSink output)
193193
/**
194194
* Format an input string (a Java compilation unit) into an output string.
195195
*
196+
* <p>Leaves import statements untouched.
197+
*
196198
* @param input the input string
197199
* @return the output string
198200
* @throws FormatterException if the input string cannot be parsed
@@ -201,6 +203,24 @@ public String formatSource(String input) throws FormatterException {
201203
return formatSource(input, ImmutableList.of(Range.closedOpen(0, input.length())));
202204
}
203205

206+
/**
207+
* Formats an input string (a Java compilation unit) and fixes imports.
208+
*
209+
* <p>Fixing imports includes ordering, spacing, and removal of unused import statements.
210+
*
211+
* @param input the input string
212+
* @return the output string
213+
* @throws FormatterException if the input string cannot be parsed
214+
* @see <a
215+
* href="https://google.github.io/styleguide/javaguide.html#s3.3.3-import-ordering-and-spacing">
216+
* Google Java Style Guide - 3.3.3 Import ordering and spacing</a>
217+
*/
218+
public String formatSourceAndFixImports(String input) throws FormatterException {
219+
input = ImportOrderer.reorderImports(input);
220+
input = RemoveUnusedImports.removeUnusedImports(input);
221+
return formatSource(input);
222+
}
223+
204224
/**
205225
* Format an input string (a Java compilation unit), for only the specified character ranges.
206226
* These ranges are extended as necessary (e.g., to encompass whole lines).

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,25 @@ public void importsNotReorderedByDefault() throws FormatterException {
248248
assertThat(output).isEqualTo(expect);
249249
}
250250

251+
@Test
252+
public void importsFixedIfRequested() throws FormatterException {
253+
String input =
254+
"package com.google.example;\n"
255+
+ UNORDERED_IMPORTS
256+
+ "\npublic class ExampleTest {\n"
257+
+ " @Nullable List<?> xs;\n"
258+
+ "}\n";
259+
String output = new Formatter().formatSourceAndFixImports(input);
260+
String expect =
261+
"package com.google.example;\n\n"
262+
+ "import java.util.List;\n"
263+
+ "import javax.annotations.Nullable;\n\n"
264+
+ "public class ExampleTest {\n"
265+
+ " @Nullable List<?> xs;\n"
266+
+ "}\n";
267+
assertThat(output).isEqualTo(expect);
268+
}
269+
251270
@Test
252271
public void importOrderingWithoutFormatting() throws IOException, UsageException {
253272
importOrdering(

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,47 @@ public void imports() throws Exception {
201201
assertThat(out.toString()).isEqualTo(joiner.join(expected));
202202
}
203203

204+
@Test
205+
public void optimizeImportsDoesNotLeaveEmptyLines() throws Exception {
206+
String[] input = {
207+
"package abc;",
208+
"",
209+
"import java.util.LinkedList;",
210+
"import java.util.List;",
211+
"import java.util.ArrayList;",
212+
"",
213+
"import static java.nio.charset.StandardCharsets.UTF_8;",
214+
"",
215+
"import java.util.EnumSet;",
216+
"",
217+
"class Test ",
218+
"extends ArrayList {",
219+
"}"
220+
};
221+
String[] expected = {
222+
"package abc;", //
223+
"",
224+
"import java.util.ArrayList;",
225+
"",
226+
"class Test extends ArrayList {}",
227+
""
228+
};
229+
230+
// pre-check expectation with local formatter instance
231+
String optimized = new Formatter().formatSourceAndFixImports(joiner.join(input));
232+
assertThat(optimized).isEqualTo(joiner.join(expected));
233+
234+
InputStream in = new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8));
235+
StringWriter out = new StringWriter();
236+
Main main =
237+
new Main(
238+
new PrintWriter(out, true),
239+
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
240+
in);
241+
assertThat(main.format("-")).isEqualTo(0);
242+
assertThat(out.toString()).isEqualTo(joiner.join(expected));
243+
}
244+
204245
// test that -lines handling works with import removal
205246
@Test
206247
public void importRemovalLines() throws Exception {

0 commit comments

Comments
 (0)