From d73aaff69f63319fd0a7085e01da7feec7e02581 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Wed, 12 Mar 2008 12:48:27 +0300 Subject: [PATCH] fix resolve for 'global' statements --- .../com/jetbrains/python/PyElementTypes.java | 3 ++ .../psi/impl/PyGlobalStatementImpl.java | 44 +++++++++---------- python/testData/resolve/Global.py | 5 +++ .../com/jetbrains/python/PyResolveTest.java | 7 +++ 4 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 python/testData/resolve/Global.py diff --git a/python/src/com/jetbrains/python/PyElementTypes.java b/python/src/com/jetbrains/python/PyElementTypes.java index 45370cc4fde8..64bcb9e1de04 100644 --- a/python/src/com/jetbrains/python/PyElementTypes.java +++ b/python/src/com/jetbrains/python/PyElementTypes.java @@ -56,6 +56,9 @@ public interface PyElementTypes { // Expressions PyElementType EMPTY_EXPRESSION = new PyElementType("EMPTY_EXPRESSION", PyEmptyExpressionImpl.class); PyElementType REFERENCE_EXPRESSION = new PyElementType("REFERENCE_EXPRESSION", PyReferenceExpressionImpl.class); + + TokenSet REFERENCE_EXPRESSION_SET = TokenSet.create(REFERENCE_EXPRESSION); + PyElementType TARGET_EXPRESSION = new PyElementType("TARGET_EXPRESSION", PyTargetExpressionImpl.class); PyElementType INTEGER_LITERAL_EXPRESSION = new PyElementType("INTEGER_LITERAL_EXPRESSION", PyNumericLiteralExpressionImpl.class); PyElementType FLOAT_LITERAL_EXPRESSION = new PyElementType("FLOAT_LITERAL_EXPRESSION", PyNumericLiteralExpressionImpl.class); diff --git a/python/src/com/jetbrains/python/psi/impl/PyGlobalStatementImpl.java b/python/src/com/jetbrains/python/psi/impl/PyGlobalStatementImpl.java index 9c1f83bcbbda..409b30cad6cb 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyGlobalStatementImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyGlobalStatementImpl.java @@ -20,7 +20,6 @@ import com.intellij.lang.ASTNode; import com.intellij.psi.PsiElement; import com.intellij.psi.ResolveState; import com.intellij.psi.scope.PsiScopeProcessor; -import com.intellij.psi.tree.TokenSet; import org.jetbrains.annotations.NotNull; import com.jetbrains.python.PyElementTypes; import com.jetbrains.python.psi.PyElementVisitor; @@ -36,29 +35,30 @@ import com.jetbrains.python.psi.PyReferenceExpression; * To change this template use File | Settings | File Templates. */ public class PyGlobalStatementImpl extends PyElementImpl implements PyGlobalStatement { - private TokenSet REFERENCES = TokenSet.create(PyElementTypes.REFERENCE_EXPRESSION); + public PyGlobalStatementImpl(ASTNode astNode) { + super(astNode); + } - public PyGlobalStatementImpl(ASTNode astNode) { - super(astNode); - } + @Override + protected void acceptPyVisitor(PyElementVisitor pyVisitor) { + pyVisitor.visitPyGlobalStatement(this); + } - @Override protected void acceptPyVisitor(PyElementVisitor pyVisitor) { - pyVisitor.visitPyGlobalStatement(this); - } + @NotNull + public PyReferenceExpression[] getGlobals() { + return childrenToPsi(PyElementTypes.REFERENCE_EXPRESSION_SET, PyReferenceExpression.EMPTY_ARRAY); + } - @NotNull public PyReferenceExpression[] getGlobals() { - return childrenToPsi(REFERENCES, PyReferenceExpression.EMPTY_ARRAY); - } - - public boolean processDeclarations(@NotNull PsiScopeProcessor processor, - @NotNull ResolveState substitutor, - PsiElement lastParent, - @NotNull PsiElement place) { - for (PyExpression expression: getGlobals()) { - if (!expression.processDeclarations(processor, substitutor, lastParent, place)) { - return false; - } - } - return true; + public boolean processDeclarations(@NotNull PsiScopeProcessor processor, + @NotNull ResolveState substitutor, + PsiElement lastParent, + @NotNull PsiElement place) { + for (PyExpression expression : getGlobals()) { + if (expression == lastParent) continue; + if (!expression.processDeclarations(processor, substitutor, lastParent, place)) { + return false; + } } + return true; + } } diff --git a/python/testData/resolve/Global.py b/python/testData/resolve/Global.py new file mode 100644 index 000000000000..9602cc843050 --- /dev/null +++ b/python/testData/resolve/Global.py @@ -0,0 +1,5 @@ +xx = 1 +def f(): + global xx + print xx + \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyResolveTest.java b/python/testSrc/com/jetbrains/python/PyResolveTest.java index 38ec32666f49..9009bb405ad6 100644 --- a/python/testSrc/com/jetbrains/python/PyResolveTest.java +++ b/python/testSrc/com/jetbrains/python/PyResolveTest.java @@ -11,6 +11,7 @@ import com.intellij.testFramework.ResolveTestCase; import com.jetbrains.python.psi.PyClass; import com.jetbrains.python.psi.PyFunction; import com.jetbrains.python.psi.PyTargetExpression; +import com.jetbrains.python.psi.PyAssignmentStatement; public class PyResolveTest extends ResolveTestCase { private PsiElement resolve() throws Exception { @@ -63,6 +64,12 @@ public class PyResolveTest extends ResolveTestCase { assertTrue(targetElement instanceof PyTargetExpression); } + public void testGlobal() throws Exception { + PsiElement targetElement = resolve(); + assertTrue(targetElement instanceof PyTargetExpression); + assertTrue(targetElement.getParent() instanceof PyAssignmentStatement); + } + @Override protected String getTestDataPath() { return PathManager.getHomePath() + "/plugins/python/testData/resolve/";