Skip to content

Commit

Permalink
Reinstate support for KtLint down to 0.31.0
Browse files Browse the repository at this point in the history
The io.github.davidburstrom.version-compatibility plugin provides sourcesets that make it possible
to write adapters against specific versions of KtLint, and the correct adapter will be selected
during runtime.
  • Loading branch information
davidburstrom committed Aug 29, 2022
1 parent 688e01a commit c8194dd
Show file tree
Hide file tree
Showing 15 changed files with 641 additions and 104 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]

### Changes
* Extend minimum supported version for `ktlint` to `0.31.0` ([#xxx](https://github.com/diffplug/spotless/pull/xxx)).

## [2.29.0] - 2022-08-23
### Added
* `scalafmt` integration now has a configuration option `majorScalaVersion` that allows you to configure the Scala version that gets resolved from the maven artifact ([#1283](https://github.com/diffplug/spotless/pull/1283))
Expand Down
35 changes: 35 additions & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'java-library'
id 'io.github.davidburstrom.version-compatibility'
}
ext.artifactId = project.artifactIdLib
version = rootProject.spotlessChangelog.versionNext
Expand All @@ -23,6 +24,21 @@ for (glue in NEEDS_GLUE) {
}
}

versionCompatibility {
adapters {
namespaces.register('KtLint') {
versions = [
'0.31.0',
'0.32.0',
'0.34.2',
'0.45.2',
'0.46.0',
]
targetSourceSetName = 'ktlint'
}
}
}

dependencies {
compileOnly 'org.slf4j:slf4j-api:2.0.0'
// zero runtime reqs is a hard requirements for spotless-lib
Expand Down Expand Up @@ -52,6 +68,25 @@ dependencies {
ktlintCompileOnly "com.pinterest.ktlint:ktlint-ruleset-experimental:$VER_KTLINT"
ktlintCompileOnly "com.pinterest.ktlint:ktlint-ruleset-standard:$VER_KTLINT"

compatKtLint0Dot31Dot0CompileOnly 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.20'
compatKtLint0Dot31Dot0CompileOnly 'com.github.shyiko.ktlint:ktlint-core:0.31.0'
compatKtLint0Dot31Dot0CompileOnly 'com.github.shyiko.ktlint:ktlint-ruleset-experimental:0.31.0'
compatKtLint0Dot31Dot0CompileOnly 'com.github.shyiko.ktlint:ktlint-ruleset-standard:0.31.0'
compatKtLint0Dot32Dot0CompileOnly 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.20'
compatKtLint0Dot32Dot0CompileOnly 'com.pinterest.ktlint:ktlint-core:0.32.0'
compatKtLint0Dot32Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.32.0'
compatKtLint0Dot32Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.32.0'
compatKtLint0Dot34Dot2CompileOnly 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.20'
compatKtLint0Dot34Dot2CompileOnly 'com.pinterest.ktlint:ktlint-core:0.34.2'
compatKtLint0Dot34Dot2CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.34.2'
compatKtLint0Dot34Dot2CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.34.2'
compatKtLint0Dot45Dot2CompileOnly 'com.pinterest.ktlint:ktlint-core:0.45.2'
compatKtLint0Dot45Dot2CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.45.2'
compatKtLint0Dot45Dot2CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.45.2'
compatKtLint0Dot46Dot0CompileOnly 'com.pinterest.ktlint:ktlint-core:0.46.0'
compatKtLint0Dot46Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.46.0'
compatKtLint0Dot46Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.46.0'

String VER_SCALAFMT="3.5.9"
scalafmtCompileOnly "org.scalameta:scalafmt-core_2.13:$VER_SCALAFMT"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.glue.ktlint.compat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.github.shyiko.ktlint.core.KtLint;
import com.github.shyiko.ktlint.core.LintError;
import com.github.shyiko.ktlint.core.RuleSet;
import com.github.shyiko.ktlint.ruleset.experimental.ExperimentalRuleSetProvider;
import com.github.shyiko.ktlint.ruleset.standard.StandardRuleSetProvider;

import kotlin.Unit;
import kotlin.jvm.functions.Function2;

public class KtLintCompat0Dot31Dot0Adapter implements KtLintCompatAdapter {

static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
@Override
public Unit invoke(LintError lint, Boolean corrected) {
if (!corrected) {
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail());
}
return null;
}
}

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

final List<RuleSet> rulesets = new ArrayList<>();
rulesets.add(new StandardRuleSetProvider().get());

if (useExperimental) {
rulesets.add(new ExperimentalRuleSetProvider().get());
}

return KtLint.INSTANCE.format(
text,
rulesets,
userData,
formatterCallback);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.glue.ktlint.compat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pinterest.ktlint.core.KtLint;
import com.pinterest.ktlint.core.LintError;
import com.pinterest.ktlint.core.RuleSet;
import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider;
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider;

import kotlin.Unit;
import kotlin.jvm.functions.Function2;

public class KtLintCompat0Dot32Dot0Adapter implements KtLintCompatAdapter {

static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
@Override
public Unit invoke(LintError lint, Boolean corrected) {
if (!corrected) {
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail());
}
return null;
}
}

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

final List<RuleSet> rulesets = new ArrayList<>();
rulesets.add(new StandardRuleSetProvider().get());

if (useExperimental) {
rulesets.add(new ExperimentalRuleSetProvider().get());
}

return KtLint.INSTANCE.format(
text,
rulesets,
userData,
formatterCallback);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.glue.ktlint.compat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pinterest.ktlint.core.KtLint;
import com.pinterest.ktlint.core.LintError;
import com.pinterest.ktlint.core.RuleSet;
import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider;
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider;

import kotlin.Unit;
import kotlin.jvm.functions.Function2;

public class KtLintCompat0Dot34Dot2Adapter implements KtLintCompatAdapter {

static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
@Override
public Unit invoke(LintError lint, Boolean corrected) {
if (!corrected) {
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail());
}
return null;
}
}

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

final List<RuleSet> rulesets = new ArrayList<>();
rulesets.add(new StandardRuleSetProvider().get());

if (useExperimental) {
rulesets.add(new ExperimentalRuleSetProvider().get());
}

return KtLint.INSTANCE.format(new KtLint.Params(
name,
text,
rulesets,
userData,
formatterCallback,
isScript,
null,
false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.glue.ktlint.compat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.pinterest.ktlint.core.KtLint;
import com.pinterest.ktlint.core.LintError;
import com.pinterest.ktlint.core.RuleSet;
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties;
import com.pinterest.ktlint.core.api.EditorConfigOverride;
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties;
import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider;
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider;

import kotlin.Pair;
import kotlin.Unit;
import kotlin.jvm.functions.Function2;

public class KtLintCompat0Dot45Dot2Adapter implements KtLintCompatAdapter {

static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
@Override
public Unit invoke(LintError lint, Boolean corrected) {
if (!corrected) {
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail());
}
return null;
}
}

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

final List<RuleSet> rulesets = new ArrayList<>();
rulesets.add(new StandardRuleSetProvider().get());

if (useExperimental) {
rulesets.add(new ExperimentalRuleSetProvider().get());
}

EditorConfigOverride editorConfigOverride;
if (editorConfigOverrideMap.isEmpty()) {
editorConfigOverride = EditorConfigOverride.Companion.getEmptyEditorConfigOverride();
} else {
editorConfigOverride = createEditorConfigOverride(rulesets, editorConfigOverrideMap);
}

return KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
name,
text,
rulesets,
userData,
formatterCallback,
isScript,
null,
false,
editorConfigOverride,
false));
}

/**
* Create EditorConfigOverride from user provided parameters.
* Calling this method requires KtLint 0.45.2.
*/
private static EditorConfigOverride createEditorConfigOverride(final List<RuleSet> rulesets, Map<String, Object> editorConfigOverrideMap) {
// Get properties from rules in the rule sets
Stream<UsesEditorConfigProperties.EditorConfigProperty<?>> ruleProperties = rulesets.stream()
.flatMap(ruleSet -> Arrays.stream(ruleSet.getRules()))
.filter(rule -> rule instanceof UsesEditorConfigProperties)
.flatMap(rule -> ((UsesEditorConfigProperties) rule).getEditorConfigProperties().stream());

// Create a mapping of properties to their names based on rule properties and default properties
Map<String, UsesEditorConfigProperties.EditorConfigProperty<?>> supportedProperties = Stream
.concat(ruleProperties, DefaultEditorConfigProperties.INSTANCE.getDefaultEditorConfigProperties().stream())
.distinct()
.collect(Collectors.toMap(property -> property.getType().getName(), property -> property));

// Create config properties based on provided property names and values
@SuppressWarnings("unchecked")
Pair<UsesEditorConfigProperties.EditorConfigProperty<?>, ?>[] properties = editorConfigOverrideMap.entrySet().stream()
.map(entry -> {
UsesEditorConfigProperties.EditorConfigProperty<?> property = supportedProperties.get(entry.getKey());
if (property != null) {
return new Pair<>(property, entry.getValue());
} else {
return null;
}
})
.filter(Objects::nonNull)
.toArray(Pair[]::new);

return EditorConfigOverride.Companion.from(properties);
}
}

0 comments on commit c8194dd

Please sign in to comment.