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

fix issue where first comment above import is duplicated #36

Merged
merged 1 commit into from
Oct 6, 2020
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
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);
}
}