Implement quick fix to convert a too long char literal to a string #KT-13635 Fixed

This commit is contained in:
gitreelike
2016-09-21 00:21:06 +02:00
committed by Mikhail Glukhikh
parent 391a0fdde5
commit 06c804787f
9 changed files with 100 additions and 0 deletions
@@ -422,5 +422,7 @@ class QuickFixRegistrar : QuickFixContributor {
FINAL_UPPER_BOUND.registerFactory(RemoveFinalUpperBoundFix)
TYPE_PARAMETER_AS_REIFIED.registerFactory(AddReifiedToTypeParameterOfFunctionFix)
TOO_MANY_CHARACTERS_IN_CHARACTER_LITERAL.registerFactory(TooLongCharLiteralToStringFix)
}
}
@@ -0,0 +1,39 @@
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtConstantExpression
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPsiFactory
class TooLongCharLiteralToStringFix(
element: KtConstantExpression
) : KotlinQuickFixAction<KtConstantExpression>(element) {
override fun getText(): String = "Convert too long character literal to string"
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val text = element.text
if (!(text.startsWith("'") && text.endsWith("'") && text.length >= 2)) {
return
}
val newStringContent = text
.slice(1..text.length - 2)
.replace("\"", "\\\"")
val newElement = KtPsiFactory(element).createStringTemplate(newStringContent)
element.replace(newElement)
}
override fun getFamilyName(): String = text
companion object Factory : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val element = Errors.TOO_MANY_CHARACTERS_IN_CHARACTER_LITERAL.cast(diagnostic).psiElement
return TooLongCharLiteralToStringFix(element = element)
}
}
}
@@ -0,0 +1,5 @@
// "Convert too long character literal to string" "true"
fun foo() {
'foo\nbar'<caret>
}
@@ -0,0 +1,5 @@
// "Convert too long character literal to string" "true"
fun foo() {
"foo\nbar"
}
@@ -0,0 +1,6 @@
// "Convert too long character literal to string" "true"
// ERROR: Unresolved reference: bar
fun foo() {
'foo$bar'<caret>
}
@@ -0,0 +1,6 @@
// "Convert too long character literal to string" "true"
// ERROR: Unresolved reference: bar
fun foo() {
"foo$bar"
}
@@ -0,0 +1,5 @@
// "Convert too long character literal to string" "true"
fun foo() {
'foo"bar'<caret>
}
@@ -0,0 +1,5 @@
// "Convert too long character literal to string" "true"
fun foo() {
"foo\"bar"
}
@@ -8348,6 +8348,33 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/tooLongCharLiteralToString")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class TooLongCharLiteralToString extends AbstractQuickFixTest {
public void testAllFilesPresentInTooLongCharLiteralToString() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/tooLongCharLiteralToString"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("backslashShouldNotBeEscaped.kt")
public void testBackslashShouldNotBeEscaped() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/tooLongCharLiteralToString/backslashShouldNotBeEscaped.kt");
doTest(fileName);
}
@TestMetadata("dollarShouldNotBeEscaped.kt")
public void testDollarShouldNotBeEscaped() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/tooLongCharLiteralToString/dollarShouldNotBeEscaped.kt");
doTest(fileName);
}
@TestMetadata("doubleQuotesShouldBeEscaped.kt")
public void testDoubleQuotesShouldBeEscaped() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/tooLongCharLiteralToString/doubleQuotesShouldBeEscaped.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/typeAddition")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)