From 8dc939367587f44f115994290edfbf3e0cc13029 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Tue, 6 Feb 2024 15:37:58 +0100 Subject: [PATCH] cleanup: get rid of mutable stuff (part of KTIJ-26893 K2: Create*FromUsageFix fix) GitOrigin-RevId: 9504fa61ca4d88d2b01f837d0d00ca7d4768f93f --- .../lang/jvm/actions/ExpectedParameter.java | 2 +- .../CreateExecutableFromKotlinUsageRequest.kt | 13 +++++-------- .../CreateKotlinCallableAction.kt | 8 ++++---- .../CreateKotlinCallablePsiEditor.kt | 2 +- .../KotlinElementActionsFactory.kt | 6 +++--- .../quickFixes/createFromUsage/Utils.kt | 12 +++++------- .../createKotlinCallableFromUsage.kt | 19 ++++++------------- 7 files changed, 25 insertions(+), 37 deletions(-) diff --git a/java/java-analysis-api/src/com/intellij/lang/jvm/actions/ExpectedParameter.java b/java/java-analysis-api/src/com/intellij/lang/jvm/actions/ExpectedParameter.java index 5066a2858b4e..98a66f9d4eec 100644 --- a/java/java-analysis-api/src/com/intellij/lang/jvm/actions/ExpectedParameter.java +++ b/java/java-analysis-api/src/com/intellij/lang/jvm/actions/ExpectedParameter.java @@ -13,7 +13,7 @@ public interface ExpectedParameter { List getExpectedTypes(); /** - * For example for unresolved call in Java {@code a.foo(bars)} this method will return 'bars' string, + * For example, for unresolved call in Java {@code a.foo(bars)} this method will return 'bars' string, * which then will be used to suggest parameter names * taking code style parameter prefix into consideration as well as its type. */ diff --git a/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/CreateExecutableFromKotlinUsageRequest.kt b/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/CreateExecutableFromKotlinUsageRequest.kt index 195bcd57dd0e..0ecb89bbc49c 100644 --- a/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/CreateExecutableFromKotlinUsageRequest.kt +++ b/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/CreateExecutableFromKotlinUsageRequest.kt @@ -17,12 +17,9 @@ internal abstract class CreateExecutableFromKotlinUsageRequest, ) : CreateExecutableRequest { - - private val psiManager = call.manager - private val project = psiManager.project + private val project = call.project private val callPointer: SmartPsiElementPointer = call.createSmartPointer() - - private val expectedParameterInfo = mutableListOf() + private val expectedParameterInfo: MutableList = mutableListOf() init { analyze(call) { @@ -45,11 +42,11 @@ internal abstract class CreateExecutableFromKotlinUsageRequest = expectedParameterInfo.map { parameterInfo -> object : ExpectedParameter { - override fun getExpectedTypes(): MutableList = - mutableListOf(parameterInfo.type?.let { ExpectedKotlinType.createExpectedKotlinType(it) } + override fun getExpectedTypes(): List = + listOf(parameterInfo.type?.let { ExpectedKotlinType.createExpectedKotlinType(it) } ?: ExpectedKotlinType.INVALID_TYPE) - override fun getSemanticNames(): MutableCollection = parameterInfo.nameCandidates + override fun getSemanticNames(): Collection = parameterInfo.nameCandidates } } } diff --git a/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/CreateKotlinCallableAction.kt b/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/CreateKotlinCallableAction.kt index 0ab7ed6f6157..6f899f9119b0 100644 --- a/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/CreateKotlinCallableAction.kt +++ b/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/CreateKotlinCallableAction.kt @@ -34,7 +34,7 @@ internal class CreateKotlinCallableAction( private val myText: String, private val pointerToContainer: SmartPsiElementPointer<*>, ) : CreateKotlinElementAction(request, pointerToContainer), JvmGroupIntentionAction { - private val candidatesOfParameterNames: List> = request.expectedParameters.map { it.semanticNames } + private val candidatesOfParameterNames: List> = request.expectedParameters.map { it.semanticNames } private val candidatesOfRenderedParameterTypes: List> = renderCandidatesOfParameterTypes() @@ -43,7 +43,7 @@ internal class CreateKotlinCallableAction( private val containerClassFqName: FqName? = (getContainer() as? KtClassOrObject)?.fqName // Note that this property must be initialized after initializing above properties, because it has dependency on them. - private val callableDefinitionAsString = buildCallableAsString() + private val callableDefinitionAsString: String? = buildCallableAsString() override fun getActionGroup(): JvmActionGroup = if (abstract) CreateAbstractMethodActionGroup else CreateMethodActionGroup @@ -65,9 +65,9 @@ internal class CreateKotlinCallableAction( override fun getText(): String = myText override fun invoke(project: Project, editor: Editor?, file: PsiFile?) { - callableDefinitionAsString?.let { callableDefinition -> + if (callableDefinitionAsString != null) { val callableInfo = NewCallableInfo( - callableDefinition, + callableDefinitionAsString, candidatesOfParameterNames, candidatesOfRenderedParameterTypes, candidatesOfRenderedReturnType, diff --git a/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/CreateKotlinCallablePsiEditor.kt b/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/CreateKotlinCallablePsiEditor.kt index 74ddb63d79f2..47a2dc92615e 100644 --- a/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/CreateKotlinCallablePsiEditor.kt +++ b/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/CreateKotlinCallablePsiEditor.kt @@ -34,7 +34,7 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset */ internal data class NewCallableInfo( val definitionAsString: String, - val candidatesOfParameterNames: List>, + val candidatesOfParameterNames: List>, val candidatesOfRenderedParameterTypes: List>, val candidatesOfRenderedReturnType: List, val containerClassFqName: FqName?, diff --git a/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/KotlinElementActionsFactory.kt b/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/KotlinElementActionsFactory.kt index 3212994def3a..618610979335 100644 --- a/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/KotlinElementActionsFactory.kt +++ b/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/KotlinElementActionsFactory.kt @@ -31,9 +31,9 @@ class KotlinElementActionsFactory : JvmElementActionsFactory() { } override fun createAddMethodActions(targetClass: JvmClass, request: CreateMethodRequest): List { - var container = - targetClass.takeIf { (targetClass as? PsiElement)?.let(BaseIntentionAction::canModify) != false } - ?.toKtClassOrFile() ?: return emptyList() + if (targetClass is PsiElement && !BaseIntentionAction.canModify(targetClass)) return emptyList() + var container = targetClass.toKtClassOrFile() ?: return emptyList() + return when (request) { is CreateMethodFromKotlinUsageRequest -> { val isExtension = request.isExtension diff --git a/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/Utils.kt b/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/Utils.kt index e1b6cd086ed8..5a49b763d7e5 100644 --- a/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/Utils.kt +++ b/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/Utils.kt @@ -68,9 +68,7 @@ context (KtAnalysisSession) internal fun KtType.convertToClass(): KtClass? = expandedClassSymbol?.psi as? KtClass context (KtAnalysisSession) -internal fun KtElement.getExpectedJvmType(): JvmType? = getExpectedType()?.let { expectedType -> - expectedType.convertToJvmType(this) -} +internal fun KtElement.getExpectedJvmType(): JvmType? = getExpectedType()?.convertToJvmType(this) context (KtAnalysisSession) private fun KtType.convertToJvmType(useSitePosition: PsiElement): JvmType? = asPsiType(useSitePosition, allowErrorTypes = false) @@ -82,16 +80,16 @@ internal fun KtExpression.getClassOfExpressionType(): PsiElement? = when (val sy else -> getKtType()?.expandedClassSymbol }?.psi -internal data class ParameterInfo(val nameCandidates: MutableList, val type: JvmType?) +internal data class ParameterInfo(val nameCandidates: List, val type: JvmType?) context (KtAnalysisSession) internal fun KtValueArgument.getExpectedParameterInfo(parameterIndex: Int): ParameterInfo { val parameterNameAsString = getArgumentName()?.asName?.asString() val argumentExpression = getArgumentExpression() val expectedArgumentType = argumentExpression?.getKtType() - val parameterName = parameterNameAsString?.let { sequenceOf(it) } ?: expectedArgumentType?.let { NAME_SUGGESTER.suggestTypeNames(it) } + val parameterNames = parameterNameAsString?.let { sequenceOf(it) } ?: expectedArgumentType?.let { NAME_SUGGESTER.suggestTypeNames(it) } val parameterType = expectedArgumentType?.convertToJvmType(argumentExpression) - return ParameterInfo(parameterName?.toMutableList() ?: mutableListOf("p$parameterIndex"), parameterType) + return ParameterInfo(parameterNames?.toList() ?: listOf("p$parameterIndex"), parameterType) } context (KtAnalysisSession) @@ -145,7 +143,7 @@ internal fun JvmType.toKtType(useSitePosition: PsiElement): KtType? = when (this try { asKtType(useSitePosition) } catch (e: Error) { - // Some requests from Java side does not have a type. For example, in `var foo = dep.foo();`, we cannot guess + // Some requests from Java side do not have a type. For example, in `var foo = dep.foo();`, we cannot guess // the type of `foo()`. In this case, the request passes "PsiType:null" whose name is "null" as a text. The analysis // API cannot get a KtType from this weird type. We return `Any?` for this case. builtinTypes.NULLABLE_ANY diff --git a/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/createKotlinCallableFromUsage.kt b/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/createKotlinCallableFromUsage.kt index 5a363cc9f8ca..f5820dd81e93 100644 --- a/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/createKotlinCallableFromUsage.kt +++ b/plugins/kotlin/code-insight/kotlin.code-insight.k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/quickFixes/createFromUsage/createKotlinCallableFromUsage.kt @@ -15,13 +15,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.KtClassOrObjectSymbol import org.jetbrains.kotlin.analysis.api.types.KtType import org.jetbrains.kotlin.idea.KotlinLanguage import org.jetbrains.kotlin.lexer.KtTokens -import org.jetbrains.kotlin.psi.KtCallExpression -import org.jetbrains.kotlin.psi.KtClass -import org.jetbrains.kotlin.psi.KtClassOrObject -import org.jetbrains.kotlin.psi.KtElement -import org.jetbrains.kotlin.psi.KtEnumEntry -import org.jetbrains.kotlin.psi.KtExpression -import org.jetbrains.kotlin.psi.KtSimpleNameExpression +import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType @@ -65,11 +59,10 @@ internal fun buildRequests(callExpression: KtCallExpression): Map() val packageNameOfReceiver = calleeExpression.getReceiverOrContainerClassPackageName() - if (packageNameOfReceiver != null && packageNameOfReceiver == callExpression.containingKtFile.packageFqName) { - modifiers.add(JvmModifier.PUBLIC) - } + val modifiers = if (packageNameOfReceiver != null && packageNameOfReceiver == callExpression.containingKtFile.packageFqName) + listOf(JvmModifier.PUBLIC) + else listOf() requests[it] = CreateMethodFromKotlinUsageRequest(callExpression, modifiers, receiverExpression) } } @@ -139,7 +132,7 @@ private fun LinkedHashMap.registerCreateAbstractC ) { val jvmClassWrapper = JvmClassWrapperForKtClass(abstractContainerClass) this[jvmClassWrapper] = CreateMethodFromKotlinUsageRequest( - callExpression, mutableSetOf(), receiverExpression, isAbstractClassOrInterface = true + callExpression, setOf(), receiverExpression, isAbstractClassOrInterface = true ) } @@ -148,5 +141,5 @@ private fun LinkedHashMap.registerCreateExtension ) { val containerClassForExtension = calleeExpression.getNonStrictParentOfType() ?: calleeExpression.containingKtFile val jvmClassWrapper = JvmClassWrapperForKtClass(containerClassForExtension) - this[jvmClassWrapper] = CreateMethodFromKotlinUsageRequest(callExpression, mutableSetOf(), receiverExpression, isExtension = true) + this[jvmClassWrapper] = CreateMethodFromKotlinUsageRequest(callExpression, setOf(), receiverExpression, isExtension = true) } \ No newline at end of file