From fc2a1fce8b717b27c1b3b9abfd85b125c6b025b4 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Mon, 12 Jun 2017 16:25:48 +0200 Subject: [PATCH] Remove line break between ) and : if there's a line break before ) --- .../idea/formatter/kotlinSpacingRules.kt | 23 ++++++++++++++++--- .../LineBreakBeforeExtendsColon.after.kt | 4 ++++ .../formatter/LineBreakBeforeExtendsColon.kt | 5 ++++ .../LineBreakBeforeExtendsColonWrap.after.kt | 6 +++++ .../LineBreakBeforeExtendsColonWrap.kt | 6 +++++ .../formatter/FormatterTestGenerated.java | 12 ++++++++++ 6 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 idea/testData/formatter/LineBreakBeforeExtendsColon.after.kt create mode 100644 idea/testData/formatter/LineBreakBeforeExtendsColon.kt create mode 100644 idea/testData/formatter/LineBreakBeforeExtendsColonWrap.after.kt create mode 100644 idea/testData/formatter/LineBreakBeforeExtendsColonWrap.kt 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 8c4ee30d2a4..9bba23bd1fc 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt @@ -33,11 +33,15 @@ import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens.* import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtFunction +import org.jetbrains.kotlin.psi.KtPrimaryConstructor 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 EXTEND_COLON_ELEMENTS = + TokenSet.create(TYPE_CONSTRAINT, CLASS, OBJECT_DECLARATION, TYPE_PARAMETER, ENUM_ENTRY, SECONDARY_CONSTRUCTOR) + fun SpacingBuilder.beforeInside(element: IElementType, tokenSet: TokenSet, spacingFun: RuleBuilder.() -> Unit) { tokenSet.types.forEach { inType -> beforeInside(element, inType).spacingFun() } } @@ -241,9 +245,6 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing beforeInside(COLON, TYPE_COLON_ELEMENTS) { spaceIf(kotlinSettings.SPACE_BEFORE_TYPE_COLON) } afterInside(COLON, TYPE_COLON_ELEMENTS) { spaceIf(kotlinSettings.SPACE_AFTER_TYPE_COLON) } - val EXTEND_COLON_ELEMENTS = - TokenSet.create(TYPE_CONSTRAINT, CLASS, OBJECT_DECLARATION, TYPE_PARAMETER, ENUM_ENTRY, SECONDARY_CONSTRUCTOR) - beforeInside(COLON, EXTEND_COLON_ELEMENTS) { spaceIf(kotlinSettings.SPACE_BEFORE_EXTEND_COLON) } afterInside(COLON, EXTEND_COLON_ELEMENTS) { spaceIf(kotlinSettings.SPACE_AFTER_EXTEND_COLON) } beforeInside(ARROW, FUNCTION_LITERAL).spaceIf(kotlinSettings.SPACE_BEFORE_LAMBDA_ARROW) @@ -446,6 +447,20 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing commonCodeStyleSettings.KEEP_LINE_BREAKS, commonCodeStyleSettings.KEEP_BLANK_LINES_IN_CODE) } + + inPosition(parentSet = EXTEND_COLON_ELEMENTS, left = PRIMARY_CONSTRUCTOR, right = COLON).customRule { parent, left, _ -> + val primaryConstructor = left.node.psi as KtPrimaryConstructor + val rightParenthesis = primaryConstructor.valueParameterList?.rightParenthesis + val prevSibling = rightParenthesis?.prevSibling + val spaces = if (kotlinSettings.SPACE_BEFORE_EXTEND_COLON) 1 else 0 + // TODO This should use DependentSpacingRule, but it doesn't set keepLineBreaks to false if max LFs is 0 + if ((prevSibling as? PsiWhiteSpace)?.textContains('\n') == true || commonCodeStyleSettings.METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE) { + createSpacing(spaces, keepLineBreaks = false) + } + else { + createSpacing(spaces) + } + } } simple { @@ -457,6 +472,8 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing // if when entry has block, spacing after arrow should be set by lbrace rule aroundInside(ARROW, WHEN_ENTRY).spaceIf(kotlinSettings.SPACE_AROUND_WHEN_ARROW) + beforeInside(COLON, EXTEND_COLON_ELEMENTS) { spaceIf(kotlinSettings.SPACE_BEFORE_EXTEND_COLON) } + after(EOL_COMMENT).lineBreakInCode() } } diff --git a/idea/testData/formatter/LineBreakBeforeExtendsColon.after.kt b/idea/testData/formatter/LineBreakBeforeExtendsColon.after.kt new file mode 100644 index 00000000000..e1724d793ba --- /dev/null +++ b/idea/testData/formatter/LineBreakBeforeExtendsColon.after.kt @@ -0,0 +1,4 @@ +open class GFMOutputBuilder( + to: StringBuilder +) : MarkdownOutputBuilder(to) { +} diff --git a/idea/testData/formatter/LineBreakBeforeExtendsColon.kt b/idea/testData/formatter/LineBreakBeforeExtendsColon.kt new file mode 100644 index 00000000000..cefb9411b5c --- /dev/null +++ b/idea/testData/formatter/LineBreakBeforeExtendsColon.kt @@ -0,0 +1,5 @@ +open class GFMOutputBuilder( + to: StringBuilder +) + : MarkdownOutputBuilder(to) { +} diff --git a/idea/testData/formatter/LineBreakBeforeExtendsColonWrap.after.kt b/idea/testData/formatter/LineBreakBeforeExtendsColonWrap.after.kt new file mode 100644 index 00000000000..d119bee2087 --- /dev/null +++ b/idea/testData/formatter/LineBreakBeforeExtendsColonWrap.after.kt @@ -0,0 +1,6 @@ +open class GFMOutputBuilder( + to: StringBuilder +) : MarkdownOutputBuilder(to) { +} + +// SET_TRUE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE diff --git a/idea/testData/formatter/LineBreakBeforeExtendsColonWrap.kt b/idea/testData/formatter/LineBreakBeforeExtendsColonWrap.kt new file mode 100644 index 00000000000..ea68d7aad1e --- /dev/null +++ b/idea/testData/formatter/LineBreakBeforeExtendsColonWrap.kt @@ -0,0 +1,6 @@ +open class GFMOutputBuilder( + to: StringBuilder) + : MarkdownOutputBuilder(to) { +} + +// SET_TRUE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE diff --git a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java index 36a6ed93d97..016deef778e 100644 --- a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java @@ -422,6 +422,18 @@ public class FormatterTestGenerated extends AbstractFormatterTest { doTest(fileName); } + @TestMetadata("LineBreakBeforeExtendsColon.after.kt") + public void testLineBreakBeforeExtendsColon() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/LineBreakBeforeExtendsColon.after.kt"); + doTest(fileName); + } + + @TestMetadata("LineBreakBeforeExtendsColonWrap.after.kt") + public void testLineBreakBeforeExtendsColonWrap() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/LineBreakBeforeExtendsColonWrap.after.kt"); + doTest(fileName); + } + @TestMetadata("LoopParameterWithExplicitType.after.kt") public void testLoopParameterWithExplicitType() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/LoopParameterWithExplicitType.after.kt");