"Remove argument name" intention: remove array literal brackets for annotation vararg argument
#KT-32318 Fixed
This commit is contained in:
committed by
Dmitry Gridin
parent
d33d1913cb
commit
9786564d75
@@ -19,16 +19,12 @@ package org.jetbrains.kotlin.idea.intentions
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
|
||||
import org.jetbrains.kotlin.psi.KtCallElement
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.KtValueArgument
|
||||
import org.jetbrains.kotlin.psi.KtValueArgumentList
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.calls.components.isVararg
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
|
||||
|
||||
class RemoveArgumentNameIntention
|
||||
: SelfTargetingRangeIntention<KtValueArgument>(KtValueArgument::class.java, "Remove argument name") {
|
||||
|
||||
class RemoveArgumentNameIntention : SelfTargetingRangeIntention<KtValueArgument>(KtValueArgument::class.java, "Remove argument name") {
|
||||
override fun applicabilityRange(element: KtValueArgument): TextRange? {
|
||||
if (!element.isNamed()) return null
|
||||
|
||||
@@ -37,8 +33,7 @@ class RemoveArgumentNameIntention
|
||||
if (arguments.asSequence().takeWhile { it != element }.any { it.isNamed() }) return null
|
||||
|
||||
val callExpr = argumentList.parent as? KtCallElement ?: return null
|
||||
val resolvedCall = callExpr.resolveToCall() ?: return null
|
||||
val argumentMatch = resolvedCall.getArgumentMapping(element) as? ArgumentMatch ?: return null
|
||||
val argumentMatch = callExpr.resolveToArgumentMatch(element) ?: return null
|
||||
if (argumentMatch.valueParameter.index != arguments.indexOf(element)) return null
|
||||
|
||||
val expression = element.getArgumentExpression() ?: return null
|
||||
@@ -46,7 +41,23 @@ class RemoveArgumentNameIntention
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtValueArgument, editor: Editor?) {
|
||||
val newArgument = KtPsiFactory(element).createArgument(element.getArgumentExpression()!!, null, element.getSpreadElement() != null)
|
||||
element.replace(newArgument)
|
||||
val argumentExpr = element.getArgumentExpression() ?: return
|
||||
val argumentList = element.parent as? KtValueArgumentList ?: return
|
||||
val callExpr = argumentList.parent as? KtCallElement ?: return
|
||||
val psiFactory = KtPsiFactory(element)
|
||||
if (argumentExpr is KtCollectionLiteralExpression && callExpr.resolveToArgumentMatch(element)?.valueParameter?.isVararg == true) {
|
||||
argumentExpr.getInnerExpressions()
|
||||
.map { psiFactory.createArgument(it) }
|
||||
.reversed()
|
||||
.forEach { argumentList.addArgumentAfter(it, element) }
|
||||
argumentList.removeArgument(element)
|
||||
} else {
|
||||
val newArgument = psiFactory.createArgument(argumentExpr, null, element.getSpreadElement() != null)
|
||||
element.replace(newArgument)
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtCallElement.resolveToArgumentMatch(element: KtValueArgument): ArgumentMatch? {
|
||||
return resolveToCall()?.getArgumentMapping(element) as? ArgumentMatch ?: return null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
annotation class AnnWithArray(val value: Array<String>)
|
||||
|
||||
interface Result {
|
||||
@AnnWithArray(<caret>value = ["foo", "bar"])
|
||||
val res1: Any
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
annotation class AnnWithArray(val value: Array<String>)
|
||||
|
||||
interface Result {
|
||||
@AnnWithArray(<caret>["foo", "bar"])
|
||||
val res1: Any
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
annotation class AnnWithVararg(vararg val value: String, val s: String)
|
||||
|
||||
interface Result {
|
||||
@AnnWithVararg(<caret>value = ["foo", "bar"], s = "")
|
||||
val res2: Any
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
annotation class AnnWithVararg(vararg val value: String, val s: String)
|
||||
|
||||
interface Result {
|
||||
@AnnWithVararg(<caret>"foo", "bar", s = "")
|
||||
val res2: Any
|
||||
}
|
||||
@@ -12618,6 +12618,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeArgumentName"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("arrayLiteral.kt")
|
||||
public void testArrayLiteral() throws Exception {
|
||||
runTest("idea/testData/intentions/removeArgumentName/arrayLiteral.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("arrayLiteralForVararg.kt")
|
||||
public void testArrayLiteralForVararg() throws Exception {
|
||||
runTest("idea/testData/intentions/removeArgumentName/arrayLiteralForVararg.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("namedArgumentBefore.kt")
|
||||
public void testNamedArgumentBefore() throws Exception {
|
||||
runTest("idea/testData/intentions/removeArgumentName/namedArgumentBefore.kt");
|
||||
|
||||
Reference in New Issue
Block a user