diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index d390ee8bac0..ab3b03f28c3 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -73,6 +73,7 @@ import org.jetbrains.kotlin.idea.decompiler.textBuilder.AbstractCommonDecompiled import org.jetbrains.kotlin.idea.decompiler.textBuilder.AbstractCommonDecompiledTextTest import org.jetbrains.kotlin.idea.decompiler.textBuilder.AbstractJsDecompiledTextFromJsMetadataTest import org.jetbrains.kotlin.idea.decompiler.textBuilder.AbstractJvmDecompiledTextTest +import org.jetbrains.kotlin.idea.editor.backspaceHandler.AbstractBackspaceHandlerTest import org.jetbrains.kotlin.idea.editor.quickDoc.AbstractQuickDocProviderTest import org.jetbrains.kotlin.idea.filters.AbstractKotlinExceptionFilterTest import org.jetbrains.kotlin.idea.folding.AbstractKotlinFoldingTest @@ -500,6 +501,10 @@ fun main(args: Array) { model("codeInsight/unwrapAndRemove/unwrapLambda", testMethod = "doTestLambdaUnwrapper") } + testClass() { + model("editor/backspaceHandler") + } + testClass() { model("editor/quickDoc", pattern = """^([^_]+)\.[^\.]*$""") } diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 7b7c97eb75e..c029b4c96de 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -455,6 +455,7 @@ id="KotlinEnterHandler" order="before EnterBetweenBracesHandler"/> + diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinStringTemplateBackspaceHandler.kt b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinStringTemplateBackspaceHandler.kt new file mode 100644 index 00000000000..10fbd523476 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinStringTemplateBackspaceHandler.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.editor + +import com.intellij.codeInsight.CodeInsightSettings +import com.intellij.codeInsight.editorActions.BackspaceHandlerDelegate +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.ex.EditorEx +import com.intellij.psi.PsiFile +import org.jetbrains.kotlin.lexer.KtTokens +import org.jetbrains.kotlin.psi.KtFile + +class KotlinStringTemplateBackspaceHandler : BackspaceHandlerDelegate() { + override fun beforeCharDeleted(c: Char, file: PsiFile?, editor: Editor?) { + if (c != '{' || file !is KtFile || !CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) return + + val offset = editor?.caretModel?.offset ?: return + + val highlighter = (editor as EditorEx).highlighter + val iterator = highlighter.createIterator(offset) + if (iterator.tokenType != KtTokens.LONG_TEMPLATE_ENTRY_END) return + iterator.retreat() + if (iterator.tokenType != KtTokens.LONG_TEMPLATE_ENTRY_START) return + editor.document.deleteString(offset, offset + 1) + } + + override fun charDeleted(c: Char, file: PsiFile?, editor: Editor?): Boolean { + return false + } +} \ No newline at end of file diff --git a/idea/testData/editor/backspaceHandler/stringTemplate/escapedStringTemplate.kt b/idea/testData/editor/backspaceHandler/stringTemplate/escapedStringTemplate.kt new file mode 100644 index 00000000000..cb91d36040a --- /dev/null +++ b/idea/testData/editor/backspaceHandler/stringTemplate/escapedStringTemplate.kt @@ -0,0 +1 @@ +"\${}" \ No newline at end of file diff --git a/idea/testData/editor/backspaceHandler/stringTemplate/escapedStringTemplate.kt.after b/idea/testData/editor/backspaceHandler/stringTemplate/escapedStringTemplate.kt.after new file mode 100644 index 00000000000..de27ab232de --- /dev/null +++ b/idea/testData/editor/backspaceHandler/stringTemplate/escapedStringTemplate.kt.after @@ -0,0 +1 @@ +"\$}" \ No newline at end of file diff --git a/idea/testData/editor/backspaceHandler/stringTemplate/stringTemplateBrackets.kt b/idea/testData/editor/backspaceHandler/stringTemplate/stringTemplateBrackets.kt new file mode 100644 index 00000000000..ace345d1e6f --- /dev/null +++ b/idea/testData/editor/backspaceHandler/stringTemplate/stringTemplateBrackets.kt @@ -0,0 +1 @@ +"${}" \ No newline at end of file diff --git a/idea/testData/editor/backspaceHandler/stringTemplate/stringTemplateBrackets.kt.after b/idea/testData/editor/backspaceHandler/stringTemplate/stringTemplateBrackets.kt.after new file mode 100644 index 00000000000..a7a0687106c --- /dev/null +++ b/idea/testData/editor/backspaceHandler/stringTemplate/stringTemplateBrackets.kt.after @@ -0,0 +1 @@ +"$" \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/editor/backspaceHandler/AbstractBackspaceHandlerTest.kt b/idea/tests/org/jetbrains/kotlin/idea/editor/backspaceHandler/AbstractBackspaceHandlerTest.kt new file mode 100644 index 00000000000..2c86fd5f264 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/editor/backspaceHandler/AbstractBackspaceHandlerTest.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.editor.backspaceHandler + +import com.intellij.openapi.actionSystem.IdeActions +import com.intellij.openapi.util.io.FileUtil +import com.intellij.testFramework.EditorTestUtil +import com.intellij.testFramework.LightCodeInsightTestCase +import java.io.File + +open class AbstractBackspaceHandlerTest : LightCodeInsightTestCase() { + fun doTest(path: String) { + configureFromFileText("a.kt", loadFile(path)) + EditorTestUtil.executeAction(getEditor(), IdeActions.ACTION_EDITOR_BACKSPACE) + checkResultByText(loadFile(path + ".after")) + } + + private fun loadFile(path: String) = FileUtil.loadFile(File(path), true) +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/editor/backspaceHandler/BackspaceHandlerTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/editor/backspaceHandler/BackspaceHandlerTestGenerated.java new file mode 100644 index 00000000000..d9e8f25fd32 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/editor/backspaceHandler/BackspaceHandlerTestGenerated.java @@ -0,0 +1,58 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.editor.backspaceHandler; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.KotlinTestUtils; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("idea/testData/editor/backspaceHandler") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class BackspaceHandlerTestGenerated extends AbstractBackspaceHandlerTest { + public void testAllFilesPresentInBackspaceHandler() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/editor/backspaceHandler"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("idea/testData/editor/backspaceHandler/stringTemplate") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class StringTemplate extends AbstractBackspaceHandlerTest { + public void testAllFilesPresentInStringTemplate() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/editor/backspaceHandler/stringTemplate"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("escapedStringTemplate.kt") + public void testEscapedStringTemplate() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/editor/backspaceHandler/stringTemplate/escapedStringTemplate.kt"); + doTest(fileName); + } + + @TestMetadata("stringTemplateBrackets.kt") + public void testStringTemplateBrackets() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/editor/backspaceHandler/stringTemplate/stringTemplateBrackets.kt"); + doTest(fileName); + } + } +}