New Intention Action: Replace an infix function call with a dot qualified method call.

This commit is contained in:
Pradyoth Kukkapalli
2014-02-07 15:39:43 -08:00
committed by Andrey Breslav
parent 64f67f30f0
commit 8b6bd8a184
20 changed files with 149 additions and 1 deletions
@@ -336,6 +336,7 @@ fun main(args: Array<String>) {
model("intentions/declarations/convertMemberToExtension", testMethod = "doTestConvertMemberToExtension")
model("intentions/reconstructedType", testMethod = "doTestReconstructType")
model("intentions/removeUnnecessaryParentheses", testMethod = "doTestRemoveUnnecessaryParentheses")
model("intentions/replaceWithDotQualifiedMethodCall", testMethod = "doTestReplaceWithDotQualifiedMethodCall")
}
testClass(javaClass<AbstractHierarchyTest>()) {
@@ -0,0 +1,3 @@
fun foo(x: Foo) {
x.foo(1)
}
@@ -0,0 +1,3 @@
fun foo(x: Foo) {
<spot>x foo 1</spot>
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts an infix function call to a dot-qualified function call.
</body>
</html>
+5
View File
@@ -488,6 +488,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.intentions.ReplaceWithDotQualifiedMethodCallIntention</className>
<category>Kotlin</category>
</intentionAction>
<project.converterProvider implementation="org.jetbrains.jet.plugin.converters.JetRunConfigurationSettingsFormatConverterProvider"/>
</extensions>
@@ -222,6 +222,8 @@ add.name.to.argument.single=Add name to argument\: ''{0}''
add.name.to.argument.multiple=Add name to argument...
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
property.is.implemented.too.many=Has implementations
property.is.overridden.too.many=Is overridden in subclasses
@@ -0,0 +1,52 @@
/*
* 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 com.intellij.openapi.editor.Editor
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
import org.jetbrains.jet.lang.psi.JetBinaryExpression
import org.jetbrains.jet.lang.psi.JetPsiFactory
import org.jetbrains.jet.lang.psi.JetFunctionLiteralExpression
import org.jetbrains.jet.lang.psi.JetParenthesizedExpression
import org.jetbrains.jet.lang.types.expressions.OperatorConventions
import com.google.common.collect.ImmutableSet
import org.jetbrains.jet.lexer.JetTokens
import org.jetbrains.jet.lexer.JetToken
public class ReplaceWithDotQualifiedMethodCallIntention : JetSelfTargetingIntention<JetBinaryExpression>("replace.with.dot.qualified.method.call.intention", javaClass()) {
override fun isApplicableTo(element: JetBinaryExpression): Boolean {
return element.getLeft() != null && element.getRight() != null && element.getOperationToken() == JetTokens.IDENTIFIER
}
override fun applyTo(element: JetBinaryExpression, editor: Editor) {
val receiverText = element.getLeft()!!.getText()
val argumentText = element.getRight()!!.getText()
val functionName = element.getOperationReference().getText()
val replacementExpressionStringBuilder = StringBuilder("$receiverText.$functionName")
replacementExpressionStringBuilder.append(
when (element.getRight()) {
is JetFunctionLiteralExpression -> " $argumentText"
is JetParenthesizedExpression -> argumentText
else -> "($argumentText)"
}
)
val replacement = JetPsiFactory.createExpression(element.getProject(), replacementExpressionStringBuilder.toString())
element.replace(replacement)
}
}
@@ -0,0 +1,3 @@
fun foo(x: Foo) {
(<caret>x foo 1).bar()
}
@@ -0,0 +1,3 @@
fun foo(x: Foo) {
(x.foo(1)).bar()
}
@@ -0,0 +1,3 @@
fun foo(x: Foo) {
<caret>x 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) {
<caret>x == x
}
@@ -0,0 +1,3 @@
fun foo(x: Foo) {
<caret>x!! foo 1
}
@@ -0,0 +1,3 @@
fun foo(x: Foo) {
x!!.foo(1)
}
@@ -0,0 +1,3 @@
fun foo(x: Foo) {
<caret>x foo (1 + 2)
}
@@ -0,0 +1,3 @@
fun foo(x: Foo) {
x.foo(1 + 2)
}
@@ -0,0 +1,3 @@
fun foo(x: Foo) {
<caret>x foo 1
}
@@ -0,0 +1,3 @@
fun foo(x: Foo) {
x.foo(1)
}
@@ -118,6 +118,10 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes
doTestIntention(path, new ReconstructTypeInCastOrIsAction());
}
public void doTestReplaceWithDotQualifiedMethodCall(@NotNull String path) throws Exception {
doTestIntention(path, new ReplaceWithDotQualifiedMethodCallIntention());
}
private void doTestIntention(@NotNull String path, @NotNull IntentionAction intentionAction) throws Exception {
configureByFile(path);
@@ -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})
@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})
public class CodeTransformationTestGenerated extends AbstractCodeTransformationTest {
@TestMetadata("idea/testData/intentions/branched/folding/ifToAssignment")
public static class IfToAssignment extends AbstractCodeTransformationTest {
@@ -1183,6 +1183,44 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
}
@TestMetadata("idea/testData/intentions/replaceWithDotQualifiedMethodCall")
public static class ReplaceWithDotQualifiedMethodCall extends AbstractCodeTransformationTest {
public void testAllFilesPresentInReplaceWithDotQualifiedMethodCall() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/replaceWithDotQualifiedMethodCall"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("functionCallAfterInfixCall.kt")
public void testFunctionCallAfterInfixCall() throws Exception {
doTestReplaceWithDotQualifiedMethodCall("idea/testData/intentions/replaceWithDotQualifiedMethodCall/functionCallAfterInfixCall.kt");
}
@TestMetadata("functionLiteralArgument.kt")
public void testFunctionLiteralArgument() throws Exception {
doTestReplaceWithDotQualifiedMethodCall("idea/testData/intentions/replaceWithDotQualifiedMethodCall/functionLiteralArgument.kt");
}
@TestMetadata("nonApplicableBinaryOperation.kt")
public void testNonApplicableBinaryOperation() throws Exception {
doTestReplaceWithDotQualifiedMethodCall("idea/testData/intentions/replaceWithDotQualifiedMethodCall/nonApplicableBinaryOperation.kt");
}
@TestMetadata("nullAssertedCall.kt")
public void testNullAssertedCall() throws Exception {
doTestReplaceWithDotQualifiedMethodCall("idea/testData/intentions/replaceWithDotQualifiedMethodCall/nullAssertedCall.kt");
}
@TestMetadata("parenthesesAroundRightHandArgument.kt")
public void testParenthesesAroundRightHandArgument() throws Exception {
doTestReplaceWithDotQualifiedMethodCall("idea/testData/intentions/replaceWithDotQualifiedMethodCall/parenthesesAroundRightHandArgument.kt");
}
@TestMetadata("simpleInfixFunctionCall.kt")
public void testSimpleInfixFunctionCall() throws Exception {
doTestReplaceWithDotQualifiedMethodCall("idea/testData/intentions/replaceWithDotQualifiedMethodCall/simpleInfixFunctionCall.kt");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("CodeTransformationTestGenerated");
suite.addTestSuite(IfToAssignment.class);
@@ -1207,6 +1245,7 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT
suite.addTestSuite(ConvertMemberToExtension.class);
suite.addTestSuite(ReconstructedType.class);
suite.addTestSuite(RemoveUnnecessaryParentheses.class);
suite.addTestSuite(ReplaceWithDotQualifiedMethodCall.class);
return suite;
}
}