RmoveUnnecessaryParenthesesIntention
This commit is contained in:
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Int) {
|
||||
x * x + x
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Int) {
|
||||
(x * x) + x
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention removes unnecessary parentheses.
|
||||
</body>
|
||||
</html>
|
||||
@@ -294,6 +294,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.RemoveUnnecessaryParenthesesIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<project.converterProvider implementation="org.jetbrains.jet.plugin.converters.JetRunConfigurationSettingsFormatConverterProvider"/>
|
||||
|
||||
</extensions>
|
||||
|
||||
@@ -146,3 +146,5 @@ transform.if.statement.with.assignments.to.expression=Transform 'if' statement w
|
||||
transform.assignment.with.if.expression.to.statement=Transform assignment with 'if' expression to statement
|
||||
transform.if.statement.with.assignments.to.expression.family=Transform 'if' Statement with Assignments to Expression
|
||||
transform.assignment.with.if.expression.to.statement.family=Transform Assignment with 'if' Expression to Statement
|
||||
remove.unnecessary.parentheses=Remove unnecessary parentheses
|
||||
remove.unnecessary.parentheses.family=Remove Unnecessary Parentheses
|
||||
@@ -27,7 +27,9 @@ import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.types.*;
|
||||
import org.jetbrains.jet.lang.types.DeferredType;
|
||||
import org.jetbrains.jet.lang.types.ErrorUtils;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2010-2013 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.codeInsight.codeTransformations;
|
||||
|
||||
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||
import org.jetbrains.jet.lang.psi.JetParenthesizedExpression;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
|
||||
public class RemoveUnnecessaryParenthesesIntention extends BaseIntentionAction {
|
||||
private JetParenthesizedExpression expression;
|
||||
|
||||
public RemoveUnnecessaryParenthesesIntention() {
|
||||
setText(JetBundle.message("remove.unnecessary.parentheses"));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return JetBundle.message("remove.unnecessary.parentheses.family");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
|
||||
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
|
||||
expression = PsiTreeUtil.getParentOfType(element, JetParenthesizedExpression.class);
|
||||
return expression != null && JetPsiUtil.areParenthesesUseless(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(
|
||||
@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file
|
||||
) throws IncorrectOperationException {
|
||||
JetExpression innerExpression = expression.getExpression();
|
||||
assert innerExpression != null : "parenthesizedExpression.getExpression() == null despite @IfNotParsed annotation";
|
||||
expression.replace(innerExpression);
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(x: Int, y: Int) {
|
||||
x + <caret>(x + y)
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo() : Any {
|
||||
return <caret>(@a{})
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(x: Int) : Any {
|
||||
return <caret>(x as Int) < 42
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(x: Int) {
|
||||
+<caret>(+x)
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// IS_APPLICABLE: false
|
||||
trait Foo {
|
||||
fun get(x : Any) : Foo
|
||||
fun plus(x : Any) : Foo
|
||||
}
|
||||
fun foo(x: Foo) {
|
||||
<caret>(x + x)[x]
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Int) {
|
||||
<caret>(x * x / x) + x
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Int) {
|
||||
x * x / x + x
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Int) : Int {
|
||||
return <caret>(x)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Int) : Int {
|
||||
return <caret>x
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
trait Foo {
|
||||
fun get(x : Any) : Foo
|
||||
}
|
||||
fun foo(x: Foo) {
|
||||
<caret>(x[x])[x]
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
trait Foo {
|
||||
fun get(x : Any) : Foo
|
||||
}
|
||||
fun foo(x: Foo) {
|
||||
x[x][x]
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
trait Foo {
|
||||
fun inc() : Foo
|
||||
fun not() : Foo
|
||||
}
|
||||
fun foo(x: Foo) {
|
||||
!(<caret>x.inc())
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
trait Foo {
|
||||
fun inc() : Foo
|
||||
fun not() : Foo
|
||||
}
|
||||
fun foo(x: Foo) {
|
||||
!x.inc()
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Boolean) : Boolean {
|
||||
return x || (x || x<caret>)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Boolean) : Boolean {
|
||||
return x || x || x
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Boolean) : Boolean {
|
||||
return x && (x && x<caret>)
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(x: Boolean) : Boolean {
|
||||
return x && x && x
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(i: Int) {
|
||||
array(42)[(i + i<caret>)]
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(i: Int) {
|
||||
array(42)[i + i]
|
||||
}
|
||||
+4
@@ -33,6 +33,10 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes
|
||||
doTest(path, new AssignmentWithIfExpressionToStatementIntention());
|
||||
}
|
||||
|
||||
public void doTestRemoveUnnecessaryParentheses(@NotNull String path) throws Exception {
|
||||
doTest(path, new RemoveUnnecessaryParenthesesIntention());
|
||||
}
|
||||
|
||||
private void doTest(@NotNull String path, @NotNull IntentionAction intentionAction) throws Exception {
|
||||
configureByFile(path);
|
||||
|
||||
|
||||
+70
-1
@@ -30,7 +30,7 @@ import org.jetbrains.jet.plugin.codeInsight.codeTransformations.AbstractCodeTran
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@InnerTestClasses({CodeTransformationsTestGenerated.IfStatementWithAssignmentsToExpression.class, CodeTransformationsTestGenerated.AssignmentWithIfExpressionToStatement.class})
|
||||
@InnerTestClasses({CodeTransformationsTestGenerated.IfStatementWithAssignmentsToExpression.class, CodeTransformationsTestGenerated.AssignmentWithIfExpressionToStatement.class, CodeTransformationsTestGenerated.RemoveUnnecessaryParentheses.class})
|
||||
public class CodeTransformationsTestGenerated extends AbstractCodeTransformationTest {
|
||||
@TestMetadata("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression")
|
||||
public static class IfStatementWithAssignmentsToExpression extends AbstractCodeTransformationTest {
|
||||
@@ -93,10 +93,79 @@ public class CodeTransformationsTestGenerated extends AbstractCodeTransformation
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses")
|
||||
public static class RemoveUnnecessaryParentheses extends AbstractCodeTransformationTest {
|
||||
public void testAllFilesPresentInRemoveUnnecessaryParentheses() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("necessaryParentheses1.kt")
|
||||
public void testNecessaryParentheses1() throws Exception {
|
||||
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses1.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("necessaryParentheses2.kt")
|
||||
public void testNecessaryParentheses2() throws Exception {
|
||||
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("necessaryParentheses3.kt")
|
||||
public void testNecessaryParentheses3() throws Exception {
|
||||
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("necessaryParentheses4.kt")
|
||||
public void testNecessaryParentheses4() throws Exception {
|
||||
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses4.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("necessaryParentheses5.kt")
|
||||
public void testNecessaryParentheses5() throws Exception {
|
||||
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses5.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("unnecessaryParentheses1.kt")
|
||||
public void testUnnecessaryParentheses1() throws Exception {
|
||||
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses1.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("unnecessaryParentheses2.kt")
|
||||
public void testUnnecessaryParentheses2() throws Exception {
|
||||
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("unnecessaryParentheses3.kt")
|
||||
public void testUnnecessaryParentheses3() throws Exception {
|
||||
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("unnecessaryParentheses4.kt")
|
||||
public void testUnnecessaryParentheses4() throws Exception {
|
||||
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses4.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("unnecessaryParentheses5.kt")
|
||||
public void testUnnecessaryParentheses5() throws Exception {
|
||||
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses5.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("unnecessaryParentheses6.kt")
|
||||
public void testUnnecessaryParentheses6() throws Exception {
|
||||
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses6.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("unnecessaryParentheses7.kt")
|
||||
public void testUnnecessaryParentheses7() throws Exception {
|
||||
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses7.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite("CodeTransformationsTestGenerated");
|
||||
suite.addTestSuite(IfStatementWithAssignmentsToExpression.class);
|
||||
suite.addTestSuite(AssignmentWithIfExpressionToStatement.class);
|
||||
suite.addTestSuite(RemoveUnnecessaryParentheses.class);
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user