Code transformation: if statement with assignments <-> assignment with if expression
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
public class MyClass {
|
||||
public method(a: Int): String {
|
||||
var res: String
|
||||
|
||||
if (a == 1) res = "one";
|
||||
else if (a == 2) res = "two";
|
||||
else res = "too many";
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
public class MyClass {
|
||||
public method(a: Int): String {
|
||||
var res: String
|
||||
|
||||
res = if (a == 1) "one";
|
||||
else if (a == 2) "two";
|
||||
else "too many";
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention converts assignment with 'if' expression as right-hand side
|
||||
into 'if' statement where each branch is terminated with assignment to the original variable
|
||||
</body>
|
||||
</html>
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
public class MyClass {
|
||||
public method(a: Int): String {
|
||||
var res: String
|
||||
|
||||
res = if (a == 1) "one";
|
||||
else if (a == 2) "two";
|
||||
else "too many";
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
public class MyClass {
|
||||
public method(a: Int): String {
|
||||
var res: String
|
||||
|
||||
if (a == 1) res = "one";
|
||||
else if (a == 2) res = "two";
|
||||
else res = "too many";
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention converts 'if' statement where each branch is terminated with assignment to the same variable
|
||||
into single assignment with 'if' expression
|
||||
</body>
|
||||
</html>
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
import jet.runtime.typeinfo.KotlinSignature;
|
||||
|
||||
public class MyClass {
|
||||
<spot>@KotlinSignature("fun method() : jet.String?")</spot>
|
||||
public String method() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
public class MyClass {
|
||||
public String method() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -283,6 +283,16 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.IfStatementWithAssignmentsToExpressionIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.AssignmentWithIfExpressionToStatementIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<project.converterProvider implementation="org.jetbrains.jet.plugin.converters.JetRunConfigurationSettingsFormatConverterProvider"/>
|
||||
|
||||
</extensions>
|
||||
|
||||
@@ -139,3 +139,7 @@ surround.with.function.template={ }
|
||||
surround.with.cannot.perform.action=Cannot perform Surround With action to the current contextsurround.with.function.template={ }
|
||||
remove.variable.family.name=Remove variable
|
||||
remove.variable.action=Remove variable ''{0}''
|
||||
|
||||
kotlin.code.transformations=Kotlin Code Transformations
|
||||
transform.if.statement.with.assignments.to.expression=Transform 'if' statement with assignments to expression
|
||||
transform.assignment.with.if.expression.to.statement=Transform assignment with 'if' expression to statement
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.JetBinaryExpression;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
|
||||
public class AssignmentWithIfExpressionToStatementIntention extends BaseIntentionAction {
|
||||
public AssignmentWithIfExpressionToStatementIntention() {
|
||||
setText(JetBundle.message("transform.assignment.with.if.expression.to.statement"));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return JetBundle.message("kotlin.code.transformations");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static JetBinaryExpression getAssignment(@NotNull Editor editor, @NotNull PsiFile file) {
|
||||
int offset = editor.getCaretModel().getOffset();
|
||||
PsiElement element = file.findElementAt(offset);
|
||||
|
||||
while (element != null) {
|
||||
if (CodeTransformationUtils.checkAssignmentWithIfExpression(element)) return (JetBinaryExpression)element;
|
||||
element = PsiTreeUtil.getParentOfType(element, JetBinaryExpression.class, false);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
|
||||
return getAssignment(editor, file) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(
|
||||
@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file
|
||||
) throws IncorrectOperationException {
|
||||
JetBinaryExpression assignment = getAssignment(editor, file);
|
||||
assert assignment != null;
|
||||
CodeTransformationUtils.transformAssignmentWithIfExpressionToStatement(assignment);
|
||||
}
|
||||
}
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* 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.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CodeTransformationUtils {
|
||||
private static List<JetExpression> getIfExpressionOutcomes(@NotNull JetElement root) {
|
||||
return root.accept(
|
||||
new JetVisitor<List<JetExpression>, List<JetExpression>>() {
|
||||
@Override
|
||||
public List<JetExpression> visitExpression(JetExpression expression, List<JetExpression> data) {
|
||||
data.add(expression);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<JetExpression> visitBlockExpression(
|
||||
JetBlockExpression expression, List<JetExpression> data) {
|
||||
int n = expression.getStatements().size();
|
||||
if (n > 0) {
|
||||
expression.getStatements().get(n - 1).accept(this, data);
|
||||
} else {
|
||||
data.add(expression);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Override
|
||||
public List<JetExpression> visitIfExpression(
|
||||
JetIfExpression expression, List<JetExpression> data) {
|
||||
if (expression.getThen() != null) {
|
||||
expression.getThen().accept(this, data);
|
||||
}
|
||||
if (expression.getElse() != null) {
|
||||
expression.getElse().accept(this, data);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
},
|
||||
new ArrayList<JetExpression>()
|
||||
);
|
||||
}
|
||||
|
||||
private static Boolean checkAllIfExpressionsAreComplete(@NotNull JetElement root) {
|
||||
return root.accept(
|
||||
new JetVisitor<Boolean, Boolean>() {
|
||||
@Override
|
||||
public Boolean visitJetElement(JetElement element, Boolean data) {
|
||||
return data;
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Override
|
||||
public Boolean visitIfExpression(JetIfExpression expression, Boolean data) {
|
||||
if (data && expression.getThen() != null) {
|
||||
data = expression.getThen().accept(this, data);
|
||||
} else {
|
||||
data = false;
|
||||
}
|
||||
if (data && expression.getElse() != null) {
|
||||
data = expression.getElse().accept(this, data);
|
||||
} else {
|
||||
data = false;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
},
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
public static boolean isAssignment(@NotNull PsiElement element) {
|
||||
if (!(element instanceof JetBinaryExpression)) return false;
|
||||
JetBinaryExpression binaryExpression = (JetBinaryExpression)element;
|
||||
if (binaryExpression.getOperationReference().getReferencedNameElementType() != JetTokens.EQ) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean checkAllOutcomesAreCompatibleAssignments(@NotNull List<JetExpression> outcomes) {
|
||||
JetExpression lastLhs = null;
|
||||
for (JetExpression outcome : outcomes) {
|
||||
if (!isAssignment(outcome)) return false;
|
||||
|
||||
JetExpression currLhs = ((JetBinaryExpression)outcome).getLeft();
|
||||
if (!(currLhs instanceof JetSimpleNameExpression)) return false;
|
||||
|
||||
if (lastLhs == null) {
|
||||
lastLhs = currLhs;
|
||||
} else if (!lastLhs.getText().equals(currLhs.getText())) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static boolean checkIfStatementWithAssignments(@NotNull JetIfExpression ifExpression) {
|
||||
if (ifExpression.getParent() == null) return false;
|
||||
List<JetExpression> outcomes = getIfExpressionOutcomes(ifExpression);
|
||||
|
||||
return !outcomes.isEmpty() && checkAllIfExpressionsAreComplete(ifExpression) && checkAllOutcomesAreCompatibleAssignments(outcomes);
|
||||
}
|
||||
|
||||
static boolean checkAssignmentWithIfExpression(@NotNull PsiElement element) {
|
||||
if (!isAssignment(element)) return false;
|
||||
JetBinaryExpression assignment = (JetBinaryExpression)element;
|
||||
return (assignment.getLeft() instanceof JetSimpleNameExpression) &&
|
||||
(assignment.getRight() instanceof JetIfExpression);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
static void transformIfStatementWithAssignmentsToExpression(@NotNull JetIfExpression ifExpression) {
|
||||
Project project = ifExpression.getProject();
|
||||
List<JetExpression> outcomes = getIfExpressionOutcomes(ifExpression);
|
||||
JetExpression lhs = ((JetBinaryExpression)outcomes.get(0)).getLeft();
|
||||
|
||||
JetBinaryExpression assignment = (JetBinaryExpression)JetPsiFactory.createExpression(project, "a = b");
|
||||
|
||||
assignment = (JetBinaryExpression)assignment.getLeft().replace(lhs).getParent();
|
||||
assignment = (JetBinaryExpression)assignment.getRight().replace(ifExpression).getParent();
|
||||
assignment = (JetBinaryExpression)ifExpression.replace(assignment);
|
||||
ifExpression = (JetIfExpression)assignment.getRight();
|
||||
|
||||
for (JetExpression outcome : getIfExpressionOutcomes(ifExpression)) {
|
||||
outcome.replace(((JetBinaryExpression)outcome).getRight());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
static void transformAssignmentWithIfExpressionToStatement(@NotNull JetBinaryExpression assignment) {
|
||||
Project project = assignment.getProject();
|
||||
String varName = assignment.getLeft().getText();
|
||||
JetIfExpression ifExpression = (JetIfExpression)assignment.getRight();
|
||||
|
||||
ifExpression = (JetIfExpression)assignment.replace(ifExpression);
|
||||
|
||||
for (JetExpression outcome : getIfExpressionOutcomes(ifExpression)) {
|
||||
JetBinaryExpression localAssignment = (JetBinaryExpression)JetPsiFactory.createExpression(project, "a = b");
|
||||
localAssignment = (JetBinaryExpression)localAssignment.getLeft().replace(JetPsiFactory.createExpression(project, varName)).getParent();
|
||||
localAssignment = (JetBinaryExpression)localAssignment.getRight().replace(outcome).getParent();
|
||||
outcome.replace(localAssignment);
|
||||
}
|
||||
}
|
||||
|
||||
private CodeTransformationUtils() {
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.JetIfExpression;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
|
||||
public class IfStatementWithAssignmentsToExpressionIntention extends BaseIntentionAction {
|
||||
public IfStatementWithAssignmentsToExpressionIntention() {
|
||||
setText(JetBundle.message("transform.if.statement.with.assignments.to.expression"));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return JetBundle.message("kotlin.code.transformations");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static JetIfExpression getOriginalExpression(@NotNull Editor editor, @NotNull PsiFile file) {
|
||||
int offset = editor.getCaretModel().getOffset();
|
||||
PsiElement element = file.findElementAt(offset);
|
||||
return PsiTreeUtil.getParentOfType(element, JetIfExpression.class, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(
|
||||
@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
|
||||
JetIfExpression ifExpression = getOriginalExpression(editor, file);
|
||||
return (ifExpression != null) && CodeTransformationUtils.checkIfStatementWithAssignments(ifExpression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(
|
||||
@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file
|
||||
) throws IncorrectOperationException {
|
||||
JetIfExpression ifExpression = getOriginalExpression(editor, file);
|
||||
assert ifExpression != null;
|
||||
CodeTransformationUtils.transformIfStatementWithAssignmentsToExpression(ifExpression);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user