Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import Sorter to cope with multi-line comments and misplaced imports #112

Merged
merged 1 commit into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure that this if..continue block is needed.

AFAICT, !next.contains("/*") can never be true at this point. I can explain my reasoning if you wish, but it's a bit long-winded, I've not tested it with JUnit or a debugger, and there's no harm to this block staying anyway AFAICT.

Copy link
Member Author

@fvgh fvgh May 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have this situation in the following case:

/* //isMultiLineComment = true
import what.so.ever */ //isMultiLineComment = false

In the old code what.so.ever would have been part of the refactored imports, so it was originally commented out.
If I refactor it to an inner class, it becomes easier to read. But at first I wanted to assure you, that I actually did not alter much in the initial logic of the method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the old code what.so.ever would have been part of the refactored imports, so it was originally commented out.

Sorry, I don't understand what this sentence is saying. Can you rephrase it for me?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original behavior:

import i.need.this
/*
import i.don.t.need.that */

Results in

import i.need.this
import i.don.t.need.that

New behavior detects multi-line comments. So result is:

import i.need.this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just saw my mistake. I wanted to say:
In the old code what.so.ever would have been part of the refactored imports, though it was originally commented out.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha I see now. Thanks for the clarifications @fvgh. :)

}

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;