diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt index eb77ab057b9..ba6c4918153 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt @@ -26,6 +26,7 @@ import com.intellij.openapi.util.text.StringUtil import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile +import org.jetbrains.annotations.TestOnly import org.jetbrains.kotlin.idea.editor.fixers.range import org.jetbrains.kotlin.lexer.KotlinLexer import org.jetbrains.kotlin.lexer.KtTokens @@ -198,7 +199,7 @@ private class TemplateTokenSequence(private val inputString: String) : Sequence< else if (lexer.tokenType == KtTokens.LONG_TEMPLATE_ENTRY_END) { depth-- if (depth == 0) { - return from + lexer.currentPosition.offset - 1 + return from + lexer.currentPosition.offset } } lexer.advance() @@ -261,3 +262,14 @@ private class TemplateTokenSequence(private val inputString: String) : Sequence< override fun iterator(): Iterator = iterTemplateChunks() } +@TestOnly +internal fun createTemplateSequenceTokenString(input:String):String{ + return TemplateTokenSequence(input).map { + when (it){ + is LiteralChunk -> "LITERAL_CHUNK(${it.text})" + is EntryChunk -> "ENTRY_CHUNK(${it.text})" + is NewLineChunk -> "NEW_LINE()" + } + }.joinToString (separator = "") +} + diff --git a/idea/tests/org/jetbrains/kotlin/idea/editor/AbstractLiteralKotlinToKotlinCopyPasteTest.kt b/idea/tests/org/jetbrains/kotlin/idea/editor/AbstractLiteralKotlinToKotlinCopyPasteTest.kt index 2175d770017..2f89db90583 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/editor/AbstractLiteralKotlinToKotlinCopyPasteTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/editor/AbstractLiteralKotlinToKotlinCopyPasteTest.kt @@ -32,9 +32,6 @@ abstract class AbstractLiteralKotlinToKotlinCopyPasteTest : AbstractCopyPasteTes override fun getProjectDescriptor() = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE - - - fun doTest(path: String) { myFixture.testDataPath = BASE_PATH val testName = getTestName(false) diff --git a/idea/tests/org/jetbrains/kotlin/idea/editor/AbstractLiteralTextToKotlinCopyPasteTest.kt b/idea/tests/org/jetbrains/kotlin/idea/editor/AbstractLiteralTextToKotlinCopyPasteTest.kt index ddd2f4ac820..2c5df29e195 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/editor/AbstractLiteralTextToKotlinCopyPasteTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/editor/AbstractLiteralTextToKotlinCopyPasteTest.kt @@ -32,14 +32,10 @@ abstract class AbstractLiteralTextToKotlinCopyPasteTest : AbstractCopyPasteTest( override fun getProjectDescriptor() = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE - - - fun doTest(path: String) { myFixture.testDataPath = BASE_PATH val testName = getTestName(false) myFixture.configureByFiles(testName + ".txt") - val fileText = myFixture.editor.document.text myFixture.editor.selectionModel.setSelection(0, fileText.length) diff --git a/idea/tests/org/jetbrains/kotlin/idea/editor/TemplateTokenSequenceTest.kt b/idea/tests/org/jetbrains/kotlin/idea/editor/TemplateTokenSequenceTest.kt new file mode 100644 index 00000000000..3447fd3de09 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/editor/TemplateTokenSequenceTest.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2017 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.testFramework.UsefulTestCase +import org.junit.Assert + + +class TemplateTokenSequenceTest:UsefulTestCase() { + fun doTest(input: String, expected:String) { + val output = createTemplateSequenceTokenString(input) + Assert.assertEquals("Unexpected template sequence output for $input: " , expected, output) + } + + fun `test multiple template tokens`(){ + doTest("literal \${a.length} literal \${b.length}", "LITERAL_CHUNK(literal )ENTRY_CHUNK(\${a.length})LITERAL_CHUNK( literal )ENTRY_CHUNK(\${b.length})") + } + + fun `test broken entry`(){ + doTest("literal \${a.lengt \n literal", "LITERAL_CHUNK(literal )LITERAL_CHUNK(\${a.lengt )NEW_LINE()LITERAL_CHUNK( literal)") + } + + fun `test multiple short entries`(){ + doTest("literal \$a literal \$a", "LITERAL_CHUNK(literal )ENTRY_CHUNK(\$a)LITERAL_CHUNK( literal )ENTRY_CHUNK(\$a)") + } + + fun `test leading new lines`(){ + doTest("\n\nliteral", "NEW_LINE()NEW_LINE()LITERAL_CHUNK(literal)") + } + + //last empty line is skipped + fun `test trailing new lines`(){ + doTest("literal\n\n", "LITERAL_CHUNK(literal)NEW_LINE()") + + } + + fun `test multi new lines`(){ + doTest("literal\n\nliteral", "LITERAL_CHUNK(literal)NEW_LINE()NEW_LINE()LITERAL_CHUNK(literal)") + + } +} \ No newline at end of file