[kotlin][indent] support more cases before close parenthesis

^KTIJ-17368 Fixed
^KTIJ-21962

GitOrigin-RevId: f010e6d39eac92fa0ae0eb83e34e207d9b0b5a36
This commit is contained in:
Dmitry Gridin
2022-06-10 12:16:07 +00:00
committed by intellij-monorepo-bot
parent 708c3f9749
commit 45e316b991
27 changed files with 96 additions and 97 deletions
@@ -1,6 +1,5 @@
// SET_TRUE: SMART_TABS
// SET_TRUE: USE_TAB_CHARACTER
// WITHOUT_CUSTOM_LINE_INDENT_PROVIDER
fun method(
arg1: String,
@@ -1,6 +1,5 @@
// SET_TRUE: SMART_TABS
// SET_TRUE: USE_TAB_CHARACTER
// WITHOUT_CUSTOM_LINE_INDENT_PROVIDER
fun method(
arg1: String,<caret>
@@ -1,8 +1,7 @@
// SET_TRUE: SMART_TABS
// SET_TRUE: USE_TAB_CHARACTER
// WITHOUT_CUSTOM_LINE_INDENT_PROVIDER
class A(
a: Int,
<caret>
a: Int,
<caret>
)
@@ -1,7 +1,6 @@
// SET_TRUE: SMART_TABS
// SET_TRUE: USE_TAB_CHARACTER
// WITHOUT_CUSTOM_LINE_INDENT_PROVIDER
class A(
a: Int,<caret>
a: Int,<caret>
)
@@ -1,7 +1,6 @@
// SET_TRUE: SMART_TABS
// SET_TRUE: USE_TAB_CHARACTER
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS
// WITHOUT_CUSTOM_LINE_INDENT_PROVIDER
class A(
a: Int,
@@ -1,7 +1,6 @@
// SET_TRUE: SMART_TABS
// SET_TRUE: USE_TAB_CHARACTER
// SET_TRUE: ALIGN_MULTILINE_PARAMETERS
// WITHOUT_CUSTOM_LINE_INDENT_PROVIDER
class A(
a: Int,<caret>
@@ -1,5 +1,4 @@
// SET_TRUE: SMART_TABS
// WITHOUT_CUSTOM_LINE_INDENT_PROVIDER
fun method(
arg1: String,
@@ -1,5 +1,4 @@
// SET_TRUE: SMART_TABS
// WITHOUT_CUSTOM_LINE_INDENT_PROVIDER
fun method(
arg1: String, <caret>
@@ -1,7 +1,8 @@
fun a() {
val (
<caret>
<caret>
)
}
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// IGNORE_FORMATTER
@@ -1,7 +1,8 @@
fun a() {
val (
<caret>
<caret>
)
}
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// IGNORE_FORMATTER
@@ -2,4 +2,5 @@ fun a() {
val (<caret>)
}
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// IGNORE_FORMATTER
@@ -1,7 +1,8 @@
fun a() {
var (
<caret>
<caret>
)
}
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// IGNORE_FORMATTER
@@ -1,7 +1,8 @@
fun a() {
var (
<caret>
<caret>
)
}
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// IGNORE_FORMATTER
@@ -2,4 +2,5 @@ fun a() {
var (<caret>)
}
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// IGNORE_FORMATTER
@@ -1,8 +1,9 @@
fun a() {
a.forEach { (
<caret>
<caret>
)}
}
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// SET_FALSE: ALIGN_MULTILINE_BINARY_OPERATION
// IGNORE_FORMATTER
@@ -1,8 +1,9 @@
fun a() {
a.forEach { (
<caret>
<caret>
)}
}
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// SET_FALSE: ALIGN_MULTILINE_BINARY_OPERATION
// IGNORE_FORMATTER
@@ -4,3 +4,4 @@ fun a() {
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// SET_FALSE: ALIGN_MULTILINE_BINARY_OPERATION
// IGNORE_FORMATTER
@@ -1,8 +1,8 @@
class Test {
val a: Boolean get(
<caret>
<caret>
)
}
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// KT-39444
// IGNORE_FORMATTER
@@ -3,4 +3,4 @@ class Test {
}
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// KT-39444
// IGNORE_FORMATTER
@@ -1,9 +1,8 @@
class Test {
val a: Boolean get(
<caret>
<caret>
)
}
// SET_TRUE: ALIGN_MULTILINE_METHOD_BRACKETS
// IGNORE_FORMATTER
// KT-39444
// IGNORE_FORMATTER
@@ -3,5 +3,4 @@ class Test {
}
// SET_TRUE: ALIGN_MULTILINE_METHOD_BRACKETS
// IGNORE_FORMATTER
// KT-39444
// IGNORE_FORMATTER
@@ -2,8 +2,9 @@ class Test {
var a: Boolean
get() = false
set(
<caret>
<caret>
)
}
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// IGNORE_FORMATTER
@@ -4,4 +4,5 @@ class Test {
set(<caret>)
}
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// SET_FALSE: ALIGN_MULTILINE_METHOD_BRACKETS
// IGNORE_FORMATTER
@@ -2,7 +2,7 @@ class Test {
var a: Boolean
get() = false
set(
<caret>
<caret>
)
}
@@ -1,5 +1,5 @@
val somelong = 3 + 4 - (
<caret>
<caret>
)
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -1,6 +1,6 @@
fun a() {
val somelong = 3 + 4 - (
<caret>
<caret>
)
}
@@ -253,72 +253,70 @@ abstract class KotlinLangLineIndentProvider : JavaLikeLangLineIndentProvider() {
val leftParenthesis = currentPosition.findLeftParenthesisBackwardsSkippingNested(LeftParenthesis, RightParenthesis)
if (!leftParenthesis.isAt(LeftParenthesis)) return null
// case only for caret before [RightParenthesis]
if (!currentPosition.hasLineBreaksAfter(offset)) {
fun createIndent(
isParameterList: Boolean,
baseLineOffset: Int,
): IndentCalculator {
val indent = if (before.isAt(Comma)) {
when {
isParameterList && settings.alignMultilineParameters ||
!isParameterList && settings.alignMultilineParametersInCalls -> {
val firstElement = leftParenthesis.afterIgnoringWhiteSpaceOrComment()
return createIndentCalculator(createAlignMultilineIndent(firstElement), firstElement.startOffset)
}
isParameterList && settings.continuationIndentInParameterLists ||
!isParameterList && settings.continuationIndentInArgumentLists -> Indent.getContinuationIndent()
else -> Indent.getNormalIndent()
val hasLineBreaksAfter = currentPosition.hasLineBreaksAfter(offset)
fun createIndent(
isParameterList: Boolean,
baseLineOffset: Int,
): IndentCalculator {
val indent = if (before.isAt(Comma) || hasLineBreaksAfter) {
val firstElement = leftParenthesis.afterIgnoringWhiteSpaceOrComment()
when {
!firstElement.isAt(RightParenthesis) && (
isParameterList && settings.alignMultilineParameters ||
!isParameterList && settings.alignMultilineParametersInCalls
) -> {
return createIndentCalculator(createAlignMultilineIndent(firstElement), firstElement.startOffset)
}
} else {
if (settings.alignWhenMultilineFunctionParentheses)
createAlignMultilineIndent(leftParenthesis)
else
Indent.getNoneIndent()
isParameterList && settings.continuationIndentInParameterLists ||
!isParameterList && settings.continuationIndentInArgumentLists -> Indent.getContinuationIndent()
else -> Indent.getNormalIndent()
}
return createIndentCalculator(indent, baseLineOffset)
}
findFunctionKeywordBeforeIdentifier(leftParenthesis.beforeIgnoringWhiteSpaceOrComment())?.let {
return createIndent(isParameterList = true, it.startOffset)
}
// NB: this covered [KtTokens.CONSTRUCTOR_KEYWORD], [KtTokens.SET_KEYWORD], [KtTokens.GET_KEYWORD], [KtTokens.INIT_KEYWORD] as well
if (isSimilarToFunctionInvocation(leftParenthesis)) {
return createIndent(isParameterList = false, leftParenthesis.startOffset)
}
if (isDestructuringDeclaration(leftParenthesis, rightParenthesis)) {
return createIndentCalculator(
if (before.isAt(Comma)) Indent.getNormalIndent() else Indent.getNoneIndent(),
leftParenthesis.startOffset
)
}
leftParenthesis.beforeIgnoringWhiteSpaceOrComment().let { keyword ->
val indent = when {
keyword.isAt(IfKeyword) && !before.isAt(LeftParenthesis) ->
if (settings.continuationIndentInIfCondition) Indent.getContinuationIndent() else Indent.getNormalIndent()
keyword.isControlFlowKeyword() -> Indent.getNoneIndent()
else -> null
}
indent?.let { return createIndentCalculator(it, keyword.startOffset) }
}
return if (settings.alignWhenMultilineBinaryExpression) {
val anchor = if (before.isAt(LeftParenthesis)) leftParenthesis else leftParenthesis.afterIgnoringWhiteSpaceOrComment()
createIndentCalculator(createAlignMultilineIndent(anchor), anchor.startOffset)
} else {
createIndentCalculator(Indent.getContinuationIndent(), leftParenthesis.startOffset)
if (settings.alignWhenMultilineFunctionParentheses)
createAlignMultilineIndent(leftParenthesis)
else
Indent.getNoneIndent()
}
return createIndentCalculator(indent, baseLineOffset)
}
return null
findFunctionKeywordBeforeIdentifier(leftParenthesis.beforeIgnoringWhiteSpaceOrComment())?.let {
return createIndent(isParameterList = true, it.startOffset)
}
// NB: this covered [KtTokens.CONSTRUCTOR_KEYWORD], [KtTokens.SET_KEYWORD], [KtTokens.GET_KEYWORD], [KtTokens.INIT_KEYWORD] as well
if (isSimilarToFunctionInvocation(leftParenthesis)) {
return createIndent(isParameterList = false, leftParenthesis.startOffset)
}
if (isDestructuringDeclaration(leftParenthesis, rightParenthesis)) {
return createIndentCalculator(
if (before.isAt(Comma) || hasLineBreaksAfter) Indent.getNormalIndent() else Indent.getNoneIndent(),
leftParenthesis.startOffset
)
}
leftParenthesis.beforeIgnoringWhiteSpaceOrComment().let { keyword ->
val indent = when {
keyword.isAt(IfKeyword) && !before.isAt(LeftParenthesis) ->
if (settings.continuationIndentInIfCondition) Indent.getContinuationIndent() else Indent.getNormalIndent()
keyword.isControlFlowKeyword() -> if (hasLineBreaksAfter) Indent.getNormalIndent() else Indent.getNoneIndent()
else -> null
}
indent?.let { return createIndentCalculator(it, keyword.startOffset) }
}
return if (settings.alignWhenMultilineBinaryExpression && !hasLineBreaksAfter) {
val anchor = if (before.isAt(LeftParenthesis)) leftParenthesis else leftParenthesis.afterIgnoringWhiteSpaceOrComment()
createIndentCalculator(createAlignMultilineIndent(anchor), anchor.startOffset)
} else {
createIndentCalculator(Indent.getContinuationIndent(), leftParenthesis.startOffset)
}
}
/**