[kotlin][editor] indent provider: support indent after '->'

^KTIJ-24220 Fixed
^FL-12651

GitOrigin-RevId: a7391fb029df9da3783e1aafe4db40c4ae2527c9
This commit is contained in:
Dmitrii Gridin
2023-01-10 15:47:00 +00:00
committed by intellij-monorepo-bot
parent 621c1dada4
commit 85cd168dc0
11 changed files with 78 additions and 10 deletions
@@ -1819,6 +1819,11 @@ public abstract class EnterHandlerTestGenerated extends AbstractEnterHandlerTest
runTest("testData/editor/enterHandler/AutoIndentInWhenClause.after.kt");
}
@TestMetadata("AutoIndentInWhenClauseWithoutCondition.after.kt")
public void testAutoIndentInWhenClauseWithoutCondition() throws Exception {
runTest("testData/editor/enterHandler/AutoIndentInWhenClauseWithoutCondition.after.kt");
}
@TestMetadata("BeforePropertyGetter.after.kt")
public void testBeforePropertyGetter() throws Exception {
runTest("testData/editor/enterHandler/BeforePropertyGetter.after.kt");
@@ -1989,6 +1994,11 @@ public abstract class EnterHandlerTestGenerated extends AbstractEnterHandlerTest
runTest("testData/editor/enterHandler/InMultilineLambdaAfterArrow.after.kt");
}
@TestMetadata("InMultilineLambdaAfterArrow2.after.kt")
public void testInMultilineLambdaAfterArrow2() throws Exception {
runTest("testData/editor/enterHandler/InMultilineLambdaAfterArrow2.after.kt");
}
@TestMetadata("IndentBeforeElseWithBlock.after.kt")
public void testIndentBeforeElseWithBlock() throws Exception {
runTest("testData/editor/enterHandler/IndentBeforeElseWithBlock.after.kt");
@@ -1,8 +1,6 @@
// WITHOUT_CUSTOM_LINE_INDENT_PROVIDER
fun test() {
when (2) {
is Int ->
<caret>
}
}
}
@@ -1,7 +1,5 @@
// WITHOUT_CUSTOM_LINE_INDENT_PROVIDER
fun test() {
when (2) {
is Int -><caret>
}
}
}
@@ -0,0 +1,6 @@
fun test() {
when {
is Int ->
<caret>
}
}
@@ -0,0 +1,5 @@
fun test() {
when {
is Int -><caret>
}
}
@@ -2,5 +2,3 @@ val a: (String) -> String = {
some ->
<caret>
}
// WITHOUT_CUSTOM_LINE_INDENT_PROVIDER
@@ -1,5 +1,3 @@
val a: (String) -> String = {
some -><caret>
}
// WITHOUT_CUSTOM_LINE_INDENT_PROVIDER
@@ -0,0 +1,11 @@
fun foo(a: String, b: String, l: (String) -> Unit) {}
fun testIndent() {
foo(a = "a",
b = "b") { aaa ->
<caret>
println()
}
}
// SET_INT: INDENT_SIZE=2
@@ -0,0 +1,10 @@
fun foo(a: String, b: String, l: (String) -> Unit) {}
fun testIndent() {
foo(a = "a",
b = "b") { aaa -><caret>
println()
}
}
// SET_INT: INDENT_SIZE=2
@@ -50,6 +50,10 @@ abstract class KotlinLangLineIndentProvider : JavaLikeLangLineIndentProvider() {
before.isAt(BlockOpeningBrace) ->
return factory.createIndentCalculatorForBrace(before, after, BlockOpeningBrace, BlockClosingBrace, Indent.getNormalIndent())
before.isAt(Arrow) -> {
return factory.createIndentCalculatorForArrow(before, after)
}
after.isAt(ArrayClosingBracket) && !currentPosition.hasLineBreaksAfter(offset) ->
return factory.createIndentCalculatorForBrace(
before,
@@ -247,6 +251,26 @@ abstract class KotlinLangLineIndentProvider : JavaLikeLangLineIndentProvider() {
ArrayOpeningBracket to ArrayClosingBracket,
)
private fun IndentCalculatorFactory.createIndentCalculatorForArrow(
arrowPosition: SemanticEditorPosition,
after: SemanticEditorPosition,
): IndentCalculator? {
val leftBrace = arrowPosition.copyAnd {
it.moveToLeftParenthesisBackwardsSkippingNested(BlockOpeningBrace, BlockClosingBrace)
}
if (leftBrace.isAtEnd) {
return null
}
val normalIndent = Indent.getNormalIndent()
return if (leftBrace.controlFlowStatementBefore() != null) {
createIndentCalculator(normalIndent, arrowPosition.startOffset)
} else {
createIndentCalculatorForBrace(leftBrace, after, BlockOpeningBrace, BlockClosingBrace, normalIndent)
}
}
private fun IndentCalculatorFactory.createIndentCalculatorForBrace(
before: SemanticEditorPosition,
after: SemanticEditorPosition,
@@ -1819,6 +1819,11 @@ public abstract class PerformanceTypingIndentationTestGenerated extends Abstract
runTest("../idea/tests/testData/editor/enterHandler/AutoIndentInWhenClause.after.kt");
}
@TestMetadata("AutoIndentInWhenClauseWithoutCondition.after.kt")
public void testAutoIndentInWhenClauseWithoutCondition() throws Exception {
runTest("../idea/tests/testData/editor/enterHandler/AutoIndentInWhenClauseWithoutCondition.after.kt");
}
@TestMetadata("BeforePropertyGetter.after.kt")
public void testBeforePropertyGetter() throws Exception {
runTest("../idea/tests/testData/editor/enterHandler/BeforePropertyGetter.after.kt");
@@ -1989,6 +1994,11 @@ public abstract class PerformanceTypingIndentationTestGenerated extends Abstract
runTest("../idea/tests/testData/editor/enterHandler/InMultilineLambdaAfterArrow.after.kt");
}
@TestMetadata("InMultilineLambdaAfterArrow2.after.kt")
public void testInMultilineLambdaAfterArrow2() throws Exception {
runTest("../idea/tests/testData/editor/enterHandler/InMultilineLambdaAfterArrow2.after.kt");
}
@TestMetadata("IndentBeforeElseWithBlock.after.kt")
public void testIndentBeforeElseWithBlock() throws Exception {
runTest("../idea/tests/testData/editor/enterHandler/IndentBeforeElseWithBlock.after.kt");