Remove line break between ) and : if there's a line break before )

This commit is contained in:
Dmitry Jemerov
2017-06-12 16:25:48 +02:00
parent 2ce430d608
commit fc2a1fce8b
6 changed files with 53 additions and 3 deletions
@@ -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()
}
}
@@ -0,0 +1,4 @@
open class GFMOutputBuilder(
to: StringBuilder
) : MarkdownOutputBuilder(to) {
}
@@ -0,0 +1,5 @@
open class GFMOutputBuilder(
to: StringBuilder
)
: MarkdownOutputBuilder(to) {
}
@@ -0,0 +1,6 @@
open class GFMOutputBuilder(
to: StringBuilder
) : MarkdownOutputBuilder(to) {
}
// SET_TRUE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
@@ -0,0 +1,6 @@
open class GFMOutputBuilder(
to: StringBuilder)
: MarkdownOutputBuilder(to) {
}
// SET_TRUE: METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE
@@ -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");