Option to wrap elvis expressions
#KT-21720 Fixed
This commit is contained in:
+1
@@ -47,6 +47,7 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
|
|||||||
public boolean CONTINUATION_INDENT_IN_SUPERTYPE_LISTS = true;
|
public boolean CONTINUATION_INDENT_IN_SUPERTYPE_LISTS = true;
|
||||||
public int BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES = 0;
|
public int BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES = 0;
|
||||||
public int WRAP_EXPRESSION_BODY_FUNCTIONS = 0;
|
public int WRAP_EXPRESSION_BODY_FUNCTIONS = 0;
|
||||||
|
public int WRAP_ELVIS_EXPRESSIONS = 1;
|
||||||
|
|
||||||
public KotlinCodeStyleSettings(CodeStyleSettings container) {
|
public KotlinCodeStyleSettings(CodeStyleSettings container) {
|
||||||
super("JetCodeStyleSettings", container);
|
super("JetCodeStyleSettings", container);
|
||||||
|
|||||||
@@ -439,6 +439,16 @@ abstract class KotlinCommonBlock(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (nodePsi.operationToken == KtTokens.ELVIS) {
|
||||||
|
return object : WrappingStrategy {
|
||||||
|
override fun getWrap(childElement: ASTNode): Wrap? {
|
||||||
|
if (childElement.elementType == KtNodeTypes.OPERATION_REFERENCE) {
|
||||||
|
return Wrap.createWrap(settings.kotlinCustomSettings.WRAP_ELVIS_EXPRESSIONS, true)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return WrappingStrategy.NoWrapping
|
return WrappingStrategy.NoWrapping
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,9 @@ fun SpacingBuilder.afterInside(element: IElementType, tokenSet: TokenSet, spacin
|
|||||||
tokenSet.types.forEach { inType -> afterInside(element, inType).spacingFun() }
|
tokenSet.types.forEach { inType -> afterInside(element, inType).spacingFun() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun SpacingBuilder.RuleBuilder.spacesNoLineBreak(spaces: Int) =
|
||||||
|
spacing(spaces, spaces, 0, false, 0)
|
||||||
|
|
||||||
fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacingBuilderUtil): KotlinSpacingBuilder {
|
fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacingBuilderUtil): KotlinSpacingBuilder {
|
||||||
val kotlinCommonSettings = settings.kotlinCommonSettings
|
val kotlinCommonSettings = settings.kotlinCommonSettings
|
||||||
val kotlinCustomSettings = settings.kotlinCustomSettings
|
val kotlinCustomSettings = settings.kotlinCustomSettings
|
||||||
@@ -199,7 +202,7 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
|
|||||||
|
|
||||||
|
|
||||||
val spacesAroundAssignment = if (kotlinCommonSettings.SPACE_AROUND_ASSIGNMENT_OPERATORS) 1 else 0
|
val spacesAroundAssignment = if (kotlinCommonSettings.SPACE_AROUND_ASSIGNMENT_OPERATORS) 1 else 0
|
||||||
beforeInside(EQ, PROPERTY).spacing(spacesAroundAssignment, spacesAroundAssignment, 0, false, 0)
|
beforeInside(EQ, PROPERTY).spacesNoLineBreak(spacesAroundAssignment)
|
||||||
beforeInside(EQ, FUN).spacing(spacesAroundAssignment, spacesAroundAssignment, 0, false, 0)
|
beforeInside(EQ, FUN).spacing(spacesAroundAssignment, spacesAroundAssignment, 0, false, 0)
|
||||||
|
|
||||||
around(TokenSet.create(EQ, MULTEQ, DIVEQ, PLUSEQ, MINUSEQ, PERCEQ)).spaceIf(kotlinCommonSettings.SPACE_AROUND_ASSIGNMENT_OPERATORS)
|
around(TokenSet.create(EQ, MULTEQ, DIVEQ, PLUSEQ, MINUSEQ, PERCEQ)).spaceIf(kotlinCommonSettings.SPACE_AROUND_ASSIGNMENT_OPERATORS)
|
||||||
@@ -209,7 +212,8 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
|
|||||||
aroundInside(TokenSet.create(PLUS, MINUS), BINARY_EXPRESSION).spaceIf(kotlinCommonSettings.SPACE_AROUND_ADDITIVE_OPERATORS)
|
aroundInside(TokenSet.create(PLUS, MINUS), BINARY_EXPRESSION).spaceIf(kotlinCommonSettings.SPACE_AROUND_ADDITIVE_OPERATORS)
|
||||||
aroundInside(TokenSet.create(MUL, DIV, PERC), BINARY_EXPRESSION).spaceIf(kotlinCommonSettings.SPACE_AROUND_MULTIPLICATIVE_OPERATORS)
|
aroundInside(TokenSet.create(MUL, DIV, PERC), BINARY_EXPRESSION).spaceIf(kotlinCommonSettings.SPACE_AROUND_MULTIPLICATIVE_OPERATORS)
|
||||||
around(TokenSet.create(PLUSPLUS, MINUSMINUS, EXCLEXCL, MINUS, PLUS, EXCL)).spaceIf(kotlinCommonSettings.SPACE_AROUND_UNARY_OPERATOR)
|
around(TokenSet.create(PLUSPLUS, MINUSMINUS, EXCLEXCL, MINUS, PLUS, EXCL)).spaceIf(kotlinCommonSettings.SPACE_AROUND_UNARY_OPERATOR)
|
||||||
around(ELVIS).spaces(1)
|
before(ELVIS).spaces(1)
|
||||||
|
after(ELVIS).spacesNoLineBreak(1)
|
||||||
around(RANGE).spaceIf(kotlinCustomSettings.SPACE_AROUND_RANGE)
|
around(RANGE).spaceIf(kotlinCustomSettings.SPACE_AROUND_RANGE)
|
||||||
|
|
||||||
after(MODIFIER_LIST).spaces(1)
|
after(MODIFIER_LIST).spaces(1)
|
||||||
|
|||||||
+8
-1
@@ -55,10 +55,12 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
|
|||||||
|
|
||||||
fun multilineMethod(
|
fun multilineMethod(
|
||||||
foo: String,
|
foo: String,
|
||||||
bar: String
|
bar: String?,
|
||||||
|
x: Int?
|
||||||
) {
|
) {
|
||||||
foo.toUpperCase().trim()
|
foo.toUpperCase().trim()
|
||||||
.length
|
.length
|
||||||
|
val barLen = bar?.length() ?: x ?: -1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,6 +302,11 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
|
|||||||
"Use continuation indent",
|
"Use continuation indent",
|
||||||
"Expression body functions"
|
"Expression body functions"
|
||||||
)
|
)
|
||||||
|
showCustomOption(
|
||||||
|
KotlinCodeStyleSettings::WRAP_ELVIS_EXPRESSIONS,
|
||||||
|
"Elvis expressions",
|
||||||
|
options = *arrayOf(CodeStyleSettingsCustomizable.WRAP_OPTIONS_FOR_SINGLETON, CodeStyleSettingsCustomizable.WRAP_VALUES_FOR_SINGLETON)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
LanguageCodeStyleSettingsProvider.SettingsType.BLANK_LINES_SETTINGS -> {
|
LanguageCodeStyleSettingsProvider.SettingsType.BLANK_LINES_SETTINGS -> {
|
||||||
consumer.showStandardOptions(
|
consumer.showStandardOptions(
|
||||||
|
|||||||
+4
-5
@@ -3,15 +3,14 @@ fun test(a: Int?) {
|
|||||||
|
|
||||||
a ?: 42
|
a ?: 42
|
||||||
|
|
||||||
a ?:
|
a ?: 42
|
||||||
42
|
|
||||||
|
|
||||||
a
|
a
|
||||||
?: 42
|
?: 42
|
||||||
|
|
||||||
val some = a ?:
|
val some = a
|
||||||
b ?:
|
?: b
|
||||||
12
|
?: 12
|
||||||
}
|
}
|
||||||
|
|
||||||
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
|
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
|
||||||
+4
-5
@@ -3,15 +3,14 @@ fun test(a: Int?) {
|
|||||||
|
|
||||||
a ?: 42
|
a ?: 42
|
||||||
|
|
||||||
a ?:
|
a ?: 42
|
||||||
42
|
|
||||||
|
|
||||||
a
|
a
|
||||||
?: 42
|
?: 42
|
||||||
|
|
||||||
val some = a ?:
|
val some = a
|
||||||
b ?:
|
?: b
|
||||||
12
|
?: 12
|
||||||
}
|
}
|
||||||
|
|
||||||
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
|
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
|
||||||
Vendored
+3
-3
@@ -9,9 +9,9 @@ fun test(a: Int?) {
|
|||||||
a
|
a
|
||||||
?: 42
|
?: 42
|
||||||
|
|
||||||
val some = a ?:
|
val some = a
|
||||||
b ?:
|
?: b
|
||||||
12
|
?: 12
|
||||||
}
|
}
|
||||||
|
|
||||||
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
|
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
val x = "abc"
|
||||||
|
?: "def"
|
||||||
|
|
||||||
|
// SET_INT: WRAP_ELVIS_EXPRESSIONS = 2
|
||||||
Vendored
+3
@@ -0,0 +1,3 @@
|
|||||||
|
val x = "abc" ?: "def"
|
||||||
|
|
||||||
|
// SET_INT: WRAP_ELVIS_EXPRESSIONS = 2
|
||||||
@@ -284,6 +284,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("ElvisWrap.after.kt")
|
||||||
|
public void testElvisWrap() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/ElvisWrap.after.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("EmptyLineAfterObjectDeclaration.after.kt")
|
@TestMetadata("EmptyLineAfterObjectDeclaration.after.kt")
|
||||||
public void testEmptyLineAfterObjectDeclaration() throws Exception {
|
public void testEmptyLineAfterObjectDeclaration() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/EmptyLineAfterObjectDeclaration.after.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/EmptyLineAfterObjectDeclaration.after.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user