From c8502bb914e3a610dafd7efc7cb4d2a7b9f5ed55 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Wed, 8 May 2013 19:47:41 +0400 Subject: [PATCH] Added TypeEvalContext.codeInsightFallback() constructor --- .../python/psi/resolve/PyResolveContext.java | 2 +- .../python/psi/types/TypeEvalContext.java | 21 ++++++++++++++----- .../src/com/jetbrains/python/psi/PyUtil.java | 4 ++-- .../psi/impl/CallArgumentsMappingImpl.java | 2 +- .../python/psi/impl/PyBuiltinCache.java | 2 +- .../python/psi/impl/PyClassImpl.java | 10 ++++----- .../psi/impl/PyTargetExpressionImpl.java | 2 +- .../python/psi/resolve/ResolveImportUtil.java | 2 +- .../python/testing/PythonUnitTestUtil.java | 4 ++-- .../PythonAtTestConfigurationProducer.java | 2 +- .../python/testing/pytest/PyTestUtil.java | 2 +- .../python/codeInsight/PyClassMROTest.java | 2 +- 12 files changed, 33 insertions(+), 22 deletions(-) diff --git a/python/psi-api/src/com/jetbrains/python/psi/resolve/PyResolveContext.java b/python/psi-api/src/com/jetbrains/python/psi/resolve/PyResolveContext.java index 4795a3e9ff79..5775b24a8c79 100644 --- a/python/psi-api/src/com/jetbrains/python/psi/resolve/PyResolveContext.java +++ b/python/psi-api/src/com/jetbrains/python/psi/resolve/PyResolveContext.java @@ -56,7 +56,7 @@ public class PyResolveContext { } public TypeEvalContext getTypeEvalContext() { - return myTypeEvalContext != null ? myTypeEvalContext : TypeEvalContext.fastStubOnly(null); + return myTypeEvalContext != null ? myTypeEvalContext : TypeEvalContext.codeInsightFallback(); } @Override diff --git a/python/psi-api/src/com/jetbrains/python/psi/types/TypeEvalContext.java b/python/psi-api/src/com/jetbrains/python/psi/types/TypeEvalContext.java index 23f9cf885024..313bebe81ac6 100644 --- a/python/psi-api/src/com/jetbrains/python/psi/types/TypeEvalContext.java +++ b/python/psi-api/src/com/jetbrains/python/psi/types/TypeEvalContext.java @@ -54,7 +54,7 @@ public class TypeEvalContext { } /** - * The most detailed type evaluation context for user-initiated actions. + * Create the most detailed type evaluation context for user-initiated actions. * * Should be used for code completion, go to definition, find usages, refactorings, documentation. */ @@ -62,20 +62,31 @@ public class TypeEvalContext { return new TypeEvalContext(true, true, null); } + /** + * Create a type evaluation context for deeper and slower code analysis. + * + * Should be used only when normal analysis context is not enough for getting good results. + */ public static TypeEvalContext fast() { return new TypeEvalContext(false, true, null); } /** - * Creates a TypeEvalContext for performing analysis operations on the specified file which is currently open in the editor, + * Create a type evaluation context for performing analysis operations on the specified file which is currently open in the editor, * without accessing stubs. For such a file, additional slow operations are allowed. - * - * @param origin the file open in the editor - * @return the type eval context for the file. */ public static TypeEvalContext fastStubOnly(@Nullable PsiFile origin) { return new TypeEvalContext(false, false, origin); } + + /** + * Create the most shallow type evaluation context for code insight purposes when other more detailed contexts are not available. + * + * It's use should be minimized. + */ + public static TypeEvalContext codeInsightFallback() { + return new TypeEvalContext(false, false, null); + } public TypeEvalContext withTracing() { if (myTrace == null) { diff --git a/python/src/com/jetbrains/python/psi/PyUtil.java b/python/src/com/jetbrains/python/psi/PyUtil.java index e96870c1a0d1..4abf744b131f 100644 --- a/python/src/com/jetbrains/python/psi/PyUtil.java +++ b/python/src/com/jetbrains/python/psi/PyUtil.java @@ -618,7 +618,7 @@ public class PyUtil { } public static boolean hasUnresolvedAncestors(@NotNull PyClass cls) { - for (PyClassLikeType type : cls.getAncestorTypes(TypeEvalContext.fastStubOnly(null))) { + for (PyClassLikeType type : cls.getAncestorTypes(TypeEvalContext.codeInsightFallback())) { if (type == null) { return true; } @@ -968,7 +968,7 @@ public class PyUtil { if (isBaseException(pyClass.getQualifiedName())) { return true; } - for (PyClassLikeType type : pyClass.getAncestorTypes(TypeEvalContext.fastStubOnly(null))) { + for (PyClassLikeType type : pyClass.getAncestorTypes(TypeEvalContext.codeInsightFallback())) { if (type != null && isBaseException(type.getClassQName())) { return true; } diff --git a/python/src/com/jetbrains/python/psi/impl/CallArgumentsMappingImpl.java b/python/src/com/jetbrains/python/psi/impl/CallArgumentsMappingImpl.java index b1805715a3e7..c3b958b6e558 100644 --- a/python/src/com/jetbrains/python/psi/impl/CallArgumentsMappingImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/CallArgumentsMappingImpl.java @@ -54,7 +54,7 @@ public class CallArgumentsMappingImpl implements CallArgumentsMapping { @Nullable TypeEvalContext type_context ) { PyExpression[] arguments = myArgumentList.getArguments(); - if (type_context == null) type_context = TypeEvalContext.fastStubOnly(null); + if (type_context == null) type_context = TypeEvalContext.codeInsightFallback(); myMarkedCallee = resolved_callee; List unmatched_args = new LinkedList(); Collections.addAll(unmatched_args, arguments); diff --git a/python/src/com/jetbrains/python/psi/impl/PyBuiltinCache.java b/python/src/com/jetbrains/python/psi/impl/PyBuiltinCache.java index 11f4d999e220..75e7040cdf32 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyBuiltinCache.java +++ b/python/src/com/jetbrains/python/psi/impl/PyBuiltinCache.java @@ -329,7 +329,7 @@ public class PyBuiltinCache { private static boolean isValid(@Nullable PyType type) { if (type instanceof PyCollectionType) { - final PyType elementType = ((PyCollectionType)type).getElementType(TypeEvalContext.fastStubOnly(null)); + final PyType elementType = ((PyCollectionType)type).getElementType(TypeEvalContext.codeInsightFallback()); if (!isValid(elementType)) { return false; } diff --git a/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java b/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java index ad48778420eb..0faad3f49cbc 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java @@ -58,7 +58,7 @@ public class PyClassImpl extends PyPresentableElementImpl implement @Nullable @Override public Result> compute() { - final TypeEvalContext context = myCachedContext != null ? myCachedContext : TypeEvalContext.fastStubOnly(null); + final TypeEvalContext context = myCachedContext != null ? myCachedContext : TypeEvalContext.codeInsightFallback(); final List ancestorTypes = isNewStyleClass() ? getMROAncestorTypes(context) : getOldStyleAncestorTypes(context); return Result.create(ancestorTypes, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT); } @@ -194,7 +194,7 @@ public class PyClassImpl extends PyPresentableElementImpl implement @NotNull @Override public List getAncestorClasses() { - return getAncestorClasses(TypeEvalContext.fastStubOnly(null)); + return getAncestorClasses(TypeEvalContext.codeInsightFallback()); } @NotNull @@ -224,7 +224,7 @@ public class PyClassImpl extends PyPresentableElementImpl implement if (superClassQName.equals(getQualifiedName())) { return true; } - for (PyClassLikeType type : getAncestorTypes(TypeEvalContext.fastStubOnly(null))) { + for (PyClassLikeType type : getAncestorTypes(TypeEvalContext.codeInsightFallback())) { if (type != null && superClassQName.equals(type.getClassQName())) { return true; } @@ -287,7 +287,7 @@ public class PyClassImpl extends PyPresentableElementImpl implement @NotNull public PyClass[] getSuperClasses() { - final List superTypes = getSuperClassTypes(TypeEvalContext.fastStubOnly(null)); + final List superTypes = getSuperClassTypes(TypeEvalContext.codeInsightFallback()); if (superTypes.isEmpty()) { return EMPTY_ARRAY; } @@ -925,7 +925,7 @@ public class PyClassImpl extends PyPresentableElementImpl implement final PyClass objClass = PyBuiltinCache.getInstance(this).getClass("object"); if (this == objClass) return true; // a rare but possible case if (hasNewStyleMetaClass(this)) return true; - for (PyClassLikeType type : getOldStyleAncestorTypes(TypeEvalContext.fastStubOnly(null))) { + for (PyClassLikeType type : getOldStyleAncestorTypes(TypeEvalContext.codeInsightFallback())) { if (type == null) { // unknown, assume new-style class return true; diff --git a/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java index 33f6ab67140e..0fa8cf546190 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java @@ -451,7 +451,7 @@ public class PyTargetExpressionImpl extends PyPresentableElementImpl results = type.resolveMember(name, null, AccessDirection.READ, PyResolveContext.noImplicits()); diff --git a/python/src/com/jetbrains/python/psi/resolve/ResolveImportUtil.java b/python/src/com/jetbrains/python/psi/resolve/ResolveImportUtil.java index c344ebe6ac18..47385ee210eb 100644 --- a/python/src/com/jetbrains/python/psi/resolve/ResolveImportUtil.java +++ b/python/src/com/jetbrains/python/psi/resolve/ResolveImportUtil.java @@ -271,7 +271,7 @@ public class ResolveImportUtil { dir = (PsiDirectory)parent; } else if (parent != null) { - PyType refType = PyReferenceExpressionImpl.getReferenceTypeFromProviders(parent, TypeEvalContext.fastStubOnly(null), null); + PyType refType = PyReferenceExpressionImpl.getReferenceTypeFromProviders(parent, TypeEvalContext.codeInsightFallback(), null); if (refType != null) { final List result = refType.resolveMember(referencedName, null, AccessDirection.READ, PyResolveContext.defaultContext()); diff --git a/python/src/com/jetbrains/python/testing/PythonUnitTestUtil.java b/python/src/com/jetbrains/python/testing/PythonUnitTestUtil.java index 9e7d1da87091..183d18c45d0d 100644 --- a/python/src/com/jetbrains/python/testing/PythonUnitTestUtil.java +++ b/python/src/com/jetbrains/python/testing/PythonUnitTestUtil.java @@ -50,7 +50,7 @@ public class PythonUnitTestUtil { } private static boolean isUnitTestCaseClass(PyClass cls, HashSet testQualifiedNames) { - for (PyClassLikeType type : cls.getAncestorTypes(TypeEvalContext.fastStubOnly(null))) { + for (PyClassLikeType type : cls.getAncestorTypes(TypeEvalContext.codeInsightFallback())) { if (type != null && testQualifiedNames.contains(type.getClassQName())) { return true; } @@ -121,7 +121,7 @@ public class PythonUnitTestUtil { } public static boolean isTestCaseClass(@NotNull PyClass cls, Set testQualifiedNames) { - for (PyClassLikeType type : cls.getAncestorTypes(TypeEvalContext.fastStubOnly(null))) { + for (PyClassLikeType type : cls.getAncestorTypes(TypeEvalContext.codeInsightFallback())) { if (type != null) { if (testQualifiedNames.contains(type.getClassQName())) { return true; diff --git a/python/src/com/jetbrains/python/testing/attest/PythonAtTestConfigurationProducer.java b/python/src/com/jetbrains/python/testing/attest/PythonAtTestConfigurationProducer.java index c9c49e055f98..055b5238dfa4 100644 --- a/python/src/com/jetbrains/python/testing/attest/PythonAtTestConfigurationProducer.java +++ b/python/src/com/jetbrains/python/testing/attest/PythonAtTestConfigurationProducer.java @@ -29,7 +29,7 @@ public class PythonAtTestConfigurationProducer extends protected boolean isTestClass(PyClass pyClass) { if (pyClass == null) return false; - for (PyClassLikeType type : pyClass.getAncestorTypes(TypeEvalContext.fastStubOnly(null))) { + for (PyClassLikeType type : pyClass.getAncestorTypes(TypeEvalContext.codeInsightFallback())) { if (type != null && "TestBase".equals(type.getName()) && hasTestFunction(pyClass)) { return true; } diff --git a/python/src/com/jetbrains/python/testing/pytest/PyTestUtil.java b/python/src/com/jetbrains/python/testing/pytest/PyTestUtil.java index e32945a561e2..880d2e43ef8e 100644 --- a/python/src/com/jetbrains/python/testing/pytest/PyTestUtil.java +++ b/python/src/com/jetbrains/python/testing/pytest/PyTestUtil.java @@ -56,7 +56,7 @@ public class PyTestUtil { } public static boolean isPyTestClass(PyClass pyClass) { - for (PyClassLikeType type : pyClass.getAncestorTypes(TypeEvalContext.fastStubOnly(null))) { + for (PyClassLikeType type : pyClass.getAncestorTypes(TypeEvalContext.codeInsightFallback())) { if (type != null && PYTHON_TEST_QUALIFIED_CLASSES.contains(type.getClassQName())) { return true; } diff --git a/python/testSrc/com/jetbrains/python/codeInsight/PyClassMROTest.java b/python/testSrc/com/jetbrains/python/codeInsight/PyClassMROTest.java index c506969839f6..8a6ef7073805 100644 --- a/python/testSrc/com/jetbrains/python/codeInsight/PyClassMROTest.java +++ b/python/testSrc/com/jetbrains/python/codeInsight/PyClassMROTest.java @@ -53,7 +53,7 @@ public class PyClassMROTest extends PyTestCase { } public void assertMRO(@NotNull PyClass cls, @NotNull String... mro) { - final List types = cls.getAncestorTypes(TypeEvalContext.fastStubOnly(null)); + final List types = cls.getAncestorTypes(TypeEvalContext.codeInsightFallback()); final List classNames = new ArrayList(); for (PyClassLikeType type : types) { if (type != null) {