From 45b5157ecf8f9a6a371c80e50b79be9116f1e60b Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Sat, 20 Aug 2016 13:53:32 +0200 Subject: [PATCH] Clean up intentions package --- .../intentions/ConvertFunctionToPropertyIntention.kt | 10 +++------- .../ConvertPropertyInitializerToGetterIntention.kt | 2 +- .../intentions/ConvertPropertyToFunctionIntention.kt | 2 +- .../idea/intentions/CreateKotlinSubClassIntention.kt | 8 +++++--- .../intentions/IntroduceBackingPropertyIntention.kt | 2 +- ...placeSubstringWithSubstringBeforeAfterIntentions.kt | 6 ++---- .../intentions/ReplaceSubstringWithTakeIntention.kt | 2 -- .../kotlin/idea/intentions/SimplifyForIntention.kt | 4 ++-- .../idea/intentions/SpecifyTypeExplicitlyIntention.kt | 3 --- idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt | 3 ++- .../intentions/IfToWhenIntention.kt | 5 +---- .../conventionNameCalls/ReplaceContainsIntention.kt | 3 --- .../idea/intentions/loopToCallChain/matchAndConvert.kt | 1 - .../kotlin/idea/intentions/loopToCallChain/utils.kt | 6 +++--- 14 files changed, 21 insertions(+), 36 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertFunctionToPropertyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertFunctionToPropertyIntention.kt index 55d7f983e81..ebe211aed27 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertFunctionToPropertyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertFunctionToPropertyIntention.kt @@ -31,14 +31,10 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde -import org.jetbrains.kotlin.idea.refactoring.checkConflictsInteractively -import org.jetbrains.kotlin.idea.refactoring.reportDeclarationConflict -import org.jetbrains.kotlin.idea.refactoring.CallableRefactoring -import org.jetbrains.kotlin.idea.refactoring.getAffectedCallables -import org.jetbrains.kotlin.idea.refactoring.getContainingScope +import org.jetbrains.kotlin.idea.core.ShortenReferences +import org.jetbrains.kotlin.idea.refactoring.* import org.jetbrains.kotlin.idea.references.KtSimpleNameReference import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers -import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.util.application.executeWriteCommand import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.load.java.JvmAbi @@ -182,7 +178,7 @@ class ConvertFunctionToPropertyIntention : SelfTargetingIntention convertFunction(it, psiFactory) - is PsiMethod -> it.setName(newGetterName) + is PsiMethod -> it.name = newGetterName } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertPropertyInitializerToGetterIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertPropertyInitializerToGetterIntention.kt index f2b882ec32e..d03311e6af8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertPropertyInitializerToGetterIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertPropertyInitializerToGetterIntention.kt @@ -56,7 +56,7 @@ class ConvertPropertyInitializerToGetterIntention : SelfTargetingRangeIntention< property.add(getter) } - property.setInitializer(null) + property.initializer = null } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertPropertyToFunctionIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertPropertyToFunctionIntention.kt index 155f04cbbcb..d30f6256528 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertPropertyToFunctionIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertPropertyToFunctionIntention.kt @@ -171,7 +171,7 @@ class ConvertPropertyToFunctionIntention : SelfTargetingIntention(Kt callables.forEach { when (it) { is KtProperty -> convertProperty(it, kotlinPsiFactory) - is PsiMethod -> it.setName(newName) + is PsiMethod -> it.name = newName } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/CreateKotlinSubClassIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/CreateKotlinSubClassIntention.kt index 7f694695c73..acef3c2cc83 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/CreateKotlinSubClassIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/CreateKotlinSubClassIntention.kt @@ -24,19 +24,21 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.module.ModuleUtilCore import com.intellij.openapi.project.Project import com.intellij.openapi.util.TextRange -import com.intellij.psi.* +import com.intellij.psi.JavaDirectoryService import com.intellij.refactoring.rename.PsiElementRenameHandler import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.idea.core.KotlinNameSuggester +import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.core.overrideImplement.ImplementMembersHandler import org.jetbrains.kotlin.idea.refactoring.getOrCreateKotlinFile -import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.KtPsiFactory.ClassHeaderBuilder import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.resolve.ModifiersChecker +import java.lang.IllegalArgumentException +import java.lang.IllegalStateException private const val IMPL_SUFFIX = "Impl" @@ -131,7 +133,7 @@ class CreateKotlinSubClassIntention : SelfTargetingRangeIntention(KtCla val dlg = chooseSubclassToCreate(baseClass, baseName) ?: return val targetName = dlg.className val file = getOrCreateKotlinFile("$targetName.kt", dlg.targetDirectory)!! - val builder = buildClassHeader(targetName, baseClass, "${baseClass.fqName!!.asString()}") + val builder = buildClassHeader(targetName, baseClass, baseClass.fqName!!.asString()) file.add(factory.createClass(builder.asString())) val klass = file.getChildOfType()!! ShortenReferences.DEFAULT.process(klass) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/IntroduceBackingPropertyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/IntroduceBackingPropertyIntention.kt index 5c4383c66c3..c1a7e7e677c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/IntroduceBackingPropertyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/IntroduceBackingPropertyIntention.kt @@ -74,7 +74,7 @@ class IntroduceBackingPropertyIntention(): SelfTargetingIntention(Kt } } - property.setInitializer(null) + property.initializer = null } private fun createGetter(element: KtProperty) { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSubstringWithSubstringBeforeAfterIntentions.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSubstringWithSubstringBeforeAfterIntentions.kt index 73f65dc7aa0..50be07a3397 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSubstringWithSubstringBeforeAfterIntentions.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSubstringWithSubstringBeforeAfterIntentions.kt @@ -18,10 +18,8 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange -import org.jetbrains.kotlin.idea.intentions.branchedTransformations.evaluatesTo -import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe -import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode +import org.jetbrains.kotlin.psi.KtDotQualifiedExpression +import org.jetbrains.kotlin.psi.KtExpression class ReplaceSubstringWithSubstringAfterIntention : ReplaceSubstringIntention("Replace 'substring' call with 'substringAfter' call") { override fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange? { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSubstringWithTakeIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSubstringWithTakeIntention.kt index 6e17dd7097c..0702a652d22 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSubstringWithTakeIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSubstringWithTakeIntention.kt @@ -19,8 +19,6 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.psi.KtDotQualifiedExpression -import org.jetbrains.kotlin.psi.KtPsiFactory -import org.jetbrains.kotlin.psi.createExpressionByPattern class ReplaceSubstringWithTakeIntention : ReplaceSubstringIntention("Replace 'substring' call with 'take' call") { override fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange? { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyForIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyForIntention.kt index 7ca5530952a..4feae149846 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyForIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/SimplifyForIntention.kt @@ -147,11 +147,11 @@ class SimplifyForIntention : SelfTargetingRangeIntention( val applicableUsage = getDataIfUsageIsApplicable(it, context) if (applicableUsage != null) { val descriptorName = applicableUsage.descriptor.name.asString() - if (descriptorName.equals("key") || descriptorName.equals("getKey")) { + if (descriptorName == "key" || descriptorName == "getKey") { process(0, applicableUsage) return@forEach true } - else if (descriptorName.equals("value") || descriptorName.equals("getValue")) { + else if (descriptorName == "value" || descriptorName == "getValue") { process(1, applicableUsage) return@forEach true } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt index 2e4abf10a8e..4aa609aafe4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/SpecifyTypeExplicitlyIntention.kt @@ -30,9 +30,6 @@ import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers import org.jetbrains.kotlin.idea.util.application.runWriteAction -import org.jetbrains.kotlin.idea.util.approximateWithResolvableType -import org.jetbrains.kotlin.idea.util.getResolutionScope -import org.jetbrains.kotlin.idea.util.isResolvableInScope import org.jetbrains.kotlin.idea.util.getResolutionScope import org.jetbrains.kotlin.idea.util.getResolvableApproximations import org.jetbrains.kotlin.psi.* diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt index cd4c0b4aec6..2c24454b6a0 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt @@ -35,6 +35,7 @@ import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.isFlexible +import java.lang.IllegalArgumentException fun KtCallableDeclaration.setType(type: KotlinType, shortenReferences: Boolean = true) { if (type.isError) return @@ -92,7 +93,7 @@ fun splitPropertyDeclaration(property: KtProperty): KtBinaryExpression { assignment = parent.addAfter(assignment, property) as KtBinaryExpression parent.addAfter(psiFactory.createNewLine(), property) - property.setInitializer(null) + property.initializer = null if (explicitTypeToSet != null) { property.setType(explicitTypeToSet) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt index 6ece5ba1c28..de3196934e3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt @@ -18,7 +18,6 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange -import com.intellij.psi.PsiComment import com.intellij.psi.PsiElement import com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention @@ -152,9 +151,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention(KtIfExpres val result = element.replace(whenExpression) (if (applyFullCommentSaver) fullCommentSaver else elementCommentSaver).restore(result) - toDelete.forEach { - it.delete() - } + toDelete.forEach(PsiElement::delete) } private fun MutableList.addOrBranches(expression: KtExpression): List { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceContainsIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceContainsIntention.kt index fc08d7dd1c0..017e0037aae 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceContainsIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/conventionNameCalls/ReplaceContainsIntention.kt @@ -18,13 +18,10 @@ package org.jetbrains.kotlin.idea.intentions.conventionNameCalls import com.intellij.codeInsight.intention.HighPriorityAction import com.intellij.openapi.editor.Editor -import com.intellij.openapi.project.Project import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze -import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde.getAnyDeclaration import org.jetbrains.kotlin.idea.intentions.* -import org.jetbrains.kotlin.idea.refactoring.canRefactor import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt index 4fb3f1aa419..db5efec3930 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt @@ -38,7 +38,6 @@ import org.jetbrains.kotlin.resolve.bindingContextUtil.getDataFlowInfo import org.jetbrains.kotlin.resolve.calls.smartcasts.ExplicitSmartCasts import org.jetbrains.kotlin.resolve.calls.smartcasts.ImplicitSmartCasts import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode -import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.utils.addToStdlib.check object MatcherRegistrar { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/utils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/utils.kt index 8793aea9498..df0b1174a11 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/utils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/utils.kt @@ -125,7 +125,7 @@ fun generateLambda(expression: KtExpression, vararg inputVariables: KtCallableDe private fun KtLambdaExpression.findParameterUsages(lambdaParam: KtParameter, context: KtExpression): Collection { val bindingContext = analyzeInContext(context) val lambdaParamDescriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, lambdaParam] - return collectDescendantsOfType { + return collectDescendantsOfType { it.mainReference.resolveToDescriptors(bindingContext).singleOrNull() == lambdaParamDescriptor } } @@ -356,11 +356,11 @@ fun KtExpression.isStableInLoop(loop: KtLoopExpression, checkNoOtherUsagesInLoop } fun KtExpression.containsEmbeddedBreakOrContinue(): Boolean { - return anyDescendantOfType(::isEmbeddedBreakOrContinue) + return anyDescendantOfType(::isEmbeddedBreakOrContinue) } fun KtExpression.countEmbeddedBreaksAndContinues(): Int { - return collectDescendantsOfType(::isEmbeddedBreakOrContinue).size + return collectDescendantsOfType(::isEmbeddedBreakOrContinue).size } private fun isEmbeddedBreakOrContinue(expression: KtExpressionWithLabel): Boolean {