Review and fix raw string quotes inserting and remove (KT-2582)
- Add more tests - Fix inserting close quotes after multiline string in file end - Use parent instead of parentOfType where possible - Add bunch files #KT-2582 Fixed
This commit is contained in:
@@ -586,6 +586,7 @@
|
||||
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.editor.KotlinTypedHandler"/>
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.kdoc.KDocTypedHandler"/>
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.editor.KotlinRawStringTypedHandler"/>
|
||||
<enterHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinEnterHandler"
|
||||
id="KotlinEnterHandler" order="before EnterBetweenBracesHandler"/>
|
||||
<enterHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinMultilineStringEnterHandler"
|
||||
@@ -593,6 +594,7 @@
|
||||
<lang.smartEnterProcessor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinBackspaceHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinStringTemplateBackspaceHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinRawStringBackspaceHandler"/>
|
||||
|
||||
<copyPastePostProcessor implementation="org.jetbrains.kotlin.idea.conversion.copy.ConvertJavaCopyPasteProcessor"/>
|
||||
<copyPastePostProcessor implementation="org.jetbrains.kotlin.idea.conversion.copy.ConvertTextJavaCopyPasteProcessor"/>
|
||||
|
||||
@@ -587,6 +587,7 @@
|
||||
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.editor.KotlinTypedHandler"/>
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.kdoc.KDocTypedHandler"/>
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.editor.KotlinRawStringTypedHandler"/>
|
||||
<enterHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinEnterHandler"
|
||||
id="KotlinEnterHandler" order="before EnterBetweenBracesHandler"/>
|
||||
<enterHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinMultilineStringEnterHandler"
|
||||
@@ -594,6 +595,7 @@
|
||||
<lang.smartEnterProcessor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinBackspaceHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinStringTemplateBackspaceHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinRawStringBackspaceHandler"/>
|
||||
|
||||
<copyPastePostProcessor implementation="org.jetbrains.kotlin.idea.conversion.copy.ConvertJavaCopyPasteProcessor"/>
|
||||
<copyPastePostProcessor implementation="org.jetbrains.kotlin.idea.conversion.copy.ConvertTextJavaCopyPasteProcessor"/>
|
||||
|
||||
@@ -587,6 +587,7 @@
|
||||
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.editor.KotlinTypedHandler"/>
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.kdoc.KDocTypedHandler"/>
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.editor.KotlinRawStringTypedHandler"/>
|
||||
<enterHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinEnterHandler"
|
||||
id="KotlinEnterHandler" order="before EnterBetweenBracesHandler"/>
|
||||
<enterHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinMultilineStringEnterHandler"
|
||||
@@ -594,6 +595,7 @@
|
||||
<lang.smartEnterProcessor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinBackspaceHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinStringTemplateBackspaceHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinRawStringBackspaceHandler"/>
|
||||
|
||||
<copyPastePostProcessor implementation="org.jetbrains.kotlin.idea.conversion.copy.ConvertJavaCopyPasteProcessor"/>
|
||||
<copyPastePostProcessor implementation="org.jetbrains.kotlin.idea.conversion.copy.ConvertTextJavaCopyPasteProcessor"/>
|
||||
|
||||
@@ -586,6 +586,7 @@
|
||||
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.editor.KotlinTypedHandler"/>
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.kdoc.KDocTypedHandler"/>
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.editor.KotlinRawStringTypedHandler"/>
|
||||
<enterHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinEnterHandler"
|
||||
id="KotlinEnterHandler" order="before EnterBetweenBracesHandler"/>
|
||||
<enterHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinMultilineStringEnterHandler"
|
||||
@@ -593,6 +594,7 @@
|
||||
<lang.smartEnterProcessor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinBackspaceHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinStringTemplateBackspaceHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinRawStringBackspaceHandler"/>
|
||||
|
||||
<copyPastePostProcessor implementation="org.jetbrains.kotlin.idea.conversion.copy.ConvertJavaCopyPasteProcessor"/>
|
||||
<copyPastePostProcessor implementation="org.jetbrains.kotlin.idea.conversion.copy.ConvertTextJavaCopyPasteProcessor"/>
|
||||
|
||||
@@ -586,6 +586,7 @@
|
||||
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.editor.KotlinTypedHandler"/>
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.kdoc.KDocTypedHandler"/>
|
||||
<typedHandler implementation="org.jetbrains.kotlin.idea.editor.KotlinRawStringTypedHandler"/>
|
||||
<enterHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinEnterHandler"
|
||||
id="KotlinEnterHandler" order="before EnterBetweenBracesHandler"/>
|
||||
<enterHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinMultilineStringEnterHandler"
|
||||
@@ -593,6 +594,7 @@
|
||||
<lang.smartEnterProcessor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.editor.KotlinSmartEnterHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinBackspaceHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinStringTemplateBackspaceHandler"/>
|
||||
<backspaceHandlerDelegate implementation="org.jetbrains.kotlin.idea.editor.KotlinRawStringBackspaceHandler"/>
|
||||
|
||||
<copyPastePostProcessor implementation="org.jetbrains.kotlin.idea.conversion.copy.ConvertJavaCopyPasteProcessor"/>
|
||||
<copyPastePostProcessor implementation="org.jetbrains.kotlin.idea.conversion.copy.ConvertTextJavaCopyPasteProcessor"/>
|
||||
|
||||
@@ -21,7 +21,6 @@ import com.intellij.codeInsight.editorActions.BackspaceHandlerDelegate
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.editor.RangeMarker
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.util.parentOfType
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
|
||||
|
||||
@@ -39,8 +38,8 @@ class KotlinRawStringBackspaceHandler : BackspaceHandlerDelegate() {
|
||||
val offset = editor.caretModel.offset
|
||||
val psiElement = file.findElementAt(offset) ?: return
|
||||
|
||||
psiElement.parentOfType(KtStringTemplateExpression::class)?.let {
|
||||
if (it.text == "\"\"\"\"\"\"") {
|
||||
psiElement.parent?.let {
|
||||
if (it is KtStringTemplateExpression && it.text == "\"\"\"\"\"\"") {
|
||||
if (editor.caretModel.offset == it.textOffset + 3) {
|
||||
rangeMarker = editor.document.createRangeMarker(it.textRange)
|
||||
}
|
||||
@@ -54,6 +53,7 @@ class KotlinRawStringBackspaceHandler : BackspaceHandlerDelegate() {
|
||||
rangeMarker = null
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -22,40 +22,46 @@ import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.fileTypes.FileType
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import com.intellij.psi.impl.source.tree.LeafPsiElement
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
|
||||
|
||||
class KotlinRawStringTypedHandler : TypedHandlerDelegate() {
|
||||
override fun beforeCharTyped(c: Char, project: Project, editor: Editor, file: PsiFile, fileType: FileType): Result {
|
||||
if (c != '"') {
|
||||
return Result.CONTINUE
|
||||
}
|
||||
if (!CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE) {
|
||||
return Result.CONTINUE
|
||||
}
|
||||
if (file !is KtFile) {
|
||||
return Result.CONTINUE
|
||||
}
|
||||
if (c != '"') {
|
||||
return Result.CONTINUE
|
||||
}
|
||||
|
||||
// A quote is typed after 2 other quotes
|
||||
val offset = editor.caretModel.offset
|
||||
// Check for caret at end of document
|
||||
if (offset < editor.document.textLength) {
|
||||
val psiElement = file.findElementAt(offset) ?: return Result.CONTINUE
|
||||
if (PsiTreeUtil.getParentOfType(psiElement, KtStringTemplateExpression::class.java) != null) {
|
||||
return Result.CONTINUE
|
||||
}
|
||||
if (offset < 2) {
|
||||
return Result.CONTINUE
|
||||
}
|
||||
|
||||
val text = editor.document.text
|
||||
if (offset >= 2)
|
||||
if (text[offset - 1] == '"')
|
||||
if (text[offset - 2] == '"') {
|
||||
editor.document.insertString(offset, "\"\"\"\"")
|
||||
editor.caretModel.currentCaret.moveToOffset(offset + 1)
|
||||
return Result.STOP
|
||||
}
|
||||
val openQuote = file.findElementAt(offset - 2)
|
||||
if (openQuote == null || openQuote !is LeafPsiElement || openQuote.elementType != KtTokens.OPEN_QUOTE) {
|
||||
return Result.CONTINUE
|
||||
}
|
||||
|
||||
return Result.CONTINUE
|
||||
val closeQuote = file.findElementAt(offset - 1)
|
||||
if (closeQuote == null || closeQuote !is LeafPsiElement || closeQuote.elementType != KtTokens.CLOSING_QUOTE) {
|
||||
return Result.CONTINUE
|
||||
}
|
||||
|
||||
if (closeQuote.text != "\"") {
|
||||
// Check it is not a multi-line quote
|
||||
return Result.CONTINUE
|
||||
}
|
||||
|
||||
editor.document.insertString(offset, "\"\"\"\"")
|
||||
editor.caretModel.currentCaret.moveToOffset(offset + 1)
|
||||
|
||||
return Result.STOP
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
"\"\"\"<caret>\"\"\""
|
||||
val test = """<caret>"""
|
||||
@@ -1 +1 @@
|
||||
<caret>
|
||||
val test = <caret>
|
||||
@@ -0,0 +1 @@
|
||||
val test = """"<caret>""
|
||||
@@ -0,0 +1 @@
|
||||
val test = """<caret>""
|
||||
@@ -0,0 +1 @@
|
||||
val test = ""<caret>""""
|
||||
@@ -0,0 +1 @@
|
||||
val test = "<caret>""""
|
||||
@@ -74,10 +74,46 @@ class TypedHandlerTest : LightCodeInsightTestCase() {
|
||||
"""val x = "$dollar{}]" """
|
||||
)
|
||||
|
||||
fun testAutoCloseRawString() = doCharTypeTest(
|
||||
fun testAutoCloseRawStringInEnd() = doCharTypeTest(
|
||||
'"',
|
||||
"""val x = ""<caret>""",
|
||||
"""val x = ""${'"'}"<caret>""${'"'}"""
|
||||
"""val x = ""${'"'}<caret>""${'"'}"""
|
||||
)
|
||||
|
||||
fun testNoAutoCloseRawStringInEnd() = doCharTypeTest(
|
||||
'"',
|
||||
"""val x = ""${'"'}<caret>""",
|
||||
"""val x = ""${'"'}""""
|
||||
)
|
||||
|
||||
fun testAutoCloseRawStringInMiddle() = doCharTypeTest(
|
||||
'"',
|
||||
"""
|
||||
val x = ""<caret>
|
||||
val y = 12
|
||||
""".trimIndent(),
|
||||
"""
|
||||
val x = ""${'"'}<caret>""${'"'}
|
||||
val y = 12
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
fun testNoAutoCloseBetweenMultiQuotes() = doCharTypeTest(
|
||||
'"',
|
||||
"""val x = ""${'"'}<caret>${'"'}""/**/""",
|
||||
"""val x = ""${'"'}${'"'}<caret>""/**/"""
|
||||
)
|
||||
|
||||
fun testNoAutoCloseBetweenMultiQuotes1() = doCharTypeTest(
|
||||
'"',
|
||||
"""val x = ""${'"'}"<caret>"${'"'}/**/""",
|
||||
"""val x = ""${'"'}""<caret>${'"'}/**/"""
|
||||
)
|
||||
|
||||
fun testNoAutoCloseAfterEscape() = doCharTypeTest(
|
||||
'"',
|
||||
"""val x = "\""<caret>""",
|
||||
"""val x = "\""${'"'}<caret>""""
|
||||
)
|
||||
|
||||
fun testAutoCloseBraceInFunctionDeclaration() = doCharTypeTest(
|
||||
|
||||
Generated
+15
@@ -29,6 +29,21 @@ public class BackspaceHandlerTestGenerated extends AbstractBackspaceHandlerTest
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/editor/backspaceHandler"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("rawStringDelete.kt")
|
||||
public void testRawStringDelete() throws Exception {
|
||||
runTest("idea/testData/editor/backspaceHandler/rawStringDelete.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("rawStringInCloseQuote.kt")
|
||||
public void testRawStringInCloseQuote() throws Exception {
|
||||
runTest("idea/testData/editor/backspaceHandler/rawStringInCloseQuote.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("rawStringInOpenQuote.kt")
|
||||
public void testRawStringInOpenQuote() throws Exception {
|
||||
runTest("idea/testData/editor/backspaceHandler/rawStringInOpenQuote.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("typeArguments.kt")
|
||||
public void testTypeArguments() throws Exception {
|
||||
runTest("idea/testData/editor/backspaceHandler/typeArguments.kt");
|
||||
|
||||
Reference in New Issue
Block a user