Don't ruin indentation for comments inside expression declarations (KT-23295)

#KT-23295 Fixed
This commit is contained in:
Nikolay Krasko
2018-12-24 21:43:26 +03:00
parent 36a6c0881c
commit 571ee4a1fe
5 changed files with 60 additions and 6 deletions
@@ -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;
}
@@ -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),
@@ -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
+17
View File
@@ -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
@@ -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");