diff --git a/python/src/com/jetbrains/python/formatter/PyBlock.java b/python/src/com/jetbrains/python/formatter/PyBlock.java index 1c94b0ecbe1d..461e0fad9b90 100644 --- a/python/src/com/jetbrains/python/formatter/PyBlock.java +++ b/python/src/com/jetbrains/python/formatter/PyBlock.java @@ -323,8 +323,13 @@ public class PyBlock implements ASTBlock { if (childType == PyTokenTypes.RPAR) { childIndent = Indent.getNoneIndent(); } - else if (childType != PyTokenTypes.LPAR){ - childIndent = Indent.getContinuationIndent(); + else { + if (parentType == PyElementTypes.PARAMETER_LIST || argumentMayHaveSameIndentAsFollowingStatementList()) { + childIndent = Indent.getContinuationIndent(); + } + else { + childIndent = Indent.getNormalIndent(); + } } } else if (parentType == PyElementTypes.SUBSCRIPTION_EXPRESSION) { @@ -421,6 +426,19 @@ public class PyBlock implements ASTBlock { return node.getPsi() instanceof PySequenceExpression && ((PySequenceExpression)node.getPsi()).isEmpty(); } + private boolean argumentMayHaveSameIndentAsFollowingStatementList() { + if (myNode.getElementType() != PyElementTypes.ARGUMENT_LIST) { + return false; + } + // This check is supposed to prevent PEP8's error: Continuation line with the same indent as next logical line + final PsiElement header = getControlStatementHeader(myNode); + if (header instanceof PyStatementListContainer) { + final PyStatementList statementList = ((PyStatementListContainer)header).getStatementList(); + return PyUtil.onSameLine(header, myNode.getPsi()) && !PyUtil.onSameLine(header, statementList); + } + return false; + } + // Check https://www.python.org/dev/peps/pep-0008/#indentation private static boolean hasHangingIndent(@NotNull PsiElement elem) { if (elem instanceof PyCallExpression) { diff --git a/python/testData/formatter/continuationIndentIsNotUsedForNestedFunctionCallsInWithStatement_after.py b/python/testData/formatter/continuationIndentIsNotUsedForNestedFunctionCallsInWithStatement_after.py index 9a4e5063b2ab..c867a3f0451a 100644 --- a/python/testData/formatter/continuationIndentIsNotUsedForNestedFunctionCallsInWithStatement_after.py +++ b/python/testData/formatter/continuationIndentIsNotUsedForNestedFunctionCallsInWithStatement_after.py @@ -1,4 +1,4 @@ with raises_assertion( has_string('Missing download_urls: {}, {}'.format( - self.other_download_url, self.another_download_url))): + self.other_download_url, self.another_download_url))): fixture.assert_detail_page_yields_expected() diff --git a/python/testData/formatter/hangingIndentInNamedArgumentValue_after.py b/python/testData/formatter/hangingIndentInNamedArgumentValue_after.py index 1102984b4b98..4585528e01cd 100644 --- a/python/testData/formatter/hangingIndentInNamedArgumentValue_after.py +++ b/python/testData/formatter/hangingIndentInNamedArgumentValue_after.py @@ -1,3 +1,3 @@ funcWithLongName(x=[ ], - y=42) + y=42) diff --git a/python/testData/formatter/noAlignForMethodArguments_after.py b/python/testData/formatter/noAlignForMethodArguments_after.py index 740f638adf4c..5682f4ca4f45 100644 --- a/python/testData/formatter/noAlignForMethodArguments_after.py +++ b/python/testData/formatter/noAlignForMethodArguments_after.py @@ -2,4 +2,4 @@ def long_method_name(bar, baz): pass long_method_name("long string one", - "long string two") + "long string two") diff --git a/python/testData/formatter/noWrapBeforeParen_after.py b/python/testData/formatter/noWrapBeforeParen_after.py index 003f387c408c..5ab3054984db 100644 --- a/python/testData/formatter/noWrapBeforeParen_after.py +++ b/python/testData/formatter/noWrapBeforeParen_after.py @@ -4,4 +4,4 @@ def foo(): if comments: for comment in comments: record += ' \n' + ca + '=' + quoteattr(comment[ca]) + ' ' for ca in comment) + '/>\n' diff --git a/python/testData/formatter/setLiteralInArgList_after.py b/python/testData/formatter/setLiteralInArgList_after.py index 48bec3381227..9c2f75d6dca6 100644 --- a/python/testData/formatter/setLiteralInArgList_after.py +++ b/python/testData/formatter/setLiteralInArgList_after.py @@ -1,3 +1,3 @@ self.assertEqual( - {"000000000000", "111111111111"}, - foo['bar']['baz']) + {"000000000000", "111111111111"}, + foo['bar']['baz']) diff --git a/python/testData/refactoring/introduceVariable/functionCallWithCommentNotInlined.after.py b/python/testData/refactoring/introduceVariable/functionCallWithCommentNotInlined.after.py index f73d8462c0c9..3140c9b2ab96 100644 --- a/python/testData/refactoring/introduceVariable/functionCallWithCommentNotInlined.after.py +++ b/python/testData/refactoring/introduceVariable/functionCallWithCommentNotInlined.after.py @@ -1,8 +1,8 @@ import subprocess as sp a = sp.check_output( - args=['python', '-c', 'print("Spam")'], - # read errors too - stderr=sp.STDOUT + args=['python', '-c', 'print("Spam")'], + # read errors too + stderr=sp.STDOUT ) print(a) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyIndentTest.java b/python/testSrc/com/jetbrains/python/PyIndentTest.java index d6c09b817227..0164e14fc5ce 100644 --- a/python/testSrc/com/jetbrains/python/PyIndentTest.java +++ b/python/testSrc/com/jetbrains/python/PyIndentTest.java @@ -16,6 +16,7 @@ package com.jetbrains.python; import com.intellij.openapi.actionSystem.IdeActions; +import com.intellij.openapi.command.CommandProcessor; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.jetbrains.python.fixtures.PyTestCase; @@ -199,7 +200,7 @@ public class PyIndentTest extends PyTestCase { public void testEnterInNonEmptyArgList() { // PY-1947 doTest("Task(params=1)", "Task(\n" + - " params=1)"); + " params=1)"); } public void testEnterInNonClosedArgList() { // PY-4863