From f85fc09f1587c9949ea3767aa0c4e0e89a308524 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 29 Jan 2013 19:06:24 +0100 Subject: [PATCH] better detection of incomplete blocks in formatter (PY-6360) --- .../jetbrains/python/formatter/PyBlock.java | 31 ++++++++++++++----- .../com/jetbrains/python/PyIndentTest.java | 12 +++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index 0e43d2eefa0d..dc7cee7ca3b8 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -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; } diff --git a/python/testSrc/com/jetbrains/python/PyIndentTest.java b/python/testSrc/com/jetbrains/python/PyIndentTest.java index 7377b2198440..d321231f557c 100644 --- a/python/testSrc/com/jetbrains/python/PyIndentTest.java +++ b/python/testSrc/com/jetbrains/python/PyIndentTest.java @@ -333,4 +333,16 @@ public class PyIndentTest extends PyTestCase { " = [a,\n" + " b]"); } + + public void testAlignInIncompleteCall() { // PY-6360 + doTest("q = query.Nested(query.Term(\"type\", \"class\"),\n" + + "\n" + + "def bar():\n" + + " print('hello')", + "q = query.Nested(query.Term(\"type\", \"class\"),\n" + + " \n" + + "\n" + + "def bar():\n" + + " print('hello')"); + } }