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

Added wildcardsLast option for Java importOrder in Maven #956

Merged
merged 2 commits into from
Oct 4, 2021
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
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