fix resolve for 'global' statements

This commit is contained in:
Dmitry Jemerov
2008-03-12 12:48:27 +03:00
parent e281dc112c
commit d73aaff69f
4 changed files with 37 additions and 22 deletions
@@ -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);
@@ -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;
}
}
+5
View File
@@ -0,0 +1,5 @@
xx = 1
def f():
global x<ref>x
print xx
@@ -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/";