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
|
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 ----------------------------------------------------------------------------------------
|
// ---------------------------------- Debug/logging ----------------------------------------------------------------------------------------
|
||||||
|
|
||||||
fun PsiElement.getElementTextWithContext(): String {
|
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.KtClass
|
||||||
import org.jetbrains.kotlin.psi.KtFunction
|
import org.jetbrains.kotlin.psi.KtFunction
|
||||||
import org.jetbrains.kotlin.psi.KtPropertyAccessor
|
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)
|
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
|
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_LINE_BREAKS,
|
||||||
codeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
|
codeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
|
||||||
}
|
}
|
||||||
@@ -439,7 +440,7 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
|
|||||||
if (funNode.name != null) return@customRule null
|
if (funNode.name != null) return@customRule null
|
||||||
|
|
||||||
// Empty block is covered in above rule
|
// 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_LINE_BREAKS,
|
||||||
codeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
|
codeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -1,8 +1,7 @@
|
|||||||
/** Doc comment for A */
|
/** Doc comment for A */
|
||||||
class A {
|
class A {
|
||||||
/** Doc comment for function */
|
/** Doc comment for function */
|
||||||
fun foo() {
|
fun foo() {}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Doc comment for property
|
* Doc comment for property
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
// No lines
|
// No lines
|
||||||
fun f1() {
|
fun f1() {}
|
||||||
}
|
|
||||||
|
|
||||||
val p1 = 1
|
val p1 = 1
|
||||||
fun f2() {}
|
fun f2() {}
|
||||||
|
|||||||
@@ -28,8 +28,7 @@ fun f6() = 1
|
|||||||
fun f7() = 8
|
fun f7() = 8
|
||||||
|
|
||||||
// Two lines between
|
// Two lines between
|
||||||
fun l1() {
|
fun l1() {}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fun l2() {}
|
fun l2() {}
|
||||||
|
|||||||
@@ -34,3 +34,15 @@ enum class E1 {
|
|||||||
fun e = fun(a: Int,
|
fun e = fun(a: Int,
|
||||||
b: String) {
|
b: String) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
fun commented1() {}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*/
|
||||||
|
fun commented2() {}
|
||||||
|
|
||||||
|
// Comment
|
||||||
|
fun commented3() {}
|
||||||
+12
@@ -28,3 +28,15 @@ enum class E1 {
|
|||||||
|
|
||||||
fun e = fun(a: Int,
|
fun e = fun(a: Int,
|
||||||
b: String) {}
|
b: String) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
fun commented1() {}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*/
|
||||||
|
fun commented2() {}
|
||||||
|
|
||||||
|
// Comment
|
||||||
|
fun commented3() {}
|
||||||
@@ -21,3 +21,20 @@ class EmptyProperties {
|
|||||||
}
|
}
|
||||||
set(value) {}
|
set(value) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class EmptyProperties {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
var newline: String
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
get() {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
set(value) {}
|
||||||
|
}
|
||||||
+16
@@ -25,3 +25,19 @@ class EmptyProperties {
|
|||||||
get() { return "" }
|
get() { return "" }
|
||||||
set(value) {}
|
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
|
public
|
||||||
fun Int.extFun2() {
|
fun Int.extFun2() {
|
||||||
|
|||||||
@@ -1,3 +1,2 @@
|
|||||||
// "Add function body" "true"
|
// "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
|
} // end of primary constructor body
|
||||||
|
|
||||||
// this is a secondary constructor 2
|
// this is a secondary constructor 2
|
||||||
constructor(s: String) : this(s.length) {
|
constructor(s: String) : this(s.length) {} // end of secondary constructor 2 body
|
||||||
} // end of secondary constructor 2 body
|
|
||||||
}// this is a secondary constructor 1
|
}// this is a secondary constructor 1
|
||||||
// end of secondary constructor 1 body
|
// end of secondary constructor 1 body
|
||||||
|
|
||||||
|
|||||||
+3
-6
@@ -25,16 +25,14 @@ internal class C {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//simple one line comment for function
|
//simple one line comment for function
|
||||||
fun f1() {
|
fun f1() {}
|
||||||
}
|
|
||||||
|
|
||||||
//simple one line comment for field
|
//simple one line comment for field
|
||||||
var j: Int = 0
|
var j: Int = 0
|
||||||
|
|
||||||
//double c style
|
//double c style
|
||||||
//comment before function
|
//comment before function
|
||||||
fun f2() {
|
fun f2() {}
|
||||||
}
|
|
||||||
|
|
||||||
//double c style
|
//double c style
|
||||||
//comment before field
|
//comment before field
|
||||||
@@ -48,8 +46,7 @@ internal class C {
|
|||||||
* different
|
* different
|
||||||
*/
|
*/
|
||||||
//comments
|
//comments
|
||||||
fun f3() {
|
fun f3() {}
|
||||||
}
|
|
||||||
|
|
||||||
//combination
|
//combination
|
||||||
/** of
|
/** of
|
||||||
|
|||||||
Reference in New Issue
Block a user