PY-13304, PY-17979 Add option that allows to use continuation indent for arguments

This commit is contained in:
Mikhail Golubev
2016-01-13 20:19:39 +03:00
parent 5b763d7de7
commit 0f0ee7d731
8 changed files with 71 additions and 7 deletions
@@ -965,6 +965,7 @@ formatter.panel.dict.alignment.align.on.colon=Align on colon
formatter.panel.dict.alignment.align.on.value=Align on value
formatter.panel.dict.alignment.label=Dict alignment:
formatter.panel.add.trailing.line.feed=Add line feed at the end of file
formatter.panel.use.continuation.indent.for.arguments=Use continuation indent for arguments
formatter.left.bracket = Left bracket
formatter.around.eq.in.named.parameter = Around = in named parameter
@@ -324,7 +324,9 @@ public class PyBlock implements ASTBlock {
childIndent = Indent.getNoneIndent();
}
else {
if (parentType == PyElementTypes.PARAMETER_LIST || argumentMayHaveSameIndentAsFollowingStatementList()) {
if (parentType == PyElementTypes.PARAMETER_LIST ||
settings.USE_CONTINUATION_INDENT_FOR_ARGUMENTS ||
argumentMayHaveSameIndentAsFollowingStatementList()) {
childIndent = Indent.getContinuationIndent();
}
else {
@@ -23,7 +23,7 @@
</vspacer>
</children>
</grid>
<grid id="d3271" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="d3271" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -47,12 +47,20 @@
</component>
<component id="b4945" class="com.intellij.ui.components.JBCheckBox" binding="myAddTrailingBlankLineCheckbox" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="formatter.panel.add.trailing.line.feed"/>
</properties>
</component>
<component id="9e329" class="com.intellij.ui.components.JBCheckBox" binding="myUseContinuationIndentForArguments" default-binding="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="com/jetbrains/python/PyBundle" key="formatter.panel.use.continuation.indent.for.arguments"/>
</properties>
</component>
</children>
</grid>
<vspacer id="4e0f0">
@@ -43,6 +43,7 @@ public class PyCodeStylePanel extends CodeStyleAbstractPanel {
private JPanel myPanel;
private JBCheckBox myAddTrailingBlankLineCheckbox;
private JBCheckBox myUseContinuationIndentForArguments;
private ComboBox myDictAlignmentCombo;
private JPanel myPreviewPanel;
@@ -71,6 +72,13 @@ public class PyCodeStylePanel extends CodeStyleAbstractPanel {
somethingChanged();
}
});
myUseContinuationIndentForArguments.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
somethingChanged();
}
});
}
@Override
@@ -103,6 +111,7 @@ public class PyCodeStylePanel extends CodeStyleAbstractPanel {
}
}
myAddTrailingBlankLineCheckbox.setSelected(getCustomSettings(settings).BLANK_LINE_AT_FILE_END);
myUseContinuationIndentForArguments.setSelected(getCustomSettings(settings).USE_CONTINUATION_INDENT_FOR_ARGUMENTS);
}
@Override
@@ -110,6 +119,7 @@ public class PyCodeStylePanel extends CodeStyleAbstractPanel {
final PyCodeStyleSettings customSettings = getCustomSettings(settings);
customSettings.DICT_ALIGNMENT = getDictAlignmentAsInt();
customSettings.BLANK_LINE_AT_FILE_END = ensureTrailingBlankLine();
customSettings.USE_CONTINUATION_INDENT_FOR_ARGUMENTS = useContinuationIndentForArguments();
}
@Override
@@ -137,8 +147,17 @@ public class PyCodeStylePanel extends CodeStyleAbstractPanel {
return myAddTrailingBlankLineCheckbox.isSelected();
}
public static final String PREVIEW = "{\n" +
" \"green\": 42,\n" +
" \"eggs and ham\": -0.0e0\n" +
"}";
private boolean useContinuationIndentForArguments() {
return myUseContinuationIndentForArguments.isSelected();
}
public static final String PREVIEW = "x = max(\n" +
" 1,\n" +
" 2,\n" +
" 3)\n" +
"\n" +
"{\n" +
" \"green\": 42,\n" +
" \"eggs and ham\": -0.0e0\n" +
"}";
}
@@ -78,6 +78,14 @@ public class PyCodeStyleSettings extends CustomCodeStyleSettings {
public boolean DICT_NEW_LINE_BEFORE_RIGHT_BRACE = false;
public int BLANK_LINES_AFTER_LOCAL_IMPORTS = 0;
/**
* Code style for most languages use continuation indent both for parameters in function definition and for arguments in function calls.
* In Python continuation indent (assuming it's 8 spaces) for parameters is required by PEP 8, because otherwise they won't be visually
* distinctive from function body. However for arguments (except several special cases) both normal and continuation indents are acceptable
* (as long as they're multiple of 4), though examples in PEP 8 itself use mostly normal indent. Nontheless some users prefer to have
* the same indetation level for arguments as for parameters.
*/
public boolean USE_CONTINUATION_INDENT_FOR_ARGUMENTS = false;
public PyCodeStyleSettings(CodeStyleSettings container) {
super("Python", container);
@@ -0,0 +1,10 @@
sum(
1,
2, 3,
5,
)
sum(
1,
2, 3,
5)
@@ -0,0 +1,10 @@
sum(
1,
2, 3,
5,
)
sum(
1,
2, 3,
5)
@@ -659,4 +659,10 @@ public class PyFormatterTest extends PyTestCase {
public void testBlanksBetweenImportsPreservedWithoutOptimizeImports() {
doTest();
}
// PY-17979, PY-13304
public void testContinuationIndentBeforeFunctionArguments() {
getPythonCodeStyleSettings().USE_CONTINUATION_INDENT_FOR_ARGUMENTS = true;
doTest();
}
}