diff --git a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatProcessor.kt b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatProcessor.kt index 3b12d2ff9d..d316089311 100644 --- a/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatProcessor.kt +++ b/diktat-api/src/main/kotlin/com/saveourtool/diktat/DiktatProcessor.kt @@ -20,13 +20,13 @@ interface DiktatProcessor { * Run `diktat fix` on provided [code] using [callback] for detected errors and returned formatted code. * * @param code - * @param isScript + * @param virtualPath a path which should be taken into account in processing of [code] * @param callback * @return result of `diktat fix` */ fun fix( code: String, - isScript: Boolean, + virtualPath: Path?, callback: DiktatCallback, ): String @@ -42,12 +42,12 @@ interface DiktatProcessor { * Run `diktat check` on provided [code] using [callback] for detected errors. * * @param code - * @param isScript + * @param virtualPath a path which should be taken into account in processing of [code] * @param callback */ fun check( code: String, - isScript: Boolean, + virtualPath: Path?, callback: DiktatCallback, ) } diff --git a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt index 6712156bfa..55cd7208c6 100644 --- a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt +++ b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/DiktatProcessorFactoryImpl.kt @@ -14,6 +14,7 @@ import org.ec4j.core.model.Glob import org.ec4j.core.model.Property import org.ec4j.core.model.PropertyType import java.nio.file.Path +import kotlin.io.path.name private typealias LintCallback = (LintError) -> Unit @@ -30,18 +31,18 @@ class DiktatProcessorFactoryImpl : DiktatProcessorFactory { ): String = ktLintRuleEngine.formatSilentlyThenLint(file.toKtLint(), callback.toKtLintForLint()) override fun fix( code: String, - isScript: Boolean, + virtualPath: Path?, callback: DiktatCallback - ): String = ktLintRuleEngine.formatSilentlyThenLint(code.toKtLint(isScript), callback.toKtLintForLint()) + ): String = ktLintRuleEngine.formatSilentlyThenLint(code.toKtLint(virtualPath), callback.toKtLintForLint()) override fun check( file: Path, callback: DiktatCallback, ) = ktLintRuleEngine.lint(file.toKtLint(), callback.toKtLintForLint()) override fun check( code: String, - isScript: Boolean, + virtualPath: Path?, callback: DiktatCallback - ) = ktLintRuleEngine.lint(code.toKtLint(isScript), callback.toKtLintForLint()) + ) = ktLintRuleEngine.lint(code.toKtLint(virtualPath), callback.toKtLintForLint()) } } @@ -66,7 +67,13 @@ class DiktatProcessorFactoryImpl : DiktatProcessorFactory { private fun Path.toKtLint(): Code = Code.fromFile(this.toFile()) - private fun String.toKtLint(isScript: Boolean): Code = Code.fromSnippet(this, isScript) + private fun String.toKtLint(virtualPath: Path?): Code = Code( + content = this, + fileName = virtualPath?.name, + filePath = virtualPath, + script = virtualPath?.name?.endsWith(".kts", ignoreCase = true) ?: false, + isStdIn = virtualPath == null, + ) private fun DiktatCallback.toKtLintForLint(): LintCallback = { error -> this(error.wrap(), false) diff --git a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintUtils.kt b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintUtils.kt index 8ea8d04a71..a77e879957 100644 --- a/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintUtils.kt +++ b/diktat-ktlint-engine/src/main/kotlin/com/saveourtool/diktat/ktlint/KtLintUtils.kt @@ -158,10 +158,10 @@ fun format( * @param ruleSetSupplier * @param file * @param cb callback to be called on unhandled [LintError]s - * @return formatted code + * @return [Unit] */ @Suppress("LAMBDA_IS_NOT_LAST_PARAMETER") -fun lint( +fun check( ruleSetSupplier: () -> DiktatRuleSet, file: Path, cb: DiktatCallback = DiktatCallback.empty @@ -175,16 +175,16 @@ fun lint( * @param ruleSetSupplier * @param text * @param cb callback to be called on unhandled [LintError]s - * @return formatted code + * @return [Unit] */ @Suppress("LAMBDA_IS_NOT_LAST_PARAMETER") -fun lint( +fun check( ruleSetSupplier: () -> DiktatRuleSet, @Language("kotlin") text: String, cb: DiktatCallback = DiktatCallback.empty ) = DiktatProcessorFactoryImpl().invoke(ruleSetSupplier()) .check( code = text, - isScript = false, + virtualPath = null, callback = cb.ignoreCorrectedErrors(), ) diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtilsTest.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtilsTest.kt index 46406b50a5..a4e2dc84c3 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtilsTest.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/ruleset/utils/AstNodeUtilsTest.kt @@ -9,7 +9,7 @@ package com.saveourtool.diktat.ruleset.utils import com.saveourtool.diktat.api.DiktatErrorEmitter import com.saveourtool.diktat.api.DiktatRule import com.saveourtool.diktat.api.DiktatRuleSet -import com.saveourtool.diktat.ktlint.lint +import com.saveourtool.diktat.ktlint.check import com.saveourtool.diktat.util.applyToCode import org.jetbrains.kotlin.KtNodeTypes @@ -809,7 +809,7 @@ private class PrettyPrintingVisitor(private val elementType: IElementType, maxLevel: Int = -1, expected: String ) { - lint( + check( ruleSetSupplier = { DiktatRuleSet(listOf(PrettyPrintingVisitor(elementType, level, maxLevel, expected))) }, text = code, ) diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/util/LintTestBase.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/util/LintTestBase.kt index 22ea6c5977..f2f33f81e3 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/util/LintTestBase.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/util/LintTestBase.kt @@ -2,7 +2,7 @@ package com.saveourtool.diktat.util import com.saveourtool.diktat.api.DiktatCallback import com.saveourtool.diktat.common.config.rules.RulesConfig -import com.saveourtool.diktat.ktlint.lint +import com.saveourtool.diktat.ktlint.check import com.saveourtool.diktat.ruleset.rules.DiktatRule import com.saveourtool.diktat.util.DiktatRuleSetFactoryImplTest.Companion.diktatRuleSetForTest import com.saveourtool.diktat.api.DiktatError @@ -127,7 +127,7 @@ open class LintTestBase(private val ruleSupplier: (rulesConfigList: List { val lintErrors: MutableList = mutableListOf() - lint( + check( ruleSetSupplier = { rulesConfigList.toDiktatRuleSet() }, file = file, cb = lintErrors.collector(), @@ -151,7 +151,7 @@ open class LintTestBase(private val ruleSupplier: (rulesConfigList: List { val lintErrors: MutableList = mutableListOf() - lint( + check( ruleSetSupplier = { rulesConfigList.toDiktatRuleSet() }, text = code, cb = lintErrors.collector(), diff --git a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/util/TestUtils.kt b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/util/TestUtils.kt index 6387089b9b..7cb33e61e2 100644 --- a/diktat-rules/src/test/kotlin/com/saveourtool/diktat/util/TestUtils.kt +++ b/diktat-rules/src/test/kotlin/com/saveourtool/diktat/util/TestUtils.kt @@ -7,7 +7,7 @@ package com.saveourtool.diktat.util import com.saveourtool.diktat.api.DiktatErrorEmitter import com.saveourtool.diktat.api.DiktatRule import com.saveourtool.diktat.api.DiktatRuleSet -import com.saveourtool.diktat.ktlint.lint +import com.saveourtool.diktat.ktlint.check import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.fail @@ -111,7 +111,7 @@ internal fun applyToCode(@Language("kotlin") code: String, applyToNode: (node: ASTNode, counter: AtomicInteger) -> Unit ) { val counter = AtomicInteger(0) - lint( + check( ruleSetSupplier = { DiktatRuleSet(listOf(object : DiktatRule { override val id: String