Skip to content

Commit

Permalink
Enhance Import Sorter/Order to cope with multi-line comments and misp…
Browse files Browse the repository at this point in the history
…laced imports
  • Loading branch information
fvgh committed May 18, 2017
1 parent eef0714 commit b325e5c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/java/ImportSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,23 @@ public String format(String raw) {
int firstImportLine = 0;
int lastImportLine = 0;
int line = 0;
boolean isMultiLineComment = false;
List<String> imports = new ArrayList<>();
while (scanner.hasNext()) {
line++;
String next = scanner.nextLine();
if (next == null) {
break;
}
//Since we have no AST, we only consider the most common use cases.
isMultiLineComment |= next.contains("/*");
if (isMultiLineComment && next.contains("*/")) {
isMultiLineComment = false;
if (!next.contains("/*")) {
continue;
}
}

if (next.startsWith("import ")) {
int i = next.indexOf(".");
if (isNotValidImport(i)) {
Expand All @@ -59,17 +69,28 @@ public String format(String raw) {
int endIndex = next.indexOf(";");

String imprt = next.substring(START_INDEX_OF_IMPORTS_PACKAGE_DECLARATION, endIndex != -1 ? endIndex : next.length());
if (!imports.contains(imprt)) {
if (!isMultiLineComment && !imports.contains(imprt)) {
imports.add(imprt);
}
}
if (!isMultiLineComment && isBeginningOfScope(next)) {
break; //Don't dare to touch lines after a scope started
}
}
scanner.close();

List<String> sortedImports = ImportSorterImpl.sort(imports, importsOrder);
return applyImportsToDocument(raw, firstImportLine, lastImportLine, sortedImports);
}

private static boolean isBeginningOfScope(String line) {
int scope = line.indexOf("{");
if (0 <= scope) {
return !line.substring(0, scope).contains("//");
}
return false;
}

private static String applyImportsToDocument(final String document, int firstImportLine, int lastImportLine, List<String> strings) {
boolean importsAlreadyAppended = false;
Scanner scanner = new Scanner(document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ public void removeDuplicates() throws Throwable {
assertOnResources(step, "java/importsorter/JavaCodeSortedDuplicateImportsUnmatched.test", "java/importsorter/JavaCodeSortedImportsUnmatched.test");
}

@Test
public void removeComments() throws Throwable {
FormatterStep step = ImportOrderStep.createFromFile(createTestFile("java/importsorter/import.properties"));
assertOnResources(step, "java/importsorter/JavaCodeImportComments.test", "java/importsorter/JavaCodeSortedImports.test");
}

@Test
public void misplacedImports() throws Throwable {
FormatterStep step = ImportOrderStep.createFromFile(createTestFile("java/importsorter/import.properties"));
assertOnResources(step, "java/importsorter/JavaCodeUnsortedMisplacedImports.test", "java/importsorter/JavaCodeSortedMisplacedImports.test");
}

@Test
public void doesntThrowIfImportOrderIsntSerializable() {
ImportOrderStep.createFromOrder(NonSerializableList.of("java", "javax", "org", "\\#com"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import static java.lang.Exception.*;
//Will be removed
import org.dooda.Didoo; /* Comment removed, import stays */
import java.lang.Thread; /* Don't */ /* get */ /* confused */
import java.lang.Runnable;
/* import org.comments.will
import org.comments.be
import org.comments.removed
*/
import static java.lang.Runnable.*;
/*
import other.multiline.comments
import will.be.removed.too */
import static com.foo.Bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.lang.Runnable;
import java.lang.Thread;

import org.dooda.Didoo;

import static java.lang.Exception.*;
import static java.lang.Runnable.*;

import static com.foo.Bar;
public class NotDeletedByFormatter {
}
import will.not;
import be.sorted;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import static java.lang.Exception.*;
import org.dooda.Didoo;
/*
public class IgnoredAndRemoved {
}
*/
import java.lang.Thread;
// Will be removed {}
import java.lang.Runnable;

import static java.lang.Runnable.*;
import static com.foo.Bar
public class NotDeletedByFormatter {
}
import will.not;
import be.sorted;

0 comments on commit b325e5c

Please sign in to comment.