[kotlin] Port TooLongCharLiteralToStringFix to K2

This quick fix is for the ILLEGAL_ESCAPE and TOO_MANY_CHARACTERS_IN_CHARACTER_LITERAL errors.

^KTIJ-29541

GitOrigin-RevId: a219a5a76592f93092780f0c13c69d75beef24dd
This commit is contained in:
Andrey Cherkasov
2024-05-23 09:10:16 +04:00
committed by intellij-monorepo-bot
parent 45eb27ca93
commit 202d22f447
14 changed files with 84 additions and 11 deletions
@@ -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 {
@@ -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()
}
}
@@ -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");
}
}
@@ -3,4 +3,5 @@
fun foo() {
'foo\nbar'<caret>
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
@@ -3,4 +3,5 @@
fun foo() {
"foo\nbar"
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
@@ -4,4 +4,5 @@
fun foo() {
'foo$bar'<caret>
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
@@ -4,4 +4,5 @@
fun foo() {
"foo$bar"
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
@@ -3,4 +3,5 @@
fun foo() {
'foo"bar'<caret>
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
@@ -3,4 +3,5 @@
fun foo() {
"foo\"bar"
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
@@ -3,4 +3,5 @@
fun foo() {
'foo\"bar'<caret>
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
@@ -3,4 +3,5 @@
fun foo() {
"foo\"bar"
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
@@ -4,4 +4,5 @@
fun foo() {
'\ bar<caret>'
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
@@ -4,4 +4,5 @@
fun foo() {
"\ bar"
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TooLongCharLiteralToStringFix
@@ -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)