-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
16 changed files
with
315 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
...1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* 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.ktlint.compat; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.ServiceLoader; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3; | ||
import com.pinterest.ktlint.rule.engine.api.Code; | ||
import com.pinterest.ktlint.rule.engine.api.EditorConfigDefaults; | ||
import com.pinterest.ktlint.rule.engine.api.EditorConfigOverride; | ||
import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine; | ||
import com.pinterest.ktlint.rule.engine.api.LintError; | ||
import com.pinterest.ktlint.rule.engine.core.api.Rule; | ||
import com.pinterest.ktlint.rule.engine.core.api.RuleId; | ||
import com.pinterest.ktlint.rule.engine.core.api.RuleProvider; | ||
import com.pinterest.ktlint.rule.engine.core.api.RuleSetId; | ||
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.CodeStyleEditorConfigPropertyKt; | ||
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfigProperty; | ||
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EndOfLinePropertyKt; | ||
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.IndentSizeEditorConfigPropertyKt; | ||
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.IndentStyleEditorConfigPropertyKt; | ||
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.InsertFinalNewLineEditorConfigPropertyKt; | ||
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MaxLineLengthEditorConfigPropertyKt; | ||
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RuleExecution; | ||
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RuleExecutionEditorConfigPropertyKt; | ||
|
||
import kotlin.Pair; | ||
import kotlin.Unit; | ||
import kotlin.jvm.functions.Function2; | ||
|
||
public class KtLintCompat1Dot0Dot0Adapter implements KtLintCompatAdapter { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(KtLintCompat1Dot0Dot0Adapter.class); | ||
|
||
private static final List<EditorConfigProperty<?>> DEFAULT_EDITOR_CONFIG_PROPERTIES; | ||
|
||
static { | ||
List<EditorConfigProperty<?>> list = new ArrayList<>(); | ||
list.add(CodeStyleEditorConfigPropertyKt.getCODE_STYLE_PROPERTY()); | ||
list.add(EndOfLinePropertyKt.getEND_OF_LINE_PROPERTY()); | ||
list.add(IndentSizeEditorConfigPropertyKt.getINDENT_SIZE_PROPERTY()); | ||
list.add(IndentStyleEditorConfigPropertyKt.getINDENT_STYLE_PROPERTY()); | ||
list.add(InsertFinalNewLineEditorConfigPropertyKt.getINSERT_FINAL_NEWLINE_PROPERTY()); | ||
list.add(MaxLineLengthEditorConfigPropertyKt.getMAX_LINE_LENGTH_PROPERTY()); | ||
list.add(RuleExecutionEditorConfigPropertyKt.getEXPERIMENTAL_RULES_EXECUTION_PROPERTY()); | ||
DEFAULT_EDITOR_CONFIG_PROPERTIES = Collections.unmodifiableList(list); | ||
} | ||
|
||
static class FormatterCallback implements Function2<LintError, Boolean, Unit> { | ||
|
||
@Override | ||
public Unit invoke(LintError lint, Boolean corrected) { | ||
if (!corrected) { | ||
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId().getValue(), lint.getDetail()); | ||
} | ||
return Unit.INSTANCE; | ||
} | ||
} | ||
|
||
@Override | ||
public String format(final String text, Path path, final boolean isScript, | ||
Path editorConfigPath, final Map<String, String> userData, | ||
final Map<String, Object> editorConfigOverrideMap) { | ||
final FormatterCallback formatterCallback = new FormatterCallback(); | ||
|
||
Set<RuleProvider> allRuleProviders = ServiceLoader.load(RuleSetProviderV3.class, RuleSetProviderV3.class.getClassLoader()) | ||
.stream() | ||
.flatMap(loader -> loader.get().getRuleProviders().stream()) | ||
.collect(Collectors.toUnmodifiableSet()); | ||
|
||
EditorConfigOverride editorConfigOverride; | ||
if (editorConfigOverrideMap.isEmpty()) { | ||
editorConfigOverride = EditorConfigOverride.Companion.getEMPTY_EDITOR_CONFIG_OVERRIDE(); | ||
} else { | ||
editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map( | ||
RuleProvider::createNewRuleInstance).collect(Collectors.toList()), | ||
editorConfigOverrideMap); | ||
} | ||
EditorConfigDefaults editorConfig; | ||
if (editorConfigPath == null || !Files.exists(editorConfigPath)) { | ||
editorConfig = EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS(); | ||
} else { | ||
editorConfig = EditorConfigDefaults.Companion.load(editorConfigPath, Collections.emptySet()); | ||
} | ||
|
||
return new KtLintRuleEngine( | ||
allRuleProviders, | ||
editorConfig, | ||
editorConfigOverride, | ||
false, | ||
path.getFileSystem()) | ||
.format(Code.Companion.fromPath(path), formatterCallback); | ||
} | ||
|
||
/** | ||
* Create EditorConfigOverride from user provided parameters. | ||
*/ | ||
private static EditorConfigOverride createEditorConfigOverride(final List<Rule> rules, Map<String, Object> editorConfigOverrideMap) { | ||
// Get properties from rules in the rule sets | ||
Stream<EditorConfigProperty<?>> ruleProperties = rules.stream() | ||
.flatMap(rule -> rule.getUsesEditorConfigProperties().stream()); | ||
|
||
// Create a mapping of properties to their names based on rule properties and default properties | ||
Map<String, EditorConfigProperty<?>> supportedProperties = Stream | ||
.concat(ruleProperties, DEFAULT_EDITOR_CONFIG_PROPERTIES.stream()) | ||
.distinct() | ||
.collect(Collectors.toMap(EditorConfigProperty::getName, property -> property)); | ||
|
||
// The default style had been changed from intellij_idea to ktlint_official in version 1.0.0 | ||
if (!editorConfigOverrideMap.containsKey("ktlint_code_style")) { | ||
editorConfigOverrideMap.put("ktlint_code_style", "intellij_idea"); | ||
} | ||
|
||
// Create config properties based on provided property names and values | ||
@SuppressWarnings("unchecked") | ||
Pair<EditorConfigProperty<?>, ?>[] properties = editorConfigOverrideMap.entrySet().stream() | ||
.map(entry -> { | ||
EditorConfigProperty<?> property = supportedProperties.get(entry.getKey()); | ||
|
||
if (property == null && entry.getKey().startsWith("ktlint_")) { | ||
String[] parts = entry.getKey().substring(7).split("_", 2); | ||
if (parts.length == 1) { | ||
// convert ktlint_{ruleset} to RuleSetId | ||
RuleSetId id = new RuleSetId(parts[0]); | ||
property = RuleExecutionEditorConfigPropertyKt.createRuleSetExecutionEditorConfigProperty(id, RuleExecution.enabled); | ||
} else { | ||
// convert ktlint_{ruleset}_{rulename} to RuleId | ||
RuleId id = new RuleId(parts[0] + ":" + parts[1]); | ||
property = RuleExecutionEditorConfigPropertyKt.createRuleExecutionEditorConfigProperty(id, RuleExecution.enabled); | ||
} | ||
} | ||
|
||
if (property == null) { | ||
return null; | ||
} else { | ||
return new Pair<>(property, entry.getValue()); | ||
} | ||
}) | ||
.filter(Objects::nonNull) | ||
.toArray(Pair[]::new); | ||
|
||
return EditorConfigOverride.Companion.from(properties); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0AdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.ktlint.compat; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
public class KtLintCompat1Dot0Dot0AdapterTest { | ||
@Test | ||
public void testDefaults(@TempDir Path path) throws IOException { | ||
KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter(); | ||
String text = loadAndWriteText(path, "EmptyClassBody.kt"); | ||
final Path filePath = Paths.get(path.toString(), "EmptyClassBody.kt"); | ||
|
||
Map<String, String> userData = new HashMap<>(); | ||
|
||
Map<String, Object> editorConfigOverrideMap = new HashMap<>(); | ||
|
||
String formatted = KtLintCompat1Dot0Dot0Adapter.format(text, filePath, false, null, userData, editorConfigOverrideMap); | ||
assertEquals("class EmptyClassBody\n", formatted); | ||
} | ||
|
||
@Test | ||
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException { | ||
KtLintCompat1Dot0Dot0Adapter KtLintCompat1Dot0Dot0Adapter = new KtLintCompat1Dot0Dot0Adapter(); | ||
String text = loadAndWriteText(path, "FailsNoSemicolons.kt"); | ||
final Path filePath = Paths.get(path.toString(), "FailsNoSemicolons.kt"); | ||
|
||
Map<String, String> userData = new HashMap<>(); | ||
|
||
Map<String, Object> editorConfigOverrideMap = new HashMap<>(); | ||
editorConfigOverrideMap.put("indent_style", "tab"); | ||
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled"); | ||
|
||
String formatted = KtLintCompat1Dot0Dot0Adapter.format(text, filePath, false, null, userData, editorConfigOverrideMap); | ||
assertEquals("class FailsNoSemicolons {\n\tval i = 0;\n}\n", formatted); | ||
} | ||
|
||
private static String loadAndWriteText(Path path, String name) throws IOException { | ||
try (InputStream is = KtLintCompat1Dot0Dot0AdapterTest.class.getResourceAsStream("/" + name)) { | ||
Files.copy(is, path.resolve(name)); | ||
} | ||
return new String(Files.readAllBytes(path.resolve(name)), StandardCharsets.UTF_8); | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
lib/src/testCompatKtLint1Dot0Dot0/resources/EmptyClassBody.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class EmptyClassBody { | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
lib/src/testCompatKtLint1Dot0Dot0/resources/FailsNoSemicolons.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class FailsNoSemicolons { | ||
val i = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.