From 6f719a9460648551e6a12d71f327b87804b36715 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 30 Oct 2018 18:15:18 +0300 Subject: [PATCH] Use binary expression as parent for better spacing (KT-27717) When BINARY_EXPRESSION is used as parent, spacing rules for it are applied. There're no rules for OPERATION_REFERENCE on the other side. #KT-27717 Fixed --- .../kotlin/idea/formatter/KotlinCommonBlock.kt | 10 +++++++++- .../kotlin/idea/formatter/SyntheticKotlinBlock.kt | 4 ++-- .../formatter/SpacesAroundOperations.after.inv.kt | 5 +++++ .../testData/formatter/SpacesAroundOperations.after.kt | 5 +++++ idea/testData/formatter/SpacesAroundOperations.kt | 5 +++++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt index f91e5708f50..ce29ebf2a6d 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt @@ -163,7 +163,15 @@ abstract class KotlinCommonBlock( operationBlock.requireNode(), subList(index, size), null, indent, wrap, spacingBuilder - ) { createSyntheticSpacingNodeBlock(it) } + ) { + val parent = it.treeParent ?: node + val skipOperationNodeParent = if (parent.elementType === KtNodeTypes.OPERATION_REFERENCE) { + parent.treeParent ?: parent + } else { + parent + } + createSyntheticSpacingNodeBlock(skipOperationNodeParent) + } return subList(0, index) + operationSyntheticBlock } diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/SyntheticKotlinBlock.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/SyntheticKotlinBlock.kt index 1c056dd53ef..2fa894144b4 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/SyntheticKotlinBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/SyntheticKotlinBlock.kt @@ -27,7 +27,7 @@ class SyntheticKotlinBlock( private val indent: Indent?, private val wrap: Wrap?, private val spacingBuilder: KotlinSpacingBuilder, - private val createSyntheticSpacingNodeBlock: (ASTNode) -> ASTBlock + private val createParentSyntheticSpacingBlock: (ASTNode) -> ASTBlock ) : ASTBlock { private val textRange = TextRange( @@ -44,7 +44,7 @@ class SyntheticKotlinBlock( override fun isLeaf() = false override fun getNode() = node override fun getSpacing(child1: Block?, child2: Block): Spacing? { - return spacingBuilder.getSpacing(createSyntheticSpacingNodeBlock(node.treeParent!!), child1, child2) + return spacingBuilder.getSpacing(createParentSyntheticSpacingBlock(node), child1, child2) } diff --git a/idea/testData/formatter/SpacesAroundOperations.after.inv.kt b/idea/testData/formatter/SpacesAroundOperations.after.inv.kt index 57936cd357f..63c4c9e056e 100644 --- a/idea/testData/formatter/SpacesAroundOperations.after.inv.kt +++ b/idea/testData/formatter/SpacesAroundOperations.after.inv.kt @@ -27,6 +27,11 @@ class Some { 12%3*12/3 1..2 + + null ?: 3+1 + + val a=1 + null ?: a+a } } diff --git a/idea/testData/formatter/SpacesAroundOperations.after.kt b/idea/testData/formatter/SpacesAroundOperations.after.kt index ad75a93c7a8..9277e3db108 100644 --- a/idea/testData/formatter/SpacesAroundOperations.after.kt +++ b/idea/testData/formatter/SpacesAroundOperations.after.kt @@ -27,6 +27,11 @@ class Some { 12 % 3 * 12 / 3 1 .. 2 + + null ?: 3 + 1 + + val a = 1 + null ?: a + a } } diff --git a/idea/testData/formatter/SpacesAroundOperations.kt b/idea/testData/formatter/SpacesAroundOperations.kt index 4a2f66d9f1b..46225c903fc 100644 --- a/idea/testData/formatter/SpacesAroundOperations.kt +++ b/idea/testData/formatter/SpacesAroundOperations.kt @@ -27,6 +27,11 @@ class Some { 12%3*12/3 1..2 + + null ?: 3 + 1 + + val a = 1 + null ?: a + a } }