Skip to content

Commit

Permalink
Add support for KtLint 0.48.0 (fixes #1430)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidburstrom committed Dec 20, 2022
1 parent 8d1467f commit 2e00cfa
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ versionCompatibility {
'0.45.2',
'0.46.0',
'0.47.0',
'0.48.0',
]
targetSourceSetName = 'ktlint'
}
Expand Down Expand Up @@ -90,6 +91,9 @@ dependencies {
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-core:0.47.0'
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.47.0'
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.47.0'
compatKtLint0Dot48Dot0CompileOnly 'com.pinterest.ktlint:ktlint-core:0.48.0'
compatKtLint0Dot48Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.48.0'
compatKtLint0Dot48Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.48.0'

String VER_SCALAFMT="3.6.1"
scalafmtCompileOnly "org.scalameta:scalafmt-core_2.13:$VER_SCALAFMT"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
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.Rule;
import com.pinterest.ktlint.core.RuleProvider;
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties;
import com.pinterest.ktlint.core.api.EditorConfigDefaults;
import com.pinterest.ktlint.core.api.EditorConfigOverride;
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties;
import com.pinterest.ktlint.core.api.editorconfig.EditorConfigProperty;
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 KtLintCompat0Dot48Dot0Adapter 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();

Set<RuleProvider> allRuleProviders = new LinkedHashSet<>(
new StandardRuleSetProvider().getRuleProviders());
if (useExperimental) {
allRuleProviders.addAll(new ExperimentalRuleSetProvider().getRuleProviders());
}

EditorConfigOverride editorConfigOverride;
if (editorConfigOverrideMap.isEmpty()) {
editorConfigOverride = EditorConfigOverride.Companion.getEmptyEditorConfigOverride();
} else {
editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map(
RuleProvider::createNewRuleInstance).collect(
Collectors.toList()),
editorConfigOverrideMap);
}

return KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
name,
text,
allRuleProviders,
userData,
formatterCallback,
isScript,
false,
EditorConfigDefaults.Companion.getEmptyEditorConfigDefaults(),
editorConfigOverride,
false));
}

/**
* Create EditorConfigOverride from user provided parameters.
*/
private static EditorConfigOverride createEditorConfigOverride(final List<Rule> rules, Map<String, Object> editorConfigOverrideMap) {
// Get properties from rules in the rule sets
Stream<EditorConfigProperty<?>> ruleProperties = rules.stream()
.filter(rule -> rule instanceof UsesEditorConfigProperties)
.flatMap(rule -> ((UsesEditorConfigProperties) rule).getEditorConfigProperties().stream());

// get complete list of supported properties in DefaultEditorConfigProperties.INSTANCE
List<EditorConfigProperty<?>> editorConfigProperties = new ArrayList<>(DefaultEditorConfigProperties.INSTANCE.getEditorConfigProperties());
editorConfigProperties.add(DefaultEditorConfigProperties.INSTANCE.getKtlintDisabledRulesProperty());

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

// Create config properties based on provided property names and values
@SuppressWarnings("unchecked")
Pair<EditorConfigProperty<?>, ?>[] properties = editorConfigOverrideMap.entrySet().stream()
.map(entry -> {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;

public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {
Expand All @@ -41,7 +42,10 @@ public class KtlintFormatterFunc implements FormatterFunc.NeedsFile {
public KtlintFormatterFunc(String version, boolean isScript, boolean useExperimental, Map<String, String> userData,
Map<String, Object> editorConfigOverrideMap) {
int minorVersion = Integer.parseInt(version.split("\\.")[1]);
if (minorVersion >= 47) {
if (minorVersion >= 48) {
// ExperimentalParams lost two constructor arguments, EditorConfigProperty moved to its own class
this.adapter = new KtLintCompat0Dot48Dot0Adapter();
} else if (minorVersion == 47) {
// rename RuleSet to RuleProvider
this.adapter = new KtLintCompat0Dot47Dot0Adapter();
} else if (minorVersion >= 46) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class KtLintStep {
// prevent direct instantiation
private KtLintStep() {}

private static final String DEFAULT_VERSION = "0.47.1";
private static final String DEFAULT_VERSION = "0.48.0";
static final String NAME = "ktlint";
static final String PACKAGE_PRE_0_32 = "com.github.shyiko";
static final String PACKAGE = "com.pinterest";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ void works0_47_1() throws Exception {
});
}

@Test
void works0_48_0() throws Exception {
FormatterStep step = KtLintStep.create("0.48.0", TestProvisioner.mavenCentral());
StepHarness.forStep(step)
.testResource("kotlin/ktlint/basic.dirty", "kotlin/ktlint/basic.clean")
.testResourceException("kotlin/ktlint/unsolvable.dirty", assertion -> {
assertion.isInstanceOf(AssertionError.class);
assertion.hasMessage("Error on line: 1, column: 1\n" +
"rule: no-wildcard-imports\n" +
"Wildcard import");
});
}

@Test
void equality() throws Exception {
new SerializableEqualityTester() {
Expand Down

0 comments on commit 2e00cfa

Please sign in to comment.