JetParameter made JetCallableDeclaration

This commit is contained in:
Valentin Kipyatkov
2014-10-07 13:00:44 +04:00
parent 852fc8bc5d
commit 93efcd637e
4 changed files with 40 additions and 42 deletions
@@ -26,7 +26,10 @@ import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
import org.jetbrains.jet.lang.psi.typeRefHelpers.TypeRefHelpersPackage; import org.jetbrains.jet.lang.psi.typeRefHelpers.TypeRefHelpersPackage;
import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.lexer.JetTokens;
public class JetParameter extends JetNamedDeclarationStub<PsiJetParameterStub> { import java.util.Collections;
import java.util.List;
public class JetParameter extends JetNamedDeclarationStub<PsiJetParameterStub> implements JetCallableDeclaration {
public JetParameter(@NotNull ASTNode node) { public JetParameter(@NotNull ASTNode node) {
super(node); super(node);
@@ -41,14 +44,15 @@ public class JetParameter extends JetNamedDeclarationStub<PsiJetParameterStub> {
return visitor.visitParameter(this, data); return visitor.visitParameter(this, data);
} }
//TODO: rename it to getTypeRef for consistency @Override
@Nullable @Nullable
public JetTypeReference getTypeReference() { public JetTypeReference getTypeReference() {
return getStubOrPsiChild(JetStubElementTypes.TYPE_REFERENCE); return getStubOrPsiChild(JetStubElementTypes.TYPE_REFERENCE);
} }
@Override
@Nullable @Nullable
public JetTypeReference setTypeRef(@Nullable JetTypeReference typeRef) { public JetTypeReference setTypeReference(@Nullable JetTypeReference typeRef) {
return TypeRefHelpersPackage.setTypeReference(this, getNameIdentifier(), typeRef); return TypeRefHelpersPackage.setTypeReference(this, getNameIdentifier(), typeRef);
} }
@@ -121,4 +125,28 @@ public class JetParameter extends JetNamedDeclarationStub<PsiJetParameterStub> {
public boolean isLoopParameter() { public boolean isLoopParameter() {
return getParent() instanceof JetForExpression; return getParent() instanceof JetForExpression;
} }
@Nullable
@Override
public JetParameterList getValueParameterList() {
return null;
}
@Nullable
@Override
public JetTypeReference getReceiverTypeReference() {
return null;
}
@NotNull
@Override
public List<JetTypeConstraint> getTypeConstraints() {
return Collections.emptyList();
}
@NotNull
@Override
public List<JetTypeParameter> getTypeParameters() {
return Collections.emptyList();
}
} }
@@ -83,7 +83,7 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
addTypeAnnotation(project, editor, parameter, type); addTypeAnnotation(project, editor, parameter, type);
} }
else { else {
parameter.setTypeRef(null); parameter.setTypeReference(null);
} }
} }
else if (parent instanceof JetNamedFunction) { else if (parent instanceof JetNamedFunction) {
@@ -200,17 +200,17 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
addTypeAnnotationWithTemplate(project, editor, parameter, exprType); addTypeAnnotationWithTemplate(project, editor, parameter, exprType);
} }
else { else {
parameter.setTypeRef(anyTypeRef(project)); parameter.setTypeReference(anyTypeRef(project));
} }
} }
private static void addTypeAnnotationWithTemplate( private static void addTypeAnnotationWithTemplate(
@NotNull Project project, @NotNull Project project,
@NotNull Editor editor, @NotNull Editor editor,
@NotNull final JetNamedDeclaration namedDeclaration, @NotNull final JetCallableDeclaration declaration,
@NotNull JetType exprType @NotNull JetType exprType
) { ) {
assert !exprType.isError() : "Unexpected error type: " + namedDeclaration.getText(); assert !exprType.isError() : "Unexpected error type: " + declaration.getText();
ClassifierDescriptor descriptor = exprType.getConstructor().getDeclarationDescriptor(); ClassifierDescriptor descriptor = exprType.getConstructor().getDeclarationDescriptor();
boolean isAnonymous = descriptor != null && DescriptorUtils.isAnonymousObject(descriptor); boolean isAnonymous = descriptor != null && DescriptorUtils.isAnonymousObject(descriptor);
@@ -235,12 +235,12 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
} }
}; };
setTypeRef(namedDeclaration, anyTypeRef(project)); declaration.setTypeReference(anyTypeRef(project));
PsiDocumentManager.getInstance(project).commitAllDocuments(); PsiDocumentManager.getInstance(project).commitAllDocuments();
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument()); PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
JetTypeReference newTypeRef = getTypeRef(namedDeclaration); JetTypeReference newTypeRef = declaration.getTypeReference();
assert newTypeRef != null; assert newTypeRef != null;
TemplateBuilderImpl builder = new TemplateBuilderImpl(newTypeRef); TemplateBuilderImpl builder = new TemplateBuilderImpl(newTypeRef);
builder.replaceElement(newTypeRef, expression); builder.replaceElement(newTypeRef, expression);
@@ -251,7 +251,7 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
manager.startTemplate(editor, builder.buildInlineTemplate(), new TemplateEditingAdapter() { manager.startTemplate(editor, builder.buildInlineTemplate(), new TemplateEditingAdapter() {
@Override @Override
public void templateFinished(Template template, boolean brokenOff) { public void templateFinished(Template template, boolean brokenOff) {
JetTypeReference typeRef = getTypeRef(namedDeclaration); JetTypeReference typeRef = declaration.getTypeReference();
assert typeRef != null; assert typeRef != null;
ShortenReferences.INSTANCE$.process(typeRef); ShortenReferences.INSTANCE$.process(typeRef);
} }
@@ -261,34 +261,4 @@ public class SpecifyTypeExplicitlyAction extends PsiElementBaseIntentionAction {
private static JetTypeReference anyTypeRef(@NotNull Project project) { private static JetTypeReference anyTypeRef(@NotNull Project project) {
return JetPsiFactory(project).createType("Any"); return JetPsiFactory(project).createType("Any");
} }
@Nullable
private static JetTypeReference getTypeRef(@NotNull JetNamedDeclaration namedDeclaration) {
if (namedDeclaration instanceof JetProperty) {
return ((JetProperty) namedDeclaration).getTypeReference();
}
else if (namedDeclaration instanceof JetParameter) {
return ((JetParameter) namedDeclaration).getTypeReference();
}
else if (namedDeclaration instanceof JetFunction) {
return ((JetFunction) namedDeclaration).getTypeReference();
}
assert false : "Wrong namedDeclaration: " + namedDeclaration.getText();
return null;
}
@Nullable
private static JetTypeReference setTypeRef(@NotNull JetNamedDeclaration namedDeclaration, @Nullable JetTypeReference typeRef) {
if (namedDeclaration instanceof JetProperty) {
return ((JetProperty) namedDeclaration).setTypeReference(typeRef);
}
else if (namedDeclaration instanceof JetParameter) {
return ((JetParameter) namedDeclaration).setTypeRef(typeRef);
}
else if (namedDeclaration instanceof JetFunction) {
return ((JetFunction) namedDeclaration).setTypeReference(typeRef);
}
assert false : "Wrong namedDeclaration: " + namedDeclaration.getText();
return null;
}
} }
@@ -65,6 +65,6 @@ public class ChangeParameterTypeFix extends JetIntentionAction<JetParameter> {
@Override @Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException { public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
element.setTypeRef(JetPsiFactory(file).createType(renderedType)); element.setTypeReference(JetPsiFactory(file).createType(renderedType));
} }
} }
@@ -139,7 +139,7 @@ public class JetFunctionDefinitionUsage extends JetUsageInfo<PsiElement> {
if (parameterInfo.isTypeChanged()) { if (parameterInfo.isTypeChanged()) {
JetTypeReference newTypeRef = psiFactory.createType(parameterInfo.getTypeText()); JetTypeReference newTypeRef = psiFactory.createType(parameterInfo.getTypeText());
parameter.setTypeRef(newTypeRef); parameter.setTypeReference(newTypeRef);
} }
PsiElement identifier = parameter.getNameIdentifier(); PsiElement identifier = parameter.getNameIdentifier();