Skip to content

Make ktlint formatters read the string content #2302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Fixed
* `ktlint` steps now read from the `string` instead of the `file` so they don't clobber earlier steps. (fixes [#1599](https://github.com/diffplug/spotless/issues/1599))

## [3.0.0.BETA3] - 2024-10-15
### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 DiffPlug
* Copyright 2023-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -74,6 +74,7 @@ public Unit invoke(LintError lint, Boolean corrected) {

@Override
public String format(
String unix,
Path path,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
Expand Down Expand Up @@ -105,7 +106,7 @@ public String format(
editorConfig,
editorConfigOverride,
false)
.format(path, formatterCallback);
.format(unix, path, formatterCallback);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public Unit invoke(LintError lint, Boolean corrected) {

@Override
public String format(
String unix,
Path path,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
Expand All @@ -146,13 +147,16 @@ public String format(
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleProviderKt.propertyTypes(allRuleProviders));
}

// create Code and then set the content to match previous steps in the Spotless pipeline
Code code = Code.Companion.fromPath(path);
KtLintCompatAdapter.setCodeContent(code, unix);
return new KtLintRuleEngine(
allRuleProviders,
editorConfig,
editorConfigOverride,
false,
path.getFileSystem())
.format(Code.Companion.fromPath(path), formatterCallback);
.format(code, formatterCallback);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public Unit invoke(LintError lint, Boolean corrected) {

@Override
public String format(
String unix,
Path path,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
Expand All @@ -108,14 +109,17 @@ public String format(
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, RuleProviderKt.propertyTypes(allRuleProviders));
}

// create Code and then set the content to match previous steps in the Spotless pipeline
Code code = Code.Companion.fromPath(path);
KtLintCompatAdapter.setCodeContent(code, unix);
return new KtLintRuleEngine(
allRuleProviders,
editorConfig,
editorConfigOverride,
true,
false,
path.getFileSystem())
.format(Code.Companion.fromPath(path), formatterCallback);
.format(code, formatterCallback);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public Unit invoke(LintError lint, Boolean corrected) {

@Override
public String format(
String unix,
Path path,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap) {
Expand All @@ -103,13 +104,16 @@ public String format(
editorConfigOverrideMap);
}

// create Code and then set the content to match previous steps in the Spotless pipeline
Code code = Code.Companion.fromPath(path);
KtLintCompatAdapter.setCodeContent(code, unix);
return new KtLintRuleEngine(
allRuleProviders,
editorConfig,
editorConfigOverride,
false,
path.getFileSystem())
.format(Code.Companion.fromPath(path), formatterCallback);
.format(code, formatterCallback);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2023 DiffPlug
* Copyright 2022-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,13 +15,31 @@
*/
package com.diffplug.spotless.glue.ktlint.compat;

import java.lang.reflect.Field;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;

public interface KtLintCompatAdapter {

String format(
String content,
Path path,
Path editorConfigPath,
Map<String, Object> editorConfigOverrideMap);
Map<String, Object> editorConfigOverrideMap) throws NoSuchFieldException, IllegalAccessException;

static void setCodeContent(Object code, String content) {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
try {
Field contentField = code.getClass().getDeclaredField("content");
contentField.setAccessible(true);
contentField.set(code, content);
} catch (NoSuchFieldException | IllegalAccessException e) {
// Handle exceptions as needed
throw new RuntimeException("Failed to set content field", e);
}
return null;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 DiffPlug
* Copyright 2021-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,12 +60,13 @@ public KtlintFormatterFunc(
}

@Override
public String applyWithFile(String unix, File file) {
public String applyWithFile(String unix, File file) throws NoSuchFieldException, IllegalAccessException {
Path absoluteEditorConfigPath = null;
if (editorConfigPath != null) {
absoluteEditorConfigPath = editorConfigPath.getOnlyFile().toPath();
}
return adapter.format(
unix,
file.toPath(),
absoluteEditorConfigPath,
editorConfigOverrideMap);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 DiffPlug
* Copyright 2023-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,19 +33,19 @@ public class KtLintCompat0Dot48Dot0AdapterTest {
@Test
public void testDefaults(@TempDir Path path) throws IOException {
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter();
loadAndWriteText(path, "empty_class_body.kt");
var content = loadAndWriteText(path, "empty_class_body.kt");
final Path filePath = Paths.get(path.toString(), "empty_class_body.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();

String formatted = ktLintCompat0Dot48Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
String formatted = ktLintCompat0Dot48Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
assertEquals("class empty_class_body\n", formatted);
}

@Test
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter();
loadAndWriteText(path, "fails_no_semicolons.kt");
var content = loadAndWriteText(path, "fails_no_semicolons.kt");
final Path filePath = Paths.get(path.toString(), "fails_no_semicolons.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();
Expand All @@ -54,7 +54,7 @@ public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
// ktlint_filename is an invalid rule in ktlint 0.48.0
editorConfigOverrideMap.put("ktlint_filename", "disabled");

String formatted = ktLintCompat0Dot48Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
String formatted = ktLintCompat0Dot48Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
assertEquals("class fails_no_semicolons {\n\tval i = 0;\n}\n", formatted);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 DiffPlug
* Copyright 2023-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,26 +33,26 @@ public class KtLintCompat0Dot49Dot0AdapterTest {
@Test
public void testDefaults(@TempDir Path path) throws IOException {
KtLintCompat0Dot49Dot0Adapter ktLintCompat0Dot49Dot0Adapter = new KtLintCompat0Dot49Dot0Adapter();
loadAndWriteText(path, "EmptyClassBody.kt");
var content = loadAndWriteText(path, "EmptyClassBody.kt");
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();

String formatted = ktLintCompat0Dot49Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
String formatted = ktLintCompat0Dot49Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
assertEquals("class EmptyClassBody\n", formatted);
}

@Test
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
KtLintCompat0Dot49Dot0Adapter ktLintCompat0Dot49Dot0Adapter = new KtLintCompat0Dot49Dot0Adapter();
loadAndWriteText(path, "FailsNoSemicolons.kt");
var content = loadAndWriteText(path, "FailsNoSemicolons.kt");
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();
editorConfigOverrideMap.put("indent_style", "tab");
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");

String formatted = ktLintCompat0Dot49Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
String formatted = ktLintCompat0Dot49Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 DiffPlug
* Copyright 2023-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,26 +33,26 @@ public class KtLintCompat0Dot50Dot0AdapterTest {
@Test
public void testDefaults(@TempDir Path path) throws IOException {
KtLintCompat0Dot50Dot0Adapter KtLintCompat0Dot50Dot0Adapter = new KtLintCompat0Dot50Dot0Adapter();
loadAndWriteText(path, "EmptyClassBody.kt");
var content = loadAndWriteText(path, "EmptyClassBody.kt");
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();

String formatted = KtLintCompat0Dot50Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
String formatted = KtLintCompat0Dot50Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
assertEquals("class EmptyClassBody\n", formatted);
}

@Test
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
KtLintCompat0Dot50Dot0Adapter KtLintCompat0Dot50Dot0Adapter = new KtLintCompat0Dot50Dot0Adapter();
loadAndWriteText(path, "FailsNoSemicolons.kt");
var content = loadAndWriteText(path, "FailsNoSemicolons.kt");
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();
editorConfigOverrideMap.put("indent_style", "tab");
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");

String formatted = KtLintCompat0Dot50Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
String formatted = KtLintCompat0Dot50Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 DiffPlug
* Copyright 2023-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,26 +33,26 @@ public class KtLintCompat1Dot0Dot0AdapterTest {
@Test
public void testDefaults(@TempDir Path path) throws IOException {
KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter();
loadAndWriteText(path, "EmptyClassBody.kt");
var content = loadAndWriteText(path, "EmptyClassBody.kt");
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();

String formatted = KtLintCompat1Dot0Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
String formatted = KtLintCompat1Dot0Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
assertEquals("class EmptyClassBody\n", formatted);
}

@Test
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter();
loadAndWriteText(path, "FailsNoSemicolons.kt");
var content = loadAndWriteText(path, "FailsNoSemicolons.kt");
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt");

Map<String, Object> editorConfigOverrideMap = new HashMap<>();
editorConfigOverrideMap.put("indent_style", "tab");
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");

String formatted = KtLintCompat1Dot0Dot0Adapter.format(filePath, null, editorConfigOverrideMap);
String formatted = KtLintCompat1Dot0Dot0Adapter.format(content, filePath, null, editorConfigOverrideMap);
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted);
}

Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Fixed
* `ktlint` steps now read from the `string` instead of the `file` so they don't clobber earlier steps. (fixes [#1599](https://github.com/diffplug/spotless/issues/1599))

## [7.0.0.BETA3] - 2024-10-15
### Added
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Fixed
* `ktlint` steps now read from the `string` instead of the `file` so they don't clobber earlier steps. (fixes [#1599](https://github.com/diffplug/spotless/issues/1599))

## [2.44.0.BETA3] - 2024-10-15
### Added
Expand Down
Loading