Prevent stepping through indirect assignments in injection search
This commit is contained in:
@@ -45,11 +45,11 @@ class KotlinLanguageInjector : LanguageInjector {
|
|||||||
injectionPlacesRegistrar.addPlace(language, TextRange.from(0, ktHost.textLength), injectionInfo.prefix, injectionInfo.suffix)
|
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)
|
return injectWithExplicitCodeInstruction(place)
|
||||||
?: injectWithCall(place)
|
?: injectWithCall(place)
|
||||||
?: injectWithReceiver(place)
|
?: injectWithReceiver(place)
|
||||||
?: injectWithVariableUsage(place)
|
?: injectWithVariableUsage(place, originalHost)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun injectWithExplicitCodeInstruction(host: KtElement): InjectionInfo? {
|
private fun injectWithExplicitCodeInstruction(host: KtElement): InjectionInfo? {
|
||||||
@@ -82,7 +82,10 @@ class KotlinLanguageInjector : LanguageInjector {
|
|||||||
return null
|
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 ktHost: KtElement = host
|
||||||
val ktProperty = host.parent as? KtProperty?: return null
|
val ktProperty = host.parent as? KtProperty?: return null
|
||||||
if (ktProperty.initializer != host) return null
|
if (ktProperty.initializer != host) return null
|
||||||
@@ -92,7 +95,7 @@ class KotlinLanguageInjector : LanguageInjector {
|
|||||||
val searchScope = LocalSearchScope(arrayOf(ktProperty.containingFile), "", true)
|
val searchScope = LocalSearchScope(arrayOf(ktProperty.containingFile), "", true)
|
||||||
return ReferencesSearch.search(ktProperty, searchScope).asSequence().mapNotNull { psiReference ->
|
return ReferencesSearch.search(ktProperty, searchScope).asSequence().mapNotNull { psiReference ->
|
||||||
val element = psiReference.element as? KtElement ?: return@mapNotNull null
|
val element = psiReference.element as? KtElement ?: return@mapNotNull null
|
||||||
findInjectionInfo(element)
|
findInjectionInfo(element, false)
|
||||||
}.firstOrNull()
|
}.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) {
|
protected fun doRemoveInjectionTest(before: String, after: String) {
|
||||||
myFixture.setCaresAboutInjection(false)
|
myFixture.setCaresAboutInjection(false)
|
||||||
|
|
||||||
|
|||||||
@@ -96,6 +96,13 @@ class KotlinInjectionTest : AbstractInjectionTest() {
|
|||||||
""",
|
""",
|
||||||
languageId = RegExpLanguage.INSTANCE.id, unInjectShouldBePresent = false)
|
languageId = RegExpLanguage.INSTANCE.id, unInjectShouldBePresent = false)
|
||||||
|
|
||||||
|
fun testNoInjectionThoughSeveralAssignmentsWithRuntime() = testNoInjection(
|
||||||
|
"""
|
||||||
|
|val first = "<caret>some"
|
||||||
|
|val test = first
|
||||||
|
|fun foo() = Regex(test)
|
||||||
|
""")
|
||||||
|
|
||||||
fun testInjectionWithMultipleCommentsOnFun() = testInjectionPresent(
|
fun testInjectionWithMultipleCommentsOnFun() = testInjectionPresent(
|
||||||
"""
|
"""
|
||||||
|// Some comment
|
|// Some comment
|
||||||
|
|||||||
Reference in New Issue
Block a user