Skip to content

Commit

Permalink
Added wildcardsLast option for Java importOrder in Maven (#956 fo…
Browse files Browse the repository at this point in the history
…llow-up to #954)
  • Loading branch information
nedtwigg authored Oct 4, 2021
2 parents 9f158c5 + c9c2688 commit d596b12
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

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

## [2.16.0] - 2021-10-02

### Added
* Added support for JBDI bind list params in sql formatter ([#955](https://github.com/diffplug/spotless/pull/955))

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 d596b12

Please sign in to comment.