Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add editor config file option to maven and gradle #1442

Merged
merged 17 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<String, String> userData,
String editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<String, String> userData,
String editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<String, String> userData,
String editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Expand All @@ -61,7 +61,7 @@ public String format(final String text, final String name, final boolean isScrip
userData,
formatterCallback,
isScript,
null,
editorConfigPath,
false));
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<String, String> userData,
String editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Expand All @@ -76,7 +76,7 @@ public String format(final String text, final String name, final boolean isScrip
userData,
formatterCallback,
isScript,
null,
editorConfigPath,
false,
editorConfigOverride,
false));
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<String, String> userData,
String editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Expand All @@ -76,7 +76,7 @@ public String format(final String text, final String name, final boolean isScrip
userData,
formatterCallback,
isScript,
null,
editorConfigPath,
false,
editorConfigOverride,
false));
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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<String, String> userData,
String editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Expand All @@ -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,
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> userData,
String editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Expand All @@ -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,
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,6 +19,6 @@

public interface KtLintCompatAdapter {

String format(String text, String name, boolean isScript, boolean useExperimental, Map<String, String> userData,
String format(String text, String name, boolean isScript, boolean useExperimental, String editorConfigPath, Map<String, String> userData,
Map<String, Object> editorConfigOverrideMap);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> editorConfigOverrideMap;

public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, Map<String, String> userData,
public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, String editorConfigPath, Map<String, String> userData,
Map<String, Object> editorConfigOverrideMap) {
int minorVersion = Integer.parseInt(version.split("\\.")[1]);
if (minorVersion >= 48) {
Expand All @@ -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;
Expand All @@ -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);
}
}
15 changes: 11 additions & 4 deletions lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> userData, Map<String, Object> editorConfigOverride) {
return create(version, provisioner, useExperimental, userData, editorConfigOverride);
}

public static FormatterStep create(String version, Provisioner provisioner, boolean isScript, boolean useExperimental,
String editorConfig, Map<String, String> userData, Map<String, Object> 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);
}

Expand All @@ -86,9 +91,10 @@ static final class State implements Serializable {
private final TreeMap<String, String> userData;
private final TreeMap<String, Object> editorConfigOverride;
private final String version;
private final String editorConfigPath;
eirnym marked this conversation as resolved.
Show resolved Hide resolved

State(String version, Provisioner provisioner, boolean isScript, boolean useExperimental,
Map<String, String> userData, Map<String, Object> editorConfigOverride) throws IOException {
String editorConfigPath, Map<String, String> userData, Map<String, Object> editorConfigOverride) throws IOException {
this.version = version;

String coordinate;
Expand All @@ -104,15 +110,16 @@ 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;
}

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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -60,7 +60,7 @@ public LicenseHeaderConfig licenseHeaderFile(Object licenseHeaderFile) {
/** Adds the specified version of <a href="https://github.com/pinterest/ktlint">ktlint</a>. */
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() {
Expand All @@ -71,13 +71,15 @@ public class KotlinFormatExtension {

private final String version;
private boolean useExperimental;
private String editorConfigPath;
private Map<String, String> userData;
private Map<String, Object> editorConfigOverride;

KotlinFormatExtension(String version, boolean useExperimental, Map<String, String> config,
KotlinFormatExtension(String version, boolean useExperimental, String editorConfigPath, Map<String, String> config,
Map<String, Object> editorConfigOverride) {
this.version = version;
this.useExperimental = useExperimental;
this.editorConfigPath = editorConfigPath;
this.userData = config;
this.editorConfigOverride = editorConfigOverride;
addStep(createStep());
Expand Down Expand Up @@ -106,7 +108,7 @@ public KotlinFormatExtension editorConfigOverride(Map<String, Object> editorConf
}

private FormatterStep createStep() {
return KtLintStep.create(version, provisioner(), useExperimental, userData, editorConfigOverride);
return KtLintStep.create(version, provisioner(), useExperimental, false, editorConfigPath, userData, editorConfigOverride);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -45,7 +45,7 @@ public KotlinGradleExtension(SpotlessExtension spotless) {
/** Adds the specified version of <a href="https://github.com/pinterest/ktlint">ktlint</a>. */
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() {
Expand All @@ -56,18 +56,26 @@ public class KotlinFormatExtension {

private final String version;
private boolean useExperimental;
private String editorConfigPath;
private Map<String, String> userData;
private Map<String, Object> editorConfigOverride;

KotlinFormatExtension(String version, boolean useExperimental, Map<String, String> config,
KotlinFormatExtension(String version, boolean useExperimental, String editorConfigPath, Map<String, String> config,
Map<String, Object> 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());
Expand Down
Loading