better detection of incomplete blocks in formatter (PY-6360)

This commit is contained in:
Dmitry Jemerov
2013-01-29 20:31:10 +01:00
parent 2cb47f69b3
commit f85fc09f15
2 changed files with 36 additions and 7 deletions
@@ -601,12 +601,14 @@ public class PyBlock implements ASTBlock {
public boolean isIncomplete() {
// if there's something following us, we're not incomplete
PsiElement element = _node.getPsi().getNextSibling();
while (element instanceof PsiWhiteSpace) {
element = element.getNextSibling();
}
if (element != null) {
return false;
if (!PsiTreeUtil.hasErrorElements(_node.getPsi())) {
PsiElement element = _node.getPsi().getNextSibling();
while (element instanceof PsiWhiteSpace) {
element = element.getNextSibling();
}
if (element != null) {
return false;
}
}
ASTNode lastChild = getLastNonSpaceChild(_node, false);
@@ -619,21 +621,36 @@ public class PyBlock implements ASTBlock {
}
}
if (lastChild.getElementType() == PyElementTypes.BINARY_EXPRESSION) {
PyBinaryExpression binaryExpression = (PyBinaryExpression) lastChild.getPsi();
PyBinaryExpression binaryExpression = (PyBinaryExpression)lastChild.getPsi();
if (binaryExpression.getRightExpression() == null) {
return true;
}
}
if (isIncompleteCall(lastChild)) return true;
}
if (_node.getPsi() instanceof PyArgumentList) {
final PyArgumentList argumentList = (PyArgumentList)_node.getPsi();
return argumentList.getClosingParen() == null;
}
if (isIncompleteCall(_node)) {
return true;
}
return false;
}
private static boolean isIncompleteCall(ASTNode node) {
if (node.getElementType() == PyElementTypes.CALL_EXPRESSION) {
PyCallExpression callExpression = (PyCallExpression)node.getPsi();
PyArgumentList argumentList = callExpression.getArgumentList();
if (argumentList == null || argumentList.getClosingParen() == null) {
return true;
}
}
return false;
}
public boolean isLeaf() {
return _node.getFirstChildNode() == null;
}
@@ -333,4 +333,16 @@ public class PyIndentTest extends PyTestCase {
" <caret>= [a,\n" +
" b]");
}
public void testAlignInIncompleteCall() { // PY-6360
doTest("q = query.Nested(query.Term(\"type\", \"class\"),<caret>\n" +
"\n" +
"def bar():\n" +
" print('hello')",
"q = query.Nested(query.Term(\"type\", \"class\"),\n" +
" <caret>\n" +
"\n" +
"def bar():\n" +
" print('hello')");
}
}