From 722cc521a97dc26ecd6d3c6274f0c383a201461c Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 22 Jun 2017 16:50:00 +0200 Subject: [PATCH] Add "Blank lines before }" option #KT-15504 Fixed --- .../idea/formatter/KotlinSpacingBuilder.kt | 6 +++++ .../idea/formatter/kotlinSpacingRules.kt | 12 +++++---- ...KotlinLanguageCodeStyleSettingsProvider.kt | 1 + .../formatter/BlankLinesBeforeRBrace.after.kt | 17 +++++++++++++ .../formatter/BlankLinesBeforeRBrace.kt | 25 +++++++++++++++++++ .../BlankLinesBeforeRBrace2.after.kt | 20 +++++++++++++++ .../formatter/BlankLinesBeforeRBrace2.kt | 23 +++++++++++++++++ .../formatter/FormatterTestGenerated.java | 12 +++++++++ 8 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 idea/testData/formatter/BlankLinesBeforeRBrace.after.kt create mode 100644 idea/testData/formatter/BlankLinesBeforeRBrace.kt create mode 100644 idea/testData/formatter/BlankLinesBeforeRBrace2.after.kt create mode 100644 idea/testData/formatter/BlankLinesBeforeRBrace2.kt diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinSpacingBuilder.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinSpacingBuilder.kt index 22dd0ad00d0..d844aaa48dd 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinSpacingBuilder.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinSpacingBuilder.kt @@ -29,6 +29,12 @@ import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.lexer.KtTokens import java.util.* +fun CommonCodeStyleSettings.createSpaceBeforeRBrace(numSpacesOtherwise: Int, textRange: TextRange): Spacing? { + return Spacing.createDependentLFSpacing(numSpacesOtherwise, numSpacesOtherwise, textRange, + KEEP_LINE_BREAKS, + KEEP_BLANK_LINES_BEFORE_RBRACE) +} + class KotlinSpacingBuilder(val commonCodeStyleSettings: CommonCodeStyleSettings, val spacingBuilderUtil: KotlinSpacingBuilderUtil) { private val builders = ArrayList() diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt index ae6d5986ac0..7722f7513e9 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt @@ -439,7 +439,9 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing } } - inPosition(parent = CLASS_BODY, right = RBRACE).lineBreakIfLineBreakInParent(numSpacesOtherwise = 1) + inPosition(parent = CLASS_BODY, right = RBRACE).customRule { parent, _, _ -> + commonCodeStyleSettings.createSpaceBeforeRBrace(1, parent.textRange) + } inPosition(parent = BLOCK, right = RBRACE).customRule { block, left, _ -> val psiElement = block.node.treeParent.psi @@ -457,9 +459,7 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing } val spaces = if (empty) 0 else spacesInSimpleFunction - Spacing.createDependentLFSpacing(spaces, spaces, psiElement.textRangeWithoutComments, - commonCodeStyleSettings.KEEP_LINE_BREAKS, - commonCodeStyleSettings.KEEP_BLANK_LINES_IN_CODE) + commonCodeStyleSettings.createSpaceBeforeRBrace(spaces, psiElement.textRangeWithoutComments) } inPosition(parent = BLOCK, left = LBRACE).customRule { parent, _, _ -> @@ -491,7 +491,9 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing simple { afterInside(LBRACE, BLOCK).lineBreakInCode() - beforeInside(RBRACE, BLOCK).lineBreakInCode() + beforeInside(RBRACE, BLOCK).spacing(1, 0, 1, + commonCodeStyleSettings.KEEP_LINE_BREAKS, + commonCodeStyleSettings.KEEP_BLANK_LINES_BEFORE_RBRACE) beforeInside(RBRACE, WHEN).lineBreakInCode() between(RPAR, BODY).spaces(1) diff --git a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt index e2be7d80325..2191725f109 100644 --- a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt @@ -253,6 +253,7 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide consumer.showStandardOptions( "KEEP_BLANK_LINES_IN_CODE", "KEEP_BLANK_LINES_IN_DECLARATIONS", + "KEEP_BLANK_LINES_BEFORE_RBRACE", "BLANK_LINES_AFTER_CLASS_HEADER" ) showCustomOption(KotlinCodeStyleSettings::BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES, diff --git a/idea/testData/formatter/BlankLinesBeforeRBrace.after.kt b/idea/testData/formatter/BlankLinesBeforeRBrace.after.kt new file mode 100644 index 00000000000..2ebd5cce5aa --- /dev/null +++ b/idea/testData/formatter/BlankLinesBeforeRBrace.after.kt @@ -0,0 +1,17 @@ +fun foo() { + println("a") + + + if (1 > 2) { + + println("foo") + } +} + + +class X { + fun foo() = 1 +} + + +// SET_INT: KEEP_BLANK_LINES_BEFORE_RBRACE = 0 diff --git a/idea/testData/formatter/BlankLinesBeforeRBrace.kt b/idea/testData/formatter/BlankLinesBeforeRBrace.kt new file mode 100644 index 00000000000..4aad276c40f --- /dev/null +++ b/idea/testData/formatter/BlankLinesBeforeRBrace.kt @@ -0,0 +1,25 @@ +fun foo() { + println("a") + + + if (1 > 2) { + + println("foo") + + + } + +} + + +class X { + fun foo() = 1 + + + +} + + + + +// SET_INT: KEEP_BLANK_LINES_BEFORE_RBRACE = 0 diff --git a/idea/testData/formatter/BlankLinesBeforeRBrace2.after.kt b/idea/testData/formatter/BlankLinesBeforeRBrace2.after.kt new file mode 100644 index 00000000000..5d5551ed35b --- /dev/null +++ b/idea/testData/formatter/BlankLinesBeforeRBrace2.after.kt @@ -0,0 +1,20 @@ +fun foo() { + println("a") + + + if (1 > 2) { + + println("foo") + + + } + + +} + + +class X { + fun foo() = 1 + + +} diff --git a/idea/testData/formatter/BlankLinesBeforeRBrace2.kt b/idea/testData/formatter/BlankLinesBeforeRBrace2.kt new file mode 100644 index 00000000000..46addfbaddc --- /dev/null +++ b/idea/testData/formatter/BlankLinesBeforeRBrace2.kt @@ -0,0 +1,23 @@ +fun foo() { + println("a") + + + if (1 > 2) { + + println("foo") + + + + } + + + +} + + +class X { + fun foo() = 1 + + + +} diff --git a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java index 399939b8f04..7fdb3c0efdd 100644 --- a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java @@ -98,6 +98,18 @@ public class FormatterTestGenerated extends AbstractFormatterTest { doTest(fileName); } + @TestMetadata("BlankLinesBeforeRBrace.after.kt") + public void testBlankLinesBeforeRBrace() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/BlankLinesBeforeRBrace.after.kt"); + doTest(fileName); + } + + @TestMetadata("BlankLinesBeforeRBrace2.after.kt") + public void testBlankLinesBeforeRBrace2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/BlankLinesBeforeRBrace2.after.kt"); + doTest(fileName); + } + @TestMetadata("BlockFor.after.kt") public void testBlockFor() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/BlockFor.after.kt");