Skip to content

Commit

Permalink
Support .editorconfig for ktlint (#1442)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Jan 9, 2023
2 parents b2778cd + 6cb3ec5 commit 4ee53aa
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 86 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]
### Added
* Add option `editorConfigFile` for `ktLint` [#142](https://github.com/diffplug/spotless/issues/142)
* Added `skipLinesMatching` option to `licenseHeader` to support formats where license header cannot be immediately added to the top of the file (e.g. xml, sh). ([#1441](https://github.com/diffplug/spotless/pull/1441)).
### Fixed
* Support `ktlint` 0.48+ new rule disabling syntax ([#1456](https://github.com/diffplug/spotless/pull/1456)) fixes ([#1444](https://github.com/diffplug/spotless/issues/1444))
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 @@ -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;
Expand All @@ -41,9 +42,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
}

@Override
public String format(final String text, final String name, final boolean isScript,
public String format(final String text, Path path, final boolean isScript,
final boolean useExperimental,
final Map<String, String> userData,
Path 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 All @@ -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;
Expand All @@ -41,9 +42,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
}

@Override
public String format(final String text, final String name, final boolean isScript,
public String format(final String text, Path path, final boolean isScript,
final boolean useExperimental,
final Map<String, String> userData,
Path 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 All @@ -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;
Expand All @@ -41,9 +42,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
}

@Override
public String format(final String text, final String name, final boolean isScript,
public String format(final String text, Path path, final boolean isScript,
final boolean useExperimental,
final Map<String, String> userData,
Path editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Expand All @@ -55,13 +56,13 @@ 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,
formatterCallback,
isScript,
null,
editorConfigPath.toFile().getAbsolutePath(),
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 All @@ -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;
Expand Down Expand Up @@ -49,9 +50,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
}

@Override
public String format(final String text, final String name, final boolean isScript,
public String format(final String text, Path path, final boolean isScript,
final boolean useExperimental,
final Map<String, String> userData,
Path editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Expand All @@ -70,13 +71,13 @@ 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,
formatterCallback,
isScript,
null,
editorConfigPath.toFile().getAbsolutePath(),
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 @@ -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;
Expand Down Expand Up @@ -49,9 +50,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
}

@Override
public String format(final String text, final String name, final boolean isScript,
public String format(final String text, Path path, final boolean isScript,
final boolean useExperimental,
final Map<String, String> userData,
Path editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Expand All @@ -70,13 +71,13 @@ 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,
formatterCallback,
isScript,
null,
editorConfigPath.toFile().getAbsolutePath(),
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.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -54,9 +56,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
}

@Override
public String format(final String text, final String name, final boolean isScript,
public String format(final String text, Path path, final boolean isScript,
final boolean useExperimental,
final Map<String, String> userData,
Path editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Expand All @@ -68,16 +70,23 @@ 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);
}

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(
name,
path.toFile().getAbsolutePath(),
text,
emptySet(),
allRuleProviders,
Expand All @@ -86,7 +95,7 @@ public String format(final String text, final String name, final boolean isScrip
isScript,
null,
false,
EditorConfigDefaults.Companion.getEmptyEditorConfigDefaults(),
editorConfig,
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.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
Expand All @@ -25,7 +27,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;
Expand Down Expand Up @@ -76,9 +78,9 @@ public Unit invoke(LintError lint, Boolean corrected) {
}

@Override
public String format(final String text, final String name, final boolean isScript,
public String format(final String text, Path path, final boolean isScript,
final boolean useExperimental,
final Map<String, String> userData,
Path editorConfigPath, final Map<String, String> userData,
final Map<String, Object> editorConfigOverrideMap) {
final FormatterCallback formatterCallback = new FormatterCallback();

Expand All @@ -97,18 +99,19 @@ public String format(final String text, final String name, final boolean isScrip
Collectors.toList()),
editorConfigOverrideMap);
}
EditorConfigDefaults editorConfig;
if (editorConfigPath == null || !Files.exists(editorConfigPath)) {
editorConfig = EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS();
} else {
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath);
}

return KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
name,
text,
return new KtLintRuleEngine(
allRuleProviders,
userData,
formatterCallback,
isScript,
false,
EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS(),
editorConfig,
editorConfigOverride,
false));
false)
.format(path, 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 All @@ -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, Map<String, String> userData,
String format(String text, Path path, boolean isScript, boolean useExperimental, Path editorConfigPath, Map<String, String> userData,
Map<String, Object> editorConfigOverrideMap);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@
package com.diffplug.spotless.glue.ktlint;

import java.io.File;
import java.nio.file.Path;
import java.util.Map;

import org.jetbrains.annotations.NotNull;

import com.diffplug.spotless.FileSignature;
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 com.diffplug.spotless.glue.ktlint.compat.*;

public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {

Expand All @@ -37,9 +32,10 @@ public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {
@NotNull
private final KtLintCompatAdapter adapter;
private final boolean useExperimental;
private final FileSignature 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, FileSignature editorConfigPath, Map<String, String> userData,
Map<String, Object> editorConfigOverrideMap) {
int minorVersion = Integer.parseInt(version.split("\\.")[1]);
if (minorVersion >= 48) {
Expand All @@ -64,14 +60,20 @@ 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;
this.isScript = isScript;
}

@Override
public String applyWithFile(String unix, File file) throws Exception {
return adapter.format(unix, file.getName(), isScript, useExperimental, userData, editorConfigOverrideMap);
public String applyWithFile(String unix, File file) {

Path absoluteEditorConfigPath = null;
if (editorConfigPath != null) {
absoluteEditorConfigPath = editorConfigPath.getOnlyFile().toPath();
}
return adapter.format(unix, file.toPath(), isScript, useExperimental, absoluteEditorConfigPath, userData, editorConfigOverrideMap);
}
}
Loading

0 comments on commit 4ee53aa

Please sign in to comment.