"Remove argument name" intention

This commit is contained in:
Valentin Kipyatkov
2015-06-23 21:32:20 +03:00
parent 6988297511
commit 46ad1b8573
19 changed files with 195 additions and 0 deletions
@@ -0,0 +1 @@
list.joinToString("\n")
@@ -0,0 +1 @@
list.joinToString(<spot>separator = </spot>"\n")
@@ -0,0 +1,5 @@
<html>
<body>
This intention removes name from a named argument to use positional argument syntax.
</body>
</html>
+5
View File
@@ -928,6 +928,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveArgumentNameIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.DeprecatedCallableAddReplaceWithInspection"
displayName="Add 'replaceWith' argument to 'deprecated' annotation"
groupName="Kotlin"
@@ -0,0 +1,54 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.analyze
import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.JetValueArgument
import org.jetbrains.kotlin.psi.JetValueArgumentList
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
public class RemoveArgumentNameIntention
: JetSelfTargetingRangeIntention<JetValueArgument>(javaClass(), "Remove argument name") {
override fun applicabilityRange(element: JetValueArgument): TextRange? {
if (!element.isNamed()) return null
val argumentList = element.getParent() as? JetValueArgumentList ?: return null
val arguments = argumentList.getArguments()
if (arguments.takeWhile { it != element }.any { it.isNamed() }) return null
val callExpr = argumentList.getParent() as? JetExpression ?: return null
val resolvedCall = callExpr.getResolvedCall(callExpr.analyze(BodyResolveMode.PARTIAL)) ?: return null
val argumentMatch = resolvedCall.getArgumentMapping(element) as? ArgumentMatch ?: return null
if (argumentMatch.valueParameter.getIndex() != arguments.indexOf(element)) return null
val expression = element.getArgumentExpression() ?: return null
return TextRange(element.startOffset, expression.startOffset)
}
override fun applyTo(element: JetValueArgument, editor: Editor) {
val newArgument = JetPsiFactory(element).createArgument(element.getArgumentExpression()!!, null, element.getSpreadElement() != null)
element.replace(newArgument)
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.RemoveArgumentNameIntention
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
fun foo(s: String, b: Boolean){}
fun bar() {
foo(s = "", <caret>b = true)
}
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
fun foo(s: String, b: Boolean){}
fun bar() {
foo("", <caret>b = )
}
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
fun foo(s: String = "", b: Boolean){}
fun bar() {
foo(<caret>b = true)
}
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
fun foo(s: String, b: Boolean){}
fun bar() {
foo("", b = t<caret>rue)
}
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
fun foo(s: String, b: Boolean){}
fun bar() {
foo("", <caret>true)
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
// ERROR: Unresolved reference: foo
fun bar() {
foo(<caret>b = true)
}
+5
View File
@@ -0,0 +1,5 @@
fun foo(s: String, b: Boolean){}
fun bar() {
foo("", b = <caret>true)
}
@@ -0,0 +1,5 @@
fun foo(s: String, b: Boolean){}
fun bar() {
foo("", <caret>true)
}
+5
View File
@@ -0,0 +1,5 @@
fun foo(s: String, b: Boolean){}
fun bar() {
foo("", <caret>b = true)
}
@@ -0,0 +1,5 @@
fun foo(s: String, b: Boolean){}
fun bar() {
foo("", <caret>true)
}
+5
View File
@@ -0,0 +1,5 @@
fun foo(vararg s: String){}
fun bar(array: Array<String>) {
foo(<caret>s = *array)
}
@@ -0,0 +1,5 @@
fun foo(vararg s: String){}
fun bar(array: Array<String>) {
foo(<caret>*array)
}
@@ -5605,6 +5605,69 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/removeArgumentName")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveArgumentName extends AbstractIntentionTest {
public void testAllFilesPresentInRemoveArgumentName() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeArgumentName"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("namedArgumentBefore.kt")
public void testNamedArgumentBefore() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeArgumentName/namedArgumentBefore.kt");
doTest(fileName);
}
@TestMetadata("noExpression.kt")
public void testNoExpression() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeArgumentName/noExpression.kt");
doTest(fileName);
}
@TestMetadata("notInPosition.kt")
public void testNotInPosition() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeArgumentName/notInPosition.kt");
doTest(fileName);
}
@TestMetadata("notInRange.kt")
public void testNotInRange() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeArgumentName/notInRange.kt");
doTest(fileName);
}
@TestMetadata("notNamed.kt")
public void testNotNamed() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeArgumentName/notNamed.kt");
doTest(fileName);
}
@TestMetadata("notResolved.kt")
public void testNotResolved() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeArgumentName/notResolved.kt");
doTest(fileName);
}
@TestMetadata("range.kt")
public void testRange() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeArgumentName/range.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeArgumentName/simple.kt");
doTest(fileName);
}
@TestMetadata("star.kt")
public void testStar() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeArgumentName/star.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/removeBraces")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)