"Blank lines after class header" option supported
This commit is contained in:
@@ -17,10 +17,12 @@
|
||||
package org.jetbrains.kotlin.idea.formatter
|
||||
|
||||
import com.intellij.formatting.ASTBlock
|
||||
import com.intellij.formatting.DependentSpacingRule
|
||||
import com.intellij.formatting.Spacing
|
||||
import com.intellij.formatting.SpacingBuilder
|
||||
import com.intellij.formatting.SpacingBuilder.RuleBuilder
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiWhiteSpace
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings
|
||||
import com.intellij.psi.tree.IElementType
|
||||
@@ -32,6 +34,7 @@ import org.jetbrains.kotlin.idea.formatter.KotlinSpacingBuilder.CustomSpacingBui
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.lexer.KtTokens.*
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isObjectLiteral
|
||||
import org.jetbrains.kotlin.psi.psiUtil.textRangeWithoutComments
|
||||
|
||||
val MODIFIERS_LIST_ENTRIES = TokenSet.orSet(TokenSet.create(ANNOTATION_ENTRY, ANNOTATION), MODIFIER_KEYWORDS)
|
||||
@@ -78,6 +81,25 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
|
||||
} else null
|
||||
}
|
||||
|
||||
inPosition(parent = CLASS_BODY, left = LBRACE).customRule { parent, left, right ->
|
||||
if (right.node.elementType == RBRACE) {
|
||||
return@customRule createSpacing(0)
|
||||
}
|
||||
val classBody = parent.node.psi as KtClassBody
|
||||
val parentPsi = classBody.parent as? KtClassOrObject ?: return@customRule null
|
||||
if (commonCodeStyleSettings.BLANK_LINES_AFTER_CLASS_HEADER == 0 || parentPsi.isObjectLiteral()) {
|
||||
null
|
||||
}
|
||||
else {
|
||||
Spacing.createDependentLFSpacing(
|
||||
0, 0,
|
||||
TextRange(parentPsi.textRange.startOffset, left.node.psi.textRange.startOffset),
|
||||
settings.KEEP_LINE_BREAKS, settings.KEEP_BLANK_LINES_IN_DECLARATIONS,
|
||||
DependentSpacingRule(DependentSpacingRule.Trigger.HAS_LINE_FEEDS)
|
||||
.registerData(DependentSpacingRule.Anchor.MIN_LINE_FEEDS, commonCodeStyleSettings.BLANK_LINES_AFTER_CLASS_HEADER + 1))
|
||||
}
|
||||
}
|
||||
|
||||
val parameterWithDocCommentRule = {
|
||||
_: ASTBlock, _: ASTBlock, right: ASTBlock ->
|
||||
if (right.node.firstChildNode.elementType == KtTokens.DOC_COMMENT) {
|
||||
@@ -130,8 +152,6 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
|
||||
after(DOC_COMMENT).lineBreakInCode()
|
||||
|
||||
// =============== Spacing ================
|
||||
betweenInside(LBRACE, RBRACE, CLASS_BODY).spaces(0)
|
||||
|
||||
before(COMMA).spaceIf(kotlinCommonSettings.SPACE_BEFORE_COMMA)
|
||||
after(COMMA).spaceIf(kotlinCommonSettings.SPACE_AFTER_COMMA)
|
||||
|
||||
|
||||
+2
-1
@@ -252,7 +252,8 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
|
||||
LanguageCodeStyleSettingsProvider.SettingsType.BLANK_LINES_SETTINGS -> {
|
||||
consumer.showStandardOptions(
|
||||
"KEEP_BLANK_LINES_IN_CODE",
|
||||
"KEEP_BLANK_LINES_IN_DECLARATIONS"
|
||||
"KEEP_BLANK_LINES_IN_DECLARATIONS",
|
||||
"BLANK_LINES_AFTER_CLASS_HEADER"
|
||||
)
|
||||
showCustomOption(KotlinCodeStyleSettings::BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES,
|
||||
"Around 'when' branches with {}",
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
class Foo {
|
||||
fun bar() {
|
||||
}
|
||||
}
|
||||
|
||||
object Bar {
|
||||
fun xyzzy() {
|
||||
}
|
||||
}
|
||||
|
||||
val f = object {
|
||||
fun foo() {}
|
||||
}
|
||||
|
||||
class FooM
|
||||
: IFoo {
|
||||
|
||||
fun bar()
|
||||
}
|
||||
|
||||
class FooC(
|
||||
val x: String
|
||||
) {
|
||||
|
||||
fun bar()
|
||||
}
|
||||
|
||||
// SET_INT: BLANK_LINES_AFTER_CLASS_HEADER = 1
|
||||
@@ -0,0 +1,26 @@
|
||||
class Foo {
|
||||
fun bar() {
|
||||
}
|
||||
}
|
||||
|
||||
object Bar {
|
||||
fun xyzzy() {
|
||||
}
|
||||
}
|
||||
|
||||
val f = object {
|
||||
fun foo() {}
|
||||
}
|
||||
|
||||
class FooM
|
||||
: IFoo {
|
||||
fun bar()
|
||||
}
|
||||
|
||||
class FooC(
|
||||
val x: String
|
||||
) {
|
||||
fun bar()
|
||||
}
|
||||
|
||||
// SET_INT: BLANK_LINES_AFTER_CLASS_HEADER = 1
|
||||
@@ -92,6 +92,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("BlankLinesAfterClassHeader.after.kt")
|
||||
public void testBlankLinesAfterClassHeader() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/BlankLinesAfterClassHeader.after.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("BlockFor.after.kt")
|
||||
public void testBlockFor() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/BlockFor.after.kt");
|
||||
|
||||
Reference in New Issue
Block a user