Skip to content

Commit

Permalink
Support ktfmt dropbox style in maven plugin (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Jan 3, 2021
2 parents 2e59dfa + 3f48d2c commit 49af781
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
4 changes: 4 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public static String defaultVersion() {
return DEFAULT_VERSION;
}

public static String defaultStyle() {
return Style.DEFAULT.name();
}

static final class State implements Serializable {
private static final long serialVersionUID = 1L;

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 @@ -109,7 +109,11 @@ public class KtfmtConfig {
}

public void dropboxStyle() {
style = Style.DROPBOX;
style(Style.DROPBOX);
}

public void style(Style style) {
this.style = style;
replaceStep(createStep());
}

Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Added support for eclipse-cdt 4.18.0.
* Added support for eclipse-jdt 4.18.0.
* Added support for eclipse-wtp 4.18.0.
* Added ability to specify dropbox style for ktfmt `<style>DROPBOX</style>` ([#764](https://github.com/diffplug/spotless/pull/764))
### Changed
* Updated default eclipse-jdt from 4.17.0 to 4.18.0.
* Updated default eclipse-wtp from 4.17.0 to 4.18.0.
Expand Down
3 changes: 2 additions & 1 deletion plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T

```xml
<ktfmt>
<version>0.13</version> <!-- optional -->
<version>0.18</version> <!-- optional -->
<style>DEFAULT</style> <!-- optional, other option is DROPBOX -->
</ktfmt>
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 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 All @@ -19,6 +19,7 @@

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.kotlin.KtfmtStep;
import com.diffplug.spotless.kotlin.KtfmtStep.Style;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

Expand All @@ -27,9 +28,13 @@ public class Ktfmt implements FormatterStepFactory {
@Parameter
private String version;

@Parameter
private String style;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
String version = this.version != null ? this.version : KtfmtStep.defaultVersion();
return KtfmtStep.create(version, config.getProvisioner());
String style = this.style != null ? this.style : KtfmtStep.defaultStyle();
return KtfmtStep.create(version, config.getProvisioner(), Style.valueOf(style));
}
}
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 @@ -39,4 +39,16 @@ public void testKtfmt() throws Exception {
assertFile(path1).sameAsResource("kotlin/ktfmt/basic.clean");
assertFile(path2).sameAsResource("kotlin/ktfmt/basic.clean");
}

@Test
public void testKtfmtStyle() throws Exception {
// ktfmt's dependency, google-java-format 1.8 requires a minimum of JRE 11+.
JreVersion.assume11OrGreater();

writePomWithKotlinSteps("<ktfmt><style>DROPBOX</style></ktfmt>");

setFile("src/main/kotlin/main.kt").toResource("kotlin/ktfmt/basic.dirty");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile("src/main/kotlin/main.kt").sameAsResource("kotlin/ktfmt/basic-dropboxstyle.clean");
}
}

0 comments on commit 49af781

Please sign in to comment.