diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt index 7b0fb085f1a..b70e6962ccc 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt @@ -664,6 +664,12 @@ private fun hasLineBreakBefore(node: ASTNode): Boolean { return prevSibling?.elementType == TokenType.WHITE_SPACE && prevSibling?.textContains('\n') == true } +private fun hasDoubleLineBreakBefore(node: ASTNode): Boolean { + val prevSibling = node.leaves(false).firstOrNull() ?: return false + + return prevSibling.text.count { it == '\n' } >= 2 +} + fun NodeIndentStrategy.PositionStrategy.continuationIf( option: (KotlinCodeStyleSettings) -> Boolean, indentFirst: Boolean = false @@ -909,8 +915,10 @@ private fun getAlignmentForChildInParenthesis( val childNodeType = node.elementType val prev = getPrevWithoutWhitespace(node) - if (prev != null && prev.elementType === TokenType.ERROR_ELEMENT || childNodeType === TokenType.ERROR_ELEMENT) { - // Prefer align to parameters on incomplete code (case of line break after comma, when next parameters is absent) + val hasTrailingComma = childNodeType === closeBracket && prev?.elementType == COMMA + + if (hasTrailingComma && hasDoubleLineBreakBefore(node)) { + // Prefer align to parameters on code with trailing comma (case of line break after comma, when before closing bracket there was a line break) return parameterAlignment } diff --git a/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt b/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt index 60e4f0a3913..f024009e61e 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt @@ -783,7 +783,7 @@ class TypedHandlerTest : KotlinLightCodeInsightTestCase() { """ |class A( | a: Int, - | + | |) """, enableSmartEnterWithTabs() @@ -801,7 +801,56 @@ class TypedHandlerTest : KotlinLightCodeInsightTestCase() { """ |fun method( | arg1: String, - | + | + |) {} + """, + enableSmartEnterWithTabs() + ) + } + + fun testEnterWithoutLineBreakBeforeClosingBracketInMethodParameters() { + doTypeTest( + '\n', + """ + |fun method( + | arg1: String,) {} + """, + """ + |fun method( + | arg1: String, + |) {} + """, + enableSmartEnterWithTabs() + ) + } + + fun testEnterWithTrailingCommaAndWhitespaceBeforeLineBreak() { + doTypeTest( + '\n', + """ + |fun method( + | arg1: String, + |) {} + """, + """ + |fun method( + | arg1: String, + | + |) {} + """, + enableSmartEnterWithTabs() + ) + } + + fun testSmartEnterBetweenOpeningAndClosingBrackets() { + doTypeTest( + '\n', + """ + |fun method() {} + """, + """ + |fun method( + | |) {} """, enableSmartEnterWithTabs()