Prevent stepping through indirect assignments in injection search

This commit is contained in:
Nikolay Krasko
2016-04-20 19:10:46 +03:00
parent 505c6624a7
commit b0433b2904
3 changed files with 21 additions and 4 deletions
@@ -45,11 +45,11 @@ class KotlinLanguageInjector : LanguageInjector {
injectionPlacesRegistrar.addPlace(language, TextRange.from(0, ktHost.textLength), injectionInfo.prefix, injectionInfo.suffix)
}
private fun findInjectionInfo(place: KtElement): InjectionInfo? {
private fun findInjectionInfo(place: KtElement, originalHost: Boolean = true): InjectionInfo? {
return injectWithExplicitCodeInstruction(place)
?: injectWithCall(place)
?: injectWithReceiver(place)
?: injectWithVariableUsage(place)
?: injectWithVariableUsage(place, originalHost)
}
private fun injectWithExplicitCodeInstruction(host: KtElement): InjectionInfo? {
@@ -82,7 +82,10 @@ class KotlinLanguageInjector : LanguageInjector {
return null
}
private fun injectWithVariableUsage(host: KtElement): InjectionInfo? {
private fun injectWithVariableUsage(host: KtElement, originalHost: Boolean): InjectionInfo? {
// Given place is not original host of the injection so we stop to prevent stepping through indirect references
if (!originalHost) return null
val ktHost: KtElement = host
val ktProperty = host.parent as? KtProperty?: return null
if (ktProperty.initializer != host) return null
@@ -92,7 +95,7 @@ class KotlinLanguageInjector : LanguageInjector {
val searchScope = LocalSearchScope(arrayOf(ktProperty.containingFile), "", true)
return ReferencesSearch.search(ktProperty, searchScope).asSequence().mapNotNull { psiReference ->
val element = psiReference.element as? KtElement ?: return@mapNotNull null
findInjectionInfo(element)
findInjectionInfo(element, false)
}.firstOrNull()
}
@@ -56,6 +56,13 @@ abstract class AbstractInjectionTest : KotlinLightCodeInsightFixtureTestCase() {
}
}
protected fun testNoInjection(text: String) {
myFixture.configureByText("${getTestName(true)}.kt", text.trimMargin())
TestCase.assertTrue("Injection action is not available. There's probably some injection but nothing was expected.",
InjectLanguageAction().isAvailable(project, myFixture.editor, myFixture.file))
}
protected fun doRemoveInjectionTest(before: String, after: String) {
myFixture.setCaresAboutInjection(false)
@@ -96,6 +96,13 @@ class KotlinInjectionTest : AbstractInjectionTest() {
""",
languageId = RegExpLanguage.INSTANCE.id, unInjectShouldBePresent = false)
fun testNoInjectionThoughSeveralAssignmentsWithRuntime() = testNoInjection(
"""
|val first = "<caret>some"
|val test = first
|fun foo() = Regex(test)
""")
fun testInjectionWithMultipleCommentsOnFun() = testInjectionPresent(
"""
|// Some comment