Unwrap: function parameter (KT-14788)

#KT-14788 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-03-12 10:07:07 +09:00
committed by Nikolay Krasko
parent 6838873a93
commit c1f43558b9
9 changed files with 102 additions and 0 deletions
@@ -0,0 +1,30 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. 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.codeInsight.unwrap
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtValueArgument
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class KotlinFunctionParameterUnwrapper(key: String) : KotlinUnwrapRemoveBase(key) {
override fun isApplicableTo(element: PsiElement): Boolean {
if (element !is KtCallExpression) return false
if (element.valueArguments.size != 1) return false
val argument = element.parent as? KtValueArgument ?: return false
if (argument.getStrictParentOfType<KtCallExpression>() == null) return false
return true
}
override fun doUnwrap(element: PsiElement?, context: Context?) {
val function = element as? KtCallExpression ?: return
val argument = element.valueArguments.firstOrNull()?.getArgumentExpression() ?: return
context?.extractFromExpression(argument, function)
context?.delete(function)
}
}
@@ -34,6 +34,7 @@ public class KotlinUnwrapDescriptor extends UnwrapDescriptorBase {
new KotlinUnwrappers.KotlinFinallyUnwrapper("unwrap.expression"),
new KotlinUnwrappers.KotlinFinallyRemover("remove.expression"),
new KotlinLambdaUnwrapper("unwrap.expression"),
new KotlinFunctionParameterUnwrapper("unwrap.expression")
};
}
}