From bdff8529ec9ad253fb1fd1aba4644275144ca4f3 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Mon, 28 Jan 2013 17:33:13 +0100 Subject: [PATCH] option to turn off alignment in collections and comprehensions (PY-8516) --- .../src/com/jetbrains/python/formatter/PyBlock.java | 9 +++++---- .../jetbrains/python/formatter/PyBlockContext.java | 12 ++++++++++-- .../python/formatter/PyCodeStyleSettings.java | 2 ++ .../PyLanguageCodeStyleSettingsProvider.java | 6 +++++- .../formatter/PythonFormattingModelBuilder.java | 3 +-- python/testData/formatter/indentInComprehensions.py | 2 ++ .../formatter/indentInComprehensions_after.py | 2 ++ .../com/jetbrains/python/PyFormatterTest.java | 5 +++++ 8 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 python/testData/formatter/indentInComprehensions.py create mode 100644 python/testData/formatter/indentInComprehensions_after.py diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index 45d2bcedfb06..df8531068341 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -149,7 +149,7 @@ public class PyBlock implements ASTBlock { } } - if (parentType == PyElementTypes.LIST_LITERAL_EXPRESSION) { + if (parentType == PyElementTypes.LIST_LITERAL_EXPRESSION || parentType == PyElementTypes.LIST_COMP_EXPRESSION) { if (childType == PyTokenTypes.RBRACKET || childType == PyTokenTypes.LBRACKET) { childIndent = Indent.getNoneIndent(); } @@ -167,7 +167,8 @@ public class PyBlock implements ASTBlock { : Indent.getNormalIndent(); } } - else if (parentType == PyElementTypes.DICT_LITERAL_EXPRESSION || parentType == PyElementTypes.SET_LITERAL_EXPRESSION) { + else if (parentType == PyElementTypes.DICT_LITERAL_EXPRESSION || parentType == PyElementTypes.SET_LITERAL_EXPRESSION || + parentType == PyElementTypes.SET_COMP_EXPRESSION || parentType == PyElementTypes.DICT_COMP_EXPRESSION) { if (childType == PyTokenTypes.RBRACE || !hasLineBreaksBefore(child, 1)) { childIndent = Indent.getNoneIndent(); } @@ -192,7 +193,7 @@ public class PyBlock implements ASTBlock { childIndent = Indent.getNormalIndent(); } } - else if ((parentType == PyElementTypes.PARENTHESIZED_EXPRESSION || parentType == PyElementTypes.GENERATOR_EXPRESSION)) { + else if (parentType == PyElementTypes.PARENTHESIZED_EXPRESSION || parentType == PyElementTypes.GENERATOR_EXPRESSION) { if (childType == PyTokenTypes.RPAR || !hasLineBreaksBefore(child, 1)) { childIndent = Indent.getNoneIndent(); } @@ -295,7 +296,7 @@ public class PyBlock implements ASTBlock { if (child.getElementType() == PyTokenTypes.COMMA) { return false; } - return true; + return myContext.getPySettings().ALIGN_COLLECTIONS_AND_COMPREHENSIONS; } @Nullable diff --git a/python/src/com/jetbrains/python/formatter/PyBlockContext.java b/python/src/com/jetbrains/python/formatter/PyBlockContext.java index 6a5aa1f84838..9974f40421bd 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlockContext.java +++ b/python/src/com/jetbrains/python/formatter/PyBlockContext.java @@ -2,18 +2,22 @@ package com.jetbrains.python.formatter; import com.intellij.formatting.FormattingMode; import com.intellij.formatting.SpacingBuilder; +import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; +import com.jetbrains.python.PythonLanguage; /** * @author yole */ public class PyBlockContext { private final CommonCodeStyleSettings mySettings; + private final PyCodeStyleSettings myPySettings; private final SpacingBuilder mySpacingBuilder; private final FormattingMode myMode; - public PyBlockContext(CommonCodeStyleSettings settings, SpacingBuilder builder, FormattingMode mode) { - mySettings = settings; + public PyBlockContext(CodeStyleSettings settings, SpacingBuilder builder, FormattingMode mode) { + mySettings = settings.getCommonSettings(PythonLanguage.getInstance()); + myPySettings = settings.getCustomSettings(PyCodeStyleSettings.class); mySpacingBuilder = builder; myMode = mode; } @@ -22,6 +26,10 @@ public class PyBlockContext { return mySettings; } + public PyCodeStyleSettings getPySettings() { + return myPySettings; + } + public SpacingBuilder getSpacingBuilder() { return mySpacingBuilder; } diff --git a/python/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java b/python/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java index c90398ced375..331fa49d832e 100644 --- a/python/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java +++ b/python/src/com/jetbrains/python/formatter/PyCodeStyleSettings.java @@ -16,6 +16,8 @@ public class PyCodeStyleSettings extends CustomCodeStyleSettings { public int BLANK_LINES_AROUND_TOP_LEVEL_CLASSES_FUNCTIONS = 2; + public boolean ALIGN_COLLECTIONS_AND_COMPREHENSIONS = true; + public PyCodeStyleSettings(CodeStyleSettings container) { super("Python", container); } diff --git a/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java b/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java index 1850d7ad6234..ff82545859c1 100644 --- a/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java +++ b/python/src/com/jetbrains/python/formatter/PyLanguageCodeStyleSettingsProvider.java @@ -73,6 +73,8 @@ public class PyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettin "WRAP_LONG_LINES", "ALIGN_MULTILINE_PARAMETERS", "ALIGN_MULTILINE_PARAMETERS_IN_CALLS"); + consumer.showCustomOption(PyCodeStyleSettings.class, "ALIGN_COLLECTIONS_AND_COMPREHENSIONS", "Align when multiline", + "Collections and Comprehensions"); } } @@ -115,7 +117,9 @@ public class PyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettin "long_parameter_2):\n" + " pass\n\n" + "xyzzy('long_string_constant1',\n" + - " 'long_string_constant2')"; + " 'long_string_constant2')\n" + + "attrs = [e.attr for e in\n" + + " items]"; @SuppressWarnings("FieldCanBeLocal") private static String INDENT_SETTINGS_PREVIEW = "def foo():\n" + " print 'bar'\n\n" + diff --git a/python/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java b/python/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java index 74a1509bf648..4e075d5075e8 100644 --- a/python/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java +++ b/python/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java @@ -35,8 +35,7 @@ public class PythonFormattingModelBuilder implements FormattingModelBuilderEx, C System.out.println("AST tree for " + element.getContainingFile().getName() + ":"); printAST(fileNode, 0); } - final CommonCodeStyleSettings codeStyleSettings = settings.getCommonSettings(PythonLanguage.getInstance()); - final PyBlockContext context = new PyBlockContext(codeStyleSettings, createSpacingBuilder(settings), mode); + final PyBlockContext context = new PyBlockContext(settings, createSpacingBuilder(settings), mode); final PyBlock block = new PyBlock(null, element.getNode(), null, Indent.getNoneIndent(), null, context); if (DUMP_FORMATTING_AST) { FormattingModelDumper.dumpFormattingModel(block, 2, System.out); diff --git a/python/testData/formatter/indentInComprehensions.py b/python/testData/formatter/indentInComprehensions.py new file mode 100644 index 000000000000..7e88b6b3d3bd --- /dev/null +++ b/python/testData/formatter/indentInComprehensions.py @@ -0,0 +1,2 @@ +attrs = [e.attr for e in +items] \ No newline at end of file diff --git a/python/testData/formatter/indentInComprehensions_after.py b/python/testData/formatter/indentInComprehensions_after.py new file mode 100644 index 000000000000..80a1223466b3 --- /dev/null +++ b/python/testData/formatter/indentInComprehensions_after.py @@ -0,0 +1,2 @@ +attrs = [e.attr for e in + items] \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyFormatterTest.java b/python/testSrc/com/jetbrains/python/PyFormatterTest.java index b56aa6006e8a..42472bf1976a 100644 --- a/python/testSrc/com/jetbrains/python/PyFormatterTest.java +++ b/python/testSrc/com/jetbrains/python/PyFormatterTest.java @@ -255,6 +255,11 @@ public class PyFormatterTest extends PyTestCase { doTest(); } + public void testIndentInComprehensions() { // PY-8516 + settings().getCustomSettings(PyCodeStyleSettings.class).ALIGN_COLLECTIONS_AND_COMPREHENSIONS = false; + doTest(); + } + public void testContinuationIndentForCallInStatementPart() { // PY-8577 doTest(); }