|
| 1 | +/* |
| 2 | + * Copyright 2022 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.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Objects; |
| 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.RuleSet; |
| 29 | +import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties; |
| 30 | +import com.pinterest.ktlint.core.api.EditorConfigOverride; |
| 31 | +import com.pinterest.ktlint.core.api.UsesEditorConfigProperties; |
| 32 | +import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider; |
| 33 | +import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider; |
| 34 | + |
| 35 | +import kotlin.Pair; |
| 36 | +import kotlin.Unit; |
| 37 | +import kotlin.jvm.functions.Function2; |
| 38 | + |
| 39 | +public class KtLintCompat0Dot45Dot2Adapter implements KtLintCompatAdapter { |
| 40 | + |
| 41 | + static class FormatterCallback implements Function2<LintError, Boolean, Unit> { |
| 42 | + @Override |
| 43 | + public Unit invoke(LintError lint, Boolean corrected) { |
| 44 | + if (!corrected) { |
| 45 | + KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail()); |
| 46 | + } |
| 47 | + return null; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public String format(final String text, final String name, final boolean isScript, |
| 53 | + final boolean useExperimental, |
| 54 | + final Map<String, String> userData, |
| 55 | + final Map<String, Object> editorConfigOverrideMap) { |
| 56 | + final FormatterCallback formatterCallback = new FormatterCallback(); |
| 57 | + |
| 58 | + final List<RuleSet> rulesets = new ArrayList<>(); |
| 59 | + rulesets.add(new StandardRuleSetProvider().get()); |
| 60 | + |
| 61 | + if (useExperimental) { |
| 62 | + rulesets.add(new ExperimentalRuleSetProvider().get()); |
| 63 | + } |
| 64 | + |
| 65 | + EditorConfigOverride editorConfigOverride; |
| 66 | + if (editorConfigOverrideMap.isEmpty()) { |
| 67 | + editorConfigOverride = EditorConfigOverride.Companion.getEmptyEditorConfigOverride(); |
| 68 | + } else { |
| 69 | + editorConfigOverride = createEditorConfigOverride(rulesets, editorConfigOverrideMap); |
| 70 | + } |
| 71 | + |
| 72 | + return KtLint.INSTANCE.format(new KtLint.ExperimentalParams( |
| 73 | + name, |
| 74 | + text, |
| 75 | + rulesets, |
| 76 | + userData, |
| 77 | + formatterCallback, |
| 78 | + isScript, |
| 79 | + null, |
| 80 | + false, |
| 81 | + editorConfigOverride, |
| 82 | + false)); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Create EditorConfigOverride from user provided parameters. |
| 87 | + * Calling this method requires KtLint 0.45.2. |
| 88 | + */ |
| 89 | + private static EditorConfigOverride createEditorConfigOverride(final List<RuleSet> rulesets, Map<String, Object> editorConfigOverrideMap) { |
| 90 | + // Get properties from rules in the rule sets |
| 91 | + Stream<UsesEditorConfigProperties.EditorConfigProperty<?>> ruleProperties = rulesets.stream() |
| 92 | + .flatMap(ruleSet -> Arrays.stream(ruleSet.getRules())) |
| 93 | + .filter(rule -> rule instanceof UsesEditorConfigProperties) |
| 94 | + .flatMap(rule -> ((UsesEditorConfigProperties) rule).getEditorConfigProperties().stream()); |
| 95 | + |
| 96 | + // Create a mapping of properties to their names based on rule properties and default properties |
| 97 | + Map<String, UsesEditorConfigProperties.EditorConfigProperty<?>> supportedProperties = Stream |
| 98 | + .concat(ruleProperties, DefaultEditorConfigProperties.INSTANCE.getDefaultEditorConfigProperties().stream()) |
| 99 | + .distinct() |
| 100 | + .collect(Collectors.toMap(property -> property.getType().getName(), property -> property)); |
| 101 | + |
| 102 | + // Create config properties based on provided property names and values |
| 103 | + @SuppressWarnings("unchecked") |
| 104 | + Pair<UsesEditorConfigProperties.EditorConfigProperty<?>, ?>[] properties = editorConfigOverrideMap.entrySet().stream() |
| 105 | + .map(entry -> { |
| 106 | + UsesEditorConfigProperties.EditorConfigProperty<?> property = supportedProperties.get(entry.getKey()); |
| 107 | + if (property != null) { |
| 108 | + return new Pair<>(property, entry.getValue()); |
| 109 | + } else { |
| 110 | + return null; |
| 111 | + } |
| 112 | + }) |
| 113 | + .filter(Objects::nonNull) |
| 114 | + .toArray(Pair[]::new); |
| 115 | + |
| 116 | + return EditorConfigOverride.Companion.from(properties); |
| 117 | + } |
| 118 | +} |
0 commit comments