diff --git a/plugins/kotlin/base/resources/resources-en/messages/KotlinBundle.properties b/plugins/kotlin/base/resources/resources-en/messages/KotlinBundle.properties index 6112b3e7d1ea..711ad898b251 100644 --- a/plugins/kotlin/base/resources/resources-en/messages/KotlinBundle.properties +++ b/plugins/kotlin/base/resources/resources-en/messages/KotlinBundle.properties @@ -568,6 +568,7 @@ formatter.title.trailing.comma=Trailing Comma formatter.title.property.annotations=Property annotations formatter.title.put.left.brace.on.new.line=Put left brace on new line formatter.title.range.operator=Range operators (.., ..<) +formatter.title.elvis.operator=Elvis operator (?:) formatter.title.top.level.symbols=Top-Level Symbols formatter.title.use.continuation.indent.in.conditions=Use continuation indent in conditions formatter.title.use.continuation.indent=Use continuation indent diff --git a/plugins/kotlin/formatter/minimal/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java b/plugins/kotlin/formatter/minimal/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java index 248ae8da093a..1b1170bd01ca 100644 --- a/plugins/kotlin/formatter/minimal/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java +++ b/plugins/kotlin/formatter/minimal/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java @@ -29,6 +29,7 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings { public static final int DEFAULT_NAME_COUNT_TO_USE_STAR_IMPORT = 5; public static final int DEFAULT_NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS = 3; + public boolean SPACE_AROUND_ELVIS = true; public boolean SPACE_AROUND_RANGE = false; public boolean SPACE_BEFORE_TYPE_COLON = false; public boolean SPACE_AFTER_TYPE_COLON = true; diff --git a/plugins/kotlin/formatter/minimal/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt b/plugins/kotlin/formatter/minimal/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt index 0c1e959d4b0b..612f5126d08e 100644 --- a/plugins/kotlin/formatter/minimal/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt +++ b/plugins/kotlin/formatter/minimal/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt @@ -85,6 +85,12 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide codeStyleSettingsCustomizableOptions.SPACES_AROUND_OPERATORS ) + showCustomOption( + KotlinCodeStyleSettings::SPACE_AROUND_ELVIS, + KotlinBundle.message("formatter.title.elvis.operator"), + codeStyleSettingsCustomizableOptions.SPACES_AROUND_OPERATORS + ) + showCustomOption( KotlinCodeStyleSettings::SPACE_BEFORE_TYPE_COLON, KotlinBundle.message("formatter.title.before.colon.after.declaration.name"), @@ -451,7 +457,8 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide else -> """open class Some { private val f: (Int)->Int = { a: Int -> a * 2 } fun foo(): Int { - val test: Int = 12 + val bar: Int? = 5 + val test: Int = bar ?: 12 for (i in 10..<42) { println (when { i < test -> -1 diff --git a/plugins/kotlin/formatter/minimal/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt b/plugins/kotlin/formatter/minimal/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt index cf8c9e50f72a..e29c971c6221 100644 --- a/plugins/kotlin/formatter/minimal/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt +++ b/plugins/kotlin/formatter/minimal/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt @@ -14,10 +14,6 @@ import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.TokenSet import com.intellij.util.text.TextRangeUtil import org.jetbrains.kotlin.KtNodeTypes -import org.jetbrains.kotlin.idea.formatter.afterInside -import org.jetbrains.kotlin.idea.formatter.beforeInside -import org.jetbrains.kotlin.idea.formatter.createSpaceBeforeRBrace -import org.jetbrains.kotlin.idea.formatter.startOfDeclaration import org.jetbrains.kotlin.idea.util.requireNode import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.KtBlockExpression @@ -344,8 +340,11 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing around( TokenSet.create(KtTokens.PLUSPLUS, KtTokens.MINUSMINUS, KtTokens.EXCLEXCL, KtTokens.MINUS, KtTokens.PLUS, KtTokens.EXCL) ).spaceIf(kotlinCommonSettings.SPACE_AROUND_UNARY_OPERATOR) - before(KtTokens.ELVIS).spaces(1) - after(KtTokens.ELVIS).spacesNoLineBreak(1) + + val spacesAroundElvis = if (kotlinCustomSettings.SPACE_AROUND_ELVIS) 1 else 0 + before(KtTokens.ELVIS).spaceIf(kotlinCustomSettings.SPACE_AROUND_ELVIS) + after(KtTokens.ELVIS).spacesNoLineBreak(spacesAroundElvis) + around(KtTokens.RANGE).spaceIf(kotlinCustomSettings.SPACE_AROUND_RANGE) around(KtTokens.RANGE_UNTIL).spaceIf(kotlinCustomSettings.SPACE_AROUND_RANGE) diff --git a/plugins/kotlin/idea/tests/testData/codeStyle/obsoleteCodeStyle.json b/plugins/kotlin/idea/tests/testData/codeStyle/obsoleteCodeStyle.json index eb1d9fa5d6fc..b49ae3456922 100644 --- a/plugins/kotlin/idea/tests/testData/codeStyle/obsoleteCodeStyle.json +++ b/plugins/kotlin/idea/tests/testData/codeStyle/obsoleteCodeStyle.json @@ -88,6 +88,7 @@ "space_before_while_parentheses": true, "spaces_around_additive_operators": true, "spaces_around_assignment_operators": true, + "spaces_around_elvis": true, "spaces_around_equality_operators": true, "spaces_around_function_type_arrow": true, "spaces_around_logical_operators": true, diff --git a/plugins/kotlin/idea/tests/testData/codeStyle/officialCodeStyle.json b/plugins/kotlin/idea/tests/testData/codeStyle/officialCodeStyle.json index a90047b28f35..9ccd015a030f 100644 --- a/plugins/kotlin/idea/tests/testData/codeStyle/officialCodeStyle.json +++ b/plugins/kotlin/idea/tests/testData/codeStyle/officialCodeStyle.json @@ -88,6 +88,7 @@ "space_before_while_parentheses": true, "spaces_around_additive_operators": true, "spaces_around_assignment_operators": true, + "spaces_around_elvis": true, "spaces_around_equality_operators": true, "spaces_around_function_type_arrow": true, "spaces_around_logical_operators": true, diff --git a/plugins/kotlin/idea/tests/testData/formatter/SpacesAroundOperations.after.inv.kt b/plugins/kotlin/idea/tests/testData/formatter/SpacesAroundOperations.after.inv.kt index 63c4c9e056e5..8e4e021a8260 100644 --- a/plugins/kotlin/idea/tests/testData/formatter/SpacesAroundOperations.after.inv.kt +++ b/plugins/kotlin/idea/tests/testData/formatter/SpacesAroundOperations.after.inv.kt @@ -28,10 +28,10 @@ class Some { 1..2 - null ?: 3+1 + null?:3+1 val a=1 - null ?: a+a + null?:a+a } } @@ -41,4 +41,5 @@ class Some { // SET_TRUE: SPACE_AROUND_RELATIONAL_OPERATORS // SET_TRUE: SPACE_AROUND_ADDITIVE_OPERATORS // SET_TRUE: SPACE_AROUND_MULTIPLICATIVE_OPERATORS -// SET_TRUE: SPACE_AROUND_RANGE \ No newline at end of file +// SET_TRUE: SPACE_AROUND_RANGE +// SET_TRUE: SPACE_AROUND_ELVIS \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/formatter/SpacesAroundOperations.after.kt b/plugins/kotlin/idea/tests/testData/formatter/SpacesAroundOperations.after.kt index 9277e3db108a..7b1ededbccbd 100644 --- a/plugins/kotlin/idea/tests/testData/formatter/SpacesAroundOperations.after.kt +++ b/plugins/kotlin/idea/tests/testData/formatter/SpacesAroundOperations.after.kt @@ -41,4 +41,5 @@ class Some { // SET_TRUE: SPACE_AROUND_RELATIONAL_OPERATORS // SET_TRUE: SPACE_AROUND_ADDITIVE_OPERATORS // SET_TRUE: SPACE_AROUND_MULTIPLICATIVE_OPERATORS -// SET_TRUE: SPACE_AROUND_RANGE \ No newline at end of file +// SET_TRUE: SPACE_AROUND_RANGE +// SET_TRUE: SPACE_AROUND_ELVIS diff --git a/plugins/kotlin/idea/tests/testData/formatter/SpacesAroundOperations.kt b/plugins/kotlin/idea/tests/testData/formatter/SpacesAroundOperations.kt index 46225c903fc6..459bb8c188a3 100644 --- a/plugins/kotlin/idea/tests/testData/formatter/SpacesAroundOperations.kt +++ b/plugins/kotlin/idea/tests/testData/formatter/SpacesAroundOperations.kt @@ -41,4 +41,5 @@ class Some { // SET_TRUE: SPACE_AROUND_RELATIONAL_OPERATORS // SET_TRUE: SPACE_AROUND_ADDITIVE_OPERATORS // SET_TRUE: SPACE_AROUND_MULTIPLICATIVE_OPERATORS -// SET_TRUE: SPACE_AROUND_RANGE \ No newline at end of file +// SET_TRUE: SPACE_AROUND_RANGE +// SET_TRUE: SPACE_AROUND_ELVIS \ No newline at end of file