KT-2169 "Specify type" fix should be accessible for the whole function signature

#KT-2169 fixed
This commit is contained in:
Evgeny Gerashchenko
2012-10-22 18:07:34 +04:00
parent 0c43e77ac0
commit 38a27b3e6b
2 changed files with 29 additions and 12 deletions
@@ -133,7 +133,7 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
if (ErrorUtils.isErrorType(getTypeForDeclaration(declaration))) { if (ErrorUtils.isErrorType(getTypeForDeclaration(declaration))) {
return false; return false;
} }
return !isDisabledForError() || !hasPublicMemberDiagnostic(declaration); return !hasPublicMemberDiagnostic(declaration);
} }
@@ -149,7 +149,7 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
} }
@NotNull @NotNull
private static JetType getTypeForDeclaration(@NotNull JetNamedDeclaration declaration) { protected static JetType getTypeForDeclaration(@NotNull JetNamedDeclaration declaration) {
BindingContext bindingContext = AnalyzeSingleFileUtil.getContextForSingleFile((JetFile) declaration.getContainingFile()); BindingContext bindingContext = AnalyzeSingleFileUtil.getContextForSingleFile((JetFile) declaration.getContainingFile());
DeclarationDescriptor descriptor = bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, declaration); 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; return type == null ? ErrorUtils.createErrorType("null type") : type;
} }
protected boolean isDisabledForError() {
return true;
}
public static void addTypeAnnotation(Project project, JetProperty property, @NotNull JetType exprType) { public static void addTypeAnnotation(Project project, JetProperty property, @NotNull JetType exprType) {
if (property.getTypeRef() != null) return; if (property.getTypeRef() != null) return;
PsiElement anchor = property.getNameIdentifier(); PsiElement anchor = property.getNameIdentifier();
@@ -16,8 +16,17 @@
package org.jetbrains.jet.plugin.quickfix; 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.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull; 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; import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction;
/** /**
@@ -26,14 +35,26 @@ import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction;
*/ */
@SuppressWarnings("IntentionDescriptionNotFoundInspection") @SuppressWarnings("IntentionDescriptionNotFoundInspection")
public class SpecifyTypeExplicitlyFix extends SpecifyTypeExplicitlyAction { public class SpecifyTypeExplicitlyFix extends SpecifyTypeExplicitlyAction {
@NotNull
@Override @Override
public String getText() { public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
return StringUtil.capitalize(super.getText().toLowerCase()); //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 // There is an implicit rule that quick fix text has only first letter capitalized, while intention text
protected boolean isDisabledForError() { @NotNull
return false; private static String getQuickFixText(@NotNull String intentionText) {
return StringUtil.capitalize(intentionText.toLowerCase());
} }
} }