Consistently convert local functions to UAST
This commit is contained in:
@@ -156,10 +156,26 @@ class KotlinUastLanguagePlugin : UastLanguagePlugin {
|
|||||||
KotlinUClass.create(lightClass, givenParent)
|
KotlinUClass.create(lightClass, givenParent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
is KtFunction -> el<UMethod> {
|
is KtFunction ->
|
||||||
val lightMethod = LightClassUtil.getLightClassMethod(original) ?: return null
|
if (original.isLocal) {
|
||||||
convertDeclaration(lightMethod, givenParent, requiredType)
|
el<ULambdaExpression> {
|
||||||
}
|
if (original.name.isNullOrEmpty()) {
|
||||||
|
createLocalFunctionLambdaExpression(original, givenParent)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
val uDeclarationsExpression = createLocalFunctionDeclaration(original, givenParent)
|
||||||
|
val localFunctionVar = uDeclarationsExpression.declarations.single() as KotlinLocalFunctionUVariable
|
||||||
|
localFunctionVar.uastInitializer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
el<UMethod> {
|
||||||
|
val lightMethod = LightClassUtil.getLightClassMethod(original) ?: return null
|
||||||
|
convertDeclaration(lightMethod, givenParent, requiredType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
is KtPropertyAccessor -> el<UMethod> {
|
is KtPropertyAccessor -> el<UMethod> {
|
||||||
val lightMethod = LightClassUtil.getLightClassAccessorMethod(original) ?: return null
|
val lightMethod = LightClassUtil.getLightClassAccessorMethod(original) ?: return null
|
||||||
convertDeclaration(lightMethod, givenParent, requiredType)
|
convertDeclaration(lightMethod, givenParent, requiredType)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import org.jetbrains.uast.kotlin.psi.UastKotlinPsiParameter
|
|||||||
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable
|
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable
|
||||||
|
|
||||||
|
|
||||||
private class KotlinLocalFunctionUVariable(
|
internal class KotlinLocalFunctionUVariable(
|
||||||
val function: KtFunction,
|
val function: KtFunction,
|
||||||
override val psi: PsiVariable,
|
override val psi: PsiVariable,
|
||||||
givenParent: UElement?
|
givenParent: UElement?
|
||||||
@@ -50,7 +50,7 @@ private class KotlinLocalFunctionULambdaExpression(
|
|||||||
|
|
||||||
|
|
||||||
fun createLocalFunctionDeclaration(function: KtFunction, parent: UElement?): UDeclarationsExpression {
|
fun createLocalFunctionDeclaration(function: KtFunction, parent: UElement?): UDeclarationsExpression {
|
||||||
return KotlinUDeclarationsExpression(parent).apply {
|
return KotlinUDeclarationsExpression(null, parent, function).apply {
|
||||||
val functionVariable = UastKotlinPsiVariable.create(function.name.orAnonymous(), function, this)
|
val functionVariable = UastKotlinPsiVariable.create(function.name.orAnonymous(), function, this)
|
||||||
declarations = listOf(KotlinLocalFunctionUVariable(function, functionVariable, this))
|
declarations = listOf(KotlinLocalFunctionUVariable(function, functionVariable, this))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ abstract class AbstractKotlinRenderLogTest : AbstractKotlinUastTest(), RenderLog
|
|||||||
if (expectedParents != null) {
|
if (expectedParents != null) {
|
||||||
assertNotNull("Expected to be able to convert PSI element $element", uElement)
|
assertNotNull("Expected to be able to convert PSI element $element", uElement)
|
||||||
val parents = generateSequence(uElement!!.uastParent) { it.uastParent }.joinToString { it.asLogString() }
|
val parents = generateSequence(uElement!!.uastParent) { it.uastParent }.joinToString { it.asLogString() }
|
||||||
assertEquals("Inconsistent parents for $uElement", expectedParents, parents)
|
assertEquals("Inconsistent parents for $uElement (converted from $element)", expectedParents, parents)
|
||||||
}
|
}
|
||||||
super.visitElement(element)
|
super.visitElement(element)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user