From 91fc770cfc11f40ce00e8b098c1dcbd00782afb9 Mon Sep 17 00:00:00 2001 From: Bruce Yu <51087152+bruce-au@users.noreply.github.com> Date: Tue, 6 Oct 2020 15:02:59 -0400 Subject: [PATCH] fix issue where first comment above import is duplicated (#36) --- .../java/net/revelc/code/impsort/ImpSort.java | 4 ++- .../net/revelc/code/impsort/ImpSortTest.java | 19 +++++++++++++- src/test/resources/FirstImportComment.java | 25 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/FirstImportComment.java diff --git a/src/main/java/net/revelc/code/impsort/ImpSort.java b/src/main/java/net/revelc/code/impsort/ImpSort.java index 41144a4..5cbbdc0 100644 --- a/src/main/java/net/revelc/code/impsort/ImpSort.java +++ b/src/main/java/net/revelc/code/impsort/ImpSort.java @@ -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 diff --git a/src/test/java/net/revelc/code/impsort/ImpSortTest.java b/src/test/java/net/revelc/code/impsort/ImpSortTest.java index e3ebb50..1f36ec6 100644 --- a/src/test/java/net/revelc/code/impsort/ImpSortTest.java +++ b/src/test/java/net/revelc/code/impsort/ImpSortTest.java @@ -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; @@ -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 @@ -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 lines = Files.readAllLines(output); + assertEquals(1, lines.stream().filter(line -> line.equals(" * Some comment")).count()); + } } + diff --git a/src/test/resources/FirstImportComment.java b/src/test/resources/FirstImportComment.java new file mode 100644 index 0000000..4ca1705 --- /dev/null +++ b/src/test/resources/FirstImportComment.java @@ -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); + } +}