Skip to content

Commit

Permalink
Merge branch 'main' into tylercrawford/issue-1567/support-sh-bash
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Jan 15, 2024
2 parents 6ebed55 + 350cf2e commit 0f36d0f
Show file tree
Hide file tree
Showing 27 changed files with 474 additions and 147 deletions.
8 changes: 7 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* New static method to `DiffMessageFormatter` which allows to retrieve diffs with their line numbers ([#1960](https://github.com/diffplug/spotless/issues/1960))
* Gradle support for shell via [shfmt](https://github.com/mvdan/sh). ([#1994](https://github.com/diffplug/spotless/pull/1994))
* Format shell via [shfmt](https://github.com/mvdan/sh). ([#1994](https://github.com/diffplug/spotless/pull/1994))
### Fixed
* Fix empty files with biome >= 1.5.0 when formatting files that are in the ignore list of the biome configuration file. ([#1989](https://github.com/diffplug/spotless/pull/1989) fixes [#1987](https://github.com/diffplug/spotless/issues/1987))
* Fix a regression in BufStep where the same arguments were being provided to every `buf` invocation. ([#1976](https://github.com/diffplug/spotless/issues/1976))
### Changes
* Use palantir-java-format 2.39.0 on Java 21. ([#1948](https://github.com/diffplug/spotless/pull/1948))
* Bump default `ktlint` version to latest `1.0.1` -> `1.1.1`. ([#1973](https://github.com/diffplug/spotless/pull/1973))
* Bump default `googleJavaFormat` version to latest `1.18.1` -> `1.19.2`. ([#1971](https://github.com/diffplug/spotless/pull/1971))
* Bump default `diktat` version to latest `1.2.5` -> `2.0.0`. ([#1972](https://github.com/diffplug/spotless/pull/1972))

## [2.43.1] - 2023-12-04
### Fixed
Expand Down
20 changes: 20 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,26 @@ The gist of it is that you will have to:

If you get something running, we'd love to host your plugin within this repo as a peer to `plugin-gradle` and `plugin-maven`.

## Run tests

To run all tests, simply do

> gradlew test
Since that takes some time, you might only want to run the tests
concerning what you are working on:

```shell
# Run only from test from the "lib" project
gradlew :testlib:test --tests com.diffplug.spotless.generic.IndentStepTest

# Run only one test from the "plugin-maven" project
gradlew :plugin-maven:test --tests com.diffplug.spotless.maven.pom.SortPomMavenTest

# Run only one test from the "plugin-gradle" project
gradlew :plugin-gradle:test --tests com.diffplug.gradle.spotless.FreshMarkExtensionTest
```

## Integration testing

### Gradle - locally
Expand Down
15 changes: 12 additions & 3 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ versionCompatibility {
]
targetSourceSetName = 'ktlint'
}
namespaces.register('Diktat') {
versions = [
'1.2.5',
'2.0.0',
]
targetSourceSetName = 'diktat'
}
}
}

Expand All @@ -76,15 +83,17 @@ dependencies {
String VER_CLEANTHAT='2.17'
cleanthatCompileOnly "io.github.solven-eu.cleanthat:java:$VER_CLEANTHAT"
compatCleanthat2Dot1CompileAndTestOnly "io.github.solven-eu.cleanthat:java:$VER_CLEANTHAT"
// diktat
diktatCompileOnly 'org.cqfn.diktat:diktat-rules:1.2.5'
// diktat old supported version 1.x
compatDiktat1Dot2Dot5CompileOnly "org.cqfn.diktat:diktat-rules:1.2.5"
// diktat latest supported version 2.x
compatDiktat2Dot0Dot0CompileOnly "com.saveourtool.diktat:diktat-runner:2.0.0"
// flexmark
flexmarkCompileOnly 'com.vladsch.flexmark:flexmark-all:0.64.0'
// gherkin
gherkinCompileOnly 'io.cucumber:gherkin-utils:8.0.2'
gherkinCompileOnly 'org.slf4j:slf4j-api:2.0.0'
// googleJavaFormat
googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.18.1'
googleJavaFormatCompileOnly 'com.google.googlejavaformat:google-java-format:1.19.2'
// gson
gsonCompileOnly 'com.google.code.gson:gson:2.10.1'
// jackson
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.glue.diktat.compat;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Nullable;

import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider;

import com.pinterest.ktlint.core.KtLint;
import com.pinterest.ktlint.core.LintError;
import com.pinterest.ktlint.core.RuleSet;
import com.pinterest.ktlint.core.api.EditorConfigOverride;

import kotlin.Unit;
import kotlin.jvm.functions.Function2;

public class DiktatCompat1Dot2Dot5Adapter implements DiktatCompatAdapter {
private final List<RuleSet> ruleSets;
private final Function2<? super LintError, ? super Boolean, Unit> formatterCallback;
private final ArrayList<LintError> errors = new ArrayList<>();

public DiktatCompat1Dot2Dot5Adapter(@Nullable File configFile) {
if (configFile != null) {
System.setProperty("diktat.config.path", configFile.getAbsolutePath());
}
this.ruleSets = Collections.singletonList(new DiktatRuleSetProvider().get());
this.formatterCallback = new FormatterCallback(errors);
}

static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
private final ArrayList<LintError> errors;

FormatterCallback(ArrayList<LintError> errors) {
this.errors = errors;
}

@Override
public Unit invoke(LintError lintError, Boolean corrected) {
if (!corrected) {
errors.add(lintError);
}
return null;
}
}

@Override
public String format(final File file, final String content, final boolean isScript) {
errors.clear();
String result = KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
// Unlike Ktlint, Diktat requires full path to the file.
// See https://github.com/diffplug/spotless/issues/1189, https://github.com/analysis-dev/diktat/issues/1202
file.getAbsolutePath(),
content,
ruleSets,
Collections.emptyMap(),
formatterCallback,
isScript,
null,
false,
new EditorConfigOverride(),
false));

DiktatReporting.reportIfRequired(errors, LintError::getLine, LintError::getCol, LintError::getDetail);

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.glue.diktat.compat;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import com.saveourtool.diktat.DiktatFactoriesKt;
import com.saveourtool.diktat.DiktatProcessor;
import com.saveourtool.diktat.api.DiktatCallback;
import com.saveourtool.diktat.api.DiktatError;
import com.saveourtool.diktat.api.DiktatRuleConfig;
import com.saveourtool.diktat.api.DiktatRuleSet;

import kotlin.Unit;

public class DiktatCompat2Dot0Dot0Adapter implements DiktatCompatAdapter {
private final DiktatProcessor processor;
private final DiktatCallback formatterCallback;
private final ArrayList<DiktatError> errors = new ArrayList<>();

public DiktatCompat2Dot0Dot0Adapter(@Nullable File configFile) {
this.processor = getDiktatReporter(configFile);
this.formatterCallback = new FormatterCallback(errors);
}

@Override
public String format(File file, String content, boolean isScript) {
errors.clear();
String result = processor.fix(content, file.toPath(), formatterCallback);
DiktatReporting.reportIfRequired(errors, DiktatError::getLine, DiktatError::getCol, DiktatError::getDetail);
return result;
}

private static class FormatterCallback implements DiktatCallback {
private final ArrayList<DiktatError> errors;

FormatterCallback(ArrayList<DiktatError> errors) {
this.errors = errors;
}

@Override
public Unit invoke(DiktatError diktatError, Boolean corrected) {
doInvoke(diktatError, corrected);
return Unit.INSTANCE;
}

@Override
public void invoke(@NotNull DiktatError diktatError, boolean corrected) {
doInvoke(diktatError, corrected);
}

private void doInvoke(@NotNull DiktatError diktatError, boolean corrected) {
if (!corrected) {
errors.add(diktatError);
}
}
}

private static DiktatProcessor getDiktatReporter(File configFile) {
final DiktatRuleSet ruleSet = DiktatFactoriesKt.getDiktatRuleSetFactory().invoke(readRuleConfigs(configFile));
return DiktatFactoriesKt.getDiktatProcessorFactory().invoke(ruleSet);
}

private static List<DiktatRuleConfig> readRuleConfigs(File configFile) {
if (configFile == null) {
return Collections.emptyList();
}
try (final InputStream configInputStream = new FileInputStream(configFile)) {
return DiktatFactoriesKt.getDiktatRuleConfigReader().invoke(configInputStream);
} catch (IOException e) {
throw new IllegalArgumentException("Fail to read configFile", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.glue.diktat.compat;

import java.io.File;

public interface DiktatCompatAdapter {
String format(File file, String content, boolean isScript);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.glue.diktat.compat;

import java.util.List;
import java.util.function.Function;
import java.util.function.ToIntFunction;

interface DiktatReporting {
static <T> void reportIfRequired(
List<T> errors,
ToIntFunction<T> lineGetter,
ToIntFunction<T> columnGetter,
Function<T, String> detailGetter) {
if (!errors.isEmpty()) {
StringBuilder error = new StringBuilder();
error.append("There are ").append(errors.size()).append(" unfixed errors:");
for (T er : errors) {
error.append(System.lineSeparator())
.append("Error on line: ").append(lineGetter.applyAsInt(er))
.append(", column: ").append(columnGetter.applyAsInt(er))
.append(" cannot be fixed automatically")
.append(System.lineSeparator())
.append(detailGetter.apply(er));
}
throw new AssertionError(error);
}
}
}
Loading

0 comments on commit 0f36d0f

Please sign in to comment.