Implement quick fix to convert a too long char literal to a string #KT-13635 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
391a0fdde5
commit
06c804787f
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user