From 9df255ed3c1a56aeec24e6e92190e6382cf45dcd Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Tue, 3 Jan 2023 00:06:41 +0100 Subject: [PATCH 01/13] Add editor config file option to maven and gradle --- .../compat/KtLintCompat0Dot31Dot0Adapter.java | 4 ++-- .../compat/KtLintCompat0Dot32Dot0Adapter.java | 4 ++-- .../compat/KtLintCompat0Dot34Dot2Adapter.java | 6 +++--- .../compat/KtLintCompat0Dot45Dot2Adapter.java | 6 +++--- .../compat/KtLintCompat0Dot46Dot0Adapter.java | 6 +++--- .../compat/KtLintCompat0Dot47Dot0Adapter.java | 17 ++++++++++++----- .../compat/KtLintCompat0Dot48Dot0Adapter.java | 15 +++++++++++---- .../glue/ktlint/compat/KtLintCompatAdapter.java | 4 ++-- .../glue/ktlint/KtlintFormatterFunc.java | 6 ++++-- .../diffplug/spotless/kotlin/KtLintStep.java | 15 +++++++++++---- .../gradle/spotless/KotlinExtension.java | 10 ++++++---- .../gradle/spotless/KotlinGradleExtension.java | 14 +++++++++++--- .../diffplug/spotless/maven/kotlin/Ktlint.java | 7 ++++--- 13 files changed, 74 insertions(+), 40 deletions(-) diff --git a/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java b/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java index 56b8da2d20..a10bf9be0e 100644 --- a/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 DiffPlug + * Copyright 2022-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, final String name, final boolean isScript, final boolean useExperimental, - final Map userData, + String editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); diff --git a/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java b/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java index 6f69fcc7ce..3f98abe3b3 100644 --- a/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 DiffPlug + * Copyright 2022-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, final String name, final boolean isScript, final boolean useExperimental, - final Map userData, + String editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); diff --git a/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java b/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java index a3c8c8df3b..0352a8e1f0 100644 --- a/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java +++ b/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 DiffPlug + * Copyright 2022-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, final String name, final boolean isScript, final boolean useExperimental, - final Map userData, + String editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); @@ -61,7 +61,7 @@ public String format(final String text, final String name, final boolean isScrip userData, formatterCallback, isScript, - null, + editorConfigPath, false)); } } diff --git a/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java b/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java index f7eadada3d..15c2238d5d 100644 --- a/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java +++ b/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 DiffPlug + * Copyright 2022-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,7 +51,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, final String name, final boolean isScript, final boolean useExperimental, - final Map userData, + String editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); @@ -76,7 +76,7 @@ public String format(final String text, final String name, final boolean isScrip userData, formatterCallback, isScript, - null, + editorConfigPath, false, editorConfigOverride, false)); diff --git a/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java b/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java index 873b91af80..28eba672a0 100644 --- a/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 DiffPlug + * Copyright 2022-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,7 +51,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, final String name, final boolean isScript, final boolean useExperimental, - final Map userData, + String editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); @@ -76,7 +76,7 @@ public String format(final String text, final String name, final boolean isScrip userData, formatterCallback, isScript, - null, + editorConfigPath, false, editorConfigOverride, false)); diff --git a/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java b/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java index 757ffc922b..3445a688b1 100644 --- a/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 DiffPlug + * Copyright 2022-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,8 @@ import static java.util.Collections.emptySet; +import java.io.File; +import java.nio.file.Path; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; @@ -56,7 +58,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, final String name, final boolean isScript, final boolean useExperimental, - final Map userData, + String editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); @@ -68,14 +70,19 @@ public String format(final String text, final String name, final boolean isScrip EditorConfigOverride editorConfigOverride; if (editorConfigOverrideMap.isEmpty()) { - editorConfigOverride = EditorConfigOverride.Companion.getEmptyEditorConfigOverride(); + editorConfigOverride = new EditorConfigOverride(); } else { editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map( RuleProvider::createNewRuleInstance).collect( Collectors.toList()), editorConfigOverrideMap); } - + Path editorConfigFilePath; + if (editorConfigPath == null) { + editorConfigFilePath = null; + } else { + editorConfigFilePath = new File(editorConfigPath).toPath(); + } return KtLint.INSTANCE.format(new KtLint.ExperimentalParams( name, text, @@ -86,7 +93,7 @@ public String format(final String text, final String name, final boolean isScrip isScript, null, false, - EditorConfigDefaults.Companion.getEmptyEditorConfigDefaults(), + EditorConfigDefaults.Companion.load(editorConfigFilePath), editorConfigOverride, false)); } diff --git a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java index 8235c535a5..3efc8c085d 100644 --- a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java @@ -15,6 +15,8 @@ */ package com.diffplug.spotless.glue.ktlint.compat; +import java.io.File; +import java.nio.file.Path; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -54,7 +56,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, final String name, final boolean isScript, final boolean useExperimental, - final Map userData, + String editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); @@ -66,14 +68,19 @@ public String format(final String text, final String name, final boolean isScrip EditorConfigOverride editorConfigOverride; if (editorConfigOverrideMap.isEmpty()) { - editorConfigOverride = EditorConfigOverride.Companion.getEmptyEditorConfigOverride(); + editorConfigOverride = new EditorConfigOverride(); } else { editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map( RuleProvider::createNewRuleInstance).collect( Collectors.toList()), editorConfigOverrideMap); } - + Path editorConfigFilePath; + if (editorConfigPath == null) { + editorConfigFilePath = null; + } else { + editorConfigFilePath = new File(editorConfigPath).toPath(); + } return KtLint.INSTANCE.format(new KtLint.ExperimentalParams( name, text, @@ -82,7 +89,7 @@ public String format(final String text, final String name, final boolean isScrip formatterCallback, isScript, false, - EditorConfigDefaults.Companion.getEmptyEditorConfigDefaults(), + EditorConfigDefaults.Companion.load(editorConfigFilePath), editorConfigOverride, false)); } diff --git a/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java b/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java index 5097cac135..b3e5d1817a 100644 --- a/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java +++ b/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 DiffPlug + * Copyright 2022-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,6 @@ public interface KtLintCompatAdapter { - String format(String text, String name, boolean isScript, boolean useExperimental, Map userData, + String format(String text, String name, boolean isScript, boolean useExperimental, String editorConfigPath, Map userData, Map editorConfigOverrideMap); } diff --git a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java index fc64cdb23e..5b43bf5e14 100644 --- a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java +++ b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java @@ -37,9 +37,10 @@ public class KtlintFormatterFunc implements FormatterFunc.NeedsFile { @NotNull private final KtLintCompatAdapter adapter; private final boolean useExperimental; + private final String editorConfigPath; private final Map editorConfigOverrideMap; - public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, Map userData, + public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, String editorConfigPath, Map userData, Map editorConfigOverrideMap) { int minorVersion = Integer.parseInt(version.split("\\.")[1]); if (minorVersion >= 48) { @@ -64,6 +65,7 @@ public KtlintFormatterFunc(String version, boolean isScript, boolean useExperime // the OG this.adapter = new KtLintCompat0Dot31Dot0Adapter(); } + this.editorConfigPath = editorConfigPath; this.useExperimental = useExperimental; this.editorConfigOverrideMap = editorConfigOverrideMap; this.userData = userData; @@ -72,6 +74,6 @@ public KtlintFormatterFunc(String version, boolean isScript, boolean useExperime @Override public String applyWithFile(String unix, File file) throws Exception { - return adapter.format(unix, file.getName(), isScript, useExperimental, userData, editorConfigOverrideMap); + return adapter.format(unix, file.getName(), isScript, useExperimental, editorConfigPath, userData, editorConfigOverrideMap); } } diff --git a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java index b444f0f1a7..9291e69fde 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java @@ -64,10 +64,15 @@ public static FormatterStep createForScript(String version, Provisioner provisio private static FormatterStep create(String version, Provisioner provisioner, boolean isScript, boolean useExperimental, Map userData, Map editorConfigOverride) { + return create(version, provisioner, useExperimental, userData, editorConfigOverride); + } + + public static FormatterStep create(String version, Provisioner provisioner, boolean isScript, boolean useExperimental, + String editorConfig, Map userData, Map editorConfigOverride) { Objects.requireNonNull(version, "version"); Objects.requireNonNull(provisioner, "provisioner"); return FormatterStep.createLazy(NAME, - () -> new State(version, provisioner, isScript, useExperimental, userData, editorConfigOverride), + () -> new State(version, provisioner, isScript, useExperimental, editorConfig, userData, editorConfigOverride), State::createFormat); } @@ -86,9 +91,10 @@ static final class State implements Serializable { private final TreeMap userData; private final TreeMap editorConfigOverride; private final String version; + private final String editorConfigPath; State(String version, Provisioner provisioner, boolean isScript, boolean useExperimental, - Map userData, Map editorConfigOverride) throws IOException { + String editorConfigPath, Map userData, Map editorConfigOverride) throws IOException { this.version = version; String coordinate; @@ -104,6 +110,7 @@ static final class State implements Serializable { this.userData = new TreeMap<>(userData); this.editorConfigOverride = new TreeMap<>(editorConfigOverride); this.jarState = JarState.from(coordinate + version, provisioner); + this.editorConfigPath = editorConfigPath; this.isScript = isScript; } @@ -111,8 +118,8 @@ FormatterFunc createFormat() throws Exception { final ClassLoader classLoader = jarState.getClassLoader(); Class formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.ktlint.KtlintFormatterFunc"); Constructor constructor = formatterFunc.getConstructor( - String.class, boolean.class, boolean.class, Map.class, Map.class); - return (FormatterFunc.NeedsFile) constructor.newInstance(version, isScript, useExperimental, userData, editorConfigOverride); + String.class, boolean.class, boolean.class, String.class, Map.class, Map.class); + return (FormatterFunc.NeedsFile) constructor.newInstance(version, isScript, useExperimental, editorConfigPath, userData, editorConfigOverride); } } } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java index 546efab46d..eddeef36d6 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2022 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,7 +60,7 @@ public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) { /** Adds the specified version of ktlint. */ public KotlinFormatExtension ktlint(String version) { Objects.requireNonNull(version); - return new KotlinFormatExtension(version, false, Collections.emptyMap(), Collections.emptyMap()); + return new KotlinFormatExtension(version, false, null, Collections.emptyMap(), Collections.emptyMap()); } public KotlinFormatExtension ktlint() { @@ -71,13 +71,15 @@ public class KotlinFormatExtension { private final String version; private boolean useExperimental; + private String editorConfigPath; private Map userData; private Map editorConfigOverride; - KotlinFormatExtension(String version, boolean useExperimental, Map config, + KotlinFormatExtension(String version, boolean useExperimental, String editorConfigPath, Map config, Map editorConfigOverride) { this.version = version; this.useExperimental = useExperimental; + this.editorConfigPath = editorConfigPath; this.userData = config; this.editorConfigOverride = editorConfigOverride; addStep(createStep()); @@ -106,7 +108,7 @@ public KotlinFormatExtension editorConfigOverride(Map editorConf } private FormatterStep createStep() { - return KtLintStep.create(version, provisioner(), useExperimental, userData, editorConfigOverride); + return KtLintStep.create(version, provisioner(), useExperimental, false, editorConfigPath, userData, editorConfigOverride); } } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java index c1abe62bd1..5120d88754 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2022 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,7 +45,7 @@ public KotlinGradleExtension(SpotlessExtension spotless) { /** Adds the specified version of ktlint. */ public KotlinFormatExtension ktlint(String version) { Objects.requireNonNull(version, "version"); - return new KotlinFormatExtension(version, false, Collections.emptyMap(), Collections.emptyMap()); + return new KotlinFormatExtension(version, false, null, Collections.emptyMap(), Collections.emptyMap()); } public KotlinFormatExtension ktlint() { @@ -56,18 +56,26 @@ public class KotlinFormatExtension { private final String version; private boolean useExperimental; + private String editorConfigPath; private Map userData; private Map editorConfigOverride; - KotlinFormatExtension(String version, boolean useExperimental, Map config, + KotlinFormatExtension(String version, boolean useExperimental, String editorConfigPath, Map config, Map editorConfigOverride) { this.version = version; this.useExperimental = useExperimental; + this.editorConfigPath = editorConfigPath; this.userData = config; this.editorConfigOverride = editorConfigOverride; addStep(createStep()); } + public KotlinFormatExtension setEditorConfigPath(String editorConfigPath) { + this.editorConfigPath = editorConfigPath; + replaceStep(createStep()); + return this; + } + public KotlinFormatExtension setUseExperimental(boolean useExperimental) { this.useExperimental = useExperimental; replaceStep(createStep()); diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java index 45bda05065..ad57c47976 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2022 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,7 +30,8 @@ public class Ktlint implements FormatterStepFactory { @Parameter private String version; - + @Parameter + private String editorConfigPath; @Parameter private Map editorConfigOverride; @@ -42,6 +43,6 @@ public FormatterStep newFormatterStep(FormatterStepConfig config) { editorConfigOverride = new HashMap<>(); } - return KtLintStep.create(ktlintVersion, config.getProvisioner(), false, Collections.emptyMap(), editorConfigOverride); + return KtLintStep.create(ktlintVersion, config.getProvisioner(), false, false, editorConfigPath, Collections.emptyMap(), editorConfigOverride); } } From 773d59c45412a349437334a2429d675236e35396 Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Tue, 3 Jan 2023 00:12:32 +0100 Subject: [PATCH 02/13] Add changes --- CHANGES.md | 2 ++ plugin-gradle/CHANGES.md | 2 ++ plugin-maven/CHANGES.md | 2 ++ 3 files changed, 6 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index cf43b4eb22..440ea59f80 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Add option `editorConfigFile` for `ktLint` [#142](https://github.com/diffplug/spotless/issues/142) ### Changes * Bump the dev version of Gradle from `7.5.1` to `7.6` ([#1409](https://github.com/diffplug/spotless/pull/1409)) * We also removed the no-longer-required dependency `org.codehaus.groovy:groovy-xml` diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index cac4f56567..3c9b51bd5a 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +* Add option `editorConfigFile` for `ktLint` [#142](https://github.com/diffplug/spotless/issues/142) ## [6.12.1] - 2023-01-02 ### Fixed diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 84e06aa847..ae8ac7e4dd 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Add option `editorConfigFile` for `ktLint` [#142](https://github.com/diffplug/spotless/issues/142) ## [2.29.0] - 2023-01-02 ### Added From 57be84fb8283e62ae971f25c5b3467ee5906db8a Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Tue, 3 Jan 2023 21:19:47 +0100 Subject: [PATCH 03/13] Use FileSignature to cache changes --- .../glue/ktlint/KtlintFormatterFunc.java | 26 +++++++------------ .../diffplug/spotless/kotlin/KtLintStep.java | 7 ++--- .../gradle/spotless/KotlinExtension.java | 14 ++++++++-- .../spotless/KotlinGradleExtension.java | 13 +++++++--- .../spotless/maven/kotlin/Ktlint.java | 11 +++++--- 5 files changed, 43 insertions(+), 28 deletions(-) diff --git a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java index 5b43bf5e14..df98b6d691 100644 --- a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java +++ b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java @@ -15,20 +15,14 @@ */ package com.diffplug.spotless.glue.ktlint; -import java.io.File; -import java.util.Map; +import com.diffplug.spotless.FileSignature; +import com.diffplug.spotless.FormatterFunc; +import com.diffplug.spotless.glue.ktlint.compat.*; import org.jetbrains.annotations.NotNull; -import com.diffplug.spotless.FormatterFunc; -import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot31Dot0Adapter; -import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot32Dot0Adapter; -import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot34Dot2Adapter; -import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot45Dot2Adapter; -import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot46Dot0Adapter; -import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot47Dot0Adapter; -import com.diffplug.spotless.glue.ktlint.compat.KtLintCompat0Dot48Dot0Adapter; -import com.diffplug.spotless.glue.ktlint.compat.KtLintCompatAdapter; +import java.io.File; +import java.util.Map; public class KtlintFormatterFunc implements FormatterFunc.NeedsFile { @@ -37,11 +31,11 @@ public class KtlintFormatterFunc implements FormatterFunc.NeedsFile { @NotNull private final KtLintCompatAdapter adapter; private final boolean useExperimental; - private final String editorConfigPath; + private final FileSignature editorConfigPath; private final Map editorConfigOverrideMap; - public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, String editorConfigPath, Map userData, - Map editorConfigOverrideMap) { + public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, FileSignature editorConfigPath, Map userData, + Map editorConfigOverrideMap) { int minorVersion = Integer.parseInt(version.split("\\.")[1]); if (minorVersion >= 48) { // ExperimentalParams lost two constructor arguments, EditorConfigProperty moved to its own class @@ -73,7 +67,7 @@ public KtlintFormatterFunc(String version, boolean isScript, boolean useExperime } @Override - public String applyWithFile(String unix, File file) throws Exception { - return adapter.format(unix, file.getName(), isScript, useExperimental, editorConfigPath, userData, editorConfigOverrideMap); + public String applyWithFile(String unix, File file) { + return adapter.format(unix, file.getName(), isScript, useExperimental, editorConfigPath.getOnlyFile().getAbsolutePath(), userData, editorConfigOverrideMap); } } diff --git a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java index 9291e69fde..6d4d8b07ed 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java @@ -23,6 +23,7 @@ import java.util.Objects; import java.util.TreeMap; +import com.diffplug.spotless.FileSignature; import com.diffplug.spotless.FormatterFunc; import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.JarState; @@ -68,7 +69,7 @@ private static FormatterStep create(String version, Provisioner provisioner, boo } public static FormatterStep create(String version, Provisioner provisioner, boolean isScript, boolean useExperimental, - String editorConfig, Map userData, Map editorConfigOverride) { + FileSignature editorConfig, Map userData, Map editorConfigOverride) { Objects.requireNonNull(version, "version"); Objects.requireNonNull(provisioner, "provisioner"); return FormatterStep.createLazy(NAME, @@ -91,10 +92,10 @@ static final class State implements Serializable { private final TreeMap userData; private final TreeMap editorConfigOverride; private final String version; - private final String editorConfigPath; + private final FileSignature editorConfigPath; State(String version, Provisioner provisioner, boolean isScript, boolean useExperimental, - String editorConfigPath, Map userData, Map editorConfigOverride) throws IOException { + FileSignature editorConfigPath, Map userData, Map editorConfigOverride) throws IOException { this.version = version; String coordinate; diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java index eddeef36d6..8af03aaefd 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java @@ -71,11 +71,11 @@ public class KotlinFormatExtension { private final String version; private boolean useExperimental; - private String editorConfigPath; + private FileSignature editorConfigPath; private Map userData; private Map editorConfigOverride; - KotlinFormatExtension(String version, boolean useExperimental, String editorConfigPath, Map config, + KotlinFormatExtension(String version, boolean useExperimental, FileSignature editorConfigPath, Map config, Map editorConfigOverride) { this.version = version; this.useExperimental = useExperimental; @@ -91,6 +91,16 @@ public KotlinFormatExtension setUseExperimental(boolean useExperimental) { return this; } + public KotlinFormatExtension setEditorConfigPath(Object editorConfigFile) throws IOException { + if (editorConfigFile == null) { + this.editorConfigPath = null; + } else { + this.editorConfigPath = FileSignature.signAsList(getProject().file(editorConfigFile)); + } + replaceStep(createStep()); + return this; + } + public KotlinFormatExtension userData(Map userData) { // Copy the map to a sorted map because up-to-date checking is based on binary-equals of the serialized // representation. diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java index 5120d88754..ca4ecc9bde 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java @@ -56,11 +56,11 @@ public class KotlinFormatExtension { private final String version; private boolean useExperimental; - private String editorConfigPath; + private FileSignature editorConfigPath; private Map userData; private Map editorConfigOverride; - KotlinFormatExtension(String version, boolean useExperimental, String editorConfigPath, Map config, + KotlinFormatExtension(String version, boolean useExperimental, FileSignature editorConfigPath, Map config, Map editorConfigOverride) { this.version = version; this.useExperimental = useExperimental; @@ -70,8 +70,13 @@ public class KotlinFormatExtension { addStep(createStep()); } - public KotlinFormatExtension setEditorConfigPath(String editorConfigPath) { - this.editorConfigPath = editorConfigPath; + public KotlinFormatExtension setEditorConfigPath(Object editorConfigPath) throws IOException { + + if (editorConfigPath == null) { + this.editorConfigPath = null; + } else { + this.editorConfigPath = FileSignature.signAsList(getProject().file(editorConfigPath)); + } replaceStep(createStep()); return this; } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java index ad57c47976..2825980733 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java @@ -21,7 +21,9 @@ import org.apache.maven.plugins.annotations.Parameter; +import com.diffplug.spotless.FileSignature; import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.ThrowingEx; import com.diffplug.spotless.kotlin.KtLintStep; import com.diffplug.spotless.maven.FormatterStepConfig; import com.diffplug.spotless.maven.FormatterStepFactory; @@ -36,13 +38,16 @@ public class Ktlint implements FormatterStepFactory { private Map editorConfigOverride; @Override - public FormatterStep newFormatterStep(FormatterStepConfig config) { + public FormatterStep newFormatterStep(final FormatterStepConfig stepConfig) { String ktlintVersion = version != null ? version : KtLintStep.defaultVersion(); - + FileSignature configPath = null; + if (editorConfigPath != null) { + configPath = ThrowingEx.get(() -> FileSignature.signAsList(stepConfig.getFileLocator().locateFile(editorConfigPath))); + } if (editorConfigOverride == null) { editorConfigOverride = new HashMap<>(); } - return KtLintStep.create(ktlintVersion, config.getProvisioner(), false, false, editorConfigPath, Collections.emptyMap(), editorConfigOverride); + return KtLintStep.create(ktlintVersion, stepConfig.getProvisioner(), false, false, configPath, Collections.emptyMap(), editorConfigOverride); } } From bd93e17a69613f04d4eab32629862c7229a2176f Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Tue, 3 Jan 2023 22:03:22 +0100 Subject: [PATCH 04/13] Fix NPE and contract to pass the variable --- .../glue/ktlint/KtlintFormatterFunc.java | 19 ++++++++++++------- .../diffplug/spotless/kotlin/KtLintStep.java | 4 ++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java index df98b6d691..cfd6ab859c 100644 --- a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java +++ b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java @@ -15,14 +15,14 @@ */ package com.diffplug.spotless.glue.ktlint; -import com.diffplug.spotless.FileSignature; -import com.diffplug.spotless.FormatterFunc; -import com.diffplug.spotless.glue.ktlint.compat.*; +import java.io.File; +import java.util.Map; import org.jetbrains.annotations.NotNull; -import java.io.File; -import java.util.Map; +import com.diffplug.spotless.FileSignature; +import com.diffplug.spotless.FormatterFunc; +import com.diffplug.spotless.glue.ktlint.compat.*; public class KtlintFormatterFunc implements FormatterFunc.NeedsFile { @@ -35,7 +35,7 @@ public class KtlintFormatterFunc implements FormatterFunc.NeedsFile { private final Map editorConfigOverrideMap; public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, FileSignature editorConfigPath, Map userData, - Map editorConfigOverrideMap) { + Map editorConfigOverrideMap) { int minorVersion = Integer.parseInt(version.split("\\.")[1]); if (minorVersion >= 48) { // ExperimentalParams lost two constructor arguments, EditorConfigProperty moved to its own class @@ -68,6 +68,11 @@ public KtlintFormatterFunc(String version, boolean isScript, boolean useExperime @Override public String applyWithFile(String unix, File file) { - return adapter.format(unix, file.getName(), isScript, useExperimental, editorConfigPath.getOnlyFile().getAbsolutePath(), userData, editorConfigOverrideMap); + + String absoluteEditorConfigPath = null; + if (editorConfigPath != null) { + absoluteEditorConfigPath = editorConfigPath.getOnlyFile().getAbsolutePath(); + } + return adapter.format(unix, file.getName(), isScript, useExperimental, absoluteEditorConfigPath, userData, editorConfigOverrideMap); } } diff --git a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java index 6d4d8b07ed..4d29a01394 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java @@ -59,7 +59,7 @@ public static FormatterStep createForScript(String version, Provisioner provisio } public static FormatterStep createForScript(String version, Provisioner provisioner, boolean useExperimental, - Map userData, Map editorConfigOverride) { + FileSignature editorConfigPath, Map userData, Map editorConfigOverride) { return create(version, provisioner, true, useExperimental, userData, editorConfigOverride); } @@ -119,7 +119,7 @@ FormatterFunc createFormat() throws Exception { final ClassLoader classLoader = jarState.getClassLoader(); Class formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.ktlint.KtlintFormatterFunc"); Constructor constructor = formatterFunc.getConstructor( - String.class, boolean.class, boolean.class, String.class, Map.class, Map.class); + String.class, boolean.class, boolean.class, FileSignature.class, Map.class, Map.class); return (FormatterFunc.NeedsFile) constructor.newInstance(version, isScript, useExperimental, editorConfigPath, userData, editorConfigOverride); } } From b18da45e4a88f5900c80bd6dd6f2422b194127f2 Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Tue, 3 Jan 2023 22:04:00 +0100 Subject: [PATCH 05/13] EditorConfig: Set sane default for Gradle --- .../gradle/spotless/KotlinGradleExtension.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java index ca4ecc9bde..a06c640060 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java @@ -15,6 +15,7 @@ */ package com.diffplug.gradle.spotless; +import java.io.File; import java.io.IOException; import java.util.Collections; import java.util.Map; @@ -43,12 +44,17 @@ public KotlinGradleExtension(SpotlessExtension spotless) { } /** Adds the specified version of ktlint. */ - public KotlinFormatExtension ktlint(String version) { + public KotlinFormatExtension ktlint(String version) throws IOException { Objects.requireNonNull(version, "version"); - return new KotlinFormatExtension(version, false, null, Collections.emptyMap(), Collections.emptyMap()); + FileSignature editorConfigPath = null; + File defaultEditorConfig = getProject().getRootProject().file(".editorConfig"); + if (defaultEditorConfig.exists() && defaultEditorConfig.isFile()) { + editorConfigPath = FileSignature.signAsList(defaultEditorConfig); + } + return new KotlinFormatExtension(version, false, editorConfigPath, Collections.emptyMap(), Collections.emptyMap()); } - public KotlinFormatExtension ktlint() { + public KotlinFormatExtension ktlint() throws IOException { return ktlint(KtLintStep.defaultVersion()); } @@ -104,7 +110,7 @@ public KotlinFormatExtension editorConfigOverride(Map editorConf } private FormatterStep createStep() { - return KtLintStep.createForScript(version, provisioner(), useExperimental, userData, editorConfigOverride); + return KtLintStep.createForScript(version, provisioner(), useExperimental, editorConfigPath, userData, editorConfigOverride); } } From 79e35c1dfe3c18aca8e216749d825752cd539d3d Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Tue, 3 Jan 2023 22:07:05 +0100 Subject: [PATCH 06/13] EditorConfig: add documentation --- plugin-gradle/README.md | 10 +++++++++- plugin-maven/README.md | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 755d21d056..84c4e626a0 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -354,7 +354,14 @@ spotless { ### ktlint -[homepage](https://github.com/pinterest/ktlint). [changelog](https://github.com/pinterest/ktlint/releases). Spotless does not ([yet](https://github.com/diffplug/spotless/issues/142)) respect the `.editorconfig` settings ([ktlint docs](https://github.com/pinterest/ktlint#editorconfig)), but you can provide them manually as `editorConfigOverride`. +[homepage](https://github.com/pinterest/ktlint). [changelog](https://github.com/pinterest/ktlint/releases). + +Spotless respects the `.editorconfig` settings by providing `editorConfigPath` option. +([ktlint docs](https://github.com/pinterest/ktlint#editorconfig)). +Default value is the `.editorconfig` file located in the top project. +Passing `null` will clear the option. + +Additionally, `editorConfigOverride` options will override what's supplied in `.editorconfig` file. ```kotlin spotless { @@ -363,6 +370,7 @@ spotless { ktlint("0.45.2") .setUseExperimental(true) .userData(mapOf("android" to "true")) + .editorConfigPath("$projectDir/config/.editorconfig") // sample unusual placement .editorConfigOverride(mapOf("indent_size" to 2)) } } diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 80be3dbc37..1ae8e3a195 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -354,7 +354,13 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T ### ktlint -[homepage](https://github.com/pinterest/ktlint). [changelog](https://github.com/pinterest/ktlint/releases). [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java). Spotless does not ([yet](https://github.com/diffplug/spotless/issues/142)) respect the `.editorconfig` settings. +[homepage](https://github.com/pinterest/ktlint). [changelog](https://github.com/pinterest/ktlint/releases). +[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktlint.java). + +Spotless respects the `.editorconfig` settings by providing `editorConfigPath` option. +([ktlint docs](https://github.com/pinterest/ktlint#editorconfig)). + +Additionally, `editorConfigOverride` options will override what's supplied in `.editorconfig` file. ```xml From 667eef943c0f89ca2415e913cf6d8206ff8c5585 Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Thu, 5 Jan 2023 13:14:32 +0100 Subject: [PATCH 07/13] Pass editor config and filenames correctly --- .../glue/ktlint/KtlintFormatterFunc.java | 2 +- .../diffplug/spotless/kotlin/KtLintStep.java | 57 +++++++++++++++---- .../gradle/spotless/KotlinExtension.java | 16 ++++-- .../spotless/KotlinGradleExtension.java | 12 +++- 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java index cfd6ab859c..034a782be6 100644 --- a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java +++ b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java @@ -73,6 +73,6 @@ public String applyWithFile(String unix, File file) { if (editorConfigPath != null) { absoluteEditorConfigPath = editorConfigPath.getOnlyFile().getAbsolutePath(); } - return adapter.format(unix, file.getName(), isScript, useExperimental, absoluteEditorConfigPath, userData, editorConfigOverrideMap); + return adapter.format(unix, file.getAbsolutePath(), isScript, useExperimental, absoluteEditorConfigPath, userData, editorConfigOverrideMap); } } diff --git a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java index 4d29a01394..20e7d885e1 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java @@ -23,6 +23,8 @@ import java.util.Objects; import java.util.TreeMap; +import javax.annotation.Nullable; + import com.diffplug.spotless.FileSignature; import com.diffplug.spotless.FormatterFunc; import com.diffplug.spotless.FormatterStep; @@ -51,25 +53,50 @@ public static FormatterStep create(String version, Provisioner provisioner) { public static FormatterStep create(String version, Provisioner provisioner, boolean useExperimental, Map userData, Map editorConfigOverride) { - return create(version, provisioner, false, useExperimental, userData, editorConfigOverride); + return create(version, provisioner, false, useExperimental, null, userData, editorConfigOverride); } public static FormatterStep createForScript(String version, Provisioner provisioner) { - return create(version, provisioner, true, false, Collections.emptyMap(), Collections.emptyMap()); + return create(version, provisioner, true, false, null, Collections.emptyMap(), Collections.emptyMap()); } - public static FormatterStep createForScript(String version, Provisioner provisioner, boolean useExperimental, - FileSignature editorConfigPath, Map userData, Map editorConfigOverride) { - return create(version, provisioner, true, useExperimental, userData, editorConfigOverride); + public static FormatterStep createForScript(String version, + Provisioner provisioner, + boolean useExperimental, + @Nullable FileSignature editorConfigPath, + Map userData, + Map editorConfigOverride) { + return create(version, + provisioner, + true, + useExperimental, + editorConfigPath, + userData, + editorConfigOverride); } - private static FormatterStep create(String version, Provisioner provisioner, boolean isScript, boolean useExperimental, - Map userData, Map editorConfigOverride) { - return create(version, provisioner, useExperimental, userData, editorConfigOverride); + private static FormatterStep create(String version, + Provisioner provisioner, + boolean isScript, + boolean useExperimental, + Map userData, + Map editorConfigOverride) { + return create(version, + provisioner, + useExperimental, + isScript, + null, + userData, + editorConfigOverride); } - public static FormatterStep create(String version, Provisioner provisioner, boolean isScript, boolean useExperimental, - FileSignature editorConfig, Map userData, Map editorConfigOverride) { + public static FormatterStep create(String version, + Provisioner provisioner, + boolean isScript, + boolean useExperimental, + @Nullable FileSignature editorConfig, + Map userData, + Map editorConfigOverride) { Objects.requireNonNull(version, "version"); Objects.requireNonNull(provisioner, "provisioner"); return FormatterStep.createLazy(NAME, @@ -92,10 +119,16 @@ static final class State implements Serializable { private final TreeMap userData; private final TreeMap editorConfigOverride; private final String version; + @Nullable private final FileSignature editorConfigPath; - State(String version, Provisioner provisioner, boolean isScript, boolean useExperimental, - FileSignature editorConfigPath, Map userData, Map editorConfigOverride) throws IOException { + State(String version, + Provisioner provisioner, + boolean isScript, + boolean useExperimental, + @Nullable FileSignature editorConfigPath, + Map userData, + Map editorConfigOverride) throws IOException { this.version = version; String coordinate; diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java index 8af03aaefd..9ebc290219 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java @@ -17,12 +17,14 @@ import static com.diffplug.spotless.kotlin.KotlinConstants.LICENSE_HEADER_DELIMITER; +import java.io.File; import java.io.IOException; import java.util.Collections; import java.util.Map; import java.util.Objects; import java.util.function.Consumer; +import javax.annotation.Nullable; import javax.inject.Inject; import org.gradle.api.GradleException; @@ -58,12 +60,17 @@ public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) { } /** Adds the specified version of ktlint. */ - public KotlinFormatExtension ktlint(String version) { + public KotlinFormatExtension ktlint(String version) throws IOException { Objects.requireNonNull(version); - return new KotlinFormatExtension(version, false, null, Collections.emptyMap(), Collections.emptyMap()); + FileSignature editorConfigPath = null; + File defaultEditorConfig = getProject().getRootProject().file(".editorconfig"); + if (defaultEditorConfig.exists() && defaultEditorConfig.isFile()) { + editorConfigPath = FileSignature.signAsList(defaultEditorConfig); + } + return new KotlinFormatExtension(version, false, editorConfigPath, Collections.emptyMap(), Collections.emptyMap()); } - public KotlinFormatExtension ktlint() { + public KotlinFormatExtension ktlint() throws IOException { return ktlint(KtLintStep.defaultVersion()); } @@ -71,11 +78,12 @@ public class KotlinFormatExtension { private final String version; private boolean useExperimental; + @Nullable private FileSignature editorConfigPath; private Map userData; private Map editorConfigOverride; - KotlinFormatExtension(String version, boolean useExperimental, FileSignature editorConfigPath, Map config, + KotlinFormatExtension(String version, boolean useExperimental, @Nullable FileSignature editorConfigPath, Map config, Map editorConfigOverride) { this.version = version; this.useExperimental = useExperimental; diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java index a06c640060..ec645a8972 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java @@ -22,6 +22,7 @@ import java.util.Objects; import java.util.function.Consumer; +import javax.annotation.Nullable; import javax.inject.Inject; import com.diffplug.common.collect.ImmutableSortedMap; @@ -47,7 +48,7 @@ public KotlinGradleExtension(SpotlessExtension spotless) { public KotlinFormatExtension ktlint(String version) throws IOException { Objects.requireNonNull(version, "version"); FileSignature editorConfigPath = null; - File defaultEditorConfig = getProject().getRootProject().file(".editorConfig"); + File defaultEditorConfig = getProject().getRootProject().file(".editorconfig"); if (defaultEditorConfig.exists() && defaultEditorConfig.isFile()) { editorConfigPath = FileSignature.signAsList(defaultEditorConfig); } @@ -62,6 +63,7 @@ public class KotlinFormatExtension { private final String version; private boolean useExperimental; + @Nullable private FileSignature editorConfigPath; private Map userData; private Map editorConfigOverride; @@ -110,7 +112,13 @@ public KotlinFormatExtension editorConfigOverride(Map editorConf } private FormatterStep createStep() { - return KtLintStep.createForScript(version, provisioner(), useExperimental, editorConfigPath, userData, editorConfigOverride); + return KtLintStep.createForScript( + version, + provisioner(), + useExperimental, + editorConfigPath, + userData, + editorConfigOverride); } } From 03eae65e91b89d8ccc3ff71c6a5b2a4b4d243748 Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Thu, 5 Jan 2023 13:17:23 +0100 Subject: [PATCH 08/13] EditorConfig: Use Path for files for KtLint --- .../ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java | 9 +++++---- .../ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java | 9 +++++---- .../ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java | 11 ++++++----- .../ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java | 11 ++++++----- .../ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java | 11 ++++++----- .../ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java | 10 +++++----- .../ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java | 10 +++++----- .../glue/ktlint/compat/KtLintCompatAdapter.java | 5 +++-- .../spotless/glue/ktlint/KtlintFormatterFunc.java | 2 +- 9 files changed, 42 insertions(+), 36 deletions(-) diff --git a/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java b/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java index a10bf9be0e..42d6e0c5bd 100644 --- a/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java @@ -15,6 +15,7 @@ */ package com.diffplug.spotless.glue.ktlint.compat; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -41,10 +42,10 @@ public Unit invoke(LintError lint, Boolean corrected) { } @Override - public String format(final String text, final String name, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + public String format(final String text, Path path, final boolean isScript, + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); final List rulesets = new ArrayList<>(); diff --git a/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java b/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java index 3f98abe3b3..11d3c0d8e5 100644 --- a/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java @@ -15,6 +15,7 @@ */ package com.diffplug.spotless.glue.ktlint.compat; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -41,10 +42,10 @@ public Unit invoke(LintError lint, Boolean corrected) { } @Override - public String format(final String text, final String name, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + public String format(final String text, Path path, final boolean isScript, + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); final List rulesets = new ArrayList<>(); diff --git a/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java b/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java index 0352a8e1f0..63d59ff74e 100644 --- a/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java +++ b/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java @@ -15,6 +15,7 @@ */ package com.diffplug.spotless.glue.ktlint.compat; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -41,10 +42,10 @@ public Unit invoke(LintError lint, Boolean corrected) { } @Override - public String format(final String text, final String name, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + public String format(final String text, Path path, final boolean isScript, + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); final List rulesets = new ArrayList<>(); @@ -55,7 +56,7 @@ public String format(final String text, final String name, final boolean isScrip } return KtLint.INSTANCE.format(new KtLint.Params( - name, + path.toFile().getAbsolutePath(), text, rulesets, userData, diff --git a/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java b/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java index 15c2238d5d..b7b2435d93 100644 --- a/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java +++ b/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java @@ -15,6 +15,7 @@ */ package com.diffplug.spotless.glue.ktlint.compat; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -49,10 +50,10 @@ public Unit invoke(LintError lint, Boolean corrected) { } @Override - public String format(final String text, final String name, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + public String format(final String text, Path path, final boolean isScript, + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); final List rulesets = new ArrayList<>(); @@ -70,7 +71,7 @@ public String format(final String text, final String name, final boolean isScrip } return KtLint.INSTANCE.format(new KtLint.ExperimentalParams( - name, + path.toFile().getAbsolutePath(), text, rulesets, userData, diff --git a/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java b/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java index 28eba672a0..198ce7ef9c 100644 --- a/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java @@ -15,6 +15,7 @@ */ package com.diffplug.spotless.glue.ktlint.compat; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -49,10 +50,10 @@ public Unit invoke(LintError lint, Boolean corrected) { } @Override - public String format(final String text, final String name, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + public String format(final String text, Path path, final boolean isScript, + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); final List rulesets = new ArrayList<>(); @@ -70,7 +71,7 @@ public String format(final String text, final String name, final boolean isScrip } return KtLint.INSTANCE.format(new KtLint.ExperimentalParams( - name, + path.toFile().getAbsolutePath(), text, rulesets, userData, diff --git a/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java b/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java index 3445a688b1..0d4f65936a 100644 --- a/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java @@ -56,10 +56,10 @@ public Unit invoke(LintError lint, Boolean corrected) { } @Override - public String format(final String text, final String name, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + public String format(final String text, Path path, final boolean isScript, + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); Set allRuleProviders = new LinkedHashSet<>( @@ -84,7 +84,7 @@ public String format(final String text, final String name, final boolean isScrip editorConfigFilePath = new File(editorConfigPath).toPath(); } return KtLint.INSTANCE.format(new KtLint.ExperimentalParams( - name, + path.toFile().getAbsolutePath(), text, emptySet(), allRuleProviders, diff --git a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java index 3efc8c085d..2cc580ea6a 100644 --- a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java @@ -54,10 +54,10 @@ public Unit invoke(LintError lint, Boolean corrected) { } @Override - public String format(final String text, final String name, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + public String format(final String text, Path path, final boolean isScript, + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); Set allRuleProviders = new LinkedHashSet<>( @@ -82,7 +82,7 @@ public String format(final String text, final String name, final boolean isScrip editorConfigFilePath = new File(editorConfigPath).toPath(); } return KtLint.INSTANCE.format(new KtLint.ExperimentalParams( - name, + path.toFile().getAbsolutePath(), text, allRuleProviders, userData, diff --git a/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java b/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java index b3e5d1817a..1693025688 100644 --- a/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java +++ b/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java @@ -15,10 +15,11 @@ */ package com.diffplug.spotless.glue.ktlint.compat; +import java.nio.file.Path; import java.util.Map; public interface KtLintCompatAdapter { - String format(String text, String name, boolean isScript, boolean useExperimental, String editorConfigPath, Map userData, - Map editorConfigOverrideMap); + String format(String text, Path path, boolean isScript, boolean useExperimental, String editorConfigPath, Map userData, + Map editorConfigOverrideMap); } diff --git a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java index 034a782be6..1fe296143c 100644 --- a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java +++ b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java @@ -73,6 +73,6 @@ public String applyWithFile(String unix, File file) { if (editorConfigPath != null) { absoluteEditorConfigPath = editorConfigPath.getOnlyFile().getAbsolutePath(); } - return adapter.format(unix, file.getAbsolutePath(), isScript, useExperimental, absoluteEditorConfigPath, userData, editorConfigOverrideMap); + return adapter.format(unix, file.toPath(), isScript, useExperimental, absoluteEditorConfigPath, userData, editorConfigOverrideMap); } } From 5f4417ede83573d9d91e48b2ad0a136617d65f1b Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Thu, 5 Jan 2023 13:58:00 +0100 Subject: [PATCH 09/13] EditorConfig: Using KtLintRuleEngine for KtLint 0.48.x --- .../compat/KtLintCompat0Dot31Dot0Adapter.java | 6 +++--- .../compat/KtLintCompat0Dot32Dot0Adapter.java | 6 +++--- .../compat/KtLintCompat0Dot34Dot2Adapter.java | 6 +++--- .../compat/KtLintCompat0Dot45Dot2Adapter.java | 6 +++--- .../compat/KtLintCompat0Dot46Dot0Adapter.java | 6 +++--- .../compat/KtLintCompat0Dot47Dot0Adapter.java | 6 +++--- .../compat/KtLintCompat0Dot48Dot0Adapter.java | 19 +++++++------------ .../ktlint/compat/KtLintCompatAdapter.java | 2 +- 8 files changed, 26 insertions(+), 31 deletions(-) diff --git a/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java b/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java index 42d6e0c5bd..b9147345e2 100644 --- a/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java @@ -43,9 +43,9 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); final List rulesets = new ArrayList<>(); diff --git a/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java b/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java index 11d3c0d8e5..de77d027f6 100644 --- a/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java @@ -43,9 +43,9 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); final List rulesets = new ArrayList<>(); diff --git a/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java b/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java index 63d59ff74e..ae86e55e40 100644 --- a/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java +++ b/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java @@ -43,9 +43,9 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); final List rulesets = new ArrayList<>(); diff --git a/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java b/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java index b7b2435d93..06fbf8cdbd 100644 --- a/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java +++ b/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java @@ -51,9 +51,9 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); final List rulesets = new ArrayList<>(); diff --git a/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java b/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java index 198ce7ef9c..8c41157946 100644 --- a/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java @@ -51,9 +51,9 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); final List rulesets = new ArrayList<>(); diff --git a/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java b/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java index 0d4f65936a..441f4636fa 100644 --- a/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java @@ -57,9 +57,9 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); Set allRuleProviders = new LinkedHashSet<>( diff --git a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java index 2cc580ea6a..51a1b283c6 100644 --- a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java @@ -25,7 +25,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import com.pinterest.ktlint.core.KtLint; +import com.pinterest.ktlint.core.KtLintRuleEngine; import com.pinterest.ktlint.core.LintError; import com.pinterest.ktlint.core.Rule; import com.pinterest.ktlint.core.RuleProvider; @@ -55,9 +55,9 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, - final boolean useExperimental, - String editorConfigPath, final Map userData, - final Map editorConfigOverrideMap) { + final boolean useExperimental, + String editorConfigPath, final Map userData, + final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); Set allRuleProviders = new LinkedHashSet<>( @@ -81,17 +81,12 @@ public String format(final String text, Path path, final boolean isScript, } else { editorConfigFilePath = new File(editorConfigPath).toPath(); } - return KtLint.INSTANCE.format(new KtLint.ExperimentalParams( - path.toFile().getAbsolutePath(), - text, + return new KtLintRuleEngine( allRuleProviders, - userData, - formatterCallback, - isScript, - false, EditorConfigDefaults.Companion.load(editorConfigFilePath), editorConfigOverride, - false)); + false) + .format(path, formatterCallback); } /** diff --git a/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java b/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java index 1693025688..9d5cc8b06e 100644 --- a/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java +++ b/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java @@ -21,5 +21,5 @@ public interface KtLintCompatAdapter { String format(String text, Path path, boolean isScript, boolean useExperimental, String editorConfigPath, Map userData, - Map editorConfigOverrideMap); + Map editorConfigOverrideMap); } From 5cd53a9bb9fa709af695ef61272bb3f9977d5571 Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Thu, 5 Jan 2023 14:02:05 +0100 Subject: [PATCH 10/13] EditorConfig: pass Path to an implementation --- .../ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java | 2 +- .../ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java | 2 +- .../ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java | 4 ++-- .../ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java | 4 ++-- .../ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java | 4 ++-- .../ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java | 12 +++--------- .../ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java | 12 +++--------- .../glue/ktlint/compat/KtLintCompatAdapter.java | 2 +- .../spotless/glue/ktlint/KtlintFormatterFunc.java | 5 +++-- 9 files changed, 18 insertions(+), 29 deletions(-) diff --git a/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java b/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java index b9147345e2..7a956de86a 100644 --- a/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot31Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot31Dot0Adapter.java @@ -44,7 +44,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, final boolean useExperimental, - String editorConfigPath, final Map userData, + Path editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); diff --git a/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java b/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java index de77d027f6..94c41c3716 100644 --- a/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot32Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot32Dot0Adapter.java @@ -44,7 +44,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, final boolean useExperimental, - String editorConfigPath, final Map userData, + Path editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); diff --git a/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java b/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java index ae86e55e40..8926a8e21f 100644 --- a/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java +++ b/lib/src/compatKtLint0Dot34Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot34Dot2Adapter.java @@ -44,7 +44,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, final boolean useExperimental, - String editorConfigPath, final Map userData, + Path editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); @@ -62,7 +62,7 @@ public String format(final String text, Path path, final boolean isScript, userData, formatterCallback, isScript, - editorConfigPath, + editorConfigPath.toFile().getAbsolutePath(), false)); } } diff --git a/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java b/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java index 06fbf8cdbd..c90cf59d2b 100644 --- a/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java +++ b/lib/src/compatKtLint0Dot45Dot2/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot45Dot2Adapter.java @@ -52,7 +52,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, final boolean useExperimental, - String editorConfigPath, final Map userData, + Path editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); @@ -77,7 +77,7 @@ public String format(final String text, Path path, final boolean isScript, userData, formatterCallback, isScript, - editorConfigPath, + editorConfigPath.toFile().getAbsolutePath(), false, editorConfigOverride, false)); diff --git a/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java b/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java index 8c41157946..54b34296a4 100644 --- a/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot46Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot46Dot0Adapter.java @@ -52,7 +52,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, final boolean useExperimental, - String editorConfigPath, final Map userData, + Path editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); @@ -77,7 +77,7 @@ public String format(final String text, Path path, final boolean isScript, userData, formatterCallback, isScript, - editorConfigPath, + editorConfigPath.toFile().getAbsolutePath(), false, editorConfigOverride, false)); diff --git a/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java b/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java index 441f4636fa..20b87eeb17 100644 --- a/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java @@ -17,7 +17,6 @@ import static java.util.Collections.emptySet; -import java.io.File; import java.nio.file.Path; import java.util.ArrayList; import java.util.LinkedHashSet; @@ -58,7 +57,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, final boolean useExperimental, - String editorConfigPath, final Map userData, + Path editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); @@ -77,12 +76,7 @@ public String format(final String text, Path path, final boolean isScript, Collectors.toList()), editorConfigOverrideMap); } - Path editorConfigFilePath; - if (editorConfigPath == null) { - editorConfigFilePath = null; - } else { - editorConfigFilePath = new File(editorConfigPath).toPath(); - } + return KtLint.INSTANCE.format(new KtLint.ExperimentalParams( path.toFile().getAbsolutePath(), text, @@ -93,7 +87,7 @@ public String format(final String text, Path path, final boolean isScript, isScript, null, false, - EditorConfigDefaults.Companion.load(editorConfigFilePath), + EditorConfigDefaults.Companion.load(editorConfigPath), editorConfigOverride, false)); } diff --git a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java index 51a1b283c6..be7f161251 100644 --- a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java @@ -15,7 +15,6 @@ */ package com.diffplug.spotless.glue.ktlint.compat; -import java.io.File; import java.nio.file.Path; import java.util.LinkedHashSet; import java.util.List; @@ -56,7 +55,7 @@ public Unit invoke(LintError lint, Boolean corrected) { @Override public String format(final String text, Path path, final boolean isScript, final boolean useExperimental, - String editorConfigPath, final Map userData, + Path editorConfigPath, final Map userData, final Map editorConfigOverrideMap) { final FormatterCallback formatterCallback = new FormatterCallback(); @@ -75,15 +74,10 @@ public String format(final String text, Path path, final boolean isScript, Collectors.toList()), editorConfigOverrideMap); } - Path editorConfigFilePath; - if (editorConfigPath == null) { - editorConfigFilePath = null; - } else { - editorConfigFilePath = new File(editorConfigPath).toPath(); - } + return new KtLintRuleEngine( allRuleProviders, - EditorConfigDefaults.Companion.load(editorConfigFilePath), + EditorConfigDefaults.Companion.load(editorConfigPath), editorConfigOverride, false) .format(path, formatterCallback); diff --git a/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java b/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java index 9d5cc8b06e..68a65eb6c8 100644 --- a/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java +++ b/lib/src/compatKtLintApi/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompatAdapter.java @@ -20,6 +20,6 @@ public interface KtLintCompatAdapter { - String format(String text, Path path, boolean isScript, boolean useExperimental, String editorConfigPath, Map userData, + String format(String text, Path path, boolean isScript, boolean useExperimental, Path editorConfigPath, Map userData, Map editorConfigOverrideMap); } diff --git a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java index 1fe296143c..e6f3715d25 100644 --- a/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java +++ b/lib/src/ktlint/java/com/diffplug/spotless/glue/ktlint/KtlintFormatterFunc.java @@ -16,6 +16,7 @@ package com.diffplug.spotless.glue.ktlint; import java.io.File; +import java.nio.file.Path; import java.util.Map; import org.jetbrains.annotations.NotNull; @@ -69,9 +70,9 @@ public KtlintFormatterFunc(String version, boolean isScript, boolean useExperime @Override public String applyWithFile(String unix, File file) { - String absoluteEditorConfigPath = null; + Path absoluteEditorConfigPath = null; if (editorConfigPath != null) { - absoluteEditorConfigPath = editorConfigPath.getOnlyFile().getAbsolutePath(); + absoluteEditorConfigPath = editorConfigPath.getOnlyFile().toPath(); } return adapter.format(unix, file.toPath(), isScript, useExperimental, absoluteEditorConfigPath, userData, editorConfigOverrideMap); } From f4c2a1d84e2cb82aa36415ae514a93cc60b59ace Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Thu, 5 Jan 2023 16:28:35 +0100 Subject: [PATCH 11/13] EditorConfig: Use default path without checking (KtLint will check them anyway) --- .../java/com/diffplug/gradle/spotless/KotlinExtension.java | 5 +---- .../com/diffplug/gradle/spotless/KotlinGradleExtension.java | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java index 9ebc290219..5d1c484521 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java @@ -62,11 +62,8 @@ public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) { /** Adds the specified version of ktlint. */ public KotlinFormatExtension ktlint(String version) throws IOException { Objects.requireNonNull(version); - FileSignature editorConfigPath = null; File defaultEditorConfig = getProject().getRootProject().file(".editorconfig"); - if (defaultEditorConfig.exists() && defaultEditorConfig.isFile()) { - editorConfigPath = FileSignature.signAsList(defaultEditorConfig); - } + FileSignature editorConfigPath = FileSignature.signAsList(defaultEditorConfig); return new KotlinFormatExtension(version, false, editorConfigPath, Collections.emptyMap(), Collections.emptyMap()); } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java index ec645a8972..82a8edd9a5 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinGradleExtension.java @@ -47,11 +47,8 @@ public KotlinGradleExtension(SpotlessExtension spotless) { /** Adds the specified version of ktlint. */ public KotlinFormatExtension ktlint(String version) throws IOException { Objects.requireNonNull(version, "version"); - FileSignature editorConfigPath = null; File defaultEditorConfig = getProject().getRootProject().file(".editorconfig"); - if (defaultEditorConfig.exists() && defaultEditorConfig.isFile()) { - editorConfigPath = FileSignature.signAsList(defaultEditorConfig); - } + FileSignature editorConfigPath = FileSignature.signAsList(defaultEditorConfig); return new KotlinFormatExtension(version, false, editorConfigPath, Collections.emptyMap(), Collections.emptyMap()); } From 5db9d64a6810237ff83e271370893b28bfa55bb4 Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Sun, 8 Jan 2023 20:27:13 +0100 Subject: [PATCH 12/13] Use Files.exists, make code more consistent * Use java.nio.file.Files.exists to be consistent * Use empty empty EditorConfigDefaults settings for KtLint 0.47 and 0.48 when possible --- .../ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java | 10 +++++++++- .../ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java | 5 +++-- .../compat/KtLintCompat0Dot48Dot0AdapterTest.java | 1 - 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java b/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java index 20b87eeb17..0a38730b6a 100644 --- a/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot47Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot47Dot0Adapter.java @@ -17,6 +17,7 @@ import static java.util.Collections.emptySet; +import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.LinkedHashSet; @@ -77,6 +78,13 @@ public String format(final String text, Path path, final boolean isScript, editorConfigOverrideMap); } + EditorConfigDefaults editorConfig; + if (editorConfigPath == null || !Files.exists(editorConfigPath)) { + editorConfig = EditorConfigDefaults.Companion.getEmptyEditorConfigDefaults(); + } else { + editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath); + } + return KtLint.INSTANCE.format(new KtLint.ExperimentalParams( path.toFile().getAbsolutePath(), text, @@ -87,7 +95,7 @@ public String format(final String text, Path path, final boolean isScript, isScript, null, false, - EditorConfigDefaults.Companion.load(editorConfigPath), + editorConfig, editorConfigOverride, false)); } diff --git a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java index 8bb96faf08..ca5cb08b74 100644 --- a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java @@ -15,9 +15,10 @@ */ package com.diffplug.spotless.glue.ktlint.compat; +import java.nio.file.Path; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Collections; -import java.nio.file.Path; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -99,7 +100,7 @@ public String format(final String text, Path path, final boolean isScript, editorConfigOverrideMap); } EditorConfigDefaults editorConfig; - if (editorConfigPath == null || !editorConfigPath.toFile().exists()) { + if (editorConfigPath == null || !Files.exists(editorConfigPath)) { editorConfig = EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS(); } else { editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath); diff --git a/lib/src/testCompatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0AdapterTest.java b/lib/src/testCompatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0AdapterTest.java index cd91633215..6825b3d818 100644 --- a/lib/src/testCompatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0AdapterTest.java +++ b/lib/src/testCompatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0AdapterTest.java @@ -17,7 +17,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; From 6cb3ec5008e738fad916bd546ad3ca3512cb2f0e Mon Sep 17 00:00:00 2001 From: Eir Nym <485399+eirnym@users.noreply.github.com> Date: Mon, 9 Jan 2023 11:22:05 +0100 Subject: [PATCH 13/13] Apply spotless --- .../glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java index ca5cb08b74..5d24603f70 100644 --- a/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java +++ b/lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java @@ -15,8 +15,8 @@ */ package com.diffplug.spotless.glue.ktlint.compat; -import java.nio.file.Path; import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet;