[kotlin] KTIJ-38334 Extract findContextToAnalyze to a new utility file in refactoring package

(cherry picked from commit c130d4f74fbfc25d973087ef1faced3dabbf14e6)

IJ-CR-212042

GitOrigin-RevId: bc4c82ec26f550da73347c13a5113a910e6713f7
This commit is contained in:
Andrey Cherkasov
2026-07-16 07:54:29 +00:00
committed by intellij-monorepo-bot
parent 4a56bf4413
commit 0e6f123b11
2 changed files with 36 additions and 25 deletions
@@ -12,27 +12,19 @@ import org.jetbrains.kotlin.analysis.api.resolution.successfulCallOrNull
import org.jetbrains.kotlin.analysis.api.resolution.symbol
import org.jetbrains.kotlin.analysis.api.symbols.KaCallableSymbol
import org.jetbrains.kotlin.analysis.api.types.KaType
import org.jetbrains.kotlin.idea.k2.refactoring.util.findContextToAnalyze
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtClassBody
import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtExpressionCodeFragment
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtFunctionLiteral
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyAccessor
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
/**
* Creates a code fragment for testing if a function resolves to stdlib, handling special syntax transformations.
@@ -162,7 +154,7 @@ internal fun isCollectionLiteralSafeAsArgument(
element: KtCallExpression,
expressionType: KaType,
): Boolean {
val context = findContextToAnalyze(element) ?: return false
val context = element.findContextToAnalyze() ?: return false
val literal = buildCodeFragmentWithCollectionLiteral(element, context) ?: return false
@@ -175,18 +167,3 @@ internal fun isCollectionLiteralSafeAsArgument(
outerCall.resolveCall() != null
}
}
private fun findContextToAnalyze(expression: KtExpression): KtExpression? {
for (element in expression.parentsWithSelf) {
when (element) {
is KtFunctionLiteral -> continue
is KtParameter -> continue
is KtPropertyAccessor -> continue
is KtProperty -> if (element.parent is KtClassBody) continue else return element
is KtFunction -> if (element.hasModifier(KtTokens.OVERRIDE_KEYWORD)) continue else return element
is KtDeclaration -> return element
else -> continue
}
}
return null
}
@@ -0,0 +1,34 @@
// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.k2.refactoring.util
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtClassBody
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtFunctionLiteral
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyAccessor
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
/**
* Finds the nearest enclosing declaration that can be used as a context for a code fragment built from [this] expression.
*/
@ApiStatus.Internal
fun KtExpression.findContextToAnalyze(): KtExpression? {
for (element in parentsWithSelf) {
when (element) {
is KtFunctionLiteral -> continue
is KtParameter -> continue
is KtPropertyAccessor -> continue
is KtProperty -> if (element.parent is KtClassBody) continue else return element
is KtFunction -> if (element.hasModifier(KtTokens.OVERRIDE_KEYWORD)) continue else return element
is KtDeclaration -> return element
else -> continue
}
}
return null
}