From 571ee4a1fee74846bff5387d49067785df9b0ffd Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Mon, 24 Dec 2018 21:43:26 +0300 Subject: [PATCH] Don't ruin indentation for comments inside expression declarations (KT-23295) #KT-23295 Fixed --- .../psi/KtDestructuringDeclaration.java | 8 +++++++- .../idea/formatter/KotlinCommonBlock.kt | 19 ++++++++++++++----- .../CommentInExpressionBodies.after.kt | 17 +++++++++++++++++ .../formatter/CommentInExpressionBodies.kt | 17 +++++++++++++++++ .../formatter/FormatterTestGenerated.java | 5 +++++ 5 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 idea/testData/formatter/CommentInExpressionBodies.after.kt create mode 100644 idea/testData/formatter/CommentInExpressionBodies.kt diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtDestructuringDeclaration.java b/compiler/psi/src/org/jetbrains/kotlin/psi/KtDestructuringDeclaration.java index c86d5420adc..9ce7eac7f96 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtDestructuringDeclaration.java +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtDestructuringDeclaration.java @@ -29,7 +29,7 @@ import java.util.List; import static org.jetbrains.kotlin.lexer.KtTokens.*; -public class KtDestructuringDeclaration extends KtDeclarationImpl implements KtValVarKeywordOwner { +public class KtDestructuringDeclaration extends KtDeclarationImpl implements KtValVarKeywordOwner, KtDeclarationWithInitializer { public KtDestructuringDeclaration(@NotNull ASTNode node) { super(node); } @@ -45,6 +45,7 @@ public class KtDestructuringDeclaration extends KtDeclarationImpl implements KtV } @Nullable + @Override public KtExpression getInitializer() { ASTNode eqNode = getNode().findChildByType(EQ); if (eqNode == null) { @@ -53,6 +54,11 @@ public class KtDestructuringDeclaration extends KtDeclarationImpl implements KtV return PsiTreeUtil.getNextSiblingOfType(eqNode.getPsi(), KtExpression.class); } + @Override + public boolean hasInitializer() { + return getInitializer() != null; + } + public boolean isVar() { return getNode().findChildByType(KtTokens.VAR_KEYWORD) != null; } 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 9def17ce666..cff11c0ae5d 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt @@ -26,10 +26,7 @@ import org.jetbrains.kotlin.kdoc.parser.KDocElementTypes import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens.* import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.psi.psiUtil.children -import org.jetbrains.kotlin.psi.psiUtil.leaves -import org.jetbrains.kotlin.psi.psiUtil.parents -import org.jetbrains.kotlin.psi.psiUtil.siblings +import org.jetbrains.kotlin.psi.psiUtil.* private val QUALIFIED_OPERATION = TokenSet.create(DOT, SAFE_ACCESS) private val QUALIFIED_EXPRESSIONS = TokenSet.create(KtNodeTypes.DOT_QUALIFIED_EXPRESSION, KtNodeTypes.SAFE_ACCESS_EXPRESSION) @@ -695,7 +692,19 @@ private val INDENT_RULES = arrayOf( strategy("Expression body") .within(KtNodeTypes.FUN) .forElement { - it.psi is KtExpression && it.psi !is KtBlockExpression + (it.psi is KtExpression && it.psi !is KtBlockExpression) + } + .continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_EXPRESSION_BODIES, indentFirst = true), + + strategy("Line comment at expression body position") + .forElement { node -> + val psi = node.psi + val parent = psi.parent + if (psi is PsiComment && parent is KtDeclarationWithInitializer) { + psi.getNextSiblingIgnoringWhitespace() == parent.initializer + } else { + false + } } .continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_EXPRESSION_BODIES, indentFirst = true), diff --git a/idea/testData/formatter/CommentInExpressionBodies.after.kt b/idea/testData/formatter/CommentInExpressionBodies.after.kt new file mode 100644 index 00000000000..e305c93b361 --- /dev/null +++ b/idea/testData/formatter/CommentInExpressionBodies.after.kt @@ -0,0 +1,17 @@ +fun foo() = + // comment + 1 + +val some = + // Comment + 2 + +fun some() { + val (a, b) = + // Some + 1 to 3 +} + +fun nice() = + /* Ha! */ + 2 \ No newline at end of file diff --git a/idea/testData/formatter/CommentInExpressionBodies.kt b/idea/testData/formatter/CommentInExpressionBodies.kt new file mode 100644 index 00000000000..ee238f96826 --- /dev/null +++ b/idea/testData/formatter/CommentInExpressionBodies.kt @@ -0,0 +1,17 @@ +fun foo() = + // comment + 1 + +val some = + // Comment + 2 + +fun some() { + val (a, b) = + // Some + 1 to 3 +} + +fun nice() = + /* Ha! */ +2 \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java index debe4363967..f89f966ae42 100644 --- a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java @@ -166,6 +166,11 @@ public class FormatterTestGenerated extends AbstractFormatterTest { runTest("idea/testData/formatter/ColonSpaces.after.kt"); } + @TestMetadata("CommentInExpressionBodies.after.kt") + public void testCommentInExpressionBodies() throws Exception { + runTest("idea/testData/formatter/CommentInExpressionBodies.after.kt"); + } + @TestMetadata("CommentInFunctionLiteral.after.kt") public void testCommentInFunctionLiteral() throws Exception { runTest("idea/testData/formatter/CommentInFunctionLiteral.after.kt");