KT-7848 -- unmerged changes (#1075)

* KT-7848 Initial implementation of literal copy/paste processor for Kotlin

* KT-7848: Tests

* KT-7848: fix issues caught in unit tests with recognizing invalid template entries

* KT-7848: check for KtFile

* KT-7848:  fix capitalization for test data file names

* KT-7848:  Initial changes according as per review

* KT-7848:  TemplateTokenSequence tests

* KT-7848:  fix off by one error caught in test

* KT-7848:  Reformat with keep empty lines in code -> 0
This commit is contained in:
Yuli Fiterman
2017-05-08 11:50:41 -04:00
committed by Dmitry Jemerov
parent 99b1bb98dc
commit 4393e2d518
4 changed files with 68 additions and 8 deletions
@@ -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<TemplateChunk> = 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 = "")
}
@@ -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)
@@ -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)
@@ -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)")
}
}