Skip to content

Commit

Permalink
Keep empty bodies for declarations with comments (KT-16078)
Browse files Browse the repository at this point in the history
 #KT-16078 Fixed
  • Loading branch information
Nikolay Krasko committed Feb 17, 2017
1 parent e16b052 commit d587079
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 20 deletions.
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions 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
Expand Down
@@ -1,6 +1,5 @@
// No lines
fun f1() {
}
fun f1() {}

val p1 = 1
fun f2() {}
Expand Down
3 changes: 1 addition & 2 deletions idea/testData/formatter/EmptyLineBetweenFunctions.after.kt
Expand Up @@ -28,8 +28,7 @@ fun f6() = 1
fun f7() = 8

// Two lines between
fun l1() {
}
fun l1() {}


fun l2() {}
Expand Down
12 changes: 12 additions & 0 deletions idea/testData/formatter/NewLineForRBrace.after.kt
Expand Up @@ -34,3 +34,15 @@ enum class E1 {
fun e = fun(a: Int,
b: String) {
}

/**
*
*/
fun commented1() {}

/*
*/
fun commented2() {}

// Comment
fun commented3() {}
12 changes: 12 additions & 0 deletions idea/testData/formatter/NewLineForRBrace.kt
Expand Up @@ -28,3 +28,15 @@ enum class E1 {

fun e = fun(a: Int,
b: String) {}

/**
*
*/
fun commented1() {}

/*
*/
fun commented2() {}

// Comment
fun commented3() {}
17 changes: 17 additions & 0 deletions idea/testData/formatter/PropertyAccessors.after.kt
Expand Up @@ -21,3 +21,20 @@ class EmptyProperties {
}
set(value) {}
}

class EmptyProperties {
/**
*
*/
var newline: String
/**
*
*/
get() {
return ""
}
/**
*
*/
set(value) {}
}
16 changes: 16 additions & 0 deletions idea/testData/formatter/PropertyAccessors.kt
Expand Up @@ -25,3 +25,19 @@ class EmptyProperties {
get() { return "" }
set(value) {}
}

class EmptyProperties {
/**
*
*/
var newline: String
/**
*
*/
get() { return "" }

/**
*
*/
set(value) {}
}
3 changes: 1 addition & 2 deletions idea/testData/formatter/SpacesInDeclarations.after.kt
Expand Up @@ -14,8 +14,7 @@ fun fooFun2() {
}

//-----------------------
public fun Int.extFun1() {
}
public fun Int.extFun1() {}

public
fun Int.extFun2() {
Expand Down
@@ -1,3 +1,2 @@
// "Add function body" "true"
fun foo() {
}
fun foo() {}
Expand Up @@ -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

Expand Down
9 changes: 3 additions & 6 deletions j2k/testData/fileOrElement/issues/comments.kt
Expand Up @@ -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
Expand All @@ -48,8 +46,7 @@ internal class C {
* different
*/
//comments
fun f3() {
}
fun f3() {}

//combination
/** of
Expand Down

0 comments on commit d587079

Please sign in to comment.