From eaf320a14490c7b38663f3f3e6debc6b07c1b4c0 Mon Sep 17 00:00:00 2001 From: oboenikui Date: Sat, 29 Jul 2023 21:25:50 +0900 Subject: [PATCH 1/2] Fixed a problem in which functions and constructors of external libraries could not be filled. --- build.gradle | 11 +------ .../inspections/BaseFillClassInspection.kt | 30 +++++++++---------- src/main/resources/META-INF/plugin.xml | 2 +- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/build.gradle b/build.gradle index c02f059..3bb74a8 100644 --- a/build.gradle +++ b/build.gradle @@ -14,14 +14,11 @@ plugins { id 'org.jetbrains.kotlin.jvm' version '1.8.10' } -apply plugin: 'org.jetbrains.intellij' -apply plugin: 'kotlin' - sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 intellij { - version = '2023.1.4' + version = '2023.2' plugins = ['Kotlin', 'java'] pluginName = 'kotlin-fill-class' updateSinceUntilBuild = false @@ -33,15 +30,9 @@ intellij { } } -repositories { - mavenCentral() -} - group 'com.github.suusan2go.kotlin-fill-class' version '1.0.21' -sourceCompatibility = 11 - repositories { mavenCentral() } diff --git a/src/main/kotlin/com/github/suusan2go/kotlinfillclass/inspections/BaseFillClassInspection.kt b/src/main/kotlin/com/github/suusan2go/kotlinfillclass/inspections/BaseFillClassInspection.kt index e0ac44c..744c902 100644 --- a/src/main/kotlin/com/github/suusan2go/kotlinfillclass/inspections/BaseFillClassInspection.kt +++ b/src/main/kotlin/com/github/suusan2go/kotlinfillclass/inspections/BaseFillClassInspection.kt @@ -32,6 +32,7 @@ import org.jetbrains.kotlin.idea.inspections.AbstractKotlinInspection import org.jetbrains.kotlin.idea.inspections.findExistingEditor import org.jetbrains.kotlin.idea.intentions.callExpression import org.jetbrains.kotlin.idea.references.mainReference +import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor import org.jetbrains.kotlin.idea.util.textRangeIn import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor @@ -69,7 +70,7 @@ abstract class BaseFillClassInspection( ) = valueArgumentListVisitor(fun(element: KtValueArgumentList) { val callElement = element.parent as? KtCallElement ?: return val descriptors = analyze(callElement).ifEmpty { return } - val description = if (descriptors.any { (_, descriptor) -> descriptor is ClassConstructorDescriptor }) { + val description = if (descriptors.any { descriptor -> descriptor is ClassConstructorDescriptor }) { getConstructorPromptTitle() } else { getFunctionPromptTitle() @@ -113,22 +114,21 @@ abstract class BaseFillClassInspection( } } -private fun analyze(call: KtCallElement): List> { +private fun analyze(call: KtCallElement): List { val context = call.analyze(BodyResolveMode.PARTIAL) val resolvedCall = call.calleeExpression?.getResolvedCall(context) val descriptors = if (resolvedCall != null) { val descriptor = resolvedCall.resultingDescriptor as? FunctionDescriptor ?: return emptyList() - val func = descriptor.psiElement as? KtFunction ?: return emptyList() - listOf(func to descriptor) + listOf(descriptor) } else { call.calleeExpression?.mainReference?.multiResolve(false).orEmpty().mapNotNull { val func = it.element as? KtFunction ?: return@mapNotNull null - val descriptor = context[DECLARATION_TO_DESCRIPTOR, func] as? FunctionDescriptor ?: return@mapNotNull null - func to descriptor + val descriptor = func.descriptor as? FunctionDescriptor ?: return@mapNotNull null + descriptor } } val argumentSize = call.valueArguments.size - return descriptors.filter { (_, descriptor) -> + return descriptors.filter { descriptor -> descriptor !is JavaCallableMemberDescriptor && descriptor.valueParameters.filterNot { it.isVararg }.size > argumentSize } @@ -156,7 +156,7 @@ open class FillClassFix( ?: ImaginaryEditor(project, argumentList.containingFile.viewProvider.document) if (descriptors.size == 1 || editor is ImaginaryEditor) { argumentList.fillArgumentsAndFormat( - descriptor = descriptors.first().second, + descriptor = descriptors.first(), editor = editor, lambdaArgument = lambdaArgument, ) @@ -169,10 +169,10 @@ open class FillClassFix( private fun createListPopup( argumentList: KtValueArgumentList, lambdaArgument: KtLambdaArgument?, - descriptors: List>, + descriptors: List, editor: Editor, ): BaseListPopupStep { - val functionName = descriptors.first().let { (_, descriptor) -> + val functionName = descriptors.first().let { descriptor -> if (descriptor is ClassConstructorDescriptor) { descriptor.containingDeclaration.name.asString() } else { @@ -180,15 +180,15 @@ open class FillClassFix( } } val functions = descriptors - .sortedBy { (_, descriptor) -> descriptor.valueParameters.size } - .associate { (function, descriptor) -> - val key = function.valueParameters.joinToString( + .sortedBy { descriptor -> descriptor.valueParameters.size } + .associateBy { descriptor -> + val key = descriptor.valueParameters.joinToString( separator = ", ", prefix = "$functionName(", postfix = ")", - transform = { "${it.name}: ${it.typeReference?.text ?: ""}" }, + transform = { "${it.name}: ${it.type}" }, ) - key to descriptor + key } return object : BaseListPopupStep("Choose Function", functions.keys.toList()) { override fun isAutoSelectionEnabled() = false diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 22a6c21..bd1450c 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -18,7 +18,7 @@ - + com.intellij.modules.lang org.jetbrains.kotlin From 5a7bd32b2c8bfb08a3e16072e649cd2dd14a0338 Mon Sep 17 00:00:00 2001 From: oboenikui Date: Sun, 30 Jul 2023 01:28:01 +0900 Subject: [PATCH 2/2] optimize import --- .../kotlinfillclass/inspections/BaseFillClassInspection.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/kotlin/com/github/suusan2go/kotlinfillclass/inspections/BaseFillClassInspection.kt b/src/main/kotlin/com/github/suusan2go/kotlinfillclass/inspections/BaseFillClassInspection.kt index 744c902..a317c48 100644 --- a/src/main/kotlin/com/github/suusan2go/kotlinfillclass/inspections/BaseFillClassInspection.kt +++ b/src/main/kotlin/com/github/suusan2go/kotlinfillclass/inspections/BaseFillClassInspection.kt @@ -15,7 +15,6 @@ import com.intellij.openapi.ui.popup.PopupStep import com.intellij.openapi.ui.popup.util.BaseListPopupStep import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiDocumentManager -import org.jetbrains.kotlin.backend.jvm.ir.psiElement import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.isFunctionType import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor @@ -49,7 +48,6 @@ import org.jetbrains.kotlin.psi.KtValueArgumentList import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments import org.jetbrains.kotlin.psi.valueArgumentListVisitor -import org.jetbrains.kotlin.resolve.BindingContext.DECLARATION_TO_DESCRIPTOR import org.jetbrains.kotlin.resolve.calls.components.isVararg import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode