[kotlin] Added option to disable spaces around elvis operator

^KTIJ-28596 fixed

GitOrigin-RevId: 839ae8af9eccff90cf82dcb2fad59e35c9aa702b
This commit is contained in:
Frederik Haselmeier
2024-08-12 16:12:56 +00:00
committed by intellij-monorepo-bot
parent bd38347379
commit 3db9d19ca5
9 changed files with 25 additions and 12 deletions
@@ -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
@@ -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;
@@ -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
@@ -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)
@@ -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,
@@ -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,
@@ -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
// SET_TRUE: SPACE_AROUND_RANGE
// SET_TRUE: SPACE_AROUND_ELVIS
@@ -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
// SET_TRUE: SPACE_AROUND_RANGE
// SET_TRUE: SPACE_AROUND_ELVIS
@@ -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
// SET_TRUE: SPACE_AROUND_RANGE
// SET_TRUE: SPACE_AROUND_ELVIS