Move KtFunctionLiteral.findLabelAndCall from kotlin-ide.frontend-independent to compiler.psi

I want to be able to use this function in `:kotlin-scripting-ide-common` & `frontend-independent`

This commit also allows to remove copy-pasted `findLabelAndCall` in `scripting-ide-services` module
This commit is contained in:
Nikita Bobko
2021-07-02 19:34:10 +02:00
parent a8a6f51a0e
commit 339231b05e
3 changed files with 27 additions and 40 deletions

View File

@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtFunctionLiteral
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.psiUtil.findLabelAndCall
import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils

View File

@@ -1,40 +0,0 @@
/*
* 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.
*/
package org.jetbrains.kotlin.idea.util
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
/**
* This extension method was copied from `idea:idea-frontend-independent` module because it's the only method that
* included in `idea:idea-frontend-independent` and needed for performing REPL completion
*/
@Suppress("unused")
fun KtFunctionLiteral.findLabelAndCall(): Pair<Name?, KtCallExpression?> {
val literalParent = (this.parent as KtLambdaExpression).parent
fun KtValueArgument.callExpression(): KtCallExpression? {
val parent = parent
return (if (parent is KtValueArgumentList) parent else this).parent as? KtCallExpression
}
when (literalParent) {
is KtLabeledExpression -> {
val callExpression = (literalParent.parent as? KtValueArgument)?.callExpression()
return Pair(literalParent.getLabelNameAsName(), callExpression)
}
is KtValueArgument -> {
val callExpression = literalParent.callExpression()
val label = (callExpression?.calleeExpression as? KtSimpleNameExpression)?.getReferencedNameAsName()
return Pair(label, callExpression)
}
else -> {
return Pair(null, null)
}
}
}