Uast: constructors calls consistency for KtObjectLiteralExpression (KT-21409)
This commit is contained in:
committed by
xiexed
parent
1dbce18afc
commit
4db5ca66e3
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
|||||||
import org.jetbrains.kotlin.idea.KotlinLanguage
|
import org.jetbrains.kotlin.idea.KotlinLanguage
|
||||||
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.getParentOfType
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||||
import org.jetbrains.uast.*
|
import org.jetbrains.uast.*
|
||||||
@@ -312,7 +313,11 @@ internal object KotlinConverter {
|
|||||||
is KtConstructorDelegationCall ->
|
is KtConstructorDelegationCall ->
|
||||||
el<UCallExpression> { KotlinUFunctionCallExpression(element, givenParent) }
|
el<UCallExpression> { KotlinUFunctionCallExpression(element, givenParent) }
|
||||||
is KtSuperTypeCallEntry ->
|
is KtSuperTypeCallEntry ->
|
||||||
el<UCallExpression> { KotlinUFunctionCallExpression(element, givenParent) }
|
el<UExpression> {
|
||||||
|
(element.getParentOfType<KtClassOrObject>(true)?.parent as? KtObjectLiteralExpression)
|
||||||
|
?.toUElementOfType<UExpression>()
|
||||||
|
?: KotlinUFunctionCallExpression(element, givenParent)
|
||||||
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
if (element is LeafPsiElement && element.elementType == KtTokens.IDENTIFIER) {
|
if (element is LeafPsiElement && element.elementType == KtTokens.IDENTIFIER) {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package org.jetbrains.uast.kotlin
|
|||||||
|
|
||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
import org.jetbrains.kotlin.asJava.classes.KtLightClass
|
import org.jetbrains.kotlin.asJava.classes.KtLightClass
|
||||||
|
import org.jetbrains.kotlin.asJava.classes.KtLightClassForLocalDeclaration
|
||||||
import org.jetbrains.kotlin.asJava.classes.KtLightClassForScript
|
import org.jetbrains.kotlin.asJava.classes.KtLightClassForScript
|
||||||
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
|
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
|
||||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||||
@@ -31,9 +32,17 @@ import org.jetbrains.uast.kotlin.declarations.KotlinUMethod
|
|||||||
import org.jetbrains.uast.kotlin.declarations.UastLightIdentifier
|
import org.jetbrains.uast.kotlin.declarations.UastLightIdentifier
|
||||||
|
|
||||||
abstract class AbstractKotlinUClass(private val givenParent: UElement?) : AbstractJavaUClass() {
|
abstract class AbstractKotlinUClass(private val givenParent: UElement?) : AbstractJavaUClass() {
|
||||||
override val uastParent: UElement? by lz {
|
override val uastParent: UElement? by lz { givenParent ?: convertParent() }
|
||||||
givenParent ?: KotlinUastLanguagePlugin().convertElementWithParent(psi.parent ?: psi.containingFile, null)
|
|
||||||
}
|
//TODO: should be merged with KotlinAbstractUElement.convertParent() after detaching from AbstractJavaUClass
|
||||||
|
open fun convertParent(): UElement? =
|
||||||
|
(this.psi as? KtLightClassForLocalDeclaration)?.kotlinOrigin?.parent?.let {
|
||||||
|
when (it) {
|
||||||
|
is KtClassBody -> it.parent.toUElement() // TODO: it seems that `class_body`-s are never created in kotlin uast in top-down walk, probably they should be completely skipped and always unwrapped
|
||||||
|
else -> it.toUElement()
|
||||||
|
}
|
||||||
|
} ?: (psi.parent ?: psi.containingFile).toUElement()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
open class KotlinUClass private constructor(
|
open class KotlinUClass private constructor(
|
||||||
@@ -109,7 +118,7 @@ class KotlinConstructorUMethod(
|
|||||||
) : KotlinUMethod(psi, givenParent) {
|
) : KotlinUMethod(psi, givenParent) {
|
||||||
|
|
||||||
val isPrimary: Boolean
|
val isPrimary: Boolean
|
||||||
get() = psi.kotlinOrigin.let { it is KtPrimaryConstructor || it is KtObjectDeclaration }
|
get() = psi.kotlinOrigin.let { it is KtPrimaryConstructor || it is KtClassOrObject }
|
||||||
|
|
||||||
override val uastBody: UExpression? by lz {
|
override val uastBody: UExpression? by lz {
|
||||||
val delegationCall: KtCallElement? = psi.kotlinOrigin.let {
|
val delegationCall: KtCallElement? = psi.kotlinOrigin.let {
|
||||||
@@ -157,6 +166,7 @@ class KotlinUAnonymousClass(
|
|||||||
val ktClassOrObject = (psi.originalElement as? KtLightClass)?.kotlinOrigin as? KtObjectDeclaration ?: return null
|
val ktClassOrObject = (psi.originalElement as? KtLightClass)?.kotlinOrigin as? KtObjectDeclaration ?: return null
|
||||||
return UIdentifier(ktClassOrObject.getObjectKeyword(), this)
|
return UIdentifier(ktClassOrObject.getObjectKeyword(), this)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class KotlinScriptUClass(
|
class KotlinScriptUClass(
|
||||||
|
|||||||
+10
-2
@@ -21,5 +21,13 @@ class C : A {
|
|||||||
object O : A("text")
|
object O : A("text")
|
||||||
|
|
||||||
val anon = object : A("textForAnon") {
|
val anon = object : A("textForAnon") {
|
||||||
fun bar() {}
|
fun bar() {
|
||||||
}
|
cons(object : A("inner literal"))
|
||||||
|
}
|
||||||
|
|
||||||
|
object innerObject : A("inner object")
|
||||||
|
|
||||||
|
inner class InnerClass : A("inner class")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun cons(a: A) {}
|
||||||
+27
@@ -7,8 +7,35 @@ UFile (package = )
|
|||||||
UClass (name = null)
|
UClass (name = null)
|
||||||
UAnnotationMethod (name = bar)
|
UAnnotationMethod (name = bar)
|
||||||
UBlockExpression
|
UBlockExpression
|
||||||
|
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 1))
|
||||||
|
UIdentifier (Identifier (cons))
|
||||||
|
USimpleNameReferenceExpression (identifier = cons)
|
||||||
|
UObjectLiteralExpression
|
||||||
|
ULiteralExpression (value = "inner literal")
|
||||||
|
UClass (name = null)
|
||||||
|
UAnnotationMethod (name = SuperCallsKt$anon$1$bar$1)
|
||||||
UAnnotationMethod (name = SuperCallsKt$anon$1)
|
UAnnotationMethod (name = SuperCallsKt$anon$1)
|
||||||
|
UClass (name = innerObject)
|
||||||
|
UField (name = INSTANCE)
|
||||||
|
UAnnotation (fqName = null)
|
||||||
|
UAnnotationMethod (name = innerObject)
|
||||||
|
UBlockExpression
|
||||||
|
UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 1))
|
||||||
|
UIdentifier (Identifier (A))
|
||||||
|
USimpleNameReferenceExpression (identifier = <init>)
|
||||||
|
ULiteralExpression (value = "inner object")
|
||||||
|
UClass (name = InnerClass)
|
||||||
|
UAnnotationMethod (name = InnerClass)
|
||||||
|
UBlockExpression
|
||||||
|
UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 1))
|
||||||
|
UIdentifier (Identifier (A))
|
||||||
|
USimpleNameReferenceExpression (identifier = <init>)
|
||||||
|
ULiteralExpression (value = "inner class")
|
||||||
UAnnotationMethod (name = getAnon)
|
UAnnotationMethod (name = getAnon)
|
||||||
|
UAnnotationMethod (name = cons)
|
||||||
|
UParameter (name = a)
|
||||||
|
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
|
||||||
|
UBlockExpression
|
||||||
UClass (name = A)
|
UClass (name = A)
|
||||||
UField (name = str)
|
UField (name = str)
|
||||||
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
|
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
|
||||||
|
|||||||
+9
-1
@@ -1,8 +1,16 @@
|
|||||||
public final class SuperCallsKt {
|
public final class SuperCallsKt {
|
||||||
private static final var anon: A = anonymous object : A("textForAnon") {
|
private static final var anon: A = anonymous object : A("textForAnon") {
|
||||||
fun bar() {}
|
fun bar() {
|
||||||
|
cons(object : A("inner literal"))
|
||||||
|
}
|
||||||
|
|
||||||
|
object innerObject : A("inner object")
|
||||||
|
|
||||||
|
inner class InnerClass : A("inner class")
|
||||||
}
|
}
|
||||||
public static final fun getAnon() : A = UastEmptyExpression
|
public static final fun getAnon() : A = UastEmptyExpression
|
||||||
|
public static final fun cons(a: A) : void {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class A {
|
public class A {
|
||||||
|
|||||||
Reference in New Issue
Block a user