diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt index f6e5089f1ab..8eded2bce7a 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt @@ -438,6 +438,13 @@ private val INDENT_RULES = arrayOf( Indent.getNormalIndent() }, + strategy("Property accessor expression body") + .within(KtNodeTypes.PROPERTY_ACCESSOR) + .forElement { + it.psi is KtExpression && it.psi !is KtBlockExpression + } + .set(Indent.getNormalIndent()), + strategy("Indent for parts") .within(KtNodeTypes.PROPERTY, KtNodeTypes.FUN, KtNodeTypes.DESTRUCTURING_DECLARATION, KtNodeTypes.SECONDARY_CONSTRUCTOR) .notForType(KtNodeTypes.BLOCK, FUN_KEYWORD, VAL_KEYWORD, VAR_KEYWORD, CONSTRUCTOR_KEYWORD) 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 b4d1ff09b63..784dcb74919 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt @@ -23,6 +23,7 @@ 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.PsiComment import com.intellij.psi.PsiWhiteSpace import com.intellij.psi.codeStyle.CodeStyleSettings import com.intellij.psi.tree.IElementType @@ -35,6 +36,7 @@ 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.siblings import org.jetbrains.kotlin.psi.psiUtil.textRangeWithoutComments val MODIFIERS_LIST_ENTRIES = TokenSet.orSet(TokenSet.create(ANNOTATION_ENTRY, ANNOTATION), MODIFIER_KEYWORDS) @@ -140,6 +142,16 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing } } inPosition(parent = VALUE_PARAMETER_LIST, right = VALUE_PARAMETER).customRule(parameterWithDocCommentRule) + + inPosition(parent = PROPERTY, right = PROPERTY_ACCESSOR).customRule { parent, _, _ -> + val startNode = parent.node.psi.firstChild + .siblings() + .dropWhile { it is PsiComment || it is PsiWhiteSpace }.firstOrNull() ?: parent.node.psi + Spacing.createDependentLFSpacing(1, 1, + TextRange(startNode.textRange.startOffset, parent.textRange.endOffset), + false, 0) + } + } simple { @@ -195,7 +207,6 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing after(VAL_KEYWORD).spaces(1) after(VAR_KEYWORD).spaces(1) - beforeInside(PROPERTY_ACCESSOR, PROPERTY).spacing(1, 0, 0, true, 0) betweenInside(TYPE_PARAMETER_LIST, IDENTIFIER, PROPERTY).spaces(1) betweenInside(TYPE_REFERENCE, DOT, PROPERTY).spacing(0, 0, 0, false, 0) betweenInside(DOT, IDENTIFIER, PROPERTY).spacing(0, 0, 0, false, 0) diff --git a/idea/testData/formatter/PropertyAccessorLineBreak.after.kt b/idea/testData/formatter/PropertyAccessorLineBreak.after.kt new file mode 100644 index 00000000000..18d1ad6c038 --- /dev/null +++ b/idea/testData/formatter/PropertyAccessorLineBreak.after.kt @@ -0,0 +1,13 @@ +class A { + override val location: Location + get() = + if (team != null) + team.location(organization) + else + organization.location().teams + + val name get() = "a" + + /** The age. */ + val age get() = 1 +} diff --git a/idea/testData/formatter/PropertyAccessorLineBreak.kt b/idea/testData/formatter/PropertyAccessorLineBreak.kt new file mode 100644 index 00000000000..25c05469578 --- /dev/null +++ b/idea/testData/formatter/PropertyAccessorLineBreak.kt @@ -0,0 +1,12 @@ +class A { + override val location: Location get() = + if (team != null) + team.location(organization) + else + organization.location().teams + + val name get() = "a" + + /** The age. */ + val age get() = 1 +} \ No newline at end of file diff --git a/idea/testData/intentions/convertFunctionToProperty/addExplicitAnonymousType.kt.after b/idea/testData/intentions/convertFunctionToProperty/addExplicitAnonymousType.kt.after index 4ab0a12df93..86ac448773b 100644 --- a/idea/testData/intentions/convertFunctionToProperty/addExplicitAnonymousType.kt.after +++ b/idea/testData/intentions/convertFunctionToProperty/addExplicitAnonymousType.kt.after @@ -3,11 +3,12 @@ interface T { } class A(val n: Int) { - val foo get() = object : T { - override fun bar() { + val foo + get() = object : T { + override fun bar() { + } } - } } fun test() { diff --git a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java index c0e71823d68..722ca585a57 100644 --- a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java @@ -596,6 +596,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest { doTest(fileName); } + @TestMetadata("PropertyAccessorLineBreak.after.kt") + public void testPropertyAccessorLineBreak() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/PropertyAccessorLineBreak.after.kt"); + doTest(fileName); + } + @TestMetadata("PropertyAccessors.after.kt") public void testPropertyAccessors() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/PropertyAccessors.after.kt");