Allow injection in strings with interpolation (KT-6610)

#KT-6610 Fixed
This commit is contained in:
Nikolay Krasko
2017-05-12 20:16:03 +03:00
parent 12002aed57
commit e5822c76fb
6 changed files with 263 additions and 7 deletions
@@ -16,7 +16,9 @@
package org.jetbrains.kotlin.psi
import com.intellij.injected.editor.DocumentWindowImpl
import com.intellij.injected.editor.EditorWindow
import com.intellij.openapi.util.TextRange
import com.intellij.psi.injection.Injectable
import com.intellij.testFramework.LightProjectDescriptor
import junit.framework.TestCase
@@ -40,9 +42,17 @@ abstract class AbstractInjectionTest : KotlinLightCodeInsightFixtureTestCase() {
}
}
data class ShredInfo(
val range: TextRange,
val hostRange: TextRange,
val prefix: String = "",
val suffix: String = "") {
}
protected fun doInjectionPresentTest(
@Language("kotlin") text: String, @Language("Java") javaText: String? = null,
languageId: String? = null, unInjectShouldBePresent: Boolean = true) {
languageId: String? = null, unInjectShouldBePresent: Boolean = true,
shreds: List<ShredInfo>? = null) {
if (javaText != null) {
myFixture.configureByText("${getTestName(true)}.java", javaText.trimIndent())
}
@@ -50,6 +60,16 @@ abstract class AbstractInjectionTest : KotlinLightCodeInsightFixtureTestCase() {
myFixture.configureByText("${getTestName(true)}.kt", text.trimIndent())
assertInjectionPresent(languageId, unInjectShouldBePresent)
if (shreds != null) {
val actualShreds = (editor.document as DocumentWindowImpl).shreds.map {
ShredInfo(it.range, it.rangeInsideHost, it.prefix, it.suffix)
}
assertOrderedEquals(
actualShreds.sortedBy { it.range.startOffset },
shreds.sortedBy { it.range.startOffset })
}
}
protected fun assertInjectionPresent(languageId: String?, unInjectShouldBePresent: Boolean) {
@@ -103,4 +123,6 @@ abstract class AbstractInjectionTest : KotlinLightCodeInsightFixtureTestCase() {
configuration.isSourceModificationAllowed = allowed
}
}
}
fun range(start: Int, end: Int) = TextRange.create(start, end)
}
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.psi
import com.intellij.lang.html.HTMLLanguage
import com.intellij.openapi.fileTypes.PlainTextLanguage
import org.intellij.lang.regexp.RegExpLanguage
import org.intellij.plugins.intelliLang.Configuration
import org.intellij.plugins.intelliLang.inject.config.BaseInjection
@@ -324,4 +325,89 @@ class KotlinInjectionTest : AbstractInjectionTest() {
""",
languageId = HTMLLanguage.INSTANCE.id, unInjectShouldBePresent = false
)
fun testInjectionOnInterpolationWithAnnotation() = doInjectionPresentTest(
"""
val b = 2
@org.intellij.lang.annotations.Language("HTML")
val test = "<caret>simple${'$'}{b}.kt"
""",
unInjectShouldBePresent = false,
shreds = listOf(
ShredInfo(range(0, 6), hostRange=range(1, 7)),
ShredInfo(range(6, 21), hostRange=range(11, 14), prefix="missingValue")
)
)
fun testInjectionOnInterpolatedStringWithComment() = doInjectionPresentTest(
"""
val some = 42
// language=HTML
val test = "<ht<caret>ml>${'$'}some</html>"
""",
languageId = HTMLLanguage.INSTANCE.id, unInjectShouldBePresent = false,
shreds = listOf(
ShredInfo(range(0, 6), hostRange = range(1, 7)),
ShredInfo(range(6, 17), hostRange = range(12, 19), prefix="some"))
)
fun testEditorShortShreadsInInterpolatedInjection() = doInjectionPresentTest(
"""
val s = 42
// language=TEXT
val test = "${'$'}s <caret>text ${'$'}s${'$'}{s}${'$'}s text ${'$'}s"
""",
languageId = PlainTextLanguage.INSTANCE.id, unInjectShouldBePresent = false,
shreds = listOf(
ShredInfo(range(0, 0), hostRange=range(1, 1)),
ShredInfo(range(0, 7), hostRange=range(3, 9), prefix="s"),
ShredInfo(range(7, 8), hostRange=range(11, 11), prefix="s"),
ShredInfo(range(8, 20), hostRange=range(15, 15), prefix="missingValue"),
ShredInfo(range(20, 27), hostRange=range(17, 23), prefix="s"),
ShredInfo(range(27, 28), hostRange=range(25, 25), prefix="s")
)
)
fun testEditorLongShreadsInInterpolatedInjection() = doInjectionPresentTest(
"""
val s = 42
// language=TEXT
val test = "${'$'}{s} <caret>text ${'$'}{s}${'$'}s${'$'}{s} text ${'$'}{s}"
""",
languageId = PlainTextLanguage.INSTANCE.id, unInjectShouldBePresent = false,
shreds = listOf(
ShredInfo(range(0, 0), hostRange=range(1, 1)),
ShredInfo(range(0, 18), hostRange=range(5, 11), prefix="missingValue"),
ShredInfo(range(18, 30), hostRange=range(15, 15), prefix="missingValue"),
ShredInfo(range(30, 31), hostRange=range(17, 17), prefix="s"),
ShredInfo(range(31, 49), hostRange=range(21, 27), prefix="missingValue"),
ShredInfo(range(49, 61), hostRange=range(31, 31), prefix="missingValue")
)
)
fun testEditorShreadsWithEscapingInjection() = doInjectionPresentTest(
"""
// language=TEXT
val test = "\rte<caret>xt\ttext\n\t"
""",
languageId = PlainTextLanguage.INSTANCE.id, unInjectShouldBePresent = false,
shreds = listOf(
ShredInfo(range(0, 12), hostRange=range(1, 17))
)
)
fun testEditorShreadsInInterpolatedWithEscapingInjection() = doInjectionPresentTest(
"""
val s = 1
// language=TEXT
val test = "\r${'$'}s te<caret>xt${'$'}s\ttext\n\t"
""",
languageId = PlainTextLanguage.INSTANCE.id, unInjectShouldBePresent = false,
shreds = listOf(
ShredInfo(range(0, 1), hostRange=range(1, 3)),
ShredInfo(range(1, 7), hostRange=range(5, 10), prefix="s"),
ShredInfo(range(7, 15), hostRange=range(12, 22), prefix="s")
)
)
}