From 329c03abd9a8f0e5cbb9eb02e20710c7eff7950c Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Wed, 18 Jan 2017 19:53:16 +0300 Subject: [PATCH] Consider option about new line after opening brace in dicts when aligning closing one Because this setting causes hanging indent to appear on reformat. --- python/src/com/jetbrains/python/formatter/PyBlock.java | 3 ++- ...ingBraceInDictLiteralWhenOpeningBraceIsForcedOnNewLine.py | 3 +++ ...ceInDictLiteralWhenOpeningBraceIsForcedOnNewLine_after.py | 4 ++++ python/testSrc/com/jetbrains/python/PyFormatterTest.java | 5 +++++ 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 python/testData/formatter/noAlignmentClosingBraceInDictLiteralWhenOpeningBraceIsForcedOnNewLine.py create mode 100644 python/testData/formatter/noAlignmentClosingBraceInDictLiteralWhenOpeningBraceIsForcedOnNewLine_after.py diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index 6f554254bf7b..e3427af2cc5b 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -617,7 +617,8 @@ public class PyBlock implements ASTBlock { myContext.getMode() == FormattingMode.ADJUST_INDENT) { return true; } - return !hasHangingIndent(myNode.getPsi()); + return !hasHangingIndent(myNode.getPsi()) && !(myNode.getElementType() == PyElementTypes.DICT_LITERAL_EXPRESSION && + myContext.getPySettings().DICT_NEW_LINE_AFTER_LEFT_BRACE); } if (myNode.getElementType() == PyElementTypes.ARGUMENT_LIST) { if (!myContext.getSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS || hasHangingIndent(myNode.getPsi())) { diff --git a/python/testData/formatter/noAlignmentClosingBraceInDictLiteralWhenOpeningBraceIsForcedOnNewLine.py b/python/testData/formatter/noAlignmentClosingBraceInDictLiteralWhenOpeningBraceIsForcedOnNewLine.py new file mode 100644 index 000000000000..318891c69c57 --- /dev/null +++ b/python/testData/formatter/noAlignmentClosingBraceInDictLiteralWhenOpeningBraceIsForcedOnNewLine.py @@ -0,0 +1,3 @@ +d = {"foo": 1, + "bar": 2 + } \ No newline at end of file diff --git a/python/testData/formatter/noAlignmentClosingBraceInDictLiteralWhenOpeningBraceIsForcedOnNewLine_after.py b/python/testData/formatter/noAlignmentClosingBraceInDictLiteralWhenOpeningBraceIsForcedOnNewLine_after.py new file mode 100644 index 000000000000..7f322119f4a7 --- /dev/null +++ b/python/testData/formatter/noAlignmentClosingBraceInDictLiteralWhenOpeningBraceIsForcedOnNewLine_after.py @@ -0,0 +1,4 @@ +d = { + "foo": 1, + "bar": 2 +} diff --git a/python/testSrc/com/jetbrains/python/PyFormatterTest.java b/python/testSrc/com/jetbrains/python/PyFormatterTest.java index 02544de4aca9..831363ec3d21 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -484,6 +484,11 @@ public class PyFormatterTest extends PyTestCase { public void testAlignmentOfClosingBraceInDictLiteralWhenNoHangingIndent() { doTest(); } + + public void testNoAlignmentClosingBraceInDictLiteralWhenOpeningBraceIsForcedOnNewLine() { + getPythonCodeStyleSettings().DICT_NEW_LINE_AFTER_LEFT_BRACE = true; + doTest(); + } // PY-13004 public void testAlignmentOfClosingParenthesisOfArgumentListWhenNoHangingIndent() {