From b9ee59b57b4d5a7dac66aeef0f5aceeae7c34579 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Fri, 17 Jan 2014 14:01:36 +0400 Subject: [PATCH] Formatter: left brace formatting in class/trait/class object/object/object literal declarations GitOrigin-RevId: a164428803f4441d876ee58c708f372977080e9c --- .../plugin/formatter/KotlinSpacingBuilder.kt | 20 +++++----- .../formatter/JetFormatterTestGenerated.java | 10 +++++ .../formatter/ClassLineBreak.after.inv.kt | 30 ++++++++++++++ .../formatter/ClassLineBreak.after.kt | 40 +++++++++++++++++++ .../testData/formatter/ClassLineBreak.kt | 35 ++++++++++++++++ .../formatter/FunctionLineBreak.after.kt | 3 +- .../AddConstructorVisibilityAfter.kt | 2 +- .../AddConstructorVisibilityBefore.kt | 2 +- .../ConstructorSwapArgumentsAfter.kt | 2 +- .../ConstructorSwapArgumentsBefore.kt | 2 +- 10 files changed, 131 insertions(+), 15 deletions(-) create mode 100644 plugins/kotlin/idea/tests/testData/formatter/ClassLineBreak.after.inv.kt create mode 100644 plugins/kotlin/idea/tests/testData/formatter/ClassLineBreak.after.kt create mode 100644 plugins/kotlin/idea/tests/testData/formatter/ClassLineBreak.kt diff --git a/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/formatter/KotlinSpacingBuilder.kt b/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/formatter/KotlinSpacingBuilder.kt index 1624a8b62136..794d67344ccd 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/formatter/KotlinSpacingBuilder.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/jet/plugin/formatter/KotlinSpacingBuilder.kt @@ -195,9 +195,8 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder { } custom { - fun spacingForLeftBrace(block: ASTNode?): Spacing? { - val noBlockSpacing = Spacing.createSpacing(1, 1, 0, settings.KEEP_LINE_BREAKS, settings.KEEP_BLANK_LINES_IN_CODE) - if (block != null && block.getElementType() == BLOCK) { + fun spacingForLeftBrace(block: ASTNode?, blockType: IElementType = BLOCK): Spacing? { + if (block != null && block.getElementType() == blockType) { val leftBrace = block.getFirstChildNode() if (leftBrace != null && leftBrace.getElementType() == LBRACE) { val previousLeaf = FormatterUtil.getPreviousNonWhitespaceLeaf(leftBrace) @@ -207,12 +206,12 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder { return Spacing.createSpacing(1, 1, minimumLF, keepLineBreaks, 0) } } - return noBlockSpacing + return Spacing.createSpacing(1, 1, 0, settings.KEEP_LINE_BREAKS, settings.KEEP_BLANK_LINES_IN_CODE) } - val leftBraceRule = { + fun leftBraceRule(blockType: IElementType = BLOCK) = { (parent: ASTBlock, left: ASTBlock, right: ASTBlock) -> - spacingForLeftBrace(right.getNode()) + spacingForLeftBrace(right.getNode(), blockType) } val leftBraceRuleIfBlockIsWrapped = { @@ -227,12 +226,13 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder { inPosition(parent = WHILE, right = BODY).customRule(leftBraceRuleIfBlockIsWrapped) inPosition(parent = DO_WHILE, right = BODY).customRule(leftBraceRuleIfBlockIsWrapped) - inPosition(parent = TRY, right = BLOCK).customRule(leftBraceRule) - inPosition(parent = CATCH, right = BLOCK).customRule(leftBraceRule) - inPosition(parent = FINALLY, right = BLOCK).customRule(leftBraceRule) + inPosition(parent = TRY, right = BLOCK).customRule(leftBraceRule()) + inPosition(parent = CATCH, right = BLOCK).customRule(leftBraceRule()) + inPosition(parent = FINALLY, right = BLOCK).customRule(leftBraceRule()) - inPosition(parent = FUN, right = BLOCK).customRule(leftBraceRule) + inPosition(parent = FUN, right = BLOCK).customRule(leftBraceRule()) + inPosition(right = CLASS_BODY).customRule(leftBraceRule(blockType = CLASS_BODY)) val spacesInSimpleFunction = if (jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD) 1 else 0 inPosition(parent = FUNCTION_LITERAL, diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java index dad1b7b687e0..ea3f3e5a703c 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/jet/formatter/JetFormatterTestGenerated.java @@ -49,6 +49,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest { doTest("idea/testData/formatter/Class.after.kt"); } + @TestMetadata("ClassLineBreak.after.kt") + public void testClassLineBreak() throws Exception { + doTest("idea/testData/formatter/ClassLineBreak.after.kt"); + } + @TestMetadata("CommentInFunctionLiteral.after.kt") public void testCommentInFunctionLiteral() throws Exception { doTest("idea/testData/formatter/CommentInFunctionLiteral.after.kt"); @@ -362,6 +367,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/formatter"), Pattern.compile("^([^\\.]+)\\.after.inv.kt$"), true); } + @TestMetadata("ClassLineBreak.after.inv.kt") + public void testClassLineBreak() throws Exception { + doTestInverted("idea/testData/formatter/ClassLineBreak.after.inv.kt"); + } + @TestMetadata("DoWhileLineBreak.after.inv.kt") public void testDoWhileLineBreak() throws Exception { doTestInverted("idea/testData/formatter/DoWhileLineBreak.after.inv.kt"); diff --git a/plugins/kotlin/idea/tests/testData/formatter/ClassLineBreak.after.inv.kt b/plugins/kotlin/idea/tests/testData/formatter/ClassLineBreak.after.inv.kt new file mode 100644 index 000000000000..8587ae3be2f4 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/formatter/ClassLineBreak.after.inv.kt @@ -0,0 +1,30 @@ +class A { + class object { + } +} + +class A { + class object { + } +} + +trait A { +} + +trait A { +} + +object A { +} + +object A { +} + +val a = object { +} + +val a = object { +} + +// SET_TRUE: LBRACE_ON_NEXT_LINE + diff --git a/plugins/kotlin/idea/tests/testData/formatter/ClassLineBreak.after.kt b/plugins/kotlin/idea/tests/testData/formatter/ClassLineBreak.after.kt new file mode 100644 index 000000000000..c743930a6367 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/formatter/ClassLineBreak.after.kt @@ -0,0 +1,40 @@ +class A +{ + class object + { + } +} + +class A +{ + class object + { + } +} + +trait A +{ +} + +trait A +{ +} + +object A +{ +} + +object A +{ +} + +val a = object +{ +} + +val a = object +{ +} + +// SET_TRUE: LBRACE_ON_NEXT_LINE + diff --git a/plugins/kotlin/idea/tests/testData/formatter/ClassLineBreak.kt b/plugins/kotlin/idea/tests/testData/formatter/ClassLineBreak.kt new file mode 100644 index 000000000000..9ca8d1ce8d39 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/formatter/ClassLineBreak.kt @@ -0,0 +1,35 @@ +class A { + class object { + } +} + +class A +{ + class object + { + } +} + +trait A { +} + +trait A +{ +} + +object A { +} + +object A +{ +} + +val a = object { +} + +val a = object +{ +} + +// SET_TRUE: LBRACE_ON_NEXT_LINE + diff --git a/plugins/kotlin/idea/tests/testData/formatter/FunctionLineBreak.after.kt b/plugins/kotlin/idea/tests/testData/formatter/FunctionLineBreak.after.kt index 9559593186ba..080e403e4942 100644 --- a/plugins/kotlin/idea/tests/testData/formatter/FunctionLineBreak.after.kt +++ b/plugins/kotlin/idea/tests/testData/formatter/FunctionLineBreak.after.kt @@ -18,7 +18,8 @@ fun f() //eol comment { } -class A { +class A +{ fun f() { } diff --git a/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/AddConstructorVisibilityAfter.kt b/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/AddConstructorVisibilityAfter.kt index c189c6339051..34a111ffaa29 100644 --- a/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/AddConstructorVisibilityAfter.kt +++ b/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/AddConstructorVisibilityAfter.kt @@ -1,4 +1,4 @@ -class C1 protected (val x: Any){} +class C1 protected (val x: Any) {} fun f() { val c = C1(12); diff --git a/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/AddConstructorVisibilityBefore.kt b/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/AddConstructorVisibilityBefore.kt index 0c95fb130360..0c8204605edb 100644 --- a/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/AddConstructorVisibilityBefore.kt +++ b/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/AddConstructorVisibilityBefore.kt @@ -1,4 +1,4 @@ -class C1{} +class C1 {} fun f() { val c = C1(); diff --git a/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/ConstructorSwapArgumentsAfter.kt b/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/ConstructorSwapArgumentsAfter.kt index 3dd24059fe7f..b40132f6d0fd 100644 --- a/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/ConstructorSwapArgumentsAfter.kt +++ b/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/ConstructorSwapArgumentsAfter.kt @@ -1,6 +1,6 @@ open class C1 protected (x3: ((Int) -> Int)?, var _x2: Float, - val _x1: Int = 1){ + val _x1: Int = 1) { fun bar() { val y1 = _x1; val y2 = _x2; diff --git a/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/ConstructorSwapArgumentsBefore.kt b/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/ConstructorSwapArgumentsBefore.kt index f18ff2adeb73..e66856e92e8d 100644 --- a/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/ConstructorSwapArgumentsBefore.kt +++ b/plugins/kotlin/idea/tests/testData/refactoring/changeSignature/ConstructorSwapArgumentsBefore.kt @@ -1,4 +1,4 @@ -open class C1 protected (val x1: Int = 1, var x2: Float, x3: ((Int) -> Int)?){ +open class C1 protected (val x1: Int = 1, var x2: Float, x3: ((Int) -> Int)?) { fun bar() { val y1 = x1; val y2 = x2;