PY-22272 Don't use brackets of indexed dict values for alignment

In general, don't apply this special casing if dict value doesn't start
with a bracket.
This commit is contained in:
Mikhail Golubev
2017-09-14 15:26:15 +03:00
parent 5ebc436808
commit 4b18fccf8f
4 changed files with 25 additions and 4 deletions

View File

@@ -390,12 +390,11 @@ public class PyBlock implements ASTBlock {
}
if (settings.DICT_ALIGNMENT == DICT_ALIGNMENT_ON_VALUE) {
if (isValueOfKeyValuePairOfDictLiteral(child) && !ourListElementTypes.contains(childType)) {
// Align not the whole value but its left bracket if it starts with it
if (isValueOfKeyValuePairOfDictLiteral(child) && !isOpeningBracket(child.getFirstChildNode())) {
childAlignment = myParent.myDictAlignment;
}
else if (isValueOfKeyValuePairOfDictLiteral(myNode) &&
ourListElementTypes.contains(parentType) &&
PyTokenTypes.OPEN_BRACES.contains(childType)) {
else if (isValueOfKeyValuePairOfDictLiteral(myNode) && isOpeningBracket(child)) {
childAlignment = myParent.myParent.myDictAlignment;
}
}
@@ -419,6 +418,10 @@ public class PyBlock implements ASTBlock {
return new PyBlock(this, child, childAlignment, childIndent, childWrap, myContext);
}
private static boolean isOpeningBracket(@Nullable ASTNode node) {
return node != null && PyTokenTypes.OPEN_BRACES.contains(node.getElementType()) && node == node.getTreeParent().getFirstChildNode();
}
private static boolean isValueOfKeyValuePairOfDictLiteral(@NotNull ASTNode node) {
return isValueOfKeyValuePair(node) && isChildOfDictLiteral(node.getTreeParent());
}

View File

@@ -0,0 +1,6 @@
my_dict = {
"one": example_list[0],
"two": example_list[1],
"three": example_list[2:3],
"some really long element name that takes a lot of space": "four"
}

View File

@@ -0,0 +1,6 @@
my_dict = {
"one": example_list[0],
"two": example_list[1],
"three": example_list[2:3],
"some really long element name that takes a lot of space": "four"
}

View File

@@ -603,6 +603,12 @@ public class PyFormatterTest extends PyTestCase {
doTest();
}
// PY-22272
public void testAlightDictLiteralOnValueSubscriptionsAndSlices() {
getPythonCodeStyleSettings().DICT_ALIGNMENT = PyCodeStyleSettings.DICT_ALIGNMENT_ON_VALUE;
doTest();
}
// PY-14962
public void testAlignDictLiteralOnColon() {
getPythonCodeStyleSettings().DICT_ALIGNMENT = PyCodeStyleSettings.DICT_ALIGNMENT_ON_COLON;