|
| 1 | +/* |
| 2 | + * Copyright 2021 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; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.logging.Logger; |
| 23 | + |
| 24 | +import com.pinterest.ktlint.core.KtLint; |
| 25 | +import com.pinterest.ktlint.core.KtLint.Params; |
| 26 | +import com.pinterest.ktlint.core.LintError; |
| 27 | +import com.pinterest.ktlint.core.RuleSet; |
| 28 | +import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider; |
| 29 | + |
| 30 | +import com.diffplug.spotless.FormatterFunc; |
| 31 | + |
| 32 | +import kotlin.Unit; |
| 33 | +import kotlin.jvm.functions.Function2; |
| 34 | + |
| 35 | +public class KtlintFormatterFunc implements FormatterFunc.NeedsFile { |
| 36 | + private static final Logger logger = Logger.getLogger(KtlintFormatterFunc.class.getName()); |
| 37 | + |
| 38 | + private final List<RuleSet> rulesets; |
| 39 | + private final Map<String, String> userData; |
| 40 | + private final Function2<? super LintError, ? super Boolean, Unit> formatterCallback; |
| 41 | + private final boolean isScript; |
| 42 | + |
| 43 | + public KtlintFormatterFunc(boolean isScript, Map<String, String> userData) { |
| 44 | + rulesets = Collections.singletonList(new StandardRuleSetProvider().get()); |
| 45 | + this.userData = userData; |
| 46 | + formatterCallback = new FormatterCallback(); |
| 47 | + this.isScript = isScript; |
| 48 | + } |
| 49 | + |
| 50 | + static class FormatterCallback implements Function2<LintError, Boolean, Unit> { |
| 51 | + @Override |
| 52 | + public Unit invoke(LintError lint, Boolean corrected) { |
| 53 | + if (!corrected) { |
| 54 | + throw new AssertionError("Error on line: " + lint.getLine() + ", column: " + lint.getCol() + "\n" + lint.getDetail()); |
| 55 | + } |
| 56 | + return null; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String applyWithFile(String unix, File file) throws Exception { |
| 62 | + return KtLint.INSTANCE.format(new Params( |
| 63 | + file.getName(), |
| 64 | + unix, |
| 65 | + rulesets, |
| 66 | + userData, |
| 67 | + formatterCallback, |
| 68 | + isScript, |
| 69 | + null, |
| 70 | + false)); |
| 71 | + } |
| 72 | +} |
0 commit comments