diff options
Diffstat (limited to 'python/src/com/jetbrains/python/psi/impl')
5 files changed, 45 insertions, 60 deletions
diff --git a/python/src/com/jetbrains/python/psi/impl/PyAnnotationImpl.java b/python/src/com/jetbrains/python/psi/impl/PyAnnotationImpl.java index f3c806dac7c5..f8a4d482c2f2 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyAnnotationImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyAnnotationImpl.java @@ -16,14 +16,16 @@ 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.PyNoneType; +import com.jetbrains.python.psi.types.PyType; +import com.jetbrains.python.psi.types.TypeEvalContext; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author yole @@ -37,19 +39,26 @@ public class PyAnnotationImpl extends PyBaseElementImpl<PyAnnotationStub> implem super(stub, PyElementTypes.ANNOTATION); } + @Nullable @Override public PyExpression getValue() { return findChildByClass(PyExpression.class); } + @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(); + } + } + else if (type instanceof PyNoneType) { + return type; } } return null; diff --git a/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java b/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java index 7538fbae2fd2..bb2104fc0c77 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java @@ -20,7 +20,6 @@ import com.intellij.lang.ASTNode; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.NotNullLazyValue; -import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; import com.intellij.psi.scope.PsiScopeProcessor; import com.intellij.psi.search.LocalSearchScope; @@ -187,6 +186,11 @@ public class PyClassImpl extends PyPresentableElementImpl<PyClassStub> implement } } } + // Heuristic: unfold Foo[Bar] to Foo for subscription expressions for superclasses + else if (expression instanceof PySubscriptionExpression) { + final PySubscriptionExpression subscriptionExpr = (PySubscriptionExpression)expression; + return subscriptionExpr.getOperand(); + } return expression; } @@ -237,27 +241,7 @@ public class PyClassImpl extends PyPresentableElementImpl<PyClassStub> implement @Nullable public String getQualifiedName() { - String name = getName(); - final PyClassStub stub = getStub(); - PsiElement ancestor = stub != null ? stub.getParentStub().getPsi() : getParent(); - while (!(ancestor instanceof PsiFile)) { - if (ancestor == null) return name; // can this happen? - if (ancestor instanceof PyClass) { - name = ((PyClass)ancestor).getName() + "." + name; - } - ancestor = stub != null ? ((StubBasedPsiElement)ancestor).getStub().getParentStub().getPsi() : ancestor.getParent(); - } - - PsiFile psiFile = ((PsiFile)ancestor).getOriginalFile(); - final PyFile builtins = PyBuiltinCache.getInstance(this).getBuiltinsFile(); - if (!psiFile.equals(builtins)) { - VirtualFile vFile = psiFile.getVirtualFile(); - if (vFile != null) { - final String packageName = QualifiedNameFinder.findShortestImportableName(this, vFile); - return packageName + "." + name; - } - } - return name; + return QualifiedNameFinder.getQualifiedName(this); } @Override diff --git a/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java index 2531b37bf8b2..86f84abb7fb6 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java @@ -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; } } } @@ -571,7 +571,7 @@ public class PyFunctionImpl extends PyPresentableElementImpl<PyFunctionStub> imp @Override public PyAnnotation getAnnotation() { - return findChildByClass(PyAnnotation.class); + return getStubOrPsiChild(PyElementTypes.ANNOTATION); } @NotNull @@ -699,21 +699,6 @@ public class PyFunctionImpl extends PyPresentableElementImpl<PyFunctionStub> imp @Nullable @Override public String getQualifiedName() { - String name = getName(); - if (name == null) { - return null; - } - PyClass containingClass = getContainingClass(); - if (containingClass != null) { - return containingClass.getQualifiedName() + "." + name; - } - if (PsiTreeUtil.getStubOrPsiParent(this) instanceof PyFile) { - VirtualFile virtualFile = getContainingFile().getVirtualFile(); - if (virtualFile != null) { - final String packageName = QualifiedNameFinder.findShortestImportableName(this, virtualFile); - return packageName + "." + name; - } - } - return null; + return QualifiedNameFinder.getQualifiedName(this); } } diff --git a/python/src/com/jetbrains/python/psi/impl/PyNamedParameterImpl.java b/python/src/com/jetbrains/python/psi/impl/PyNamedParameterImpl.java index bf792fef0dae..cc9178c07f9e 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyNamedParameterImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyNamedParameterImpl.java @@ -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(); diff --git a/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java index 545e8e86cba1..3805c8df525e 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java @@ -43,6 +43,7 @@ import com.jetbrains.python.psi.impl.references.PyQualifiedReference; import com.jetbrains.python.psi.impl.references.PyTargetReference; import com.jetbrains.python.psi.impl.stubs.CustomTargetExpressionStub; import com.jetbrains.python.psi.resolve.PyResolveContext; +import com.jetbrains.python.psi.resolve.QualifiedNameFinder; import com.jetbrains.python.psi.resolve.RatedResolveResult; import com.jetbrains.python.psi.stubs.PyClassStub; import com.jetbrains.python.psi.stubs.PyFunctionStub; @@ -707,4 +708,10 @@ public class PyTargetExpressionImpl extends PyPresentableElementImpl<PyTargetExp super.subtreeChanged(); myQualifiedName = null; } + + @Nullable + @Override + public String getQualifiedName() { + return QualifiedNameFinder.getQualifiedName(this); + } } |