From 38a27b3e6b5cbb87b45610968ae726ed170cffdc Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Mon, 22 Oct 2012 18:07:34 +0400 Subject: [PATCH] KT-2169 "Specify type" fix should be accessible for the whole function signature #KT-2169 fixed --- .../SpecifyTypeExplicitlyAction.java | 8 ++--- .../quickfix/SpecifyTypeExplicitlyFix.java | 33 +++++++++++++++---- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java b/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java index cc5941fb737..5df8cbf580a 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java +++ b/idea/src/org/jetbrains/jet/plugin/intentions/SpecifyTypeExplicitlyAction.java @@ -133,7 +133,7 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction { if (ErrorUtils.isErrorType(getTypeForDeclaration(declaration))) { return false; } - return !isDisabledForError() || !hasPublicMemberDiagnostic(declaration); + return !hasPublicMemberDiagnostic(declaration); } @@ -149,7 +149,7 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction { } @NotNull - private static JetType getTypeForDeclaration(@NotNull JetNamedDeclaration declaration) { + protected static JetType getTypeForDeclaration(@NotNull JetNamedDeclaration declaration) { BindingContext bindingContext = AnalyzeSingleFileUtil.getContextForSingleFile((JetFile) declaration.getContainingFile()); DeclarationDescriptor descriptor = bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, declaration); @@ -167,10 +167,6 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction { return type == null ? ErrorUtils.createErrorType("null type") : type; } - protected boolean isDisabledForError() { - return true; - } - public static void addTypeAnnotation(Project project, JetProperty property, @NotNull JetType exprType) { if (property.getTypeRef() != null) return; PsiElement anchor = property.getNameIdentifier(); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/SpecifyTypeExplicitlyFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/SpecifyTypeExplicitlyFix.java index f33d24bb835..1ce41a30e22 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/SpecifyTypeExplicitlyFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/SpecifyTypeExplicitlyFix.java @@ -16,8 +16,17 @@ package org.jetbrains.jet.plugin.quickfix; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; +import com.intellij.psi.PsiElement; +import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetNamedDeclaration; +import org.jetbrains.jet.lang.psi.JetNamedFunction; +import org.jetbrains.jet.lang.psi.JetProperty; +import org.jetbrains.jet.lang.types.ErrorUtils; +import org.jetbrains.jet.plugin.JetBundle; import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction; /** @@ -26,14 +35,26 @@ import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction; */ @SuppressWarnings("IntentionDescriptionNotFoundInspection") public class SpecifyTypeExplicitlyFix extends SpecifyTypeExplicitlyAction { - @NotNull @Override - public String getText() { - return StringUtil.capitalize(super.getText().toLowerCase()); + public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { + //noinspection unchecked + JetNamedDeclaration declaration = PsiTreeUtil.getParentOfType(element, JetProperty.class, JetNamedFunction.class); + if (declaration instanceof JetProperty) { + setText(getQuickFixText(JetBundle.message("specify.type.explicitly.add.action.name"))); + } + else if (declaration instanceof JetNamedFunction) { + setText(getQuickFixText(JetBundle.message("specify.type.explicitly.add.return.type.action.name"))); + } + else { + assert false : "Couldn't find property or function"; + } + + return !ErrorUtils.isErrorType(getTypeForDeclaration(declaration)); } - @Override - protected boolean isDisabledForError() { - return false; + // There is an implicit rule that quick fix text has only first letter capitalized, while intention text + @NotNull + private static String getQuickFixText(@NotNull String intentionText) { + return StringUtil.capitalize(intentionText.toLowerCase()); } }