Reformat expression body property accessors

#KT-17394 Fixed
This commit is contained in:
Dmitry Jemerov
2017-07-17 19:48:35 +02:00
parent 4db8639bf8
commit 325e950bfe
6 changed files with 54 additions and 4 deletions
@@ -438,6 +438,13 @@ private val INDENT_RULES = arrayOf<NodeIndentStrategy>(
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)
@@ -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)
@@ -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
}
+12
View File
@@ -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
}
@@ -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() {
@@ -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");