|
| 1 | +/* |
| 2 | + * Copyright 2023 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.glue.ktlint.compat; |
| 17 | + |
| 18 | +import java.util.LinkedHashSet; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.stream.Collectors; |
| 24 | +import java.util.stream.Stream; |
| 25 | + |
| 26 | +import com.pinterest.ktlint.core.KtLint; |
| 27 | +import com.pinterest.ktlint.core.LintError; |
| 28 | +import com.pinterest.ktlint.core.Rule; |
| 29 | +import com.pinterest.ktlint.core.RuleProvider; |
| 30 | +import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties; |
| 31 | +import com.pinterest.ktlint.core.api.EditorConfigDefaults; |
| 32 | +import com.pinterest.ktlint.core.api.EditorConfigOverride; |
| 33 | +import com.pinterest.ktlint.core.api.UsesEditorConfigProperties; |
| 34 | +import com.pinterest.ktlint.core.api.editorconfig.EditorConfigProperty; |
| 35 | +import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider; |
| 36 | +import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider; |
| 37 | + |
| 38 | +import kotlin.Pair; |
| 39 | +import kotlin.Unit; |
| 40 | +import kotlin.jvm.functions.Function2; |
| 41 | + |
| 42 | +public class KtLintCompat0Dot48Dot0Adapter implements KtLintCompatAdapter { |
| 43 | + |
| 44 | + static class FormatterCallback implements Function2<LintError, Boolean, Unit> { |
| 45 | + @Override |
| 46 | + public Unit invoke(LintError lint, Boolean corrected) { |
| 47 | + if (!corrected) { |
| 48 | + KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail()); |
| 49 | + } |
| 50 | + return null; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String format(final String text, final String name, final boolean isScript, |
| 56 | + final boolean useExperimental, |
| 57 | + final Map<String, String> userData, |
| 58 | + final Map<String, Object> editorConfigOverrideMap) { |
| 59 | + final FormatterCallback formatterCallback = new FormatterCallback(); |
| 60 | + |
| 61 | + Set<RuleProvider> allRuleProviders = new LinkedHashSet<>( |
| 62 | + new StandardRuleSetProvider().getRuleProviders()); |
| 63 | + if (useExperimental) { |
| 64 | + allRuleProviders.addAll(new ExperimentalRuleSetProvider().getRuleProviders()); |
| 65 | + } |
| 66 | + |
| 67 | + EditorConfigOverride editorConfigOverride; |
| 68 | + if (editorConfigOverrideMap.isEmpty()) { |
| 69 | + editorConfigOverride = EditorConfigOverride.Companion.getEmptyEditorConfigOverride(); |
| 70 | + } else { |
| 71 | + editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map( |
| 72 | + RuleProvider::createNewRuleInstance).collect( |
| 73 | + Collectors.toList()), |
| 74 | + editorConfigOverrideMap); |
| 75 | + } |
| 76 | + |
| 77 | + return KtLint.INSTANCE.format(new KtLint.ExperimentalParams( |
| 78 | + name, |
| 79 | + text, |
| 80 | + allRuleProviders, |
| 81 | + userData, |
| 82 | + formatterCallback, |
| 83 | + isScript, |
| 84 | + false, |
| 85 | + EditorConfigDefaults.Companion.getEmptyEditorConfigDefaults(), |
| 86 | + editorConfigOverride, |
| 87 | + false)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Create EditorConfigOverride from user provided parameters. |
| 92 | + */ |
| 93 | + private static EditorConfigOverride createEditorConfigOverride(final List<Rule> rules, Map<String, Object> editorConfigOverrideMap) { |
| 94 | + // Get properties from rules in the rule sets |
| 95 | + Stream<EditorConfigProperty<?>> ruleProperties = rules.stream() |
| 96 | + .filter(rule -> rule instanceof UsesEditorConfigProperties) |
| 97 | + .flatMap(rule -> ((UsesEditorConfigProperties) rule).getEditorConfigProperties().stream()); |
| 98 | + |
| 99 | + // Create a mapping of properties to their names based on rule properties and default properties |
| 100 | + Map<String, EditorConfigProperty<?>> supportedProperties = Stream |
| 101 | + .concat(ruleProperties, DefaultEditorConfigProperties.INSTANCE.getEditorConfigProperties().stream()) |
| 102 | + .distinct() |
| 103 | + .collect(Collectors.toMap(EditorConfigProperty::getName, property -> property)); |
| 104 | + |
| 105 | + // Create config properties based on provided property names and values |
| 106 | + @SuppressWarnings("unchecked") |
| 107 | + Pair<EditorConfigProperty<?>, ?>[] properties = editorConfigOverrideMap.entrySet().stream() |
| 108 | + .map(entry -> { |
| 109 | + EditorConfigProperty<?> property = supportedProperties.get(entry.getKey()); |
| 110 | + if (property != null) { |
| 111 | + return new Pair<>(property, entry.getValue()); |
| 112 | + } else { |
| 113 | + return null; |
| 114 | + } |
| 115 | + }) |
| 116 | + .filter(Objects::nonNull) |
| 117 | + .toArray(Pair[]::new); |
| 118 | + |
| 119 | + return EditorConfigOverride.Companion.from(properties); |
| 120 | + } |
| 121 | +} |
0 commit comments