Cleanup 192 patchset files (KTI-315)
This commit is contained in:
@@ -1,262 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.uast.kotlin
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiMethod
|
||||
import org.jetbrains.kotlin.asJava.LightClassUtil
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightElement
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
|
||||
import org.jetbrains.kotlin.asJava.toLightGetter
|
||||
import org.jetbrains.kotlin.asJava.toLightSetter
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isPropertyParameter
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
import org.jetbrains.uast.*
|
||||
import org.jetbrains.uast.kotlin.expressions.KotlinLocalFunctionULambdaExpression
|
||||
import org.jetbrains.uast.kotlin.expressions.KotlinUElvisExpression
|
||||
import org.jetbrains.uast.kotlin.internal.KotlinUElementWithComments
|
||||
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiParameter
|
||||
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable
|
||||
|
||||
abstract class KotlinAbstractUElement(private val givenParent: UElement?) : KotlinUElementWithComments {
|
||||
|
||||
final override val uastParent: UElement? by lz {
|
||||
givenParent ?: convertParent()
|
||||
}
|
||||
|
||||
protected open fun convertParent(): UElement? {
|
||||
@Suppress("DEPRECATION")
|
||||
val psi = psi //TODO: `psi` is deprecated but it seems that it couldn't be simply replaced for this case
|
||||
var parent = psi?.parent ?: sourcePsi?.parent ?: psi?.containingFile
|
||||
|
||||
if (psi is PsiMethod && psi !is KtLightMethod) { // handling of synthetic things not represented in lightclasses directly
|
||||
when (parent) {
|
||||
is KtClassBody -> {
|
||||
val grandParent = parent.parent
|
||||
doConvertParent(this, grandParent)?.let { return it }
|
||||
parent = grandParent
|
||||
}
|
||||
is KtFile -> {
|
||||
parent.toUElementOfType<UClass>()?.let { return it } // mutlifile facade class
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (psi is KtLightElement<*, *> && sourcePsi.safeAs<KtClassOrObject>()?.isLocal == true) {
|
||||
val originParent = psi.kotlinOrigin?.parent
|
||||
parent = when (originParent) {
|
||||
null -> parent
|
||||
is KtClassBody -> originParent.parent
|
||||
else -> originParent
|
||||
}
|
||||
}
|
||||
|
||||
if (psi is KtAnnotationEntry) {
|
||||
val parentUnwrapped = KotlinConverter.unwrapElements(parent) ?: return null
|
||||
val target = psi.useSiteTarget?.getAnnotationUseSiteTarget()
|
||||
when (target) {
|
||||
AnnotationUseSiteTarget.PROPERTY_GETTER ->
|
||||
parent = (parentUnwrapped as? KtProperty)?.getter
|
||||
?: (parentUnwrapped as? KtParameter)?.toLightGetter()
|
||||
?: parent
|
||||
|
||||
AnnotationUseSiteTarget.PROPERTY_SETTER ->
|
||||
parent = (parentUnwrapped as? KtProperty)?.setter
|
||||
?: (parentUnwrapped as? KtParameter)?.toLightSetter()
|
||||
?: parent
|
||||
AnnotationUseSiteTarget.FIELD ->
|
||||
parent = (parentUnwrapped as? KtProperty)
|
||||
?: (parentUnwrapped as? KtParameter)
|
||||
?.takeIf { it.isPropertyParameter() }
|
||||
?.let(LightClassUtil::getLightClassBackingField)
|
||||
?: parent
|
||||
AnnotationUseSiteTarget.SETTER_PARAMETER ->
|
||||
parent = (parentUnwrapped as? KtParameter)
|
||||
?.toLightSetter()?.parameterList?.parameters?.firstOrNull() ?: parent
|
||||
}
|
||||
}
|
||||
if ((psi is UastKotlinPsiVariable || psi is UastKotlinPsiParameter) && parent != null) {
|
||||
parent = parent.parent
|
||||
}
|
||||
|
||||
if (KotlinConverter.forceUInjectionHost) {
|
||||
if (parent is KtBlockStringTemplateEntry) {
|
||||
parent = parent.parent
|
||||
}
|
||||
} else
|
||||
while (parent is KtStringTemplateEntryWithExpression || parent is KtStringTemplateExpression && parent.entries.size == 1) {
|
||||
parent = parent.parent
|
||||
}
|
||||
|
||||
if (parent is KtWhenConditionWithExpression) {
|
||||
parent = parent.parent
|
||||
}
|
||||
|
||||
if (parent is KtImportList) {
|
||||
parent = parent.parent
|
||||
}
|
||||
|
||||
if (psi is KtFunctionLiteral && parent is KtLambdaExpression) {
|
||||
parent = parent.parent
|
||||
}
|
||||
|
||||
if (parent is KtLambdaArgument) {
|
||||
parent = parent.parent
|
||||
}
|
||||
|
||||
if (psi is KtSuperTypeCallEntry) {
|
||||
parent = parent?.parent
|
||||
}
|
||||
|
||||
if (parent is KtPropertyDelegate) {
|
||||
parent = parent.parent
|
||||
}
|
||||
|
||||
val result = doConvertParent(this, parent)
|
||||
if (result == this) {
|
||||
throw IllegalStateException("Loop in parent structure when converting a $psi of type ${psi?.javaClass} with parent $parent of type ${parent?.javaClass} text: [${parent?.text}], result = $result")
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is UElement) {
|
||||
return false
|
||||
}
|
||||
|
||||
return this.psi == other.psi
|
||||
}
|
||||
|
||||
override fun hashCode() = psi?.hashCode() ?: 0
|
||||
}
|
||||
|
||||
fun doConvertParent(element: UElement, parent: PsiElement?): UElement? {
|
||||
val parentUnwrapped = KotlinConverter.unwrapElements(parent) ?: return null
|
||||
if (parent is KtValueArgument && parentUnwrapped is KtAnnotationEntry) {
|
||||
return (KotlinUastLanguagePlugin().convertElementWithParent(parentUnwrapped, null) as? KotlinUAnnotation)
|
||||
?.findAttributeValueExpression(parent)
|
||||
}
|
||||
|
||||
if (parent is KtParameter) {
|
||||
val annotationClass = findAnnotationClassFromConstructorParameter(parent)
|
||||
if (annotationClass != null) {
|
||||
return annotationClass.methods.find { it.name == parent.name }
|
||||
}
|
||||
}
|
||||
|
||||
if (parent is KtClassInitializer) {
|
||||
val containingClass = parent.containingClassOrObject
|
||||
if (containingClass != null) {
|
||||
val containingUClass = KotlinUastLanguagePlugin().convertElementWithParent(containingClass, null) as? KotlinUClass
|
||||
containingUClass?.methods?.filterIsInstance<KotlinConstructorUMethod>()?.firstOrNull { it.isPrimary }?.let {
|
||||
return it.uastBody
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val result = KotlinUastLanguagePlugin().convertElementWithParent(parentUnwrapped, null)
|
||||
|
||||
if (result is KotlinUBlockExpression && element is UClass) {
|
||||
return KotlinUDeclarationsExpression(result).apply {
|
||||
declarations = listOf(element)
|
||||
}
|
||||
}
|
||||
|
||||
if (result is UEnumConstant && element is UDeclaration) {
|
||||
return result.initializingClass
|
||||
}
|
||||
|
||||
if (result is UCallExpression && result.uastParent is UEnumConstant) {
|
||||
return result.uastParent
|
||||
}
|
||||
|
||||
if (result is USwitchClauseExpressionWithBody && !isInConditionBranch(element, result)) {
|
||||
return result.body
|
||||
}
|
||||
|
||||
if (result is KotlinUDestructuringDeclarationExpression &&
|
||||
when (parent) {
|
||||
is KtDestructuringDeclaration -> parent.initializer?.let { it == element.psi } == true
|
||||
is KtDeclarationModifierList -> parent == element.sourcePsi?.parent
|
||||
else -> false
|
||||
}
|
||||
) {
|
||||
return result.tempVarAssignment
|
||||
}
|
||||
|
||||
if (result is KotlinUElvisExpression && parentUnwrapped is KtBinaryExpression) {
|
||||
val branch: Sequence<PsiElement?> = element.psi?.parentsWithSelf.orEmpty().takeWhile { it != parentUnwrapped }
|
||||
if (branch.contains(parentUnwrapped.left))
|
||||
return result.lhsDeclaration
|
||||
if (branch.contains(parentUnwrapped.right))
|
||||
return result.rhsIfExpression
|
||||
}
|
||||
|
||||
if ((result is UMethod || result is KotlinLocalFunctionULambdaExpression)
|
||||
&& result !is KotlinConstructorUMethod // no sense to wrap super calls with `return`
|
||||
&& element is UExpression
|
||||
&& element !is UBlockExpression
|
||||
&& element !is UTypeReferenceExpression // when element is a type in extension methods
|
||||
) {
|
||||
return KotlinUBlockExpression.KotlinLazyUBlockExpression(result, { block ->
|
||||
listOf(KotlinUImplicitReturnExpression(block).apply { returnExpression = element })
|
||||
}).expressions.single()
|
||||
}
|
||||
|
||||
if (result is KotlinULambdaExpression.Body && element is UExpression && result.implicitReturn?.returnExpression == element) {
|
||||
return result.implicitReturn!!
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun isInConditionBranch(element: UElement, result: USwitchClauseExpressionWithBody) =
|
||||
element.psi?.parentsWithSelf?.takeWhile { it !== result.psi }?.any { it is KtWhenCondition } ?: false
|
||||
|
||||
|
||||
private fun findAnnotationClassFromConstructorParameter(parameter: KtParameter): UClass? {
|
||||
val primaryConstructor = parameter.getStrictParentOfType<KtPrimaryConstructor>() ?: return null
|
||||
val containingClass = primaryConstructor.getContainingClassOrObject()
|
||||
if (containingClass.isAnnotation()) {
|
||||
return KotlinUastLanguagePlugin().convertElementWithParent(containingClass, null) as? UClass
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
abstract class KotlinAbstractUExpression(givenParent: UElement?) :
|
||||
KotlinAbstractUElement(givenParent),
|
||||
UExpression {
|
||||
|
||||
override val javaPsi: PsiElement? = null
|
||||
|
||||
override val psi
|
||||
get() = sourcePsi
|
||||
|
||||
override val annotations: List<UAnnotation>
|
||||
get() {
|
||||
val annotatedExpression = sourcePsi?.parent as? KtAnnotatedExpression ?: return emptyList()
|
||||
return annotatedExpression.annotationEntries.map { KotlinUAnnotation(it, this) }
|
||||
}
|
||||
}
|
||||
|
||||
-94
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.uast.kotlin
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.psi.KtBlockExpression
|
||||
import org.jetbrains.kotlin.psi.KtWhenEntry
|
||||
import org.jetbrains.kotlin.psi.KtWhenExpression
|
||||
import org.jetbrains.uast.*
|
||||
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
|
||||
import org.jetbrains.uast.kotlin.kinds.KotlinSpecialExpressionKinds
|
||||
|
||||
class KotlinUSwitchExpression(
|
||||
override val sourcePsi: KtWhenExpression,
|
||||
givenParent: UElement?
|
||||
) : KotlinAbstractUExpression(givenParent), USwitchExpression, KotlinUElementWithType {
|
||||
override val expression by lz { KotlinConverter.convertOrNull(sourcePsi.subjectExpression, this) }
|
||||
|
||||
override val body: UExpressionList by lz {
|
||||
object : KotlinUExpressionList(sourcePsi, KotlinSpecialExpressionKinds.WHEN, this@KotlinUSwitchExpression) {
|
||||
override fun asRenderString() = expressions.joinToString("\n") { it.asRenderString().withMargin }
|
||||
}.apply {
|
||||
expressions = this@KotlinUSwitchExpression.sourcePsi.entries.map { KotlinUSwitchEntry(it, this) }
|
||||
}
|
||||
}
|
||||
|
||||
override fun asRenderString() = buildString {
|
||||
val expr = expression?.let { "(" + it.asRenderString() + ") " } ?: ""
|
||||
appendln("switch $expr {")
|
||||
appendln(body.asRenderString())
|
||||
appendln("}")
|
||||
}
|
||||
|
||||
override val switchIdentifier: UIdentifier
|
||||
get() = KotlinUIdentifier(null, this)
|
||||
}
|
||||
|
||||
class KotlinUSwitchEntry(
|
||||
override val sourcePsi: KtWhenEntry,
|
||||
givenParent: UElement?
|
||||
) : KotlinAbstractUExpression(givenParent), USwitchClauseExpressionWithBody {
|
||||
override val caseValues by lz {
|
||||
sourcePsi.conditions.map { KotlinConverter.convertWhenCondition(it, this, DEFAULT_EXPRESSION_TYPES_LIST) ?: UastEmptyExpression(null) }
|
||||
}
|
||||
|
||||
override val body: UExpressionList by lz {
|
||||
object : KotlinUExpressionList(sourcePsi, KotlinSpecialExpressionKinds.WHEN_ENTRY, this@KotlinUSwitchEntry) {
|
||||
override fun asRenderString() = buildString {
|
||||
appendln("{")
|
||||
expressions.forEach { appendln(it.asRenderString().withMargin) }
|
||||
appendln("}")
|
||||
}
|
||||
}.apply {
|
||||
val exprPsi = this@KotlinUSwitchEntry.sourcePsi.expression
|
||||
val userExpressions = when (exprPsi) {
|
||||
is KtBlockExpression -> exprPsi.statements.map { KotlinConverter.convertOrEmpty(it, this) }
|
||||
else -> listOf(KotlinConverter.convertOrEmpty(exprPsi, this))
|
||||
}
|
||||
expressions = userExpressions + object : UBreakExpression {
|
||||
override val javaPsi: PsiElement? = null
|
||||
override val sourcePsi: PsiElement? = null
|
||||
override val psi: PsiElement?
|
||||
get() = null
|
||||
override val label: String?
|
||||
get() = null
|
||||
override val uastParent: UElement?
|
||||
get() = this@KotlinUSwitchEntry
|
||||
override val annotations: List<UAnnotation>
|
||||
get() = emptyList()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun convertParent(): UElement? {
|
||||
val result = KotlinConverter.unwrapElements(sourcePsi.parent)?.let { parentUnwrapped ->
|
||||
KotlinUastLanguagePlugin().convertElementWithParent(parentUnwrapped, null)
|
||||
}
|
||||
return (result as? KotlinUSwitchExpression)?.body ?: result
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
UFile (package = )
|
||||
UClass (name = AnnotatedExpressionsKt)
|
||||
UMethod (name = foo)
|
||||
UBlockExpression
|
||||
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0))
|
||||
UAnnotation (fqName = kotlin.Suppress)
|
||||
UIdentifier (Identifier (foo))
|
||||
USimpleNameReferenceExpression (identifier = foo, resolvesTo = null)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = a)
|
||||
UAnnotation (fqName = kotlin.Suppress)
|
||||
ULiteralExpression (value = 1)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = b)
|
||||
UAnnotation (fqName = kotlin.Suppress)
|
||||
ULiteralExpression (value = 2)
|
||||
UBinaryExpression (operator = =)
|
||||
UAnnotation (fqName = kotlin.Suppress)
|
||||
USimpleNameReferenceExpression (identifier = b)
|
||||
USimpleNameReferenceExpression (identifier = a)
|
||||
UIfExpression
|
||||
UAnnotation (fqName = kotlin.Suppress)
|
||||
UBinaryExpression (operator = >)
|
||||
USimpleNameReferenceExpression (identifier = a)
|
||||
ULiteralExpression (value = 2)
|
||||
USimpleNameReferenceExpression (identifier = a)
|
||||
USimpleNameReferenceExpression (identifier = b)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = c)
|
||||
UExpressionList (elvis)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = varae507364)
|
||||
USimpleNameReferenceExpression (identifier = a)
|
||||
UAnnotation (fqName = kotlin.Suppress)
|
||||
UIfExpression
|
||||
UBinaryExpression (operator = !=)
|
||||
USimpleNameReferenceExpression (identifier = varae507364)
|
||||
ULiteralExpression (value = null)
|
||||
USimpleNameReferenceExpression (identifier = varae507364)
|
||||
USimpleNameReferenceExpression (identifier = b)
|
||||
UMethod (name = annotatedSwitch)
|
||||
UParameter (name = str)
|
||||
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
|
||||
UBlockExpression
|
||||
UReturnExpression
|
||||
USwitchExpression
|
||||
UExpressionList (when)
|
||||
USwitchClauseExpressionWithBody
|
||||
UQualifiedReferenceExpression
|
||||
UAnnotation (fqName = kotlin.Suppress)
|
||||
UNamedExpression (name = names)
|
||||
ULiteralExpression (value = "DEPRECATION")
|
||||
USimpleNameReferenceExpression (identifier = str)
|
||||
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0))
|
||||
UIdentifier (Identifier (isBlank))
|
||||
USimpleNameReferenceExpression (identifier = isBlank, resolvesTo = null)
|
||||
UExpressionList (when_entry)
|
||||
ULiteralExpression (value = null)
|
||||
UBreakExpression (label = null)
|
||||
USwitchClauseExpressionWithBody
|
||||
UBinaryExpression (operator = !=)
|
||||
UQualifiedReferenceExpression
|
||||
USimpleNameReferenceExpression (identifier = str)
|
||||
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0))
|
||||
UIdentifier (Identifier (isNotEmpty))
|
||||
USimpleNameReferenceExpression (identifier = isNotEmpty, resolvesTo = null)
|
||||
ULiteralExpression (value = null)
|
||||
UExpressionList (when_entry)
|
||||
ULiteralExpression (value = null)
|
||||
UBreakExpression (label = null)
|
||||
USwitchClauseExpressionWithBody
|
||||
UExpressionList (when_entry)
|
||||
ULiteralExpression (value = 1)
|
||||
UBreakExpression (label = null)
|
||||
@@ -1,33 +0,0 @@
|
||||
public final class AnnotatedExpressionsKt {
|
||||
public static final fun foo() : void {
|
||||
foo()
|
||||
@kotlin.Suppress var a: int = 1
|
||||
@kotlin.Suppress var b: int = 2
|
||||
b = a
|
||||
if (a > 2) a else b
|
||||
var c: int = elvis {
|
||||
var varae507364: int = a
|
||||
if (varae507364 != null) varae507364 else b
|
||||
}
|
||||
}
|
||||
public static final fun annotatedSwitch(@org.jetbrains.annotations.NotNull str: java.lang.String) : java.lang.Integer {
|
||||
return switch {
|
||||
str.isBlank() -> {
|
||||
null
|
||||
break
|
||||
}
|
||||
|
||||
str.isNotEmpty() != null -> {
|
||||
null
|
||||
break
|
||||
}
|
||||
|
||||
-> {
|
||||
1
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
UFile (package = )
|
||||
UClass (name = WhenAndDestructingKt)
|
||||
UMethod (name = getElementsAdditionalResolve)
|
||||
UParameter (name = string)
|
||||
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
|
||||
UBlockExpression
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = arr)
|
||||
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2))
|
||||
UIdentifier (Identifier (listOf))
|
||||
USimpleNameReferenceExpression (identifier = listOf, resolvesTo = null)
|
||||
ULiteralExpression (value = "1")
|
||||
ULiteralExpression (value = "2")
|
||||
USwitchExpression
|
||||
USimpleNameReferenceExpression (identifier = string)
|
||||
UExpressionList (when)
|
||||
USwitchClauseExpressionWithBody
|
||||
ULiteralExpression (value = "aaaa")
|
||||
UExpressionList (when_entry)
|
||||
UReturnExpression
|
||||
ULiteralExpression (value = "bindingContext")
|
||||
UBreakExpression (label = null)
|
||||
USwitchClauseExpressionWithBody
|
||||
ULiteralExpression (value = "empty-switch")
|
||||
UExpressionList (when_entry)
|
||||
UBreakExpression (label = null)
|
||||
USwitchClauseExpressionWithBody
|
||||
UExpressionList (when_entry)
|
||||
UDeclarationsExpression
|
||||
ULocalVariable (name = var837f2350)
|
||||
UAnnotation (fqName = null)
|
||||
USimpleNameReferenceExpression (identifier = arr)
|
||||
ULocalVariable (name = bindingContext)
|
||||
UAnnotation (fqName = null)
|
||||
UQualifiedReferenceExpression
|
||||
USimpleNameReferenceExpression (identifier = var837f2350)
|
||||
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0))
|
||||
UIdentifier (Identifier (component1))
|
||||
USimpleNameReferenceExpression (identifier = <anonymous class>, resolvesTo = null)
|
||||
ULocalVariable (name = statementFilter)
|
||||
UAnnotation (fqName = null)
|
||||
UQualifiedReferenceExpression
|
||||
USimpleNameReferenceExpression (identifier = var837f2350)
|
||||
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0))
|
||||
UIdentifier (Identifier (component2))
|
||||
USimpleNameReferenceExpression (identifier = <anonymous class>, resolvesTo = null)
|
||||
UReturnExpression
|
||||
USimpleNameReferenceExpression (identifier = bindingContext)
|
||||
UBreakExpression (label = null)
|
||||
@@ -1,25 +0,0 @@
|
||||
public final class WhenAndDestructingKt {
|
||||
public static final fun getElementsAdditionalResolve(@org.jetbrains.annotations.NotNull string: java.lang.String) : java.lang.String {
|
||||
var arr: java.util.List<? extends java.lang.String> = listOf("1", "2")
|
||||
switch (string) {
|
||||
"aaaa" -> {
|
||||
return "bindingContext"
|
||||
break
|
||||
}
|
||||
|
||||
"empty-switch" -> {
|
||||
break
|
||||
}
|
||||
|
||||
-> {
|
||||
@null var var837f2350: <ErrorType> = arr
|
||||
@null var bindingContext: java.lang.String = var837f2350.<anonymous class>()
|
||||
@null var statementFilter: java.lang.String = var837f2350.<anonymous class>()
|
||||
return bindingContext
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
UFile (package = )
|
||||
UClass (name = WhenIsKt)
|
||||
UMethod (name = foo)
|
||||
UParameter (name = bar)
|
||||
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
|
||||
UBlockExpression
|
||||
UReturnExpression
|
||||
USwitchExpression
|
||||
USimpleNameReferenceExpression (identifier = bar)
|
||||
UExpressionList (when)
|
||||
USwitchClauseExpressionWithBody
|
||||
UBinaryExpressionWithType
|
||||
USimpleNameReferenceExpression (identifier = it)
|
||||
UTypeReferenceExpression (name = java.lang.String)
|
||||
UExpressionList (when_entry)
|
||||
USimpleNameReferenceExpression (identifier = bar)
|
||||
UBreakExpression (label = null)
|
||||
USwitchClauseExpressionWithBody
|
||||
UBinaryExpressionWithType
|
||||
USimpleNameReferenceExpression (identifier = it)
|
||||
UTypeReferenceExpression (name = java.lang.String)
|
||||
UExpressionList (when_entry)
|
||||
ULiteralExpression (value = "<error>")
|
||||
UBreakExpression (label = null)
|
||||
@@ -1,17 +0,0 @@
|
||||
public final class WhenIsKt {
|
||||
public static final fun foo(@org.jetbrains.annotations.NotNull bar: java.lang.Object) : java.lang.String {
|
||||
return switch (bar) {
|
||||
it is java.lang.String -> {
|
||||
bar
|
||||
break
|
||||
}
|
||||
|
||||
it !is java.lang.String -> {
|
||||
"<error>"
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
UFile (package = )
|
||||
UClass (name = WhenStringLiteralKt)
|
||||
UField (name = a)
|
||||
UAnnotation (fqName = org.jetbrains.annotations.Nullable)
|
||||
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0))
|
||||
UIdentifier (Identifier (readLine))
|
||||
USimpleNameReferenceExpression (identifier = readLine, resolvesTo = null)
|
||||
UField (name = b)
|
||||
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
|
||||
USwitchExpression
|
||||
USimpleNameReferenceExpression (identifier = a)
|
||||
UExpressionList (when)
|
||||
USwitchClauseExpressionWithBody
|
||||
ULiteralExpression (value = "abc")
|
||||
UExpressionList (when_entry)
|
||||
ULiteralExpression (value = 1)
|
||||
UBreakExpression (label = null)
|
||||
USwitchClauseExpressionWithBody
|
||||
ULiteralExpression (value = "def")
|
||||
ULiteralExpression (value = "ghi")
|
||||
UExpressionList (when_entry)
|
||||
ULiteralExpression (value = 2)
|
||||
UBreakExpression (label = null)
|
||||
USwitchClauseExpressionWithBody
|
||||
UExpressionList (when_entry)
|
||||
ULiteralExpression (value = 3)
|
||||
UBreakExpression (label = null)
|
||||
UMethod (name = getA)
|
||||
UMethod (name = getB)
|
||||
UMethod (name = <no name provided>)
|
||||
UBlockExpression
|
||||
ULiteralExpression (value = "abc1")
|
||||
ULiteralExpression (value = "def1")
|
||||
@@ -1,27 +0,0 @@
|
||||
public final class WhenStringLiteralKt {
|
||||
@org.jetbrains.annotations.Nullable private static final var a: java.lang.String = readLine()
|
||||
@org.jetbrains.annotations.NotNull private static final var b: int = switch (a) {
|
||||
"abc" -> {
|
||||
1
|
||||
break
|
||||
}
|
||||
|
||||
"def", "ghi" -> {
|
||||
2
|
||||
break
|
||||
}
|
||||
|
||||
-> {
|
||||
3
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static final fun getA() : java.lang.String = UastEmptyExpression
|
||||
public static final fun getB() : int = UastEmptyExpression
|
||||
public static final fun <no name provided>() : void {
|
||||
"abc1"
|
||||
"def1"
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2019 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.uast.test.kotlin
|
||||
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.extensions.Extensions
|
||||
import com.intellij.openapi.util.Disposer
|
||||
import com.intellij.patterns.uast.injectionHostUExpression
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.impl.source.resolve.reference.PsiReferenceContributorEP
|
||||
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry
|
||||
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistryImpl
|
||||
import com.intellij.psi.util.PropertyUtil
|
||||
import com.intellij.testFramework.LightProjectDescriptor
|
||||
import com.intellij.testFramework.registerServiceInstance
|
||||
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
|
||||
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
|
||||
import org.jetbrains.kotlin.test.JUnit3WithIdeaConfigurationRunner
|
||||
import org.jetbrains.uast.UExpression
|
||||
import org.jetbrains.uast.evaluateString
|
||||
import org.jetbrains.uast.toUElementOfType
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import kotlin.test.fail
|
||||
|
||||
@RunWith(JUnit3WithIdeaConfigurationRunner::class)
|
||||
class KotlinUastReferencesTest : KotlinLightCodeInsightFixtureTestCase() {
|
||||
|
||||
override fun getProjectDescriptor(): LightProjectDescriptor = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
|
||||
|
||||
|
||||
@Test
|
||||
fun `test original getter is visible when reference is under renaming`() {
|
||||
|
||||
registerReferenceProviders(testRootDisposable) {
|
||||
registerUastReferenceProvider(injectionHostUExpression(), uastInjectionHostReferenceProvider { _, psiLanguageInjectionHost ->
|
||||
arrayOf(GetterReference("KotlinBean", psiLanguageInjectionHost))
|
||||
})
|
||||
}
|
||||
|
||||
myFixture.configureByText(
|
||||
"KotlinBean.kt", """
|
||||
data class KotlinBean(val myF<caret>ield: String)
|
||||
|
||||
val reference = "myField"
|
||||
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
myFixture.renameElementAtCaret("myRenamedField")
|
||||
|
||||
myFixture.checkResult(
|
||||
"""
|
||||
data class KotlinBean(val myRenamedField: String)
|
||||
|
||||
val reference = "myRenamedField"
|
||||
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class GetterReference(
|
||||
val className: String,
|
||||
psiElement: PsiElement
|
||||
) : PsiReferenceBase<PsiElement>(psiElement) {
|
||||
override fun resolve(): PsiMethod? {
|
||||
val psiClass = JavaPsiFacade.getInstance(element.project).findClass(className, element.resolveScope) ?: return null
|
||||
val name = element.toUElementOfType<UExpression>()?.evaluateString() ?: return null
|
||||
return PropertyUtil.getGetters(psiClass, name).firstOrNull()
|
||||
}
|
||||
|
||||
override fun handleElementRename(newElementName: String): PsiElement {
|
||||
val resolve = resolve()
|
||||
?: fail("can't resolve during rename, looks like someone renamed or removed the source element before updating references")
|
||||
|
||||
val newName =
|
||||
if (PropertyUtil.getPropertyName(resolve) != null)
|
||||
PropertyUtil.getPropertyName(newElementName) ?: newElementName
|
||||
else newElementName
|
||||
|
||||
return super.handleElementRename(newName)
|
||||
}
|
||||
|
||||
override fun getVariants(): Array<Any> = emptyArray()
|
||||
}
|
||||
|
||||
|
||||
fun registerReferenceProviders(disposable: Disposable, registerContributors: PsiReferenceRegistrar.() -> Unit) {
|
||||
registerReferenceContributor(object : PsiReferenceContributor() {
|
||||
override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) = registrar.registerContributors()
|
||||
}, disposable)
|
||||
}
|
||||
|
||||
fun registerReferenceContributor(contributor: PsiReferenceContributor, disposable: Disposable) {
|
||||
val referenceContributorEp = Extensions.getArea(null).getExtensionPoint<PsiReferenceContributorEP>(PsiReferenceContributor.EP_NAME.name)
|
||||
|
||||
val contributorEp = object : PsiReferenceContributorEP() {
|
||||
override fun getInstance(): PsiReferenceContributor = contributor
|
||||
}
|
||||
|
||||
referenceContributorEp.registerExtension(contributorEp)
|
||||
|
||||
val application = ApplicationManager.getApplication()
|
||||
|
||||
//we need a fresh ReferenceProvidersRegistry after updating ReferenceContributors
|
||||
val oldReferenceProviderRegistry =
|
||||
application.picoContainer.getComponentInstance(ReferenceProvidersRegistry::class.java) as ReferenceProvidersRegistry
|
||||
application.registerServiceInstance(ReferenceProvidersRegistry::class.java, ReferenceProvidersRegistryImpl())
|
||||
|
||||
Disposer.register(disposable, Disposable {
|
||||
referenceContributorEp.unregisterExtension(contributorEp)
|
||||
application.registerServiceInstance(ReferenceProvidersRegistry::class.java, oldReferenceProviderRegistry)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user