Add "Blank lines before }" option

#KT-15504 Fixed
This commit is contained in:
Dmitry Jemerov
2017-06-22 16:50:00 +02:00
parent 5c8a90fe85
commit 722cc521a9
8 changed files with 111 additions and 5 deletions
@@ -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<Builder>()
@@ -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)
@@ -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,
+17
View File
@@ -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
+25
View File
@@ -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
@@ -0,0 +1,20 @@
fun foo() {
println("a")
if (1 > 2) {
println("foo")
}
}
class X {
fun foo() = 1
}
+23
View File
@@ -0,0 +1,23 @@
fun foo() {
println("a")
if (1 > 2) {
println("foo")
}
}
class X {
fun foo() = 1
}
@@ -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");