Changed parsing of object and enum entries to drop "KtObjectDeclarationName"
This commit is contained in:
@@ -33,7 +33,6 @@ public interface KtNodeTypes {
|
||||
|
||||
KtNodeType TYPEDEF = new KtNodeType("TYPEDEF", KtTypedef.class);
|
||||
IElementType OBJECT_DECLARATION = KtStubElementTypes.OBJECT_DECLARATION;
|
||||
KtNodeType OBJECT_DECLARATION_NAME = new KtNodeType("OBJECT_DECLARATION_NAME", KtObjectDeclarationName.class);
|
||||
|
||||
IElementType ENUM_ENTRY = KtStubElementTypes.ENUM_ENTRY;
|
||||
IElementType ANONYMOUS_INITIALIZER = KtStubElementTypes.ANONYMOUS_INITIALIZER;
|
||||
|
||||
@@ -34,7 +34,6 @@ import java.util.Set;
|
||||
import static org.jetbrains.kotlin.KtNodeTypes.*;
|
||||
import static org.jetbrains.kotlin.lexer.KtTokens.*;
|
||||
import static org.jetbrains.kotlin.parsing.JetParsing.AnnotationParsingMode.DEFAULT;
|
||||
import static org.jetbrains.kotlin.parsing.JetParsing.DeclarationParsingMode.LOCAL;
|
||||
|
||||
public class JetExpressionParsing extends AbstractJetParsing {
|
||||
private static final TokenSet WHEN_CONDITION_RECOVERY_SET = TokenSet.create(RBRACE, IN_KEYWORD, NOT_IN, IS_KEYWORD, NOT_IS, ELSE_KEYWORD);
|
||||
@@ -1240,7 +1239,7 @@ public class JetExpressionParsing extends AbstractJetParsing {
|
||||
IElementType keywordToken = tt();
|
||||
IElementType declType = null;
|
||||
if (keywordToken == CLASS_KEYWORD || keywordToken == INTERFACE_KEYWORD) {
|
||||
declType = myJetParsing.parseClass(isEnum, LOCAL);
|
||||
declType = myJetParsing.parseClass(isEnum);
|
||||
}
|
||||
else if (keywordToken == FUN_KEYWORD) {
|
||||
declType = myJetParsing.parseFunction();
|
||||
@@ -1264,7 +1263,7 @@ public class JetExpressionParsing extends AbstractJetParsing {
|
||||
return null;
|
||||
}
|
||||
|
||||
myJetParsing.parseObject(NameParsingMode.REQUIRED, true, LOCAL);
|
||||
myJetParsing.parseObject(NameParsingMode.REQUIRED, true);
|
||||
declType = OBJECT_DECLARATION;
|
||||
}
|
||||
return declType;
|
||||
@@ -1756,7 +1755,7 @@ public class JetExpressionParsing extends AbstractJetParsing {
|
||||
public void parseObjectLiteral() {
|
||||
PsiBuilder.Marker literal = mark();
|
||||
PsiBuilder.Marker declaration = mark();
|
||||
myJetParsing.parseObject(NameParsingMode.PROHIBITED, false, LOCAL); // Body is not optional because of foo(object : A, B)
|
||||
myJetParsing.parseObject(NameParsingMode.PROHIBITED, false); // Body is not optional because of foo(object : A, B)
|
||||
declaration.done(OBJECT_DECLARATION);
|
||||
literal.done(OBJECT_LITERAL);
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ import java.util.Map;
|
||||
import static org.jetbrains.kotlin.KtNodeTypes.*;
|
||||
import static org.jetbrains.kotlin.lexer.KtTokens.*;
|
||||
import static org.jetbrains.kotlin.parsing.JetParsing.AnnotationParsingMode.*;
|
||||
import static org.jetbrains.kotlin.parsing.JetParsing.DeclarationParsingMode.*;
|
||||
|
||||
public class JetParsing extends AbstractJetParsing {
|
||||
private static final Logger LOG = Logger.getInstance(JetParsing.class);
|
||||
@@ -400,7 +399,7 @@ public class JetParsing extends AbstractJetParsing {
|
||||
// }
|
||||
// else
|
||||
if (keywordToken == CLASS_KEYWORD || keywordToken == INTERFACE_KEYWORD) {
|
||||
declType = parseClass(detector.isEnumDetected(), TOP_LEVEL);
|
||||
declType = parseClass(detector.isEnumDetected());
|
||||
}
|
||||
else if (keywordToken == FUN_KEYWORD) {
|
||||
declType = parseFunction();
|
||||
@@ -412,7 +411,7 @@ public class JetParsing extends AbstractJetParsing {
|
||||
declType = parseTypeAlias();
|
||||
}
|
||||
else if (keywordToken == OBJECT_KEYWORD) {
|
||||
parseObject(NameParsingMode.REQUIRED, true, TOP_LEVEL);
|
||||
parseObject(NameParsingMode.REQUIRED, true);
|
||||
declType = OBJECT_DECLARATION;
|
||||
}
|
||||
else if (at(LBRACE)) {
|
||||
@@ -768,8 +767,7 @@ public class JetParsing extends AbstractJetParsing {
|
||||
boolean object,
|
||||
NameParsingMode nameParsingMode,
|
||||
boolean optionalBody,
|
||||
boolean enumClass,
|
||||
DeclarationParsingMode declarationParsingMode
|
||||
boolean enumClass
|
||||
) {
|
||||
if (object) {
|
||||
assert _at(OBJECT_KEYWORD);
|
||||
@@ -780,9 +778,7 @@ public class JetParsing extends AbstractJetParsing {
|
||||
advance(); // CLASS_KEYWORD, INTERFACE_KEYWORD or OBJECT_KEYWORD
|
||||
|
||||
if (nameParsingMode == NameParsingMode.REQUIRED) {
|
||||
OptionalMarker marker = new OptionalMarker(object);
|
||||
expect(IDENTIFIER, "Name expected", CLASS_NAME_RECOVERY_SET);
|
||||
marker.done(OBJECT_DECLARATION_NAME);
|
||||
}
|
||||
else {
|
||||
assert object : "Must be an object to be nameless";
|
||||
@@ -792,9 +788,7 @@ public class JetParsing extends AbstractJetParsing {
|
||||
}
|
||||
else {
|
||||
assert nameParsingMode == NameParsingMode.ALLOWED;
|
||||
PsiBuilder.Marker marker = mark();
|
||||
advance();
|
||||
marker.done(OBJECT_DECLARATION_NAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -868,12 +862,12 @@ public class JetParsing extends AbstractJetParsing {
|
||||
return object ? OBJECT_DECLARATION : CLASS;
|
||||
}
|
||||
|
||||
IElementType parseClass(boolean enumClass, DeclarationParsingMode declarationParsingMode) {
|
||||
return parseClassOrObject(false, NameParsingMode.REQUIRED, true, enumClass, declarationParsingMode);
|
||||
IElementType parseClass(boolean enumClass) {
|
||||
return parseClassOrObject(false, NameParsingMode.REQUIRED, true, enumClass);
|
||||
}
|
||||
|
||||
void parseObject(NameParsingMode nameParsingMode, boolean optionalBody, DeclarationParsingMode declarationParsingMode) {
|
||||
parseClassOrObject(true, nameParsingMode, optionalBody, false, declarationParsingMode);
|
||||
void parseObject(NameParsingMode nameParsingMode, boolean optionalBody) {
|
||||
parseClassOrObject(true, nameParsingMode, optionalBody, false);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -947,9 +941,7 @@ public class JetParsing extends AbstractJetParsing {
|
||||
parseModifierList(DEFAULT, TokenSet.create(COMMA, SEMICOLON, RBRACE));
|
||||
|
||||
if (!atSet(SOFT_KEYWORDS_AT_MEMBER_START) && at(IDENTIFIER)) {
|
||||
PsiBuilder.Marker nameAsDeclaration = mark();
|
||||
advance(); // IDENTIFIER
|
||||
nameAsDeclaration.done(OBJECT_DECLARATION_NAME);
|
||||
|
||||
if (at(LPAR)) {
|
||||
// Arguments should be parsed here
|
||||
@@ -1068,7 +1060,7 @@ public class JetParsing extends AbstractJetParsing {
|
||||
IElementType keywordToken = tt();
|
||||
IElementType declType = null;
|
||||
if (keywordToken == CLASS_KEYWORD || keywordToken == INTERFACE_KEYWORD) {
|
||||
declType = parseClass(isEnum, CLASS_MEMBER);
|
||||
declType = parseClass(isEnum);
|
||||
}
|
||||
else if (keywordToken == FUN_KEYWORD) {
|
||||
declType = parseFunction();
|
||||
@@ -1080,7 +1072,7 @@ public class JetParsing extends AbstractJetParsing {
|
||||
declType = parseTypeAlias();
|
||||
}
|
||||
else if (keywordToken == OBJECT_KEYWORD) {
|
||||
parseObject(isDefault ? NameParsingMode.ALLOWED : NameParsingMode.REQUIRED, true, CLASS_MEMBER);
|
||||
parseObject(isDefault ? NameParsingMode.ALLOWED : NameParsingMode.REQUIRED, true);
|
||||
declType = OBJECT_DECLARATION;
|
||||
}
|
||||
else if (at(INIT_KEYWORD)) {
|
||||
|
||||
@@ -63,9 +63,6 @@ abstract public class KtClassOrObject :
|
||||
|
||||
public fun getAnonymousInitializers(): List<KtClassInitializer> = getBody()?.anonymousInitializers.orEmpty()
|
||||
|
||||
public fun getNameAsDeclaration(): KtObjectDeclarationName? =
|
||||
findChildByType<PsiElement>(KtNodeTypes.OBJECT_DECLARATION_NAME) as KtObjectDeclarationName?
|
||||
|
||||
public fun getBody(): KtClassBody? = getStubOrPsiChild(KtStubElementTypes.CLASS_BODY)
|
||||
|
||||
public fun getOrCreateBody(): KtClassBody = getBody() ?: add(KtPsiFactory(this).createEmptyClassBody()) as KtClassBody
|
||||
|
||||
@@ -40,29 +40,6 @@ public class KtEnumEntry extends KtClass {
|
||||
super(stub);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
KotlinClassOrObjectStub<? extends KtClassOrObject> classStub = getStub();
|
||||
if (classStub != null) {
|
||||
return classStub.getName();
|
||||
}
|
||||
|
||||
KtObjectDeclarationName nameAsDeclaration = getNameAsDeclaration();
|
||||
return nameAsDeclaration == null ? "<Anonymous>" : nameAsDeclaration.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiElement getNameIdentifier() {
|
||||
KtObjectDeclarationName nameAsDeclaration = getNameAsDeclaration();
|
||||
return nameAsDeclaration == null ? null : nameAsDeclaration.getNameIdentifier();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
|
||||
KtObjectDeclarationName nameAsDeclaration = getNameAsDeclaration();
|
||||
return nameAsDeclaration == null ? null : nameAsDeclaration.setName(name);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<KtDelegationSpecifier> getDelegationSpecifiers() {
|
||||
|
||||
@@ -32,29 +32,26 @@ public class KtObjectDeclaration : KtClassOrObject {
|
||||
get() = stub as? KotlinObjectStub
|
||||
|
||||
override fun getName(): String? {
|
||||
_stub?.name?.let { return it }
|
||||
super.getName()?.let { return it }
|
||||
|
||||
val nameAsDeclaration = getNameAsDeclaration()
|
||||
if (nameAsDeclaration == null && isCompanion()) {
|
||||
if (isCompanion() && !isTopLevel()) {
|
||||
//NOTE: a hack in PSI that simplifies writing frontend code
|
||||
return SpecialNames.DEFAULT_NAME_FOR_COMPANION_OBJECT.toString()
|
||||
}
|
||||
return nameAsDeclaration?.name
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
override fun getNameIdentifier(): PsiElement? = getNameAsDeclaration()?.nameIdentifier
|
||||
|
||||
override fun setName(@NonNls name: String): PsiElement {
|
||||
val declarationName = getNameAsDeclaration()
|
||||
if (declarationName == null) {
|
||||
if (nameIdentifier == null) {
|
||||
val psiFactory = KtPsiFactory(project)
|
||||
val result = addAfter(psiFactory.createObjectDeclarationName(name), getObjectKeyword())
|
||||
val result = addAfter(psiFactory.createIdentifier(name), getObjectKeyword())
|
||||
addAfter(psiFactory.createWhiteSpace(), getObjectKeyword())
|
||||
|
||||
return result
|
||||
}
|
||||
else {
|
||||
return declarationName.setName(name)
|
||||
return super.setName(name)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.psi;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.search.SearchScope;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.lexer.KtTokens;
|
||||
|
||||
import static org.jetbrains.kotlin.psi.KtPsiFactoryKt.KtPsiFactory;
|
||||
|
||||
public class KtObjectDeclarationName extends KtExpressionImpl {
|
||||
public KtObjectDeclarationName(@NotNull ASTNode node) {
|
||||
super(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public String getName() {
|
||||
PsiElement identifier = getNameIdentifier();
|
||||
if (identifier != null) {
|
||||
String text = identifier.getText();
|
||||
return text != null ? KtPsiUtil.unquoteIdentifier(text) : null;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public PsiElement getNameIdentifier() {
|
||||
return findChildByType(KtTokens.IDENTIFIER);
|
||||
}
|
||||
|
||||
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
|
||||
return getNameIdentifier().replace(KtPsiFactory(this).createNameIdentifier(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTextOffset() {
|
||||
PsiElement identifier = getNameIdentifier();
|
||||
return identifier != null ? identifier.getTextRange().getStartOffset() : getTextRange().getStartOffset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
|
||||
return visitor.visitObjectDeclarationName(this, data);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public SearchScope getUseScope() {
|
||||
KtObjectDeclaration objectDeclaration = PsiTreeUtil.getParentOfType(this, KtObjectDeclaration.class);
|
||||
return objectDeclaration != null ? objectDeclaration.getUseScope() : super.getUseScope();
|
||||
}
|
||||
}
|
||||
@@ -201,10 +201,6 @@ public class KtPsiFactory(private val project: Project) {
|
||||
return (createExpression("0 $name 0") as KtBinaryExpression).getOperationReference()
|
||||
}
|
||||
|
||||
public fun createObjectDeclarationName(name: String): KtObjectDeclarationName {
|
||||
return createDeclaration<KtObjectDeclaration>("object $name").getNameAsDeclaration()!!
|
||||
}
|
||||
|
||||
public fun createIdentifier(name: String): PsiElement {
|
||||
return createSimpleName(name).getIdentifier()!!
|
||||
}
|
||||
|
||||
@@ -414,10 +414,6 @@ public class KtVisitor<R, D> extends PsiElementVisitor {
|
||||
return visitJetElement(condition, data);
|
||||
}
|
||||
|
||||
public R visitObjectDeclarationName(@NotNull KtObjectDeclarationName declarationName, D data) {
|
||||
return visitExpression(declarationName, data);
|
||||
}
|
||||
|
||||
public R visitStringTemplateEntry(@NotNull KtStringTemplateEntry entry, D data) {
|
||||
return visitJetElement(entry, data);
|
||||
}
|
||||
|
||||
@@ -401,10 +401,6 @@ public class KtVisitorVoid extends KtVisitor<Void, Void> {
|
||||
super.visitObjectDeclaration(declaration, null);
|
||||
}
|
||||
|
||||
public void visitObjectDeclarationName(@NotNull KtObjectDeclarationName declarationName) {
|
||||
super.visitObjectDeclarationName(declarationName, null);
|
||||
}
|
||||
|
||||
public void visitStringTemplateEntry(@NotNull KtStringTemplateEntry entry) {
|
||||
super.visitStringTemplateEntry(entry, null);
|
||||
}
|
||||
@@ -1006,12 +1002,6 @@ public class KtVisitorVoid extends KtVisitor<Void, Void> {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Void visitObjectDeclarationName(@NotNull KtObjectDeclarationName declarationName, Void data) {
|
||||
visitObjectDeclarationName(declarationName);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Void visitStringTemplateEntry(@NotNull KtStringTemplateEntry entry, Void data) {
|
||||
visitStringTemplateEntry(entry);
|
||||
|
||||
@@ -398,10 +398,6 @@ public class KtVisitorVoidWithParameter<P> extends KtVisitor<Void, P> {
|
||||
super.visitObjectDeclaration(declaration, data);
|
||||
}
|
||||
|
||||
public void visitObjectDeclarationNameVoid(@NotNull KtObjectDeclarationName declarationName, P data) {
|
||||
super.visitObjectDeclarationName(declarationName, data);
|
||||
}
|
||||
|
||||
public void visitStringTemplateEntryVoid(@NotNull KtStringTemplateEntry entry, P data) {
|
||||
super.visitStringTemplateEntry(entry, data);
|
||||
}
|
||||
@@ -981,12 +977,6 @@ public class KtVisitorVoidWithParameter<P> extends KtVisitor<Void, P> {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Void visitObjectDeclarationName(@NotNull KtObjectDeclarationName declarationName, P data) {
|
||||
visitObjectDeclarationNameVoid(declarationName, data);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Void visitStringTemplateEntry(@NotNull KtStringTemplateEntry entry, P data) {
|
||||
visitStringTemplateEntryVoid(entry, data);
|
||||
|
||||
Reference in New Issue
Block a user