Intentions: Implement "Add names to call arguments" intention

#KT-14729 Fixed
This commit is contained in:
Alexey Sedunov
2016-11-28 15:17:43 +03:00
parent 890578a6a0
commit c773afdbfa
25 changed files with 262 additions and 1 deletions

View File

@@ -338,6 +338,10 @@ These artifacts include extensions for the types available in the latter JDKs, s
- [`KT-14044`](https://youtrack.jetbrains.com/issue/KT-14044) Fix exception on deleting unused declaration in IDEA 2016.3
- [`KT-14019`](https://youtrack.jetbrains.com/issue/KT-14019) Create from Usage: Support generation of abstract members for superclasses
##### New features
- [`KT-14729`](https://youtrack.jetbrains.com/issue/KT-14729) Implement "Add names to call arguments" intention
#### Refactorings
- [`KT-14583`](https://youtrack.jetbrains.com/issue/KT-14583) Change Signature: Use new signature when looking for redeclaration conflicts

View File

@@ -0,0 +1,7 @@
fun foo(x: Int, y: Int, comment: String) {
}
fun test() {
<spot>foo(x = 24, y = 42, comment = "Hello")</spot>
}

View File

@@ -0,0 +1,7 @@
fun foo(x: Int, y: Int, comment: String) {
}
fun test() {
<spot>foo(24, 42, "Hello")</spot>
}

View File

@@ -0,0 +1,5 @@
<html>
<body>
This intention adds names to all positional arguments of a function call according to the named argument syntax.
</body>
</html>

View File

@@ -1,4 +1,4 @@
<idea-plugin version="2" url="http://kotlinlang.org" xmlns:xi="http://www.w3.org/2001/XInclude" allow-bundled-update="true">
<idea-plugin xmlns:xi="http://www.w3.org/2001/XInclude" version="2" url="http://kotlinlang.org" allow-bundled-update="true">
<id>org.jetbrains.kotlin</id>
<name>Kotlin</name>
@@ -1451,6 +1451,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.AddNamesToCallArgumentsIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupName="Kotlin"

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2010-2016 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.KtCallElement
import org.jetbrains.kotlin.psi.KtLambdaArgument
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtValueArgument
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatchStatus
import org.jetbrains.kotlin.resolve.calls.model.VarargValueArgument
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class AddNamesToCallArgumentsIntention : SelfTargetingRangeIntention<KtCallElement>(
KtCallElement::class.java,
"Add names to call arguments"
) {
override fun applicabilityRange(element: KtCallElement): TextRange? {
val arguments = element.valueArguments
if (arguments.all { it.isNamed() }) return null
val resolvedCall = element.getResolvedCall(element.analyze(BodyResolveMode.PARTIAL)) ?: return null
if (!resolvedCall.resultingDescriptor.hasStableParameterNames()) return null
for (argument in arguments) {
val argumentMatch = resolvedCall.getArgumentMapping(argument) as? ArgumentMatch ?: return null
if (argumentMatch.status != ArgumentMatchStatus.SUCCESS) return null
if (argumentMatch.valueParameter.varargElementType != null) {
val varargArgument = resolvedCall.valueArguments[argumentMatch.valueParameter] as? VarargValueArgument ?: return null
if (varargArgument.arguments.size != 1) return null
}
}
return element.calleeExpression?.textRange
}
override fun applyTo(element: KtCallElement, editor: Editor?) {
val arguments = element.valueArguments
val resolvedCall = element.getResolvedCall(element.analyze(BodyResolveMode.PARTIAL)) ?: return
for (argument in arguments) {
if (argument !is KtValueArgument || argument is KtLambdaArgument) continue
val argumentMatch = resolvedCall.getArgumentMapping(argument) as? ArgumentMatch ?: continue
val name = argumentMatch.valueParameter.name
val newArgument = KtPsiFactory(element).createArgument(argument.getArgumentExpression()!!,
name,
argument.getSpreadElement() != null)
argument.replace(newArgument)
}
}
}

View File

@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.AddNamesToCallArgumentsIntention

View File

@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
fun foo(s: String, b: Boolean){}
fun bar() {
<caret>foo(s = "", b = true)
}

View File

@@ -0,0 +1,8 @@
// IS_APPLICABLE: false
// ERROR: None of the following functions can be called with the arguments supplied: <br>public fun foo(s: String, b: Boolean, c: Char): Unit defined in root package<br>public fun foo(s: String, b: Boolean, p: Int): Unit defined in root package
fun foo(s: String, b: Boolean, p: Int){}
fun foo(s: String, b: Boolean, c: Char){}
fun bar() {
<caret>foo("", true)
}

View File

@@ -0,0 +1,7 @@
// ERROR: No value passed for parameter p
fun foo(s: String, b: Boolean, p: Int){}
fun bar() {
<caret>foo("", true)
}

View File

@@ -0,0 +1,7 @@
// ERROR: No value passed for parameter p
fun foo(s: String, b: Boolean, p: Int){}
fun bar() {
<caret>foo(s = "", b = true)
}

View File

@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun f() {
java.io.File(<caret>"file")
}

View File

@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
fun foo(s: String){}
fun bar() {
foo(<caret>"")
}

View File

@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
// ERROR: Unresolved reference: foo
fun bar() {
<caret>foo("", true)
}

View File

@@ -0,0 +1,5 @@
fun foo(s: String, b: Boolean){}
fun bar() {
<caret>foo("", true)
}

View File

@@ -0,0 +1,5 @@
fun foo(s: String, b: Boolean){}
fun bar() {
<caret>foo(s = "", b = true)
}

View File

@@ -0,0 +1,3 @@
open class C(p1: Int, p2: Int)
class D : <caret>C(1, 2)

View File

@@ -0,0 +1,3 @@
open class C(p1: Int, p2: Int)
class D : <caret>C(p1 = 1, p2 = 2)

View File

@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
fun foo(n: Int, vararg s: String){}
fun bar() {
<caret>foo(1, "2", "3")
}

View File

@@ -0,0 +1,5 @@
fun foo(n: Int, vararg s: String){}
fun bar() {
<caret>foo(1, "")
}

View File

@@ -0,0 +1,5 @@
fun foo(n: Int, vararg s: String){}
fun bar() {
<caret>foo(n = 1, s = "")
}

View File

@@ -0,0 +1,5 @@
fun foo(n: Int, vararg s: String){}
fun bar(array: Array<String>) {
<caret>foo(1, *array)
}

View File

@@ -0,0 +1,5 @@
fun foo(n: Int, vararg s: String){}
fun bar(array: Array<String>) {
<caret>foo(n = 1, s = *array)
}

View File

@@ -1,5 +1,6 @@
// "Make bar internal" "false"
// ACTION: Convert property initializer to getter
// ACTION: Add names to call arguments
// ERROR: Cannot access 'bar': it is private in 'First'
private data class Data(val x: Int)

View File

@@ -417,6 +417,81 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/addNamesToCallArguments")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddNamesToCallArguments extends AbstractIntentionTest {
public void testAllFilesPresentInAddNamesToCallArguments() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addNamesToCallArguments"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("allNamed.kt")
public void testAllNamed() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addNamesToCallArguments/allNamed.kt");
doTest(fileName);
}
@TestMetadata("ambiguousCall.kt")
public void testAmbiguousCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addNamesToCallArguments/ambiguousCall.kt");
doTest(fileName);
}
@TestMetadata("incompleteCall.kt")
public void testIncompleteCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addNamesToCallArguments/incompleteCall.kt");
doTest(fileName);
}
@TestMetadata("javaMethod.kt")
public void testJavaMethod() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addNamesToCallArguments/javaMethod.kt");
doTest(fileName);
}
@TestMetadata("notOnCallee.kt")
public void testNotOnCallee() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addNamesToCallArguments/notOnCallee.kt");
doTest(fileName);
}
@TestMetadata("notResolved.kt")
public void testNotResolved() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addNamesToCallArguments/notResolved.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addNamesToCallArguments/simple.kt");
doTest(fileName);
}
@TestMetadata("superClassConstructor.kt")
public void testSuperClassConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addNamesToCallArguments/superClassConstructor.kt");
doTest(fileName);
}
@TestMetadata("varargMultiple.kt")
public void testVarargMultiple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addNamesToCallArguments/varargMultiple.kt");
doTest(fileName);
}
@TestMetadata("varargSingle.kt")
public void testVarargSingle() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addNamesToCallArguments/varargSingle.kt");
doTest(fileName);
}
@TestMetadata("varargSingleWithSpread.kt")
public void testVarargSingleWithSpread() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addNamesToCallArguments/varargSingleWithSpread.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/addOperatorModifier")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)