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:
@@ -546,6 +546,32 @@ fun KtFunctionLiteral.getOrCreateParameterList(): KtParameterList {
|
|||||||
return newParameterList
|
return newParameterList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun KtCallExpression.getOrCreateValueArgumentList(): KtValueArgumentList {
|
fun KtCallExpression.getOrCreateValueArgumentList(): KtValueArgumentList {
|
||||||
valueArgumentList?.let { return it }
|
valueArgumentList?.let { return it }
|
||||||
return addAfter(
|
return addAfter(
|
||||||
|
|||||||
+1
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.name.Name
|
|||||||
import org.jetbrains.kotlin.psi.KtExpression
|
import org.jetbrains.kotlin.psi.KtExpression
|
||||||
import org.jetbrains.kotlin.psi.KtFunctionLiteral
|
import org.jetbrains.kotlin.psi.KtFunctionLiteral
|
||||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.findLabelAndCall
|
||||||
import org.jetbrains.kotlin.renderer.render
|
import org.jetbrains.kotlin.renderer.render
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||||
|
|||||||
-40
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user