diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt index 4ae3ca1780c2..258679871e31 100644 --- a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt @@ -95,6 +95,8 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() { registerFactory(AddTypeAnnotationToValueParameterFixFactory.addTypeAnnotationToValueParameterFixFactory) registerFactory(ChangeToFunctionInvocationFixFactory.changeToFunctionInvocationFixFactory) registerFactory(TypeOfAnnotationMemberFixFactory.typeOfAnnotationMemberFixFactory) + registerFactory(TooLongCharLiteralToStringFixFactory.illegalEscapeFactory) + registerFactory(TooLongCharLiteralToStringFixFactory.tooManyCharactersInCharacterLiteralFactory) } private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix { diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/TooLongCharLiteralToStringFixFactory.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/TooLongCharLiteralToStringFixFactory.kt new file mode 100644 index 000000000000..96fe768808b3 --- /dev/null +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/TooLongCharLiteralToStringFixFactory.kt @@ -0,0 +1,20 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.kotlin.idea.k2.codeinsight.fixes + +import org.jetbrains.kotlin.analysis.api.fir.diagnostics.KtFirDiagnostic +import org.jetbrains.kotlin.idea.codeinsight.api.applicators.fixes.KotlinQuickFixFactory +import org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix +import org.jetbrains.kotlin.psi.KtConstantExpression + +internal object TooLongCharLiteralToStringFixFactory { + + val illegalEscapeFactory = KotlinQuickFixFactory.ModCommandBased { diagnostic: KtFirDiagnostic.IllegalEscape -> + val element = diagnostic.psi as? KtConstantExpression ?: return@ModCommandBased emptyList() + TooLongCharLiteralToStringFix.createIfApplicable(element)?.let(::listOf) ?: emptyList() + } + + val tooManyCharactersInCharacterLiteralFactory = KotlinQuickFixFactory.ModCommandBased { diagnostic: KtFirDiagnostic.TooManyCharactersInCharacterLiteral -> + val element = diagnostic.psi as? KtConstantExpression ?: return@ModCommandBased emptyList() + TooLongCharLiteralToStringFix.createIfApplicable(element)?.let(::listOf) ?: emptyList() + } +} diff --git a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java index e910304c8959..3826090466f7 100644 --- a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java +++ b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java @@ -5951,7 +5951,48 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("../../../idea/tests/testData/quickfix/tooLongCharLiteralToString") + public static class TooLongCharLiteralToString extends AbstractHighLevelQuickFixTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + @TestMetadata("backslash.kt") + public void testBackslash() throws Exception { + runTest("../../../idea/tests/testData/quickfix/tooLongCharLiteralToString/backslash.kt"); + } + + @TestMetadata("backslashShouldNotBeEscaped.kt") + public void testBackslashShouldNotBeEscaped() throws Exception { + runTest("../../../idea/tests/testData/quickfix/tooLongCharLiteralToString/backslashShouldNotBeEscaped.kt"); + } + + @TestMetadata("dollarShouldNotBeEscaped.kt") + public void testDollarShouldNotBeEscaped() throws Exception { + runTest("../../../idea/tests/testData/quickfix/tooLongCharLiteralToString/dollarShouldNotBeEscaped.kt"); + } + + @TestMetadata("doubleQuotesShouldBeEscaped.kt") + public void testDoubleQuotesShouldBeEscaped() throws Exception { + runTest("../../../idea/tests/testData/quickfix/tooLongCharLiteralToString/doubleQuotesShouldBeEscaped.kt"); + } + + @TestMetadata("ecapedDoubleQuotesShouldNotBeEscaped.kt") + public void testEcapedDoubleQuotesShouldNotBeEscaped() throws Exception { + runTest("../../../idea/tests/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt"); + } + + @TestMetadata("startWithBackslash.kt") + public void testStartWithBackslash() throws Exception { + runTest("../../../idea/tests/testData/quickfix/tooLongCharLiteralToString/startWithBackslash.kt"); + } + + @TestMetadata("startWithBackslashInString.kt") + public void testStartWithBackslashInString() throws Exception { + runTest("../../../idea/tests/testData/quickfix/tooLongCharLiteralToString/startWithBackslashInString.kt"); + } + } diff --git a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/backslashShouldNotBeEscaped.kt b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/backslashShouldNotBeEscaped.kt index 97a6f27dd599..3e0868f1a8de 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/backslashShouldNotBeEscaped.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/backslashShouldNotBeEscaped.kt @@ -3,4 +3,5 @@ fun foo() { 'foo\nbar' } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/backslashShouldNotBeEscaped.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/backslashShouldNotBeEscaped.kt.after index 1463dc199291..ae5cfa730882 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/backslashShouldNotBeEscaped.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/backslashShouldNotBeEscaped.kt.after @@ -3,4 +3,5 @@ fun foo() { "foo\nbar" } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/dollarShouldNotBeEscaped.kt b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/dollarShouldNotBeEscaped.kt index 099377337e81..e81c93215454 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/dollarShouldNotBeEscaped.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/dollarShouldNotBeEscaped.kt @@ -4,4 +4,5 @@ fun foo() { 'foo$bar' } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/dollarShouldNotBeEscaped.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/dollarShouldNotBeEscaped.kt.after index 47a5150ec321..11f537caa409 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/dollarShouldNotBeEscaped.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/dollarShouldNotBeEscaped.kt.after @@ -4,4 +4,5 @@ fun foo() { "foo$bar" } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/doubleQuotesShouldBeEscaped.kt b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/doubleQuotesShouldBeEscaped.kt index 9b84e97d4b6d..18688e3ab0f4 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/doubleQuotesShouldBeEscaped.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/doubleQuotesShouldBeEscaped.kt @@ -3,4 +3,5 @@ fun foo() { 'foo"bar' } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/doubleQuotesShouldBeEscaped.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/doubleQuotesShouldBeEscaped.kt.after index 8ad4f1bf06fe..6a41c04d25d0 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/doubleQuotesShouldBeEscaped.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/doubleQuotesShouldBeEscaped.kt.after @@ -3,4 +3,5 @@ fun foo() { "foo\"bar" } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt index 340208dee2e4..9aebc25bebe6 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt @@ -3,4 +3,5 @@ fun foo() { 'foo\"bar' } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt.after index 8ad4f1bf06fe..6a41c04d25d0 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/ecapedDoubleQuotesShouldNotBeEscaped.kt.after @@ -3,4 +3,5 @@ fun foo() { "foo\"bar" } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/startWithBackslash.kt b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/startWithBackslash.kt index 753d65618ee8..b90b0a3da0b4 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/startWithBackslash.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/startWithBackslash.kt @@ -4,4 +4,5 @@ fun foo() { '\ bar' } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/startWithBackslash.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/startWithBackslash.kt.after index 47e4332a56d7..59f326167ac2 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/startWithBackslash.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/tooLongCharLiteralToString/startWithBackslash.kt.after @@ -4,4 +4,5 @@ fun foo() { "\ bar" } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix \ No newline at end of file diff --git a/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2QuickFixTests.kt b/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2QuickFixTests.kt index 86f693f84e07..2232bc2f9ce8 100644 --- a/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2QuickFixTests.kt +++ b/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2QuickFixTests.kt @@ -190,7 +190,7 @@ internal fun MutableTWorkspace.generateK2FixTests() { model("$idea/quickfix/suppress", pattern = pattern, isIgnored = true) model("$idea/quickfix/surroundWithNullCheck", pattern = pattern) model("$idea/quickfix/suspiciousCollectionReassignment", pattern = pattern, isIgnored = true) - model("$idea/quickfix/tooLongCharLiteralToString", pattern = pattern, isIgnored = true) + model("$idea/quickfix/tooLongCharLiteralToString", pattern = pattern) model("$idea/quickfix/typeImports", pattern = pattern, isIgnored = true) model("$idea/quickfix/typeInferenceExpectedTypeMismatch", pattern = pattern, isIgnored = true) model("$idea/quickfix/typeOfAnnotationMember", pattern = pattern)