[PSI, FE, PSI2IR] Use labels for referencing specific receiver

This commit is contained in:
Anastasiya Shadrina
2021-01-18 19:47:07 +07:00
committed by TeamCityServer
parent aaabf5e1ca
commit 1bcaeabd84
54 changed files with 744 additions and 134 deletions
@@ -40,6 +40,7 @@ public interface KtNodeTypes {
IElementType SECONDARY_CONSTRUCTOR = KtStubElementTypes.SECONDARY_CONSTRUCTOR;
IElementType PRIMARY_CONSTRUCTOR = KtStubElementTypes.PRIMARY_CONSTRUCTOR;
IElementType CONTEXT_RECEIVER = KtStubElementTypes.CONTEXT_RECEIVER;
IElementType CONTEXT_RECEIVER_LIST = KtStubElementTypes.CONTEXT_RECEIVER_LIST;
IElementType TYPE_PARAMETER_LIST = KtStubElementTypes.TYPE_PARAMETER_LIST;
IElementType TYPE_PARAMETER = KtStubElementTypes.TYPE_PARAMETER;
@@ -597,7 +597,7 @@ public class KotlinExpressionParsing extends AbstractKotlinParsing {
}
}
private boolean isAtLabelDefinitionOrMissingIdentifier() {
boolean isAtLabelDefinitionOrMissingIdentifier() {
return (at(IDENTIFIER) && myBuilder.rawLookup(1) == AT) || at(AT);
}
@@ -1690,7 +1690,7 @@ public class KotlinExpressionParsing extends AbstractKotlinParsing {
/*
* IDENTIFIER "@"
*/
private void parseLabelDefinition() {
void parseLabelDefinition() {
PsiBuilder.Marker labelWrap = mark();
PsiBuilder.Marker mark = mark();
@@ -33,7 +33,7 @@ public interface KtCallableDeclaration extends KtNamedDeclaration, KtTypeParamet
KtTypeReference getReceiverTypeReference();
@NotNull
List<KtTypeReference> getContextReceiverTypeReferences();
List<KtContextReceiver> getContextReceivers();
@Nullable
KtTypeReference getTypeReference();
@@ -188,17 +188,17 @@ abstract class KtClassOrObject :
return parts.joinToString(separator = ".")
}
override fun getContextReceiverTypeReferences(): List<KtTypeReference> {
override fun getContextReceivers(): List<KtContextReceiver> {
val stub = stub
if (stub != null) {
return getStubOrPsiChildrenAsList(KtStubElementTypes.TYPE_REFERENCE)
return getStubOrPsiChildrenAsList(KtStubElementTypes.CONTEXT_RECEIVER)
}
var node = node.firstChildNode
while (node != null) {
val tt = node.elementType
if (tt === KtNodeTypes.CONTEXT_RECEIVER) {
val contextReceiver = node.psi as KtContextReceiver
return contextReceiver.typeReferences()
if (tt === KtNodeTypes.CONTEXT_RECEIVER_LIST) {
val contextReceiver = node.psi as KtContextReceiverList
return contextReceiver.contextReceivers()
}
node = node.treeNext
}
@@ -40,7 +40,7 @@ abstract class KtConstructor<T : KtConstructor<T>> : KtDeclarationStub<KotlinPla
override fun getReceiverTypeReference() = null
override fun getContextReceiverTypeReferences(): List<KtTypeReference> = emptyList()
override fun getContextReceivers(): List<KtContextReceiver> = emptyList()
override fun getTypeReference() = null
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.psi
import com.intellij.lang.ASTNode
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
@@ -14,9 +15,14 @@ class KtContextReceiver : KtElementImplStub<KotlinPlaceHolderStub<KtContextRecei
constructor(node: ASTNode) : super(node)
constructor(stub: KotlinPlaceHolderStub<KtContextReceiver>) : super(stub, KtStubElementTypes.CONTEXT_RECEIVER)
override fun <R : Any?, D : Any?> accept(visitor: KtVisitor<R, D>, data: D): R {
return visitor.visitContextReceiver(this, data)
}
fun targetLabel(): KtSimpleNameExpression? =
findChildByType<KtContainerNode?>(KtNodeTypes.LABEL_QUALIFIER)
?.findChildByType(KtNodeTypes.LABEL)
fun typeReferences(): List<KtTypeReference> = findChildrenByType(KtNodeTypes.TYPE_REFERENCE)
fun labelName(): String? = targetLabel()?.getReferencedName()
fun labelNameAsName(): Name? = targetLabel()?.getReferencedNameAsName()
fun typeReference(): KtTypeReference? = findChildByType(KtNodeTypes.TYPE_REFERENCE)
fun name(): String? = labelName() ?: typeReference()?.nameForReceiverLabel()
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.psi
import com.intellij.lang.ASTNode
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
class KtContextReceiverList : KtElementImplStub<KotlinPlaceHolderStub<KtContextReceiverList>> {
constructor(node: ASTNode) : super(node)
constructor(stub: KotlinPlaceHolderStub<KtContextReceiverList>) : super(stub, KtStubElementTypes.CONTEXT_RECEIVER_LIST)
override fun <R : Any?, D : Any?> accept(visitor: KtVisitor<R, D>, data: D): R {
return visitor.visitContextReceiverList(this, data)
}
fun contextReceivers(): List<KtContextReceiver> = findChildrenByType(KtNodeTypes.CONTEXT_RECEIVER)
fun typeReferences(): List<KtTypeReference> = contextReceivers().mapNotNull { it.typeReference() }
}
@@ -77,7 +77,7 @@ public class KtDestructuringDeclarationEntry extends KtNamedDeclarationNotStubbe
@NotNull
@Override
public List<KtTypeReference> getContextReceiverTypeReferences() {
public List<KtContextReceiver> getContextReceivers() {
return Collections.emptyList();
}
@@ -64,7 +64,7 @@ public abstract class KtFunctionNotStubbed extends KtTypeParameterListOwnerNotSt
@NotNull
@Override
public List<KtTypeReference> getContextReceiverTypeReferences() {
public List<KtContextReceiver> getContextReceivers() {
return Collections.emptyList();
}
@@ -185,12 +185,12 @@ public class KtNamedFunction extends KtTypeParameterListOwnerStub<KotlinFunction
@NotNull
@Override
public List<KtTypeReference> getContextReceiverTypeReferences() {
public List<KtContextReceiver> getContextReceivers() {
KotlinFunctionStub stub = getStub();
if (stub != null) {
List<KtContextReceiver> childContextReceivers = getStubOrPsiChildrenAsList(KtStubElementTypes.CONTEXT_RECEIVER);
List<KtContextReceiverList> childContextReceivers = getStubOrPsiChildrenAsList(KtStubElementTypes.CONTEXT_RECEIVER_LIST);
if (!childContextReceivers.isEmpty()) {
return childContextReceivers.get(0).typeReferences();
return childContextReceivers.get(0).contextReceivers();
}
else {
return Collections.emptyList();
@@ -200,13 +200,13 @@ public class KtNamedFunction extends KtTypeParameterListOwnerStub<KotlinFunction
}
@NotNull
private List<KtTypeReference> getContextReceiverTypeRefsByTree() {
private List<KtContextReceiver> getContextReceiverTypeRefsByTree() {
PsiElement child = getFirstChild();
while (child != null) {
IElementType tt = child.getNode().getElementType();
if (tt == KtTokens.LPAR || tt == KtTokens.COLON) break;
if (child instanceof KtContextReceiver) {
return ((KtContextReceiver) child).typeReferences();
if (child instanceof KtContextReceiverList) {
return ((KtContextReceiverList) child).contextReceivers();
}
child = child.getNextSibling();
}
@@ -195,7 +195,7 @@ public class KtParameter extends KtNamedDeclarationStub<KotlinParameterStub> imp
@NotNull
@Override
public List<KtTypeReference> getContextReceiverTypeReferences() {
public List<KtContextReceiver> getContextReceivers() {
return Collections.emptyList();
}
@@ -116,12 +116,12 @@ public class KtProperty extends KtTypeParameterListOwnerStub<KotlinPropertyStub>
@NotNull
@Override
public List<KtTypeReference> getContextReceiverTypeReferences() {
public List<KtContextReceiver> getContextReceivers() {
KotlinPropertyStub stub = getStub();
if (stub != null) {
KtContextReceiver contextReceiver = getStubOrPsiChild(KtStubElementTypes.CONTEXT_RECEIVER);
KtContextReceiverList contextReceiver = getStubOrPsiChild(KtStubElementTypes.CONTEXT_RECEIVER_LIST);
if (contextReceiver != null) {
return contextReceiver.typeReferences();
return contextReceiver.contextReceivers();
} else {
return Collections.emptyList();
}
@@ -130,16 +130,16 @@ public class KtProperty extends KtTypeParameterListOwnerStub<KotlinPropertyStub>
}
@NotNull
private List<KtTypeReference> getContextReceiverTypeRefsByTree() {
private List<KtContextReceiver> getContextReceiverTypeRefsByTree() {
ASTNode node = getNode().getFirstChildNode();
while (node != null) {
IElementType tt = node.getElementType();
if (tt == KtTokens.COLON) {
break;
}
if (tt == KtNodeTypes.CONTEXT_RECEIVER) {
KtContextReceiver contextReceiver = (KtContextReceiver) node.getPsi();
return contextReceiver.typeReferences();
if (tt == KtNodeTypes.CONTEXT_RECEIVER_LIST) {
KtContextReceiverList contextReceiver = (KtContextReceiverList) node.getPsi();
return contextReceiver.contextReceivers();
}
node = node.getTreeNext();
}
@@ -60,7 +60,7 @@ public interface KtPureClassOrObject extends KtPureElement, KtDeclarationContain
@NotNull
@ReadOnly
List<KtTypeReference> getContextReceiverTypeReferences();
List<KtContextReceiver> getContextReceivers();
@Nullable
KtClassBody getBody();
@@ -55,4 +55,6 @@ class KtTypeReference : KtModifierListOwnerStub<KotlinPlaceHolderStub<KtTypeRefe
fun hasParentheses(): Boolean {
return findChildByType<PsiElement>(KtTokens.LPAR) != null && findChildByType<PsiElement>(KtTokens.RPAR) != null
}
fun nameForReceiverLabel() = (typeElement as? KtUserType)?.referencedName
}
@@ -158,8 +158,8 @@ public class KtVisitor<R, D> extends PsiElementVisitor {
return visitSuperTypeListEntry(specifier, data);
}
public R visitContextReceiver(@NotNull KtContextReceiver contextReceiver, D data) {
return visitKtElement(contextReceiver, data);
public R visitContextReceiverList(@NotNull KtContextReceiverList contextReceiverList, D data) {
return visitKtElement(contextReceiverList, data);
}
public R visitConstructorDelegationCall(@NotNull KtConstructorDelegationCall call, D data) {
@@ -145,6 +145,8 @@ public interface KtStubElementTypes {
KtPlaceHolderStubElementType<KtContextReceiver> CONTEXT_RECEIVER =
new KtPlaceHolderStubElementType<>("CONTEXT_RECEIVER", KtContextReceiver.class);
KtPlaceHolderStubElementType<KtContextReceiverList> CONTEXT_RECEIVER_LIST =
new KtPlaceHolderStubElementType<>("CONTEXT_RECEIVER_LIST", KtContextReceiverList.class);
KtConstantExpressionElementType NULL = new KtConstantExpressionElementType("NULL");
KtConstantExpressionElementType BOOLEAN_CONSTANT = new KtConstantExpressionElementType("BOOLEAN_CONSTANT");