Support trailing comma

^KT-34743 Fixed
This commit is contained in:
victor.petukhov
2019-10-07 13:21:25 +03:00
parent e638b9fd12
commit 62d204f4d6
83 changed files with 4717 additions and 96 deletions
@@ -933,6 +933,9 @@ public class KotlinExpressionParsing extends AbstractKotlinParsing {
parseWhenCondition();
if (!at(COMMA)) break;
advance(); // COMMA
if (at(ARROW)) {
break;
}
}
expect(ARROW, "Expecting '->'", WHEN_CONDITION_RECOVERY_SET);
@@ -1032,22 +1035,13 @@ public class KotlinExpressionParsing extends AbstractKotlinParsing {
}
private void parseInnerExpressions(String missingElementErrorMessage) {
boolean firstElement = true;
while (true) {
if (at(COMMA)) errorAndAdvance(missingElementErrorMessage);
if (at(RBRACKET)) {
if (firstElement) {
break;
}
else {
error(missingElementErrorMessage);
}
break;
}
parseExpression();
firstElement = false;
if (!at(COMMA)) break;
advance(); // COMMA
}
@@ -1202,6 +1196,9 @@ public class KotlinExpressionParsing extends AbstractKotlinParsing {
PsiBuilder.Marker parameterList = mark();
while (!eof()) {
if (at(ARROW)) {
break;
}
PsiBuilder.Marker parameter = mark();
if (at(COLON)) {
@@ -1521,6 +1518,9 @@ public class KotlinExpressionParsing extends AbstractKotlinParsing {
expect(LPAR, "Expecting '('", recoverySet);
if (!atSet(recoverySet)) {
myKotlinParsing.parseValueParameter(/*typeRequired = */ true);
if (at(COMMA)) {
advance(); // trailing comma
}
expect(RPAR, "Expecting ')'", recoverySet);
}
else {
@@ -1813,7 +1813,6 @@ public class KotlinExpressionParsing extends AbstractKotlinParsing {
}
advance(); // COMMA
if (at(RPAR)) {
error("Expecting an argument");
break;
}
}
@@ -1466,7 +1466,7 @@ public class KotlinParsing extends AbstractKotlinParsing {
if (at(COMMA)) {
errorAndAdvance("Expecting a name");
}
else if (at(RPAR)) {
else if (at(RPAR)) { // For declaration similar to `val () = somethingCall()`
error("Expecting a name");
break;
}
@@ -1484,6 +1484,7 @@ public class KotlinParsing extends AbstractKotlinParsing {
if (!at(COMMA)) break;
advance(); // COMMA
if (at(RPAR)) break;
}
}
@@ -1549,10 +1550,13 @@ public class KotlinParsing extends AbstractKotlinParsing {
expect(IDENTIFIER, "Expecting parameter name", TokenSet.create(RPAR, COLON, LBRACE, EQ));
if (at(COLON)) {
advance(); // COLON
advance(); // COLON
parseTypeRef();
}
setterParameter.done(VALUE_PARAMETER);
if (at(COMMA)) {
advance(); // COMMA
}
parameterList.done(VALUE_PARAMETER_LIST);
}
if (!at(RPAR)) {
@@ -1896,6 +1900,9 @@ public class KotlinParsing extends AbstractKotlinParsing {
if (!at(COMMA)) break;
advance(); // COMMA
if (at(GT)) {
break;
}
}
expect(GT, "Missing '>'", recoverySet);
@@ -2263,6 +2270,9 @@ public class KotlinParsing extends AbstractKotlinParsing {
projection.done(TYPE_PROJECTION);
if (!at(COMMA)) break;
advance(); // COMMA
if (at(GT)) {
break;
}
}
boolean atGT = at(GT);
@@ -2325,7 +2335,6 @@ public class KotlinParsing extends AbstractKotlinParsing {
errorAndAdvance("Expecting a parameter declaration");
}
else if (at(RPAR)) {
error("Expecting a parameter declaration");
break;
}
@@ -25,6 +25,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.KtNodeTypes;
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt;
import java.util.Collections;
import java.util.List;
@@ -75,4 +76,8 @@ public class KtArrayAccessExpression extends KtExpressionImpl implements KtRefer
public PsiElement getRightBracket() {
return getIndicesNode().findChildByType(KtTokens.RBRACKET);
}
public PsiElement getTrailingComma() {
return KtPsiUtilKt.getTrailingCommaByClosingElement(getRightBracket());
}
}
@@ -20,6 +20,7 @@ import com.intellij.lang.ASTNode
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.psiUtil.getTrailingCommaByClosingElement
class KtCollectionLiteralExpression(node: ASTNode) : KtExpressionImpl(node), KtReferenceExpression {
override fun <R, D> accept(visitor: KtVisitor<R, D>, data: D): R {
@@ -32,6 +33,9 @@ class KtCollectionLiteralExpression(node: ASTNode) : KtExpressionImpl(node), KtR
val rightBracket: PsiElement?
get() = findChildByType(KtTokens.RBRACKET)
val trailingComma: PsiElement?
get() = getTrailingCommaByClosingElement(rightBracket)
fun getInnerExpressions(): List<KtExpression> {
return PsiTreeUtil.getChildrenOfTypeAsList(this, KtExpression::class.java)
}
@@ -24,6 +24,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.KtNodeTypes;
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt;
import java.util.List;
@@ -78,4 +79,9 @@ public class KtDestructuringDeclaration extends KtDeclarationImpl implements KtV
public PsiElement getLPar() {
return findChildByType(KtTokens.LPAR);
}
@Nullable
public PsiElement getTrailingComma() {
return KtPsiUtilKt.getTrailingCommaByClosingElement(getRPar());
}
}
@@ -21,6 +21,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
@@ -94,4 +95,14 @@ public class KtParameterList extends KtElementImplStub<KotlinPlaceHolderStub<KtP
public PsiElement getFirstComma() {
return findChildByType(KtTokens.COMMA);
}
@Nullable
public PsiElement getTrailingComma() {
PsiElement parentElement = getParent();
if (parentElement instanceof KtFunctionLiteral || parentElement instanceof KtPropertyAccessor) {
return KtPsiUtilKt.getTrailingCommaByElementsList(this);
} else {
return KtPsiUtilKt.getTrailingCommaByClosingElement(getRightParenthesis());
}
}
}
@@ -17,8 +17,11 @@
package org.jetbrains.kotlin.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
@@ -47,4 +50,9 @@ public class KtTypeArgumentList extends KtElementImplStub<KotlinPlaceHolderStub<
public KtTypeProjection addArgument(@NotNull KtTypeProjection typeArgument) {
return EditCommaSeparatedListHelper.INSTANCE.addItem(this, getArguments(), typeArgument, KtTokens.LT);
}
@Nullable
public PsiElement getTrailingComma() {
return KtPsiUtilKt.getTrailingCommaByClosingElement(findChildByType(KtTokens.GT));
}
}
@@ -17,8 +17,11 @@
package org.jetbrains.kotlin.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
@@ -47,4 +50,9 @@ public class KtTypeParameterList extends KtElementImplStub<KotlinPlaceHolderStub
public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
return visitor.visitTypeParameterList(this, data);
}
@Nullable
public PsiElement getTrailingComma() {
return KtPsiUtilKt.getTrailingCommaByClosingElement(findChildByType(KtTokens.GT));
}
}
@@ -21,6 +21,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
@@ -78,4 +79,8 @@ public class KtValueArgumentList extends KtElementImplStub<KotlinPlaceHolderStub
public void removeArgument(int index) {
removeArgument(getArguments().get(index));
}
public PsiElement getTrailingComma() {
return KtPsiUtilKt.getTrailingCommaByClosingElement(getRightParenthesis());
}
}
@@ -21,6 +21,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt;
public class KtWhenEntry extends KtElementImpl {
public KtWhenEntry(@NotNull ASTNode node) {
@@ -50,4 +51,8 @@ public class KtWhenEntry extends KtElementImpl {
public KtWhenCondition[] getConditions() {
return findChildrenByClass(KtWhenCondition.class);
}
public PsiElement getTrailingComma() {
return KtPsiUtilKt.getTrailingCommaByClosingElement(findChildByType(KtTokens.ARROW));
}
}
@@ -672,3 +672,14 @@ fun KtExpression.topParenthesizedParentOrMe(): KtExpression {
}
return result
}
fun getTrailingCommaByClosingElement(closingElement: PsiElement?): PsiElement? {
val elementBeforeClosingElement =
closingElement?.getPrevSiblingIgnoringWhitespaceAndComments() ?: return null
return elementBeforeClosingElement.run { if (node.elementType == KtTokens.COMMA) this else null }
}
fun getTrailingCommaByElementsList(elementList: PsiElement?): PsiElement? {
return elementList?.lastChild?.run { if (node.elementType == KtTokens.COMMA) this else null }
}