From 8921ced9dd62dc99cec4b7ea4c7003ec50a252d8 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Thu, 16 Jun 2016 14:36:55 +0300 Subject: [PATCH] Indenter: fix indentation on top level in scripts --- .../kotlin/idea/formatter/KotlinCommonBlock.kt | 5 +++++ .../script/ScriptAfterClosingBrace.after.kts | 6 ++++++ .../script/ScriptAfterClosingBrace.kts | 5 +++++ .../script/ScriptBetweenFunctionCalls.after.kts | 8 ++++++++ .../script/ScriptBetweenFunctionCalls.kts | 7 +++++++ .../TypingIndentationTestBaseGenerated.java | 12 ++++++++++++ 6 files changed, 43 insertions(+) create mode 100644 idea/testData/indentationOnNewline/script/ScriptAfterClosingBrace.after.kts create mode 100644 idea/testData/indentationOnNewline/script/ScriptAfterClosingBrace.kts create mode 100644 idea/testData/indentationOnNewline/script/ScriptBetweenFunctionCalls.after.kts create mode 100644 idea/testData/indentationOnNewline/script/ScriptBetweenFunctionCalls.kts 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 94cba885d95..e774b507731 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt @@ -151,6 +151,11 @@ abstract class KotlinCommonBlock( fun getChildAttributes(newChildIndex: Int): ChildAttributes { val type = node.elementType + + if (node.elementType == KtNodeTypes.BLOCK && node.treeParent.elementType == KtNodeTypes.SCRIPT) { + return ChildAttributes(Indent.getNoneIndent(), null) + } + return when (type) { in CODE_BLOCKS, KtNodeTypes.WHEN, KtNodeTypes.IF, KtNodeTypes.FOR, KtNodeTypes.WHILE, KtNodeTypes.DO_WHILE -> ChildAttributes(Indent.getNormalIndent(), null) diff --git a/idea/testData/indentationOnNewline/script/ScriptAfterClosingBrace.after.kts b/idea/testData/indentationOnNewline/script/ScriptAfterClosingBrace.after.kts new file mode 100644 index 00000000000..622c0d60c05 --- /dev/null +++ b/idea/testData/indentationOnNewline/script/ScriptAfterClosingBrace.after.kts @@ -0,0 +1,6 @@ +foo() { + +} + + +val c = 4 \ No newline at end of file diff --git a/idea/testData/indentationOnNewline/script/ScriptAfterClosingBrace.kts b/idea/testData/indentationOnNewline/script/ScriptAfterClosingBrace.kts new file mode 100644 index 00000000000..abeccd8e81d --- /dev/null +++ b/idea/testData/indentationOnNewline/script/ScriptAfterClosingBrace.kts @@ -0,0 +1,5 @@ +foo() { + +} + +val c = 4 \ No newline at end of file diff --git a/idea/testData/indentationOnNewline/script/ScriptBetweenFunctionCalls.after.kts b/idea/testData/indentationOnNewline/script/ScriptBetweenFunctionCalls.after.kts new file mode 100644 index 00000000000..c69b8d9032c --- /dev/null +++ b/idea/testData/indentationOnNewline/script/ScriptBetweenFunctionCalls.after.kts @@ -0,0 +1,8 @@ +fun foo() { + +} + +foo() + + +foo() \ No newline at end of file diff --git a/idea/testData/indentationOnNewline/script/ScriptBetweenFunctionCalls.kts b/idea/testData/indentationOnNewline/script/ScriptBetweenFunctionCalls.kts new file mode 100644 index 00000000000..14fdf916487 --- /dev/null +++ b/idea/testData/indentationOnNewline/script/ScriptBetweenFunctionCalls.kts @@ -0,0 +1,7 @@ +fun foo() { + +} + +foo() + +foo() \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/formatter/TypingIndentationTestBaseGenerated.java b/idea/tests/org/jetbrains/kotlin/formatter/TypingIndentationTestBaseGenerated.java index 79f1342c39d..6e82a52949c 100644 --- a/idea/tests/org/jetbrains/kotlin/formatter/TypingIndentationTestBaseGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/formatter/TypingIndentationTestBaseGenerated.java @@ -303,6 +303,12 @@ public class TypingIndentationTestBaseGenerated extends AbstractTypingIndentatio KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/indentationOnNewline/script"), Pattern.compile("^([^\\.]+)\\.after\\.kt.*$"), true); } + @TestMetadata("ScriptAfterClosingBrace.after.kts") + public void testScriptAfterClosingBrace() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/indentationOnNewline/script/ScriptAfterClosingBrace.after.kts"); + doNewlineTest(fileName); + } + @TestMetadata("ScriptAfterExpression.after.kts") public void testScriptAfterExpression() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/indentationOnNewline/script/ScriptAfterExpression.after.kts"); @@ -321,6 +327,12 @@ public class TypingIndentationTestBaseGenerated extends AbstractTypingIndentatio doNewlineTest(fileName); } + @TestMetadata("ScriptBetweenFunctionCalls.after.kts") + public void testScriptBetweenFunctionCalls() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/indentationOnNewline/script/ScriptBetweenFunctionCalls.after.kts"); + doNewlineTest(fileName); + } + @TestMetadata("ScriptInsideFun.after.kts") public void testScriptInsideFun() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/indentationOnNewline/script/ScriptInsideFun.after.kts");