Keep empty bodies for declarations with comments (KT-16078)
#KT-16078 Fixed
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
+1
-2
@@ -1,8 +1,7 @@
|
||||
/** Doc comment for A */
|
||||
class A {
|
||||
/** Doc comment for function */
|
||||
fun foo() {
|
||||
}
|
||||
fun foo() {}
|
||||
|
||||
/**
|
||||
* Doc comment for property
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// No lines
|
||||
fun f1() {
|
||||
}
|
||||
fun f1() {}
|
||||
|
||||
val p1 = 1
|
||||
fun f2() {}
|
||||
|
||||
@@ -28,8 +28,7 @@ fun f6() = 1
|
||||
fun f7() = 8
|
||||
|
||||
// Two lines between
|
||||
fun l1() {
|
||||
}
|
||||
fun l1() {}
|
||||
|
||||
|
||||
fun l2() {}
|
||||
|
||||
@@ -34,3 +34,15 @@ enum class E1 {
|
||||
fun e = fun(a: Int,
|
||||
b: String) {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
fun commented1() {}
|
||||
|
||||
/*
|
||||
*/
|
||||
fun commented2() {}
|
||||
|
||||
// Comment
|
||||
fun commented3() {}
|
||||
+12
@@ -28,3 +28,15 @@ enum class E1 {
|
||||
|
||||
fun e = fun(a: Int,
|
||||
b: String) {}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
fun commented1() {}
|
||||
|
||||
/*
|
||||
*/
|
||||
fun commented2() {}
|
||||
|
||||
// Comment
|
||||
fun commented3() {}
|
||||
@@ -21,3 +21,20 @@ class EmptyProperties {
|
||||
}
|
||||
set(value) {}
|
||||
}
|
||||
|
||||
class EmptyProperties {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
var newline: String
|
||||
/**
|
||||
*
|
||||
*/
|
||||
get() {
|
||||
return ""
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
set(value) {}
|
||||
}
|
||||
+16
@@ -25,3 +25,19 @@ class EmptyProperties {
|
||||
get() { return "" }
|
||||
set(value) {}
|
||||
}
|
||||
|
||||
class EmptyProperties {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
var newline: String
|
||||
/**
|
||||
*
|
||||
*/
|
||||
get() { return "" }
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
set(value) {}
|
||||
}
|
||||
+1
-2
@@ -14,8 +14,7 @@ fun fooFun2() {
|
||||
}
|
||||
|
||||
//-----------------------
|
||||
public fun Int.extFun1() {
|
||||
}
|
||||
public fun Int.extFun1() {}
|
||||
|
||||
public
|
||||
fun Int.extFun2() {
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
// "Add function body" "true"
|
||||
fun foo() {
|
||||
}
|
||||
fun foo() {}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
+3
-6
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user