From 720dad5207869efdff9ead835afec7d5d43f9401 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Fri, 4 Sep 2015 19:09:51 +0300 Subject: [PATCH] Minor code simplifications --- .../kotlin/idea/JetBundle.properties | 1 - .../idea/quickfix/AddNameToArgumentFix.kt | 73 +++++++------------ .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 2 +- 3 files changed, 29 insertions(+), 47 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties index 7b71fca33a6..58cd86d0973 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/JetBundle.properties @@ -200,7 +200,6 @@ change.function.signature.action.multiple=Change function signature... change.function.signature.family=Change function signature change.function.signature.chooser.title=Choose signature change.function.signature.action=Change function signature -add.name.to.parameter.name.chooser.title=Choose parameter name replace.java.class.argument=Replace javaClass() with T::class replace.java.class.argument.family=Replace javaClass() with T::class diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddNameToArgumentFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddNameToArgumentFix.kt index b006914934a..a38b178bdcb 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddNameToArgumentFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddNameToArgumentFix.kt @@ -16,10 +16,7 @@ package org.jetbrains.kotlin.idea.quickfix -import com.google.common.collect.Lists import com.intellij.codeInsight.intention.IntentionAction -import com.intellij.openapi.application.ApplicationManager -import com.intellij.openapi.command.CommandProcessor import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import com.intellij.openapi.ui.popup.JBPopupFactory @@ -27,26 +24,27 @@ import com.intellij.openapi.ui.popup.ListPopupStep import com.intellij.openapi.ui.popup.PopupStep import com.intellij.openapi.ui.popup.util.BaseListPopupStep import com.intellij.psi.PsiDocumentManager -import com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.diagnostics.Diagnostic -import org.jetbrains.kotlin.idea.JetBundle import org.jetbrains.kotlin.idea.JetIcons import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.core.mapArgumentsToParameters -import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil +import org.jetbrains.kotlin.idea.util.application.executeWriteCommand import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.JetCallElement import org.jetbrains.kotlin.psi.JetFile import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.JetValueArgument +import org.jetbrains.kotlin.psi.psiUtil.getParentOfType import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.types.checker.JetTypeChecker +import java.util.* public class AddNameToArgumentFix(argument: JetValueArgument, private val possibleNames: List) : JetIntentionAction(argument) { override fun invoke(project: Project, editor: Editor?, file: JetFile) { if (possibleNames.size() == 1 || editor == null || !editor.component.isShowing) { - addName(project, element, possibleNames.get(0)) + addName(project, element, possibleNames.first()) } else { chooseNameAndAdd(project, editor) @@ -58,7 +56,7 @@ public class AddNameToArgumentFix(argument: JetValueArgument, private val possib } private fun getNamePopup(project: Project): ListPopupStep { - return object : BaseListPopupStep(JetBundle.message("add.name.to.parameter.name.chooser.title"), possibleNames) { + return object : BaseListPopupStep("Choose parameter name", possibleNames) { override fun onChosen(selectedName: String, finalChoice: Boolean): PopupStep { if (finalChoice) { addName(project, element, selectedName) @@ -81,21 +79,27 @@ public class AddNameToArgumentFix(argument: JetValueArgument, private val possib override fun getFamilyName() = "Add Name to Argument" - companion object { - private fun generatePossibleNames(argument: JetValueArgument): List { - val callElement = PsiTreeUtil.getParentOfType(argument, JetCallElement::class.java) - assert(callElement != null, "The argument has to be inside a function or constructor call") + companion object : JetSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): IntentionAction? { + val argument = diagnostic.psiElement.getParentOfType(false) ?: return null + val possibleNames = generatePossibleNames(argument) + if (possibleNames.isEmpty()) return null + return AddNameToArgumentFix(argument, possibleNames) + } - val context = argument.analyze() - val resolvedCall = callElement!!.getResolvedCall(context) ?: return emptyList() + private fun generatePossibleNames(argument: JetValueArgument): List { + val callElement = argument.getParentOfType(true) ?: return emptyList() + + val context = argument.analyze(BodyResolveMode.PARTIAL) + val resolvedCall = callElement.getResolvedCall(context) ?: return emptyList() val callableDescriptor = resolvedCall.resultingDescriptor - val argExpression = argument.getArgumentExpression() - val type = if (argExpression != null) context.getType(argExpression) else null + val argumentExpression = argument.getArgumentExpression() + val argumentType = argumentExpression?.let { context.getType(it) } val usedParameters = resolvedCall.call.mapArgumentsToParameters(callableDescriptor).values().toSet() - val names = Lists.newArrayList() + val names = ArrayList() for (parameter in callableDescriptor.valueParameters) { - if (!usedParameters.contains(parameter) && (type == null || JetTypeChecker.DEFAULT.isSubtypeOf(type, parameter.type))) { + if (parameter !in usedParameters && (argumentType == null || JetTypeChecker.DEFAULT.isSubtypeOf(argumentType, parameter.type))) { names.add(parameter.name.asString()) } } @@ -105,37 +109,16 @@ public class AddNameToArgumentFix(argument: JetValueArgument, private val possib private fun addName(project: Project, argument: JetValueArgument, name: String) { PsiDocumentManager.getInstance(project).commitAllDocuments() - CommandProcessor.getInstance().executeCommand( - project, - object : Runnable { - override fun run() { - ApplicationManager.getApplication().runWriteAction(object : Runnable { - override fun run() { - val newArgument = getParsedArgumentWithName(name, argument) - argument.replace(newArgument) - } - }) - } - }, - "Add name to argument...", - null) + project.executeWriteCommand("Add name to argument...") { + val newArgument = getParsedArgumentWithName(name, argument) + argument.replace(newArgument) + } } private fun getParsedArgumentWithName(name: String, argument: JetValueArgument): JetValueArgument { val argumentExpression = argument.getArgumentExpression() - assert(argumentExpression != null, "Argument should be already parsed.") - return JetPsiFactory(argument).createArgument(argumentExpression!!, Name.identifier(name), argument.getSpreadElement() != null) - } - - public fun createFactory(): JetIntentionActionsFactory { - return object : JetSingleIntentionActionFactory() { - override fun createAction(diagnostic: Diagnostic): IntentionAction? { - val argument = QuickFixUtil.getParentElementOfType(diagnostic, JetValueArgument::class.java) ?: return null - val possibleNames = generatePossibleNames(argument) - if (possibleNames.isEmpty()) return null - return AddNameToArgumentFix(argument, possibleNames) - } - } + ?: error("Argument should be already parsed.") + return JetPsiFactory(argument).createArgument(argumentExpression, Name.identifier(name), argument.getSpreadElement() != null) } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index abe788d4b15..0e8285f3eee 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -81,7 +81,7 @@ public class QuickFixRegistrar : QuickFixContributor { NON_VARARG_SPREAD.registerFactory(RemovePsiElementSimpleFix.createRemoveSpreadFactory()) - MIXING_NAMED_AND_POSITIONED_ARGUMENTS.registerFactory(AddNameToArgumentFix.createFactory()) + MIXING_NAMED_AND_POSITIONED_ARGUMENTS.registerFactory(AddNameToArgumentFix) NON_MEMBER_FUNCTION_NO_BODY.registerFactory(addFunctionBodyFactory)