Skip to content

Commit

Permalink
Added wildcardsLast option for Java importOrder in Maven
Browse files Browse the repository at this point in the history
  • Loading branch information
tisoft committed Oct 2, 2021
1 parent 9f158c5 commit 3151bc0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
* Added `wildcardsLast` option for Java `importOrder` ([#954](https://github.com/diffplug/spotless/pull/954))

## [2.16.0] - 2021-10-02

Expand Down
1 change: 1 addition & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ any other maven phase (i.e. compile) then it can be configured as below;

<importOrder /> <!-- standard import order -->
<importOrder> <!-- or a custom ordering -->
<wildcardsLast>false</wildcardsLast> <!-- Optional, default false. Sort wildcard import after specific imports -->
<order>java,javax,org,com,com.diffplug,</order> <!-- or use <file>${project.basedir}/eclipse.importorder</file> -->
<!-- You probably want an empty string at the end - all of the
imports you didn't specify explicitly will go there. -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 DiffPlug
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,17 +31,20 @@ public class ImportOrder implements FormatterStepFactory {
@Parameter
private String order;

@Parameter
private boolean wildcardsLast = false;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
if (file != null ^ order != null) {
if (file != null) {
File importsFile = config.getFileLocator().locateFile(file);
return ImportOrderStep.forJava().createFrom(importsFile);
return ImportOrderStep.forJava().createFrom(wildcardsLast, importsFile);
} else {
return ImportOrderStep.forJava().createFrom(order.split(",", -1));
return ImportOrderStep.forJava().createFrom(wildcardsLast, order.split(",", -1));
}
} else if (file == null && order == null) {
return ImportOrderStep.forJava().createFrom();
return ImportOrderStep.forJava().createFrom(wildcardsLast);
} else {
throw new IllegalArgumentException("Must specify exactly one of 'file' or 'order'.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ void standard() throws Exception {
runTest("java/importsorter/JavaCodeSortedImportsDefault.test");
}

@Test
void wildcardsLast() throws Exception {
writePomWithJavaSteps(
"<importOrder>",
" <wildcardsLast>true</wildcardsLast>",
"</importOrder>");
runTest("java/importsorter/JavaCodeSortedImportsWildcardsLast.test");
}

private void runTest() throws Exception {
runTest("java/importsorter/JavaCodeSortedImports.test");
}
Expand Down

0 comments on commit 3151bc0

Please sign in to comment.