PY-12145 Align closing brace when there is no hanging indent in parental expression

Also update expected test data in a couple of existing tests to be PEP8 complaint
This commit is contained in:
Mikhail Golubev
2015-01-19 21:41:13 +03:00
parent 697afe15ec
commit 4ced2f94f7
6 changed files with 51 additions and 10 deletions
@@ -336,7 +336,16 @@ public class PyBlock implements ASTBlock {
else if (elem instanceof PyFunction) {
return hasHangingIndent(((PyFunction)elem).getParameterList());
}
else if (elem instanceof PySequenceExpression) {
final PsiElement firstChild = elem.getFirstChild();
if (firstChild == null) {
return false;
}
if (PyTokenTypes.OPEN_BRACES.contains(firstChild.getNode().getElementType()) && hasLineBreaksAfter(firstChild.getNode(), 1)) {
return true;
}
if (elem instanceof PySequenceExpression) {
items = ((PySequenceExpression)elem).getElements();
}
else if (elem instanceof PyParameterList) {
@@ -345,14 +354,19 @@ public class PyBlock implements ASTBlock {
else if (elem instanceof PyArgumentList) {
items = ((PyArgumentList)elem).getArguments();
}
else if (elem instanceof PyParenthesizedExpression) {
final PyParenthesizedExpression parenthesizedExpr = (PyParenthesizedExpression)elem;
if (parenthesizedExpr.getContainedExpression() instanceof PyTupleExpression) {
items = (((PyTupleExpression)parenthesizedExpr.getContainedExpression()).getElements());
}
else {
items = new PsiElement[]{parenthesizedExpr.getContainedExpression()};
}
}
else {
return false;
}
if (items.length == 0) {
return true;
}
final PsiElement firstItem = items[0];
return hasLineBreaksBefore(firstItem.getNode(), 1) || hasHangingIndent(firstItem);
return items.length == 0 || hasHangingIndent(items[0]);
}
private static boolean breaksAlignment(IElementType type) {
@@ -439,7 +453,7 @@ public class PyBlock implements ASTBlock {
myContext.getMode() == FormattingMode.ADJUST_INDENT) {
return true;
}
return false;
return !hasHangingIndent(_node.getPsi());
}
if (_node.getElementType() == PyElementTypes.ARGUMENT_LIST) {
if (!myContext.getSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS || hasHangingIndent(_node.getPsi())) {
@@ -477,12 +491,18 @@ public class PyBlock implements ASTBlock {
return node;
}
private static boolean hasLineBreaksBefore(ASTNode child, int minCount) {
private static boolean hasLineBreaksBefore(@NotNull ASTNode child, int minCount) {
final ASTNode treePrev = child.getTreePrev();
return (treePrev != null && isWhitespaceWithLineBreaks(TreeUtil.findLastLeaf(treePrev), minCount)) ||
isWhitespaceWithLineBreaks(child.getFirstChildNode(), minCount);
}
private static boolean hasLineBreaksAfter(@NotNull ASTNode child, int minCount) {
final ASTNode treeNext = child.getTreeNext();
return (treeNext != null && isWhitespaceWithLineBreaks(TreeUtil.findLastLeaf(treeNext), minCount)) ||
isWhitespaceWithLineBreaks(child.getLastChildNode(), minCount);
}
private static boolean isWhitespaceWithLineBreaks(ASTNode node, int minCount) {
if (node != null && node.getElementType() == TokenType.WHITE_SPACE) {
String prevNodeText = node.getText();
@@ -5,4 +5,4 @@ print(1 + (1 + (1
+
x.
calc()
))
))
@@ -0,0 +1,8 @@
class Checkpoints(webapp2.RequestHandler):
def get(self):
self.response.write(json.dumps({"meta": {"code": 400,
"errorType": "paramError",
"errorDetail": "Parameter 'api_key' is missing"
},
"response": {}
}))
@@ -0,0 +1,8 @@
class Checkpoints(webapp2.RequestHandler):
def get(self):
self.response.write(json.dumps({"meta": {"code": 400,
"errorType": "paramError",
"errorDetail": "Parameter 'api_key' is missing"
},
"response": {}
}))
@@ -1,4 +1,4 @@
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
)
@@ -466,6 +466,11 @@ public class PyFormatterTest extends PyTestCase {
}
}
// PY-12145
public void testAlignmentOfClosingBraceInDictLiteralWhenNoHangingIndent() {
doTest();
}
/**
* This test merely checks that call to {@link com.intellij.psi.codeStyle.CodeStyleManager#reformat(com.intellij.psi.PsiElement)}
* is possible for Python sources.