New Intention Action: Replace a dot-qualified function call with an infix function call.
This commit is contained in:
@@ -343,6 +343,7 @@ fun main(args: Array<String>) {
|
||||
model("intentions/reconstructedType", testMethod = "doTestReconstructType")
|
||||
model("intentions/removeUnnecessaryParentheses", testMethod = "doTestRemoveUnnecessaryParentheses")
|
||||
model("intentions/replaceWithDotQualifiedMethodCall", testMethod = "doTestReplaceWithDotQualifiedMethodCall")
|
||||
model("intentions/replaceWithInfixFunctionCall", testMethod = "doTestReplaceWithInfixFunctionCall")
|
||||
}
|
||||
|
||||
testClass(javaClass<AbstractHierarchyTest>()) {
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
x foo 1
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
<spot>x.foo(1)</spot>
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention converts a dot qualified method call to an infix function call.
|
||||
</body>
|
||||
</html>
|
||||
@@ -493,6 +493,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.intentions.ReplaceWithInfixFunctionCallIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<project.converterProvider implementation="org.jetbrains.jet.plugin.converters.JetRunConfigurationSettingsFormatConverterProvider"/>
|
||||
|
||||
</extensions>
|
||||
|
||||
@@ -224,6 +224,8 @@ add.name.to.argument.action=Add name to argument...
|
||||
add.name.to.parameter.name.chooser.title=Choose parameter name
|
||||
replace.with.dot.qualified.method.call.intention=Replace with simple method call
|
||||
replace.with.dot.qualified.method.call.intention.family=Replace with simple method call
|
||||
replace.with.infix.function.call.intention=Replace with infix function call
|
||||
replace.with.infix.function.call.intention.family=Replace with infix function call
|
||||
|
||||
property.is.implemented.too.many=Has implementations
|
||||
property.is.overridden.too.many=Is overridden in subclasses
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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.jet.plugin.intentions
|
||||
|
||||
import org.jetbrains.jet.lang.psi.JetCallExpression
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression
|
||||
import org.jetbrains.jet.lang.psi.JetValueArgumentList
|
||||
import org.jetbrains.jet.lang.psi.JetValueArgument
|
||||
import org.jetbrains.jet.lang.psi.JetPsiFactory
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil
|
||||
import org.jetbrains.jet.lang.psi.JetBinaryExpression
|
||||
import org.jetbrains.jet.lang.psi.JetParenthesizedExpression
|
||||
import org.jetbrains.jet.lang.psi.JetBinaryExpressionWithTypeRHS
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils
|
||||
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache
|
||||
import javax.naming.Binding
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext
|
||||
import org.jetbrains.jet.lang.psi.JetFile
|
||||
|
||||
public class ReplaceWithInfixFunctionCallIntention : JetSelfTargetingIntention<JetCallExpression>("replace.with.infix.function.call.intention", javaClass()) {
|
||||
override fun isApplicableTo(element: JetCallExpression): Boolean {
|
||||
val parent = element.getParent()
|
||||
|
||||
if (parent is JetDotQualifiedExpression) {
|
||||
val callee = element.getCalleeExpression()
|
||||
val typeArguments = element.getTypeArgumentList()
|
||||
val valueArguments = element.getValueArgumentList()
|
||||
val functionLiteralArguments = element.getFunctionLiteralArguments()
|
||||
val numOfTotalValueArguments = (valueArguments?.getArguments()?.size() ?: 0) + functionLiteralArguments.size()
|
||||
|
||||
if (typeArguments?.getArguments()?.size() ?: 0 == 0 &&
|
||||
numOfTotalValueArguments == 1 &&
|
||||
callee != null) {
|
||||
if (valueArguments?.getArguments()?.size() == 1 && valueArguments?.getArguments()?.first()?.isNamed() ?: false) {
|
||||
val file: JetFile = element.getContainingFile() as JetFile
|
||||
val bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache(file).getBindingContext()
|
||||
val descriptor = bindingContext.get(BindingContext.RESOLVED_CALL, callee)
|
||||
val valueArgumentsMap = descriptor?.getValueArguments()
|
||||
val firstArgument = valueArguments?.getArguments()?.first()
|
||||
|
||||
return valueArgumentsMap?.keySet()?.any { it.getName().asString() == firstArgument?.getArgumentName()?.getText() && it.getIndex() == 0 } ?: false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
override fun applyTo(element: JetCallExpression, editor: Editor) {
|
||||
val parent = element.getParent() as JetDotQualifiedExpression
|
||||
val leftHandText = parent.getReceiverExpression().getText()
|
||||
val rightHandTextStringBuilder = StringBuilder()
|
||||
val operatorText = element.getCalleeExpression()!!.getText()
|
||||
val valueArguments = element.getValueArgumentList()?.getArguments() ?: listOf<JetValueArgument>()
|
||||
val functionLiteralArguments = element.getFunctionLiteralArguments()
|
||||
|
||||
rightHandTextStringBuilder.append(
|
||||
if (valueArguments.size() > 0)
|
||||
JetPsiUnparsingUtils.parenthesizeIfNeeded(valueArguments.first().getArgumentExpression())
|
||||
else
|
||||
functionLiteralArguments.first().getText()
|
||||
)
|
||||
|
||||
val replacement = JetPsiFactory.createExpression(element.getProject(), "$leftHandText $operatorText ${rightHandTextStringBuilder.toString()}")
|
||||
|
||||
parent.replace(replacement)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
x.<caret>foo(1 + 2)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
x foo (1 + 2)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
(x.<caret>foo(1)).bar(2)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
(x foo 1).bar(2)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
x.<caret>foo(1).bar(2)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
(x foo 1).bar(2)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class Foo {
|
||||
fun foo(x: Int) {
|
||||
println("lol")
|
||||
}
|
||||
}
|
||||
|
||||
fun bar(baz: Foo) {
|
||||
baz.<caret>foo(x = 1)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class Foo {
|
||||
fun foo(x: Int) {
|
||||
println("lol")
|
||||
}
|
||||
}
|
||||
|
||||
fun bar(baz: Foo) {
|
||||
baz foo 1
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
x.<caret>foo { it * 2 }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
x foo { it * 2 }
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(x: Foo) {
|
||||
x?.<caret>foo(1)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(x: Foo) {
|
||||
x.<caret>foo(1, 2)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(x: Foo) {
|
||||
x.<caret>foo(bar = x)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
x!!.<caret>foo(1)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
x!! foo 1
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(x: foo) {
|
||||
x.<caret>foo
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// IS_APPLICABLE: false
|
||||
class Foo {
|
||||
fun foo(x: Int = 0, y: Int = 0) {
|
||||
println("lol")
|
||||
}
|
||||
}
|
||||
|
||||
fun bar(baz: Foo) {
|
||||
baz.<caret>foo(y = 1)
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(x: Foo) {
|
||||
x.<caret>foo(1) { it * 2 }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
x.<caret>foo(1)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Foo) {
|
||||
x foo 1
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(x: Foo) {
|
||||
x.<caret>foo()
|
||||
}
|
||||
@@ -122,6 +122,10 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes
|
||||
doTestIntention(path, new ReplaceWithDotQualifiedMethodCallIntention());
|
||||
}
|
||||
|
||||
public void doTestReplaceWithInfixFunctionCall(@NotNull String path) throws Exception {
|
||||
doTestIntention(path, new ReplaceWithInfixFunctionCallIntention());
|
||||
}
|
||||
|
||||
private void doTestIntention(@NotNull String path, @NotNull IntentionAction intentionAction) throws Exception {
|
||||
configureByFile(path);
|
||||
|
||||
|
||||
+80
-1
@@ -30,7 +30,7 @@ import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationTest;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@InnerTestClasses({CodeTransformationTestGenerated.IfToAssignment.class, CodeTransformationTestGenerated.IfToReturn.class, CodeTransformationTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationTestGenerated.WhenToAssignment.class, CodeTransformationTestGenerated.WhenToReturn.class, CodeTransformationTestGenerated.AssignmentToIf.class, CodeTransformationTestGenerated.AssignmentToWhen.class, CodeTransformationTestGenerated.PropertyToIf.class, CodeTransformationTestGenerated.PropertyToWhen.class, CodeTransformationTestGenerated.ReturnToIf.class, CodeTransformationTestGenerated.ReturnToWhen.class, CodeTransformationTestGenerated.IfToWhen.class, CodeTransformationTestGenerated.WhenToIf.class, CodeTransformationTestGenerated.Flatten.class, CodeTransformationTestGenerated.Merge.class, CodeTransformationTestGenerated.IntroduceSubject.class, CodeTransformationTestGenerated.EliminateSubject.class, CodeTransformationTestGenerated.Split.class, CodeTransformationTestGenerated.Join.class, CodeTransformationTestGenerated.ConvertMemberToExtension.class, CodeTransformationTestGenerated.ReconstructedType.class, CodeTransformationTestGenerated.RemoveUnnecessaryParentheses.class, CodeTransformationTestGenerated.ReplaceWithDotQualifiedMethodCall.class})
|
||||
@InnerTestClasses({CodeTransformationTestGenerated.IfToAssignment.class, CodeTransformationTestGenerated.IfToReturn.class, CodeTransformationTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationTestGenerated.WhenToAssignment.class, CodeTransformationTestGenerated.WhenToReturn.class, CodeTransformationTestGenerated.AssignmentToIf.class, CodeTransformationTestGenerated.AssignmentToWhen.class, CodeTransformationTestGenerated.PropertyToIf.class, CodeTransformationTestGenerated.PropertyToWhen.class, CodeTransformationTestGenerated.ReturnToIf.class, CodeTransformationTestGenerated.ReturnToWhen.class, CodeTransformationTestGenerated.IfToWhen.class, CodeTransformationTestGenerated.WhenToIf.class, CodeTransformationTestGenerated.Flatten.class, CodeTransformationTestGenerated.Merge.class, CodeTransformationTestGenerated.IntroduceSubject.class, CodeTransformationTestGenerated.EliminateSubject.class, CodeTransformationTestGenerated.Split.class, CodeTransformationTestGenerated.Join.class, CodeTransformationTestGenerated.ConvertMemberToExtension.class, CodeTransformationTestGenerated.ReconstructedType.class, CodeTransformationTestGenerated.RemoveUnnecessaryParentheses.class, CodeTransformationTestGenerated.ReplaceWithDotQualifiedMethodCall.class, CodeTransformationTestGenerated.ReplaceWithInfixFunctionCall.class})
|
||||
public class CodeTransformationTestGenerated extends AbstractCodeTransformationTest {
|
||||
@TestMetadata("idea/testData/intentions/branched/folding/ifToAssignment")
|
||||
public static class IfToAssignment extends AbstractCodeTransformationTest {
|
||||
@@ -1221,6 +1221,84 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/replaceWithInfixFunctionCall")
|
||||
public static class ReplaceWithInfixFunctionCall extends AbstractCodeTransformationTest {
|
||||
public void testAllFilesPresentInReplaceWithInfixFunctionCall() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/replaceWithInfixFunctionCall"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("binaryExpressionArgument.kt")
|
||||
public void testBinaryExpressionArgument() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/binaryExpressionArgument.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("doubleFunctionCall.kt")
|
||||
public void testDoubleFunctionCall() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/doubleFunctionCall.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("doubleFunctionCallWithoutParentheses.kt")
|
||||
public void testDoubleFunctionCallWithoutParentheses() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/doubleFunctionCallWithoutParentheses.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("firstParameterLabeled.kt")
|
||||
public void testFirstParameterLabeled() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/firstParameterLabeled.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("functionLiteralArgument.kt")
|
||||
public void testFunctionLiteralArgument() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/functionLiteralArgument.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("functionSafeCall.kt")
|
||||
public void testFunctionSafeCall() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/functionSafeCall.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("multipleArguments.kt")
|
||||
public void testMultipleArguments() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/multipleArguments.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("namedArgument.kt")
|
||||
public void testNamedArgument() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/namedArgument.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nullAssertedReceiver.kt")
|
||||
public void testNullAssertedReceiver() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/nullAssertedReceiver.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("propertyAccess.kt")
|
||||
public void testPropertyAccess() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/propertyAccess.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("secondParameterLabeled.kt")
|
||||
public void testSecondParameterLabeled() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/secondParameterLabeled.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleArgumentAndFunctionLiteralArgument.kt")
|
||||
public void testSimpleArgumentAndFunctionLiteralArgument() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/simpleArgumentAndFunctionLiteralArgument.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleMethodCall.kt")
|
||||
public void testSimpleMethodCall() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/simpleMethodCall.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("zeroArguments.kt")
|
||||
public void testZeroArguments() throws Exception {
|
||||
doTestReplaceWithInfixFunctionCall("idea/testData/intentions/replaceWithInfixFunctionCall/zeroArguments.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite("CodeTransformationTestGenerated");
|
||||
suite.addTestSuite(IfToAssignment.class);
|
||||
@@ -1246,6 +1324,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
|
||||
suite.addTestSuite(ReconstructedType.class);
|
||||
suite.addTestSuite(RemoveUnnecessaryParentheses.class);
|
||||
suite.addTestSuite(ReplaceWithDotQualifiedMethodCall.class);
|
||||
suite.addTestSuite(ReplaceWithInfixFunctionCall.class);
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user