Simplified quickfix code
This commit is contained in:
@@ -30,10 +30,7 @@ import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||
import org.jetbrains.jet.lang.diagnostics.Errors;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
@@ -66,33 +63,13 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
|
||||
if (typeRefParent != null) {
|
||||
element = typeRefParent;
|
||||
}
|
||||
PsiElement parent = element.getParent();
|
||||
JetType type = getTypeForDeclaration((JetNamedDeclaration) parent);
|
||||
if (parent instanceof JetProperty) {
|
||||
JetProperty property = (JetProperty) parent;
|
||||
if (property.getTypeReference() == null) {
|
||||
addTypeAnnotation(project, editor, property, type);
|
||||
}
|
||||
else {
|
||||
property.setTypeReference(null);
|
||||
}
|
||||
}
|
||||
else if (parent instanceof JetParameter) {
|
||||
JetParameter parameter = (JetParameter) parent;
|
||||
if (parameter.getTypeReference() == null) {
|
||||
addTypeAnnotation(project, editor, parameter, type);
|
||||
}
|
||||
else {
|
||||
parameter.setTypeReference(null);
|
||||
}
|
||||
}
|
||||
else if (parent instanceof JetNamedFunction) {
|
||||
JetNamedFunction function = (JetNamedFunction) parent;
|
||||
assert function.getTypeReference() == null;
|
||||
addTypeAnnotation(project, editor, function, type);
|
||||
JetCallableDeclaration declaration = (JetCallableDeclaration)element.getParent();
|
||||
JetType type = getTypeForDeclaration(declaration);
|
||||
if (declaration.getTypeReference() == null) {
|
||||
addTypeAnnotation(project, editor, declaration, type);
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("Unexpected parent: " + parent);
|
||||
declaration.setTypeReference(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,13 +84,11 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
|
||||
element = typeRefParent;
|
||||
}
|
||||
PsiElement parent = element.getParent();
|
||||
if (!(parent instanceof JetNamedDeclaration)) {
|
||||
return false;
|
||||
}
|
||||
JetNamedDeclaration declaration = (JetNamedDeclaration) parent;
|
||||
if (!(parent instanceof JetCallableDeclaration)) return false;
|
||||
JetCallableDeclaration declaration = (JetCallableDeclaration) parent;
|
||||
|
||||
if (declaration instanceof JetProperty && !PsiTreeUtil.isAncestor(((JetProperty) declaration).getInitializer(), element, false)) {
|
||||
if (((JetProperty) declaration).getTypeReference() != null) {
|
||||
if (declaration.getTypeReference() != null) {
|
||||
setText(JetBundle.message("specify.type.explicitly.remove.action.name"));
|
||||
return true;
|
||||
}
|
||||
@@ -121,7 +96,7 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
|
||||
setText(JetBundle.message("specify.type.explicitly.add.action.name"));
|
||||
}
|
||||
}
|
||||
else if (declaration instanceof JetNamedFunction && ((JetNamedFunction) declaration).getTypeReference() == null
|
||||
else if (declaration instanceof JetNamedFunction && declaration.getTypeReference() == null
|
||||
&& !((JetNamedFunction) declaration).hasBlockBody()) {
|
||||
setText(JetBundle.message("specify.type.explicitly.add.return.type.action.name"));
|
||||
}
|
||||
@@ -157,50 +132,20 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JetType getTypeForDeclaration(@NotNull JetNamedDeclaration declaration) {
|
||||
public static JetType getTypeForDeclaration(@NotNull JetCallableDeclaration declaration) {
|
||||
BindingContext bindingContext = ResolvePackage.getBindingContext(declaration.getContainingJetFile());
|
||||
DeclarationDescriptor descriptor = bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, declaration);
|
||||
|
||||
JetType type;
|
||||
if (descriptor instanceof VariableDescriptor) {
|
||||
type = ((VariableDescriptor) descriptor).getType();
|
||||
}
|
||||
else if (descriptor instanceof SimpleFunctionDescriptor) {
|
||||
type = ((SimpleFunctionDescriptor) descriptor).getReturnType();
|
||||
}
|
||||
else {
|
||||
return ErrorUtils.createErrorType("unknown declaration type");
|
||||
}
|
||||
CallableDescriptor descriptor = (CallableDescriptor) bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, declaration);
|
||||
|
||||
JetType type = descriptor.getReturnType();
|
||||
return type == null ? ErrorUtils.createErrorType("null type") : type;
|
||||
}
|
||||
|
||||
public static void addTypeAnnotation(
|
||||
@NotNull Project project,
|
||||
@NotNull Editor editor,
|
||||
@NotNull JetProperty property,
|
||||
@NotNull JetType exprType
|
||||
) {
|
||||
if (property.getTypeReference() != null) return;
|
||||
|
||||
addTypeAnnotationWithTemplate(project, editor, property, exprType);
|
||||
}
|
||||
|
||||
public static void addTypeAnnotation(Project project, @Nullable Editor editor, JetFunction function, @NotNull JetType exprType) {
|
||||
public static void addTypeAnnotation(Project project, @Nullable Editor editor, @NotNull JetCallableDeclaration declaration, @NotNull JetType exprType) {
|
||||
if (editor != null) {
|
||||
addTypeAnnotationWithTemplate(project, editor, function, exprType);
|
||||
addTypeAnnotationWithTemplate(project, editor, declaration, exprType);
|
||||
}
|
||||
else {
|
||||
function.setTypeReference(anyTypeRef(project));
|
||||
}
|
||||
}
|
||||
|
||||
public static void addTypeAnnotation(Project project, @Nullable Editor editor, JetParameter parameter, @NotNull JetType exprType) {
|
||||
if (editor != null) {
|
||||
addTypeAnnotationWithTemplate(project, editor, parameter, exprType);
|
||||
}
|
||||
else {
|
||||
parameter.setTypeReference(anyTypeRef(project));
|
||||
declaration.setTypeReference(anyTypeRef(project));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,7 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetElement;
|
||||
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.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
|
||||
@@ -44,23 +40,15 @@ public class SpecifyTypeExplicitlyFix extends PsiElementBaseIntentionAction {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement element) {
|
||||
//noinspection unchecked
|
||||
JetNamedDeclaration declaration = PsiTreeUtil.getParentOfType(element, JetProperty.class, JetNamedFunction.class);
|
||||
JetCallableDeclaration declaration = PsiTreeUtil.getParentOfType(element, JetProperty.class, JetNamedFunction.class);
|
||||
JetType type = getTypeForDeclaration(declaration);
|
||||
if (declaration instanceof JetProperty) {
|
||||
addTypeAnnotation(project, editor, (JetProperty) declaration, type);
|
||||
}
|
||||
else if (declaration instanceof JetNamedFunction) {
|
||||
addTypeAnnotation(project, editor, (JetNamedFunction) declaration, type);
|
||||
}
|
||||
else {
|
||||
assert false : "Couldn't find property or function " + JetPsiUtil.getElementTextWithContext((JetElement) element);
|
||||
}
|
||||
addTypeAnnotation(project, editor, declaration, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement element) {
|
||||
//noinspection unchecked
|
||||
JetNamedDeclaration declaration = PsiTreeUtil.getParentOfType(element, JetProperty.class, JetNamedFunction.class);
|
||||
JetCallableDeclaration declaration = PsiTreeUtil.getParentOfType(element, JetProperty.class, JetNamedFunction.class);
|
||||
if (declaration instanceof JetProperty) {
|
||||
setText(JetBundle.message("specify.type.explicitly.add.action.name"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user