[kotlin] fix formatting for context receivers

^KTIJ-21346 fixed

GitOrigin-RevId: 56a761537dfb37a6f739a17f76abc2f5b9126271
This commit is contained in:
Ilya Kirillov
2022-06-20 13:35:07 +00:00
committed by intellij-monorepo-bot
parent 2ce894e6eb
commit fb26110f31
20 changed files with 163 additions and 1 deletions
@@ -1007,7 +1007,7 @@ private val INDENT_RULES = arrayOf(
strategy("Indent for parts")
.within(PROPERTY, FUN, DESTRUCTURING_DECLARATION, SECONDARY_CONSTRUCTOR)
.notForType(BLOCK, FUN_KEYWORD, VAL_KEYWORD, VAR_KEYWORD, CONSTRUCTOR_KEYWORD, RPAR, EOL_COMMENT)
.notForType(BLOCK, FUN_KEYWORD, VAL_KEYWORD, VAR_KEYWORD, CONSTRUCTOR_KEYWORD, RPAR, EOL_COMMENT, CONTEXT_RECEIVER_LIST, MODIFIER_LIST)
.set(Indent.getContinuationWithoutFirstIndent()),
strategy("Chained calls")
@@ -165,6 +165,54 @@ public abstract class EnterHandlerTestGenerated extends AbstractEnterHandlerTest
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("testData/editor/enterHandler/contextReceivers")
public static class ContextReceivers extends AbstractEnterHandlerTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doNewlineTest, this, testDataFilePath);
}
@TestMetadata("memberFunctionWithContextReceiver.after.kt")
public void testMemberFunctionWithContextReceiver() throws Exception {
runTest("testData/editor/enterHandler/contextReceivers/memberFunctionWithContextReceiver.after.kt");
}
@TestMetadata("memberFunctionWithContextReceiverNoModifiers.after.kt")
public void testMemberFunctionWithContextReceiverNoModifiers() throws Exception {
runTest("testData/editor/enterHandler/contextReceivers/memberFunctionWithContextReceiverNoModifiers.after.kt");
}
@TestMetadata("memberPropertyWithContextReceiver.after.kt")
public void testMemberPropertyWithContextReceiver() throws Exception {
runTest("testData/editor/enterHandler/contextReceivers/memberPropertyWithContextReceiver.after.kt");
}
@TestMetadata("memberPropertyWithContextReceiverNoModifiers.after.kt")
public void testMemberPropertyWithContextReceiverNoModifiers() throws Exception {
runTest("testData/editor/enterHandler/contextReceivers/memberPropertyWithContextReceiverNoModifiers.after.kt");
}
@TestMetadata("topLevelFunctionWithContextReceiver.after.kt")
public void testTopLevelFunctionWithContextReceiver() throws Exception {
runTest("testData/editor/enterHandler/contextReceivers/topLevelFunctionWithContextReceiver.after.kt");
}
@TestMetadata("topLevelFunctionWithContextReceiverNoModifiers.after.kt")
public void testTopLevelFunctionWithContextReceiverNoModifiers() throws Exception {
runTest("testData/editor/enterHandler/contextReceivers/topLevelFunctionWithContextReceiverNoModifiers.after.kt");
}
@TestMetadata("topLevelPropertyWithContextReceiver.after.kt")
public void testTopLevelPropertyWithContextReceiver() throws Exception {
runTest("testData/editor/enterHandler/contextReceivers/topLevelPropertyWithContextReceiver.after.kt");
}
@TestMetadata("topLevelPropertyWithContextReceiverNoModifiers.after.kt")
public void testTopLevelPropertyWithContextReceiverNoModifiers() throws Exception {
runTest("testData/editor/enterHandler/contextReceivers/topLevelPropertyWithContextReceiverNoModifiers.after.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("testData/editor/enterHandler/controlFlowConstructions")
public static class ControlFlowConstructions extends AbstractEnterHandlerTest {
@@ -0,0 +1,4 @@
class C {
context(A)
<caret>private fun x() = Unit
}
@@ -0,0 +1,3 @@
class C {
context(A)<caret> private fun x() = Unit
}
@@ -0,0 +1,4 @@
class C {
context(A)
<caret>fun x() = Unit
}
@@ -0,0 +1,3 @@
class C {
context(A)<caret>fun x() = Unit
}
@@ -0,0 +1,4 @@
class C {
context(A)
<caret>private val x = 1
}
@@ -0,0 +1,3 @@
class C {
context(A)<caret> private val x = 1
}
@@ -0,0 +1,3 @@
class C {
context(A)<caret> val x = 1
}
@@ -0,0 +1,2 @@
context(A)
<caret>private fun x() = Unit
@@ -0,0 +1 @@
context(A) <caret>private fun x() = Unit
@@ -0,0 +1,2 @@
context(A)
<caret>private val x = 1
@@ -0,0 +1 @@
context(A) <caret>private val x = 1
@@ -118,6 +118,10 @@ abstract class KotlinLangLineIndentProvider : JavaLikeLangLineIndentProvider() {
return factory.createIndentCalculator(indent, it.startOffset)
}
findContextReceiverListBefore(before)?.let {
return factory.createIndentCalculator(Indent.getNoneIndent(), it.startOffset)
}
return before.controlFlowStatementBefore()?.let { controlFlowKeywordPosition ->
val indent = when {
controlFlowKeywordPosition.similarToCatchKeyword() -> if (before.isAt(RightParenthesis)) Indent.getNoneIndent() else Indent.getNormalIndent()
@@ -326,6 +330,28 @@ abstract class KotlinLangLineIndentProvider : JavaLikeLangLineIndentProvider() {
}
}
/**
* ```
* context(A) <caret> fun x()
* ```
*/
private fun findContextReceiverListBefore(before: SemanticEditorPosition): SemanticEditorPosition? {
if (!before.isAt(RightParenthesis)) return null
val probableContextReceiverKeyword = before.copy().apply {
moveBeforeParentheses(LeftParenthesis, RightParenthesis)
}
if (probableContextReceiverKeyword.isAt(Identifier)
&& probableContextReceiverKeyword.after().isAt(LeftParenthesis)
&& probableContextReceiverKeyword.textOfCurrentPosition() == KtTokens.CONTEXT_KEYWORD.value
) {
return probableContextReceiverKeyword
}
return null
}
/**
* @param endOfDeclaration is position before '=' for expression body or '{' for block body
*/
@@ -165,6 +165,54 @@ public abstract class PerformanceTypingIndentationTestGenerated extends Abstract
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../idea/tests/testData/editor/enterHandler/contextReceivers")
public static class ContextReceivers extends AbstractPerformanceTypingIndentationTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doNewlineTest, this, testDataFilePath);
}
@TestMetadata("memberFunctionWithContextReceiver.after.kt")
public void testMemberFunctionWithContextReceiver() throws Exception {
runTest("../idea/tests/testData/editor/enterHandler/contextReceivers/memberFunctionWithContextReceiver.after.kt");
}
@TestMetadata("memberFunctionWithContextReceiverNoModifiers.after.kt")
public void testMemberFunctionWithContextReceiverNoModifiers() throws Exception {
runTest("../idea/tests/testData/editor/enterHandler/contextReceivers/memberFunctionWithContextReceiverNoModifiers.after.kt");
}
@TestMetadata("memberPropertyWithContextReceiver.after.kt")
public void testMemberPropertyWithContextReceiver() throws Exception {
runTest("../idea/tests/testData/editor/enterHandler/contextReceivers/memberPropertyWithContextReceiver.after.kt");
}
@TestMetadata("memberPropertyWithContextReceiverNoModifiers.after.kt")
public void testMemberPropertyWithContextReceiverNoModifiers() throws Exception {
runTest("../idea/tests/testData/editor/enterHandler/contextReceivers/memberPropertyWithContextReceiverNoModifiers.after.kt");
}
@TestMetadata("topLevelFunctionWithContextReceiver.after.kt")
public void testTopLevelFunctionWithContextReceiver() throws Exception {
runTest("../idea/tests/testData/editor/enterHandler/contextReceivers/topLevelFunctionWithContextReceiver.after.kt");
}
@TestMetadata("topLevelFunctionWithContextReceiverNoModifiers.after.kt")
public void testTopLevelFunctionWithContextReceiverNoModifiers() throws Exception {
runTest("../idea/tests/testData/editor/enterHandler/contextReceivers/topLevelFunctionWithContextReceiverNoModifiers.after.kt");
}
@TestMetadata("topLevelPropertyWithContextReceiver.after.kt")
public void testTopLevelPropertyWithContextReceiver() throws Exception {
runTest("../idea/tests/testData/editor/enterHandler/contextReceivers/topLevelPropertyWithContextReceiver.after.kt");
}
@TestMetadata("topLevelPropertyWithContextReceiverNoModifiers.after.kt")
public void testTopLevelPropertyWithContextReceiverNoModifiers() throws Exception {
runTest("../idea/tests/testData/editor/enterHandler/contextReceivers/topLevelPropertyWithContextReceiverNoModifiers.after.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../idea/tests/testData/editor/enterHandler/controlFlowConstructions")
public static class ControlFlowConstructions extends AbstractPerformanceTypingIndentationTest {