Refactored PyAnnotation into a PyTypedElement

This commit is contained in:
Andrey Vlasovskikh
2014-08-19 00:14:18 +04:00
parent 184e12ec19
commit b8d75dc401
4 changed files with 24 additions and 25 deletions
@@ -22,10 +22,7 @@ import org.jetbrains.annotations.Nullable;
/**
* @author yole
*/
public interface PyAnnotation extends PyElement, StubBasedPsiElement<PyAnnotationStub> {
public interface PyAnnotation extends PyTypedElement, StubBasedPsiElement<PyAnnotationStub> {
@Nullable
PyExpression getValue();
@Nullable
PyClass resolveToClass();
}
@@ -16,14 +16,14 @@
package com.jetbrains.python.psi.impl;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiPolyVariantReference;
import com.jetbrains.python.PyElementTypes;
import com.jetbrains.python.psi.PyAnnotation;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyReferenceExpression;
import com.jetbrains.python.psi.stubs.PyAnnotationStub;
import com.jetbrains.python.psi.types.PyClassLikeType;
import com.jetbrains.python.psi.types.PyType;
import com.jetbrains.python.psi.types.TypeEvalContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
@@ -46,13 +46,15 @@ public class PyAnnotationImpl extends PyBaseElementImpl<PyAnnotationStub> implem
@Nullable
@Override
public PyClass resolveToClass() {
PyExpression expr = getValue();
if (expr instanceof PyReferenceExpression) {
final PsiPolyVariantReference reference = ((PyReferenceExpression)expr).getReference();
final PsiElement result = reference.resolve();
if (result instanceof PyClass) {
return (PyClass) result;
public PyType getType(@NotNull TypeEvalContext context, @NotNull TypeEvalContext.Key key) {
final PyExpression value = getValue();
if (value != null) {
final PyType type = context.getType(value);
if (type instanceof PyClassLikeType) {
final PyClassLikeType classType = (PyClassLikeType)type;
if (classType.isDefinition()) {
return classType.toInstance();
}
}
}
return null;
@@ -184,11 +184,11 @@ public class PyFunctionImpl extends PyPresentableElementImpl<PyFunctionStub> imp
}
}
if (context.maySwitchToAST(this) && LanguageLevel.forElement(this).isAtLeast(LanguageLevel.PYTHON30)) {
PyAnnotation anno = getAnnotation();
if (anno != null) {
PyClass pyClass = anno.resolveToClass();
if (pyClass != null) {
return new PyClassTypeImpl(pyClass, false);
final PyAnnotation annotation = getAnnotation();
if (annotation != null) {
final PyType type = context.getType(annotation);
if (type != null) {
return type;
}
}
}
@@ -181,11 +181,11 @@ public class PyNamedParameterImpl extends PyPresentableElementImpl<PyNamedParame
PyParameterList parameterList = (PyParameterList)parent;
PyFunction func = parameterList.getContainingFunction();
if (func != null) {
PyAnnotation anno = getAnnotation();
if (anno != null) {
final PyClass pyClass = anno.resolveToClass();
if (pyClass != null) {
return new PyClassTypeImpl(pyClass, false);
final PyAnnotation annotation = getAnnotation();
if (annotation != null) {
final PyType type = context.getType(annotation);
if (type != null) {
return type;
}
}
StructuredDocString docString = func.getStructuredDocString();