Skip to content

Commit

Permalink
fix issue where first comment above import is duplicated (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-y authored Oct 6, 2020
1 parent a82b09a commit 91fc770
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main/java/net/revelc/code/impsort/ImpSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ public Result parseFile(final Path path) throws IOException {
Stream.concat(orphanedComments, importDeclarations.stream()).collect(Collectors.toList());
importSectionNodes.sort(BY_POSITION);
// position line numbers start at 1, not 0
int start = importSectionNodes.get(0).getBegin().get().line - 1;
Node firstImport = importSectionNodes.get(0);
int start = firstImport.getComment().map(c -> c.getBegin().get())
.orElse(firstImport.getBegin().get()).line - 1;
int stop = importSectionNodes.get(importSectionNodes.size() - 1).getEnd().get().line;
// get the original import section lines from the file
// include surrounding whitespace
Expand Down
19 changes: 18 additions & 1 deletion src/test/java/net/revelc/code/impsort/ImpSortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -159,7 +160,7 @@ public void testIso8859ForIssue3() throws IOException {
new ImpSort(StandardCharsets.ISO_8859_1, eclipseDefaults, true, true, LineEnding.AUTO)
.parseFile(p);
assertTrue(result.getImports().isEmpty());
Path output = File.createTempFile("impSort", null).toPath();
Path output = File.createTempFile("impSortIso8859", null, new File("target")).toPath();
result.saveSorted(output);
byte[] testData = Files.readAllBytes(p);
// ensure expected ISO_8859_1 byte is present in test data, this defends against file being
Expand Down Expand Up @@ -201,4 +202,20 @@ public void testRemoveSamePackageImports() {
assertTrue(imports.stream().anyMatch(imp -> "abcd.ef.Blah.Blah".equals(imp.getImport())));
assertTrue(imports.stream().anyMatch(imp -> "abcd.efg.Blah2".equals(imp.getImport())));
}

@Test
public void testResultStartWithComment() throws IOException {
Path p =
Paths.get(System.getProperty("user.dir"), "src", "test", "resources", "FirstImportComment.java");
Result result =
new ImpSort(StandardCharsets.UTF_8, eclipseDefaults, true, true, LineEnding.AUTO)
.parseFile(p);

Path output = File.createTempFile("impSortComment", null, new File("target")).toPath();
result.saveSorted(output);

List<String> lines = Files.readAllLines(output);
assertEquals(1, lines.stream().filter(line -> line.equals(" * Some comment")).count());
}
}

25 changes: 25 additions & 0 deletions src/test/resources/FirstImportComment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Some comment
*
*/
import java.lang.Math;

class TestClass{
TestClass() {
int foo = Math.abs(1);
}
}

0 comments on commit 91fc770

Please sign in to comment.