From a7a561952389f3afa3eee3a1db48ee1acb59932c Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 6 Jan 2016 00:51:00 +0300 Subject: [PATCH] Change to Star-Projection Quick-Fix: Convert to Kotlin & refactor --- .../kotlin/idea/KotlinBundle.properties | 2 - .../quickfix/ChangeToStarProjectionFix.kt | 75 ++++++------------- .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 5 +- 3 files changed, 23 insertions(+), 59 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties index d4b8a91eba8..fa1031662b6 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties @@ -9,8 +9,6 @@ add.init.keyword.in.whole.project.family=Add 'init' keyword in whole project insert.delegation.call=Insert ''{0}()'' call make.class.annotation.class=Make ''{0}'' an annotation class make.class.annotation.class.family=Make class an annotation class -change.to.star.projection=Change type arguments to {0} -change.to.star.projection.family=Change to star projection remove.parts.from.property=Remove {0} from property remove.parts.from.property.family=Remove parts from property remove.psi.element.family=Remove element diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToStarProjectionFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToStarProjectionFix.kt index e3434c982da..1ae9e3d99b2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToStarProjectionFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeToStarProjectionFix.kt @@ -14,64 +14,31 @@ * limitations under the License. */ -package org.jetbrains.kotlin.idea.quickfix; +package org.jetbrains.kotlin.idea.quickfix -import com.intellij.codeInsight.intention.IntentionAction; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.diagnostics.Diagnostic; -import org.jetbrains.kotlin.idea.KotlinBundle; -import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil; -import org.jetbrains.kotlin.psi.*; -import org.jetbrains.kotlin.types.expressions.TypeReconstructionUtil; +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType -public class ChangeToStarProjectionFix extends KotlinQuickFixAction { - public ChangeToStarProjectionFix(@NotNull KtTypeElement element) { - super(element); +class ChangeToStarProjectionFix(element: KtTypeElement) : KotlinQuickFixAction(element) { + override fun getFamilyName() = "Change to star projection" + + override fun getText() = "Change type arguments to <${element.typeArgumentsAsTypes.joinToString { "*" }}>" + + public override fun invoke(project: Project, editor: Editor?, file: KtFile) { + val star = KtPsiFactory(file).createStar() + element.typeArgumentsAsTypes.forEach { it?.replace(star) } } - @NotNull - @Override - public String getText() { - String stars = TypeReconstructionUtil.getTypeNameAndStarProjectionsString("", getElement().getTypeArgumentsAsTypes().size()); - return KotlinBundle.message("change.to.star.projection", stars); - } - - @NotNull - @Override - public String getFamilyName() { - return KotlinBundle.message("change.to.star.projection.family"); - } - - @Override - public void invoke(@NotNull Project project, Editor editor, @NotNull KtFile file) throws IncorrectOperationException { - for (KtTypeReference typeReference : getElement().getTypeArgumentsAsTypes()) { - if (typeReference != null) { - typeReference.replace(KtPsiFactoryKt.KtPsiFactory(file).createStar()); - } + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): IntentionAction? { + val typeReference = diagnostic.psiElement.getNonStrictParentOfType()?.right + ?: diagnostic.psiElement.getNonStrictParentOfType() + val typeElement = typeReference?.typeElement ?: return null + return ChangeToStarProjectionFix(typeElement) } } - - public static KotlinSingleIntentionActionFactory createFactory() { - return new KotlinSingleIntentionActionFactory() { - @Override - public IntentionAction createAction(@NotNull Diagnostic diagnostic) { - KtBinaryExpressionWithTypeRHS expression = QuickFixUtil - .getParentElementOfType(diagnostic, KtBinaryExpressionWithTypeRHS.class); - KtTypeReference typeReference; - if (expression == null) { - typeReference = QuickFixUtil.getParentElementOfType(diagnostic, KtTypeReference.class); - } - else { - typeReference = expression.getRight(); - } - if (typeReference == null) return null; - KtTypeElement typeElement = typeReference.getTypeElement(); - assert typeElement != null; - return new ChangeToStarProjectionFix(typeElement); - } - }; - } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 0f63b9c86f7..cd7e4071c94 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -195,9 +195,8 @@ class QuickFixRegistrar : QuickFixContributor { TYPE_ARGUMENTS_REDUNDANT_IN_SUPER_QUALIFIER.registerFactory(RemovePsiElementSimpleFix.createRemoveTypeArgumentsFactory()) - val changeToStarProjectionFactory = ChangeToStarProjectionFix.createFactory() - UNCHECKED_CAST.registerFactory(changeToStarProjectionFactory) - CANNOT_CHECK_FOR_ERASED.registerFactory(changeToStarProjectionFactory) + UNCHECKED_CAST.registerFactory(ChangeToStarProjectionFix) + CANNOT_CHECK_FOR_ERASED.registerFactory(ChangeToStarProjectionFix) INACCESSIBLE_OUTER_CLASS_EXPRESSION.registerFactory(AddModifierFix.createFactory(INNER_KEYWORD, KtClass::class.java))