|
| 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.nio.file.Files; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.ServiceLoader; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.stream.Collectors; |
| 28 | +import java.util.stream.Stream; |
| 29 | + |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3; |
| 34 | +import com.pinterest.ktlint.rule.engine.api.Code; |
| 35 | +import com.pinterest.ktlint.rule.engine.api.EditorConfigDefaults; |
| 36 | +import com.pinterest.ktlint.rule.engine.api.EditorConfigOverride; |
| 37 | +import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine; |
| 38 | +import com.pinterest.ktlint.rule.engine.api.LintError; |
| 39 | +import com.pinterest.ktlint.rule.engine.core.api.Rule; |
| 40 | +import com.pinterest.ktlint.rule.engine.core.api.RuleId; |
| 41 | +import com.pinterest.ktlint.rule.engine.core.api.RuleProvider; |
| 42 | +import com.pinterest.ktlint.rule.engine.core.api.RuleSetId; |
| 43 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.CodeStyleEditorConfigPropertyKt; |
| 44 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfigProperty; |
| 45 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EndOfLinePropertyKt; |
| 46 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.IndentSizeEditorConfigPropertyKt; |
| 47 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.IndentStyleEditorConfigPropertyKt; |
| 48 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.InsertFinalNewLineEditorConfigPropertyKt; |
| 49 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MaxLineLengthEditorConfigPropertyKt; |
| 50 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RuleExecution; |
| 51 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RuleExecutionEditorConfigPropertyKt; |
| 52 | + |
| 53 | +import kotlin.Pair; |
| 54 | +import kotlin.Unit; |
| 55 | +import kotlin.jvm.functions.Function2; |
| 56 | + |
| 57 | +public class KtLintCompat1Dot0Dot0Adapter implements KtLintCompatAdapter { |
| 58 | + |
| 59 | + private static final Logger logger = LoggerFactory.getLogger(KtLintCompat1Dot0Dot0Adapter.class); |
| 60 | + |
| 61 | + private static final List<EditorConfigProperty<?>> DEFAULT_EDITOR_CONFIG_PROPERTIES; |
| 62 | + |
| 63 | + static { |
| 64 | + List<EditorConfigProperty<?>> list = new ArrayList<>(); |
| 65 | + list.add(CodeStyleEditorConfigPropertyKt.getCODE_STYLE_PROPERTY()); |
| 66 | + list.add(EndOfLinePropertyKt.getEND_OF_LINE_PROPERTY()); |
| 67 | + list.add(IndentSizeEditorConfigPropertyKt.getINDENT_SIZE_PROPERTY()); |
| 68 | + list.add(IndentStyleEditorConfigPropertyKt.getINDENT_STYLE_PROPERTY()); |
| 69 | + list.add(InsertFinalNewLineEditorConfigPropertyKt.getINSERT_FINAL_NEWLINE_PROPERTY()); |
| 70 | + list.add(MaxLineLengthEditorConfigPropertyKt.getMAX_LINE_LENGTH_PROPERTY()); |
| 71 | + list.add(RuleExecutionEditorConfigPropertyKt.getEXPERIMENTAL_RULES_EXECUTION_PROPERTY()); |
| 72 | + DEFAULT_EDITOR_CONFIG_PROPERTIES = Collections.unmodifiableList(list); |
| 73 | + } |
| 74 | + |
| 75 | + static class FormatterCallback implements Function2<LintError, Boolean, Unit> { |
| 76 | + |
| 77 | + @Override |
| 78 | + public Unit invoke(LintError lint, Boolean corrected) { |
| 79 | + if (!corrected) { |
| 80 | + KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId().getValue(), lint.getDetail()); |
| 81 | + } |
| 82 | + return Unit.INSTANCE; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public String format(final String text, Path path, final boolean isScript, |
| 88 | + Path editorConfigPath, final Map<String, String> userData, |
| 89 | + final Map<String, Object> editorConfigOverrideMap) { |
| 90 | + final FormatterCallback formatterCallback = new FormatterCallback(); |
| 91 | + |
| 92 | + Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV3.class, RuleSetProviderV3.class.getClassLoader()) |
| 93 | + .stream() |
| 94 | + .flatMap(loader -> loader.get().getRuleProviders().stream()) |
| 95 | + .collect(Collectors.toUnmodifiableSet()); |
| 96 | + |
| 97 | + EditorConfigOverride editorConfigOverride; |
| 98 | + if (editorConfigOverrideMap.isEmpty()) { |
| 99 | + editorConfigOverride = EditorConfigOverride.Companion.getEMPTY_EDITOR_CONFIG_OVERRIDE(); |
| 100 | + } else { |
| 101 | + editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map( |
| 102 | + RuleProvider::createNewRuleInstance).collect(Collectors.toList()), |
| 103 | + editorConfigOverrideMap); |
| 104 | + } |
| 105 | + EditorConfigDefaults editorConfig; |
| 106 | + if (editorConfigPath == null || !Files.exists(editorConfigPath)) { |
| 107 | + editorConfig = EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS(); |
| 108 | + } else { |
| 109 | + editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, Collections.emptySet()); |
| 110 | + } |
| 111 | + |
| 112 | + return new KtLintRuleEngine( |
| 113 | + allRuleProviders, |
| 114 | + editorConfig, |
| 115 | + editorConfigOverride, |
| 116 | + false, |
| 117 | + path.getFileSystem()) |
| 118 | + .format(Code.Companion.fromPath(path), formatterCallback); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Create EditorConfigOverride from user provided parameters. |
| 123 | + */ |
| 124 | + private static EditorConfigOverride createEditorConfigOverride(final List<Rule> rules, Map<String, Object> editorConfigOverrideMap) { |
| 125 | + // Get properties from rules in the rule sets |
| 126 | + Stream<EditorConfigProperty<?>> ruleProperties = rules.stream() |
| 127 | + .flatMap(rule -> rule.getUsesEditorConfigProperties().stream()); |
| 128 | + |
| 129 | + // Create a mapping of properties to their names based on rule properties and default properties |
| 130 | + Map<String, EditorConfigProperty<?>> supportedProperties = Stream |
| 131 | + .concat(ruleProperties, DEFAULT_EDITOR_CONFIG_PROPERTIES.stream()) |
| 132 | + .distinct() |
| 133 | + .collect(Collectors.toMap(EditorConfigProperty::getName, property -> property)); |
| 134 | + |
| 135 | + // The default style had been changed from intellij_idea to ktlint_official in version 1.0.0 |
| 136 | + if (!editorConfigOverrideMap.containsKey("ktlint_code_style")) { |
| 137 | + editorConfigOverrideMap.put("ktlint_code_style", "intellij_idea"); |
| 138 | + } |
| 139 | + |
| 140 | + // Create config properties based on provided property names and values |
| 141 | + @SuppressWarnings("unchecked") |
| 142 | + Pair<EditorConfigProperty<?>, ?>[] properties = editorConfigOverrideMap.entrySet().stream() |
| 143 | + .map(entry -> { |
| 144 | + EditorConfigProperty<?> property = supportedProperties.get(entry.getKey()); |
| 145 | + |
| 146 | + if (property == null && entry.getKey().startsWith("ktlint_")) { |
| 147 | + String[] parts = entry.getKey().substring(7).split("_", 2); |
| 148 | + if (parts.length == 1) { |
| 149 | + // convert ktlint_{ruleset} to RuleSetId |
| 150 | + RuleSetId id = new RuleSetId(parts[0]); |
| 151 | + property = RuleExecutionEditorConfigPropertyKt.createRuleSetExecutionEditorConfigProperty(id, RuleExecution.enabled); |
| 152 | + } else { |
| 153 | + // convert ktlint_{ruleset}_{rulename} to RuleId |
| 154 | + RuleId id = new RuleId(parts[0] + ":" + parts[1]); |
| 155 | + property = RuleExecutionEditorConfigPropertyKt.createRuleExecutionEditorConfigProperty(id, RuleExecution.enabled); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + if (property == null) { |
| 160 | + return null; |
| 161 | + } else { |
| 162 | + return new Pair<>(property, entry.getValue()); |
| 163 | + } |
| 164 | + }) |
| 165 | + .filter(Objects::nonNull) |
| 166 | + .toArray(Pair[]::new); |
| 167 | + |
| 168 | + return EditorConfigOverride.Companion.from(properties); |
| 169 | + } |
| 170 | +} |
0 commit comments