From b7eeecb03845b519446ee9a802348a209cadc6f6 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Thu, 26 Jul 2012 18:58:07 +0400 Subject: [PATCH 1/2] Revert "Return property with unknown calls for target expression stubs with assigned calls to 'property'" This reverts commit bbd13f72291cac0cef8af0d4cf756247d5a4c1d0. --- .../src/com/jetbrains/python/psi/impl/PyClassImpl.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java b/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java index 6e22ce19aea1..e4735cc33188 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java @@ -39,8 +39,6 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.util.*; -import static com.jetbrains.python.psi.stubs.PyTargetExpressionStub.InitializerType.CallExpression; - /** * @author yole */ @@ -603,14 +601,6 @@ public class PyClassImpl extends PyPresentableElementImpl implement if (propertyProcessor == null || propertyProcessor.process(property)) return property; } } - final PyQualifiedName initializer = targetStub.getInitializer(); - if (targetStub.getInitializerType() == CallExpression && initializer != null && PyNames.PROPERTY.equals(initializer.toString())) { - final PropertyImpl property = new PropertyImpl(targetStub.getName(), UNKNOWN_CALL, UNKNOWN_CALL, UNKNOWN_CALL, - null, targetStub.getPsi()); - if (propertyProcessor == null || propertyProcessor.process(property)) { - return property; - } - } } } } From cdcbe98e05e50718e82a97e9003cdf971c2c95da Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Thu, 26 Jul 2012 20:36:36 +0400 Subject: [PATCH 2/2] Fixed Python builtins file detection and lambda accessor value for PropertyStubStorage --- .../python/psi/PyFileElementType.java | 2 +- .../python/psi/impl/PropertyBunch.java | 35 ++++++++++--------- .../python/psi/impl/PyBuiltinCache.java | 7 ---- .../python/psi/impl/PyClassImpl.java | 15 ++++---- .../python/psi/stubs/PropertyStubStorage.java | 10 ++++-- .../python/PyClassicPropertyTest.java | 2 +- 6 files changed, 36 insertions(+), 35 deletions(-) diff --git a/python/src/com/jetbrains/python/psi/PyFileElementType.java b/python/src/com/jetbrains/python/psi/PyFileElementType.java index e086ba379c2c..f32596677f1f 100644 --- a/python/src/com/jetbrains/python/psi/PyFileElementType.java +++ b/python/src/com/jetbrains/python/psi/PyFileElementType.java @@ -44,7 +44,7 @@ public class PyFileElementType extends IStubFileElementType { @Override public int getStubVersion() { - return 43; + return 44; } @Nullable diff --git a/python/src/com/jetbrains/python/psi/impl/PropertyBunch.java b/python/src/com/jetbrains/python/psi/impl/PropertyBunch.java index ac3f339c11c2..3aed21efa32d 100644 --- a/python/src/com/jetbrains/python/psi/impl/PropertyBunch.java +++ b/python/src/com/jetbrains/python/psi/impl/PropertyBunch.java @@ -49,8 +49,8 @@ public abstract class PropertyBunch { * @param ref a reference as an argument in property() call * @return value we want to store (resolved callable, name, etc) */ - @Nullable - protected abstract MType translate(@NotNull PyExpression ref); + @NotNull + protected abstract Maybe translate(@Nullable PyExpression ref); @Nullable public static PyCallExpression findPropertyCallSite(@Nullable PyExpression source) { @@ -60,20 +60,25 @@ public abstract class PropertyBunch { if (callee instanceof PyReferenceExpression) { PyReferenceExpression ref = (PyReferenceExpression)callee; if (ref.getQualifier() != null) return null; - boolean is_inside_builtins = false; - PsiFile psifile = source.getContainingFile(); - is_inside_builtins = psifile != null && psifile.getUserData(PyBuiltinCache.MARKER_KEY) != null; - if (PyNames.PROPERTY.equals(callee.getName()) && (is_inside_builtins || !resolvesLocally(ref))) { - // we assume that a non-local name 'property' is a built-in name. - // ref.resolve() is not used because we run in stub building phase where resolve() is frowned upon. - // NOTE: this logic fails if (quite unusually) name 'property' is directly imported from builtins. - return call; + if (PyNames.PROPERTY.equals(callee.getName())) { + PsiFile file = source.getContainingFile(); + if (isBuiltinFile(file) || !resolvesLocally(ref)) { + // we assume that a non-local name 'property' is a built-in name. + // ref.resolve() is not used because we run in stub building phase where resolve() is frowned upon. + // NOTE: this logic fails if (quite unusually) name 'property' is directly imported from builtins. + return call; + } } } } return null; } + private static boolean isBuiltinFile(PsiFile file) { + final String name = file.getName(); + return PyBuiltinCache.BUILTIN_FILE.equals(name) || PyBuiltinCache.BUILTIN_FILE_3K.equals(name); + } + /** * Resolve in containing file only. * @param ref what to resolve @@ -129,17 +134,13 @@ public abstract class PropertyBunch { } } } - target.myGetter = translateIfSet(target, accessors [0]); - target.mySetter = translateIfSet(target, accessors [1]); - target.myDeleter = translateIfSet(target, accessors [2]); + target.myGetter = target.translate(accessors[0]); + target.mySetter = target.translate(accessors[1]); + target.myDeleter = target.translate(accessors[2]); target.myDoc = doc; return true; } } return false; } - - private static Maybe translateIfSet(PropertyBunch target, PyExpression accessor) { - return new Maybe(accessor == null ? null : target.translate(accessor)); - } } diff --git a/python/src/com/jetbrains/python/psi/impl/PyBuiltinCache.java b/python/src/com/jetbrains/python/psi/impl/PyBuiltinCache.java index 6fd67ed8edc3..d37d7b095311 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyBuiltinCache.java +++ b/python/src/com/jetbrains/python/psi/impl/PyBuiltinCache.java @@ -9,7 +9,6 @@ import com.intellij.openapi.roots.JdkOrderEntry; import com.intellij.openapi.roots.OrderEntry; import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.roots.impl.ModuleLibraryOrderEntryImpl; -import com.intellij.openapi.util.Key; import com.intellij.openapi.util.Ref; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VfsUtil; @@ -149,15 +148,9 @@ public class PyBuiltinCache { public PyBuiltinCache() { } - - public static final Key MARKER_KEY = new Key("python.builtins.skeleton.file"); - public PyBuiltinCache(@Nullable final PyFile builtins, @Nullable PyFile exceptions) { myBuiltinsFile = builtins; myExceptionsFile = exceptions; - if (myBuiltinsFile != null) { - myBuiltinsFile.putUserData(MARKER_KEY, ""); // mark this file as builtins - } } @Nullable diff --git a/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java b/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java index e4735cc33188..54efeb1dd7e4 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyClassImpl.java @@ -730,21 +730,24 @@ public class PyClassImpl extends PyPresentableElementImpl implement throw new IllegalArgumentException("Unknown direction " + PyUtil.nvl(direction)); } - @Nullable + @NotNull @Override - protected Callable translate(@NotNull PyExpression expr) { - if (PyNames.NONE.equals(expr.getName())) return null; // short-circuit a common case + protected Maybe translate(@Nullable PyExpression expr) { + if (expr == null) { + return NONE; + } + if (PyNames.NONE.equals(expr.getName())) return NONE; // short-circuit a common case if (expr instanceof Callable) { - return (Callable)expr; + return new Maybe((Callable)expr); } final PsiReference ref = expr.getReference(); if (ref != null) { PsiElement something = ref.resolve(); if (something instanceof Callable) { - return (Callable)something; + return new Maybe((Callable)something); } } - return null; + return NONE; } public String toString() { diff --git a/python/src/com/jetbrains/python/psi/stubs/PropertyStubStorage.java b/python/src/com/jetbrains/python/psi/stubs/PropertyStubStorage.java index 1030bd1bdd20..bdb8d32259dd 100644 --- a/python/src/com/jetbrains/python/psi/stubs/PropertyStubStorage.java +++ b/python/src/com/jetbrains/python/psi/stubs/PropertyStubStorage.java @@ -22,10 +22,14 @@ import java.io.IOException; */ public class PropertyStubStorage extends PropertyBunch implements CustomTargetExpressionStub { - @Nullable + @NotNull @Override - protected String translate(@NotNull PyExpression ref) { - return ref.getName(); + protected Maybe translate(@Nullable PyExpression ref) { + if (ref != null) { + final String name = ref.getName(); + return name != null ? new Maybe(name) : unknown; + } + return none; } private static final String IMPOSSIBLE_NAME = "#"; diff --git a/python/testSrc/com/jetbrains/python/PyClassicPropertyTest.java b/python/testSrc/com/jetbrains/python/PyClassicPropertyTest.java index 90de66323c72..20a9fe3148a3 100644 --- a/python/testSrc/com/jetbrains/python/PyClassicPropertyTest.java +++ b/python/testSrc/com/jetbrains/python/PyClassicPropertyTest.java @@ -73,7 +73,7 @@ public class PyClassicPropertyTest extends PyTestCase { assertEquals("v3", site.getText()); accessor = p.getGetter(); - assertTrue(accessor.isDefined()); + assertFalse(accessor.isDefined()); accessor = p.getSetter(); assertTrue(accessor.isDefined());