Add option for wrapping closing paren in multiline if conditions

This commit is contained in:
Dmitry Jemerov
2017-12-21 14:22:20 +01:00
parent 7711f8e3a7
commit 6208c69c72
7 changed files with 43 additions and 0 deletions
@@ -49,6 +49,7 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
public int BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES = 0;
public int WRAP_EXPRESSION_BODY_FUNCTIONS = 0;
public int WRAP_ELVIS_EXPRESSIONS = 1;
public boolean IF_RPAREN_ON_NEW_LINE = false;
public KotlinCodeStyleSettings(CodeStyleSettings container) {
super("JetCodeStyleSettings", container);
@@ -30,6 +30,7 @@ class KotlinStyleGuideCodeStyle : PredefinedCodeStyle("Kotlin style guide", Kotl
CONTINUATION_INDENT_IN_SUPERTYPE_LISTS = false
CONTINUATION_INDENT_IN_IF_CONDITIONS = false
WRAP_EXPRESSION_BODY_FUNCTIONS = CodeStyleSettings.WRAP_AS_NEEDED
IF_RPAREN_ON_NEW_LINE = true
}
settings.kotlinCommonSettings.apply {
@@ -185,6 +185,18 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
createSpacing(0)
}
}
inPosition(left = CONDITION, right = RPAR).customRule { parent, _, _ ->
if (kotlinCustomSettings.IF_RPAREN_ON_NEW_LINE) {
Spacing.createDependentLFSpacing(0, 0,
excludeLambdasAndObjects(parent),
commonCodeStyleSettings.KEEP_LINE_BREAKS,
commonCodeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
}
else {
createSpacing(0)
}
}
}
simple {
@@ -65,6 +65,10 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
foo.toUpperCase().trim()
.length
val barLen = bar?.length() ?: x ?: -1
if (foo.length > 0 &&
barLen > 0) {
println("> 0")
}
}
}
+10
View File
@@ -0,0 +1,10 @@
fun x() {
if (foo.length > 0 &&
barLen > 0
) {
println("> 0")
}
}
// SET_TRUE: IF_RPAREN_ON_NEW_LINE
// SET_FALSE: CONTINUATION_INDENT_IN_IF_CONDITIONS
+9
View File
@@ -0,0 +1,9 @@
fun x() {
if (foo.length > 0 &&
barLen > 0) {
println("> 0")
}
}
// SET_TRUE: IF_RPAREN_ON_NEW_LINE
// SET_FALSE: CONTINUATION_INDENT_IN_IF_CONDITIONS
@@ -494,6 +494,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
doTest(fileName);
}
@TestMetadata("IfRParen.after.kt")
public void testIfRParen() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/IfRParen.after.kt");
doTest(fileName);
}
@TestMetadata("IfSpacing.after.kt")
public void testIfSpacing() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/IfSpacing.after.kt");