diff --git a/python/src/com/jetbrains/python/parsing/StatementParsing.java b/python/src/com/jetbrains/python/parsing/StatementParsing.java index 391b69ac8369..55473e16ccfe 100644 --- a/python/src/com/jetbrains/python/parsing/StatementParsing.java +++ b/python/src/com/jetbrains/python/parsing/StatementParsing.java @@ -586,7 +586,7 @@ public class StatementParsing getExpressionParser().parseExpression(); if (myBuilder.getTokenType() == PyTokenTypes.AS_KEYWORD) { myBuilder.advanceLexer(); - getExpressionParser().parseExpression(); + getExpressionParser().parseExpression(true, true); // 'as' is followed by a target } checkMatches(PyTokenTypes.COLON, "colon expected"); parseSuite(); diff --git a/python/src/com/jetbrains/python/psi/PyWithStatement.java b/python/src/com/jetbrains/python/psi/PyWithStatement.java index 88535bf05925..c0b71569bee9 100644 --- a/python/src/com/jetbrains/python/psi/PyWithStatement.java +++ b/python/src/com/jetbrains/python/psi/PyWithStatement.java @@ -3,5 +3,5 @@ package com.jetbrains.python.psi; /** * @author yole */ -public interface PyWithStatement extends PyStatement { +public interface PyWithStatement extends PyStatement, NameDefiner { } diff --git a/python/src/com/jetbrains/python/psi/impl/PyWithStatementImpl.java b/python/src/com/jetbrains/python/psi/impl/PyWithStatementImpl.java index bcf2b6c1f8d9..9a81159c3feb 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyWithStatementImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyWithStatementImpl.java @@ -1,8 +1,11 @@ package com.jetbrains.python.psi.impl; import com.intellij.lang.ASTNode; -import com.jetbrains.python.psi.PyElementVisitor; -import com.jetbrains.python.psi.PyWithStatement; +import com.intellij.psi.PsiElement; +import com.jetbrains.python.psi.*; +import com.jetbrains.python.PyElementTypes; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author yole @@ -15,4 +18,26 @@ public class PyWithStatementImpl extends PyElementImpl implements PyWithStatemen protected void acceptPyVisitor(final PyElementVisitor pyVisitor) { pyVisitor.visitPyWithStatement(this); } + + @Nullable + public PyExpression getTargetExpression() { + final ASTNode asNameNode = getNode().findChildByType(PyElementTypes.TARGET_EXPRESSION); + if (asNameNode == null) return null; + return (PyTargetExpression)asNameNode.getPsi(); + } + + @NotNull + public Iterable iterateNames() { + PyElement ret = getTargetExpression(); + return new SingleIterable(ret); + } + + public PsiElement getElementNamed(final String the_name) { + PyElement named_elt = IterHelper.findName(iterateNames(), the_name); + return named_elt; + } + + public boolean mustResolveOutside() { + return false; + } } diff --git a/python/testData/psi/WithStatement.txt b/python/testData/psi/WithStatement.txt index db22d47ba510..85ed7427adb9 100644 --- a/python/testData/psi/WithStatement.txt +++ b/python/testData/psi/WithStatement.txt @@ -19,7 +19,7 @@ PyFile:WithStatement.py PsiWhiteSpace(' ') PsiElement(Py:AS_KEYWORD)('as') PsiWhiteSpace(' ') - PyReferenceExpression: y + PyTargetExpression: y PsiElement(Py:IDENTIFIER)('y') PsiElement(Py:COLON)(':') PsiWhiteSpace('\n ') diff --git a/python/testData/psi/WithStatement2.txt b/python/testData/psi/WithStatement2.txt index 4f29c7dea2d3..6f9f462b6129 100644 --- a/python/testData/psi/WithStatement2.txt +++ b/python/testData/psi/WithStatement2.txt @@ -55,7 +55,7 @@ PyFile:WithStatement2.py PsiWhiteSpace(' ') PsiElement(Py:AS_KEYWORD)('as') PsiWhiteSpace(' ') - PyReferenceExpression: y + PyTargetExpression: y PsiElement(Py:IDENTIFIER)('y') PsiElement(Py:COLON)(':') PsiWhiteSpace(' ') diff --git a/python/testData/resolve/WithStatement.py b/python/testData/resolve/WithStatement.py new file mode 100644 index 000000000000..076847616304 --- /dev/null +++ b/python/testData/resolve/WithStatement.py @@ -0,0 +1,3 @@ +from __future__ import with_statement +with None as boom: + boom + 1 diff --git a/python/testSrc/com/jetbrains/python/PyResolveTest.java b/python/testSrc/com/jetbrains/python/PyResolveTest.java index bedbe219363d..54a5669e6723 100644 --- a/python/testSrc/com/jetbrains/python/PyResolveTest.java +++ b/python/testSrc/com/jetbrains/python/PyResolveTest.java @@ -140,6 +140,11 @@ public class PyResolveTest extends ResolveTestCase { assertNotNull(PsiTreeUtil.getParentOfType(targetElement, PyAssignmentStatement.class)); // it's deep in a tuple } + public void testWithStatement() throws Exception { + PsiElement targetElement = resolve(); + assertTrue(targetElement instanceof PyTargetExpression); + assertTrue(targetElement.getParent() instanceof PyWithStatement); + } @Override