Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.io.IOError;
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -241,9 +242,11 @@ private static RangeMap<Integer, String> buildReplacements(
Set<String> usedNames,
Multimap<String, Range<Integer>> usedInJavadoc) {
RangeMap<Integer, String> replacements = TreeRangeMap.create();
boolean isAllUnused = true;
for (JCImport importTree : unit.getImports()) {
String simpleName = getSimpleName(importTree);
if (!isUnused(unit, usedNames, usedInJavadoc, importTree, simpleName)) {
isAllUnused = false;
continue;
}
// delete the import
Expand All @@ -256,6 +259,36 @@ private static RangeMap<Integer, String> buildReplacements(
}
replacements.put(Range.closedOpen(importTree.getStartPosition(), endPosition), "");
}

// If after deleting all import statements, there are blank lines before or after,
// delete one blank line.
if (isAllUnused && replacements.asMapOfRanges().size() > 0) {
Iterator<Range<Integer>> iterator = replacements.asMapOfRanges().keySet().iterator();
Range<Integer> range = iterator.next();
int startPosition = range.lowerEndpoint();
int endPosition;
boolean isDuplicateRange = true;
while (iterator.hasNext()) {
int endPositionOfPrevious = range.upperEndpoint();
range = iterator.next();
if (endPositionOfPrevious != range.lowerEndpoint()) {
isDuplicateRange = false;
break;
}
}
endPosition = range.upperEndpoint();
if (isDuplicateRange) {
String sep = Newlines.guessLineSeparator(contents);
if (startPosition - sep.length() * 2 >= 0
&& contents.subSequence(startPosition - sep.length(), startPosition).toString().equals(sep)
&& contents.subSequence(startPosition - sep.length() * 2, startPosition - sep.length()).toString().equals(sep)) {
replacements.put(Range.closedOpen(startPosition - sep.length(), startPosition), "");
} else if (endPosition + sep.length() < contents.length()
&& contents.subSequence(endPosition, endPosition + sep.length()).toString().equals(sep)) {
replacements.put(Range.closedOpen(endPosition, endPosition + sep.length()), "");
}
}
}
return replacements;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,72 @@ public static Collection<Object[]> parameters() {
"interface Test { private static void foo() {} }",
},
},
{
{
"package com.foo;",
"",
"import com.bar.A;",
"",
"class Test {",
"}",
},
{
"package com.foo;",
"",
"class Test {",
"}",
},
},
{
{
"package com.sun.something;",
"",
"import com.x;",
"import com.y;",
"import com.z;",
"",
"class Test {",
"}",
},
{
"package com.sun.something;",
"",
"class Test {",
"}",
},
},
{
{
"package com.foo;",
"// hello",
"import com.bar.A;",
"",
"class Test {",
"}",
},
{
"package com.foo;",
"// hello",
"class Test {",
"}",
},
},
{
{
"package com.foo;",
"",
"import com.bar.A;",
"// hello",
"class Test {",
"}"
},
{
"package com.foo;",
"// hello",
"class Test {",
"}"
},
}
};
ImmutableList.Builder<Object[]> builder = ImmutableList.builder();
for (String[][] inputAndOutput : inputsOutputs) {
Expand Down