|
| 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.diktat.compat; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.FileInputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.InputStream; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.jetbrains.annotations.NotNull; |
| 27 | +import org.jetbrains.annotations.Nullable; |
| 28 | + |
| 29 | +import com.saveourtool.diktat.DiktatFactoriesKt; |
| 30 | +import com.saveourtool.diktat.DiktatProcessor; |
| 31 | +import com.saveourtool.diktat.api.DiktatCallback; |
| 32 | +import com.saveourtool.diktat.api.DiktatError; |
| 33 | +import com.saveourtool.diktat.api.DiktatRuleConfig; |
| 34 | +import com.saveourtool.diktat.api.DiktatRuleSet; |
| 35 | + |
| 36 | +import kotlin.Unit; |
| 37 | + |
| 38 | +public class DiktatCompat2Dot0Dot0Adapter implements DiktatCompatAdapter { |
| 39 | + private final DiktatProcessor processor; |
| 40 | + private final DiktatCallback formatterCallback; |
| 41 | + private final ArrayList<DiktatError> errors = new ArrayList<>(); |
| 42 | + |
| 43 | + public DiktatCompat2Dot0Dot0Adapter(@Nullable File configFile) { |
| 44 | + this.processor = getDiktatReporter(configFile); |
| 45 | + this.formatterCallback = new FormatterCallback(errors); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String format(File file, String content, boolean isScript) { |
| 50 | + errors.clear(); |
| 51 | + String result = processor.fix(content, file.toPath(), formatterCallback); |
| 52 | + DiktatReporting.reportIfRequired(errors, DiktatError::getLine, DiktatError::getCol, DiktatError::getDetail); |
| 53 | + return result; |
| 54 | + } |
| 55 | + |
| 56 | + private static class FormatterCallback implements DiktatCallback { |
| 57 | + private final ArrayList<DiktatError> errors; |
| 58 | + |
| 59 | + FormatterCallback(ArrayList<DiktatError> errors) { |
| 60 | + this.errors = errors; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Unit invoke(DiktatError diktatError, Boolean corrected) { |
| 65 | + doInvoke(diktatError, corrected); |
| 66 | + return Unit.INSTANCE; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void invoke(@NotNull DiktatError diktatError, boolean corrected) { |
| 71 | + doInvoke(diktatError, corrected); |
| 72 | + } |
| 73 | + |
| 74 | + private void doInvoke(@NotNull DiktatError diktatError, boolean corrected) { |
| 75 | + if (!corrected) { |
| 76 | + errors.add(diktatError); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static DiktatProcessor getDiktatReporter(File configFile) { |
| 82 | + final DiktatRuleSet ruleSet = DiktatFactoriesKt.getDiktatRuleSetFactory().invoke(readRuleConfigs(configFile)); |
| 83 | + return DiktatFactoriesKt.getDiktatProcessorFactory().invoke(ruleSet); |
| 84 | + } |
| 85 | + |
| 86 | + private static List<DiktatRuleConfig> readRuleConfigs(File configFile) { |
| 87 | + if (configFile == null) { |
| 88 | + return Collections.emptyList(); |
| 89 | + } |
| 90 | + try (final InputStream configInputStream = new FileInputStream(configFile)) { |
| 91 | + return DiktatFactoriesKt.getDiktatRuleConfigReader().invoke(configInputStream); |
| 92 | + } catch (IOException e) { |
| 93 | + throw new IllegalArgumentException("Fail to read configFile", e); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments