Don't ruin indentation for comments inside expression declarations (KT-23295)
#KT-23295 Fixed
This commit is contained in:
@@ -29,7 +29,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import static org.jetbrains.kotlin.lexer.KtTokens.*;
|
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) {
|
public KtDestructuringDeclaration(@NotNull ASTNode node) {
|
||||||
super(node);
|
super(node);
|
||||||
}
|
}
|
||||||
@@ -45,6 +45,7 @@ public class KtDestructuringDeclaration extends KtDeclarationImpl implements KtV
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
@Override
|
||||||
public KtExpression getInitializer() {
|
public KtExpression getInitializer() {
|
||||||
ASTNode eqNode = getNode().findChildByType(EQ);
|
ASTNode eqNode = getNode().findChildByType(EQ);
|
||||||
if (eqNode == null) {
|
if (eqNode == null) {
|
||||||
@@ -53,6 +54,11 @@ public class KtDestructuringDeclaration extends KtDeclarationImpl implements KtV
|
|||||||
return PsiTreeUtil.getNextSiblingOfType(eqNode.getPsi(), KtExpression.class);
|
return PsiTreeUtil.getNextSiblingOfType(eqNode.getPsi(), KtExpression.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasInitializer() {
|
||||||
|
return getInitializer() != null;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isVar() {
|
public boolean isVar() {
|
||||||
return getNode().findChildByType(KtTokens.VAR_KEYWORD) != null;
|
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.lexer.KtTokens.*
|
import org.jetbrains.kotlin.lexer.KtTokens.*
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.children
|
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.leaves
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.parents
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.siblings
|
|
||||||
|
|
||||||
private val QUALIFIED_OPERATION = TokenSet.create(DOT, SAFE_ACCESS)
|
private val QUALIFIED_OPERATION = TokenSet.create(DOT, SAFE_ACCESS)
|
||||||
private val QUALIFIED_EXPRESSIONS = TokenSet.create(KtNodeTypes.DOT_QUALIFIED_EXPRESSION, KtNodeTypes.SAFE_ACCESS_EXPRESSION)
|
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")
|
strategy("Expression body")
|
||||||
.within(KtNodeTypes.FUN)
|
.within(KtNodeTypes.FUN)
|
||||||
.forElement {
|
.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),
|
.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
|
||||||
@@ -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");
|
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")
|
@TestMetadata("CommentInFunctionLiteral.after.kt")
|
||||||
public void testCommentInFunctionLiteral() throws Exception {
|
public void testCommentInFunctionLiteral() throws Exception {
|
||||||
runTest("idea/testData/formatter/CommentInFunctionLiteral.after.kt");
|
runTest("idea/testData/formatter/CommentInFunctionLiteral.after.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user