J2K KtQualifiedExpression and subclasses: convert
Also merge KtQualifiedExpressionImpl into KtQualifiedExpression and remove calls from subclasses
This commit is contained in:
@@ -14,91 +14,66 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.psi;
|
||||
package org.jetbrains.kotlin.psi
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.lexer.KtToken;
|
||||
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
|
||||
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
|
||||
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
|
||||
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes.INSIDE_DIRECTIVE_EXPRESSIONS
|
||||
|
||||
import static org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes.INSIDE_DIRECTIVE_EXPRESSIONS;
|
||||
class KtDotQualifiedExpression : KtExpressionImplStub<KotlinPlaceHolderStub<KtDotQualifiedExpression>>, KtQualifiedExpression {
|
||||
constructor(node: ASTNode) : super(node)
|
||||
|
||||
public class KtDotQualifiedExpression extends KtExpressionImplStub<KotlinPlaceHolderStub<KtDotQualifiedExpression>>
|
||||
implements KtQualifiedExpression {
|
||||
constructor(stub: KotlinPlaceHolderStub<KtDotQualifiedExpression>) : super(stub, KtStubElementTypes.DOT_QUALIFIED_EXPRESSION)
|
||||
|
||||
private static final Logger LOG = Logger.getInstance(KtDotQualifiedExpression.class);
|
||||
|
||||
public KtDotQualifiedExpression(@NotNull ASTNode node) {
|
||||
super(node);
|
||||
override fun <R, D> accept(visitor: KtVisitor<R, D>, data: D): R {
|
||||
return visitor.visitDotQualifiedExpression(this, data)
|
||||
}
|
||||
|
||||
public KtDotQualifiedExpression(@NotNull KotlinPlaceHolderStub<KtDotQualifiedExpression> stub) {
|
||||
super(stub, KtStubElementTypes.DOT_QUALIFIED_EXPRESSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
|
||||
return visitor.visitDotQualifiedExpression(this, data);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public KtExpression getReceiverExpression() {
|
||||
KotlinPlaceHolderStub<KtDotQualifiedExpression> stub = getStub();
|
||||
if (stub != null) {
|
||||
KtExpression[] childExpressionsByStub = getChildExpressionsByStub(stub);
|
||||
if (childExpressionsByStub != null) {
|
||||
return childExpressionsByStub[0];
|
||||
override val receiverExpression: KtExpression
|
||||
get() {
|
||||
val stub = stub
|
||||
if (stub != null) {
|
||||
val childExpressionsByStub = getChildExpressionsByStub(stub)
|
||||
if (childExpressionsByStub != null) {
|
||||
return childExpressionsByStub[0]
|
||||
}
|
||||
}
|
||||
return super.receiverExpression
|
||||
}
|
||||
return KtQualifiedExpressionImpl.INSTANCE.getReceiverExpression(this);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public KtExpression getSelectorExpression() {
|
||||
KotlinPlaceHolderStub<KtDotQualifiedExpression> stub = getStub();
|
||||
if (stub != null) {
|
||||
KtExpression[] childExpressionsByStub = getChildExpressionsByStub(stub);
|
||||
if (childExpressionsByStub != null && childExpressionsByStub.length == 2) {
|
||||
return childExpressionsByStub[1];
|
||||
override val selectorExpression: KtExpression?
|
||||
get() {
|
||||
val stub = stub
|
||||
if (stub != null) {
|
||||
val childExpressionsByStub = getChildExpressionsByStub(stub)
|
||||
if (childExpressionsByStub != null && childExpressionsByStub.size == 2) {
|
||||
return childExpressionsByStub[1]
|
||||
}
|
||||
}
|
||||
return super.selectorExpression
|
||||
}
|
||||
return KtQualifiedExpressionImpl.INSTANCE.getSelectorExpression(this);
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
private KtExpression[] getChildExpressionsByStub(@NotNull KotlinPlaceHolderStub<KtDotQualifiedExpression> stub) {
|
||||
if (stub.getParentStubOfType(KtImportDirective.class) == null && stub.getParentStubOfType(KtPackageDirective.class) == null) {
|
||||
LOG.error("JetDotQualifiedExpression should only have stubs inside import or package directives.\n " +
|
||||
"Stubs were created for:\n " + getText() +
|
||||
"\nFile text:\n" + getContainingFile().getText());
|
||||
return null;
|
||||
private fun getChildExpressionsByStub(stub: KotlinPlaceHolderStub<KtDotQualifiedExpression>): Array<KtExpression>? {
|
||||
if (stub.getParentStubOfType(KtImportDirective::class.java) == null &&
|
||||
stub.getParentStubOfType(KtPackageDirective::class.java) == null) {
|
||||
LOG.error("KtDotQualifiedExpression should only have stubs inside import or package directives.\n" +
|
||||
"Stubs were created for:\n$text\nFile text:\n${containingFile.text}")
|
||||
return null
|
||||
}
|
||||
else {
|
||||
KtExpression[] expressions = stub.getChildrenByType(INSIDE_DIRECTIVE_EXPRESSIONS, KtExpression.ARRAY_FACTORY);
|
||||
if (expressions.length < 1 || expressions.length > 2) {
|
||||
LOG.error("Invalid stub structure. DOT_QUALIFIED_EXPRESSION must have one or two children. Was: " + expressions.length +
|
||||
"\nFile text:\n" + getContainingFile().getText());
|
||||
return null;
|
||||
val expressions = stub.getChildrenByType(INSIDE_DIRECTIVE_EXPRESSIONS, KtExpression.ARRAY_FACTORY)
|
||||
if (expressions.size < 1 || expressions.size > 2) {
|
||||
LOG.error("Invalid stub structure. DOT_QUALIFIED_EXPRESSION must have one or two children. Was: ${expressions.size}\n" +
|
||||
"File text:\n${containingFile.text}")
|
||||
return null
|
||||
}
|
||||
return expressions;
|
||||
return expressions
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ASTNode getOperationTokenNode() {
|
||||
return KtQualifiedExpressionImpl.INSTANCE.getOperationTokenNode(this);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public KtToken getOperationSign() {
|
||||
return KtQualifiedExpressionImpl.INSTANCE.getOperationSign(this);
|
||||
companion object {
|
||||
private val LOG = Logger.getInstance(KtDotQualifiedExpression::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,23 +14,29 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.psi;
|
||||
package org.jetbrains.kotlin.psi
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.lexer.KtToken;
|
||||
import com.intellij.lang.ASTNode
|
||||
import org.jetbrains.kotlin.lexer.KtToken
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
|
||||
import org.jetbrains.kotlin.psi.psiUtil.siblings
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||
|
||||
public interface KtQualifiedExpression extends KtExpression {
|
||||
@NotNull
|
||||
KtExpression getReceiverExpression();
|
||||
interface KtQualifiedExpression : KtExpression {
|
||||
val receiverExpression: KtExpression
|
||||
get() = getExpression(false) ?: throw AssertionError("No receiver found: ${getElementTextWithContext()}")
|
||||
|
||||
@Nullable
|
||||
KtExpression getSelectorExpression();
|
||||
val selectorExpression: KtExpression?
|
||||
get() = getExpression(true)
|
||||
|
||||
@NotNull
|
||||
ASTNode getOperationTokenNode();
|
||||
val operationTokenNode: ASTNode
|
||||
get() = node.findChildByType(KtTokens.OPERATIONS)!!
|
||||
|
||||
@NotNull
|
||||
KtToken getOperationSign();
|
||||
val operationSign: KtToken
|
||||
get() = operationTokenNode.elementType as KtToken
|
||||
|
||||
private fun KtQualifiedExpression.getExpression(afterOperation: Boolean): KtExpression? {
|
||||
return operationTokenNode.psi?.siblings(afterOperation, false)?.firstIsInstanceOrNull<KtExpression>()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +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 org.jetbrains.kotlin.lexer.KtToken
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||
|
||||
object KtQualifiedExpressionImpl {
|
||||
fun KtQualifiedExpression.getOperationTokenNode(): ASTNode {
|
||||
val operationNode = this.node!!.findChildByType(KtTokens.OPERATIONS)
|
||||
return operationNode!!
|
||||
}
|
||||
|
||||
fun KtQualifiedExpression.getOperationSign(): KtToken {
|
||||
return this.operationTokenNode.elementType as KtToken
|
||||
}
|
||||
|
||||
private fun KtQualifiedExpression.getExpression(afterOperation: Boolean): KtExpression? {
|
||||
return operationTokenNode.psi?.siblings(afterOperation, false)?.firstOrNull { it is KtExpression } as? KtExpression
|
||||
}
|
||||
|
||||
fun KtQualifiedExpression.getReceiverExpression(): KtExpression {
|
||||
return getExpression(false) ?: throw AssertionError("No receiver found: ${getElementTextWithContext()}")
|
||||
}
|
||||
|
||||
fun KtQualifiedExpression.getSelectorExpression(): KtExpression? {
|
||||
return getExpression(true)
|
||||
}
|
||||
}
|
||||
@@ -14,44 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.psi;
|
||||
package org.jetbrains.kotlin.psi
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.lexer.KtToken;
|
||||
import com.intellij.lang.ASTNode
|
||||
|
||||
public class KtSafeQualifiedExpression extends KtExpressionImpl implements KtQualifiedExpression {
|
||||
public KtSafeQualifiedExpression(@NotNull ASTNode node) {
|
||||
super(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
|
||||
return visitor.visitSafeQualifiedExpression(this, data);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public KtExpression getReceiverExpression() {
|
||||
return KtQualifiedExpressionImpl.INSTANCE.getReceiverExpression(this);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public KtExpression getSelectorExpression() {
|
||||
return KtQualifiedExpressionImpl.INSTANCE.getSelectorExpression(this);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ASTNode getOperationTokenNode() {
|
||||
return KtQualifiedExpressionImpl.INSTANCE.getOperationTokenNode(this);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public KtToken getOperationSign() {
|
||||
return KtQualifiedExpressionImpl.INSTANCE.getOperationSign(this);
|
||||
class KtSafeQualifiedExpression(node: ASTNode) : KtExpressionImpl(node), KtQualifiedExpression {
|
||||
override fun <R, D> accept(visitor: KtVisitor<R, D>, data: D): R {
|
||||
return visitor.visitSafeQualifiedExpression(this, data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ import com.intellij.psi.PsiParameterList
|
||||
import com.intellij.psi.PsiWhiteSpace
|
||||
import com.intellij.psi.stubs.StubElement
|
||||
import com.intellij.psi.tree.TokenSet
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.kotlin.KtNodeTypes
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
@@ -38,7 +37,6 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
|
||||
import org.jetbrains.kotlin.types.expressions.OperatorConventions
|
||||
import java.util.*
|
||||
import kotlin.text.Regex
|
||||
|
||||
// NOTE: in this file we collect only Kotlin-specific methods working with PSI and not modifying it
|
||||
|
||||
@@ -148,7 +146,7 @@ fun KtExpression.getQualifiedExpressionForReceiverOrThis(): KtExpression {
|
||||
}
|
||||
|
||||
fun KtExpression.isDotReceiver(): Boolean =
|
||||
(parent as? KtDotQualifiedExpression)?.getReceiverExpression() == this
|
||||
(parent as? KtDotQualifiedExpression)?.receiverExpression == this
|
||||
|
||||
// ---------- Block expression -------------------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -209,7 +207,7 @@ fun StubBasedPsiElementBase<out KotlinClassOrObjectStub<out KtClassOrObject>>.ge
|
||||
if (directive != null) {
|
||||
var reference = directive.getImportedReference()
|
||||
while (reference is KtDotQualifiedExpression) {
|
||||
reference = reference.getSelectorExpression()
|
||||
reference = reference.selectorExpression
|
||||
}
|
||||
if (reference is KtSimpleNameExpression) {
|
||||
result.add(reference.getReferencedName())
|
||||
|
||||
Reference in New Issue
Block a user