From d58707972d5cc817afd05563c4eb839f9ff4ded6 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 16 Feb 2017 17:59:36 +0300 Subject: [PATCH] Keep empty bodies for declarations with comments (KT-16078) #KT-16078 Fixed --- .../jetbrains/kotlin/psi/psiUtil/psiUtils.kt | 6 ++++++ .../kotlin/idea/formatter/kotlinSpacingRules.kt | 5 +++-- idea/testData/formatter/DocComments.after.kt | 3 +-- .../EmptyLineBetweenFunAndProperty.after.kt | 3 +-- .../EmptyLineBetweenFunctions.after.kt | 3 +-- .../formatter/NewLineForRBrace.after.kt | 12 ++++++++++++ idea/testData/formatter/NewLineForRBrace.kt | 12 ++++++++++++ .../formatter/PropertyAccessors.after.kt | 17 +++++++++++++++++ idea/testData/formatter/PropertyAccessors.kt | 16 ++++++++++++++++ .../formatter/SpacesInDeclarations.after.kt | 3 +-- .../abstract/nonMemberFunctionNoBody.kt.after | 3 +-- .../comments/commentsForConstructors.kt | 3 +-- j2k/testData/fileOrElement/issues/comments.kt | 9 +++------ 13 files changed, 75 insertions(+), 20 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/psiUtils.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/psiUtils.kt index d87aa8f7d08..bbc5c991091 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/psiUtils.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/psiUtils.kt @@ -311,6 +311,12 @@ private fun findFirstLeafWhollyInRange(file: PsiFile, range: TextRange): PsiElem return if (elementRange.endOffset <= range.endOffset) element else null } +val PsiElement.textRangeWithoutComments: TextRange + get() { + val firstNonCommentChild = children.firstOrNull { it !is PsiWhiteSpace && it !is PsiComment } ?: return textRange + return TextRange(firstNonCommentChild.startOffset, endOffset) + } + // ---------------------------------- Debug/logging ---------------------------------------------------------------------------------------- fun PsiElement.getElementTextWithContext(): String { diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt index 04f5f808220..f1336dacb11 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt @@ -34,6 +34,7 @@ import org.jetbrains.kotlin.lexer.KtTokens.* import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtFunction import org.jetbrains.kotlin.psi.KtPropertyAccessor +import org.jetbrains.kotlin.psi.psiUtil.textRangeWithoutComments val MODIFIERS_LIST_ENTRIES = TokenSet.orSet(TokenSet.create(ANNOTATION_ENTRY, ANNOTATION), MODIFIER_KEYWORDS) @@ -427,7 +428,7 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing } val spaces = if (empty) 0 else spacesInSimpleFunction - Spacing.createDependentLFSpacing(spaces, spaces, psiElement.textRange, + Spacing.createDependentLFSpacing(spaces, spaces, psiElement.textRangeWithoutComments, codeStyleSettings.KEEP_LINE_BREAKS, codeStyleSettings.KEEP_BLANK_LINES_IN_CODE) } @@ -439,7 +440,7 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing if (funNode.name != null) return@customRule null // Empty block is covered in above rule - Spacing.createDependentLFSpacing(spacesInSimpleFunction, spacesInSimpleFunction, funNode.textRange, + Spacing.createDependentLFSpacing(spacesInSimpleFunction, spacesInSimpleFunction, funNode.textRangeWithoutComments, codeStyleSettings.KEEP_LINE_BREAKS, codeStyleSettings.KEEP_BLANK_LINES_IN_CODE) } diff --git a/idea/testData/formatter/DocComments.after.kt b/idea/testData/formatter/DocComments.after.kt index 3bb3d07c557..1b4249dde38 100644 --- a/idea/testData/formatter/DocComments.after.kt +++ b/idea/testData/formatter/DocComments.after.kt @@ -1,8 +1,7 @@ /** Doc comment for A */ class A { /** Doc comment for function */ - fun foo() { - } + fun foo() {} /** * Doc comment for property diff --git a/idea/testData/formatter/EmptyLineBetweenFunAndProperty.after.kt b/idea/testData/formatter/EmptyLineBetweenFunAndProperty.after.kt index 9f03cc5acec..d886cf0cef5 100644 --- a/idea/testData/formatter/EmptyLineBetweenFunAndProperty.after.kt +++ b/idea/testData/formatter/EmptyLineBetweenFunAndProperty.after.kt @@ -1,6 +1,5 @@ // No lines -fun f1() { -} +fun f1() {} val p1 = 1 fun f2() {} diff --git a/idea/testData/formatter/EmptyLineBetweenFunctions.after.kt b/idea/testData/formatter/EmptyLineBetweenFunctions.after.kt index f46ebb22f8b..0ec7f7c4380 100644 --- a/idea/testData/formatter/EmptyLineBetweenFunctions.after.kt +++ b/idea/testData/formatter/EmptyLineBetweenFunctions.after.kt @@ -28,8 +28,7 @@ fun f6() = 1 fun f7() = 8 // Two lines between -fun l1() { -} +fun l1() {} fun l2() {} diff --git a/idea/testData/formatter/NewLineForRBrace.after.kt b/idea/testData/formatter/NewLineForRBrace.after.kt index 800b6fe82ad..926fc8fb621 100644 --- a/idea/testData/formatter/NewLineForRBrace.after.kt +++ b/idea/testData/formatter/NewLineForRBrace.after.kt @@ -34,3 +34,15 @@ enum class E1 { fun e = fun(a: Int, b: String) { } + +/** + * + */ +fun commented1() {} + +/* + */ +fun commented2() {} + +// Comment +fun commented3() {} \ No newline at end of file diff --git a/idea/testData/formatter/NewLineForRBrace.kt b/idea/testData/formatter/NewLineForRBrace.kt index 8256e87f416..4e6d590565e 100644 --- a/idea/testData/formatter/NewLineForRBrace.kt +++ b/idea/testData/formatter/NewLineForRBrace.kt @@ -28,3 +28,15 @@ enum class E1 { fun e = fun(a: Int, b: String) {} + +/** + * + */ +fun commented1() {} + +/* + */ +fun commented2() {} + +// Comment +fun commented3() {} \ No newline at end of file diff --git a/idea/testData/formatter/PropertyAccessors.after.kt b/idea/testData/formatter/PropertyAccessors.after.kt index 32289cf9e2f..66cd4cca83f 100644 --- a/idea/testData/formatter/PropertyAccessors.after.kt +++ b/idea/testData/formatter/PropertyAccessors.after.kt @@ -21,3 +21,20 @@ class EmptyProperties { } set(value) {} } + +class EmptyProperties { + /** + * + */ + var newline: String + /** + * + */ + get() { + return "" + } + /** + * + */ + set(value) {} +} \ No newline at end of file diff --git a/idea/testData/formatter/PropertyAccessors.kt b/idea/testData/formatter/PropertyAccessors.kt index 352ce65c8e6..2b8a188c15d 100644 --- a/idea/testData/formatter/PropertyAccessors.kt +++ b/idea/testData/formatter/PropertyAccessors.kt @@ -25,3 +25,19 @@ class EmptyProperties { get() { return "" } set(value) {} } + +class EmptyProperties { + /** + * + */ + var newline: String + /** + * + */ + get() { return "" } + + /** + * + */ + set(value) {} +} \ No newline at end of file diff --git a/idea/testData/formatter/SpacesInDeclarations.after.kt b/idea/testData/formatter/SpacesInDeclarations.after.kt index 28c87941c84..19b12c610fc 100644 --- a/idea/testData/formatter/SpacesInDeclarations.after.kt +++ b/idea/testData/formatter/SpacesInDeclarations.after.kt @@ -14,8 +14,7 @@ fun fooFun2() { } //----------------------- -public fun Int.extFun1() { -} +public fun Int.extFun1() {} public fun Int.extFun2() { diff --git a/idea/testData/quickfix/abstract/nonMemberFunctionNoBody.kt.after b/idea/testData/quickfix/abstract/nonMemberFunctionNoBody.kt.after index 3be25643ed8..6a650abbcbf 100644 --- a/idea/testData/quickfix/abstract/nonMemberFunctionNoBody.kt.after +++ b/idea/testData/quickfix/abstract/nonMemberFunctionNoBody.kt.after @@ -1,3 +1,2 @@ // "Add function body" "true" -fun foo() { -} +fun foo() {} diff --git a/j2k/testData/fileOrElement/comments/commentsForConstructors.kt b/j2k/testData/fileOrElement/comments/commentsForConstructors.kt index 607404c7373..b3687498752 100644 --- a/j2k/testData/fileOrElement/comments/commentsForConstructors.kt +++ b/j2k/testData/fileOrElement/comments/commentsForConstructors.kt @@ -7,8 +7,7 @@ internal class A// this is a primary constructor } // end of primary constructor body // this is a secondary constructor 2 - constructor(s: String) : this(s.length) { - } // end of secondary constructor 2 body + constructor(s: String) : this(s.length) {} // end of secondary constructor 2 body }// this is a secondary constructor 1 // end of secondary constructor 1 body diff --git a/j2k/testData/fileOrElement/issues/comments.kt b/j2k/testData/fileOrElement/issues/comments.kt index 90cbbf8ee2c..c6d62dd6f7a 100644 --- a/j2k/testData/fileOrElement/issues/comments.kt +++ b/j2k/testData/fileOrElement/issues/comments.kt @@ -25,16 +25,14 @@ internal class C { } //simple one line comment for function - fun f1() { - } + fun f1() {} //simple one line comment for field var j: Int = 0 //double c style //comment before function - fun f2() { - } + fun f2() {} //double c style //comment before field @@ -48,8 +46,7 @@ internal class C { * different */ //comments - fun f3() { - } + fun f3() {} //combination /** of