mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fix resolve for 'global' statements
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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/";
|
||||
|
||||
Reference in New Issue
Block a user