Translate intention classes to Kotlin and apply refactoring

This commit is contained in:
Alexey Sedunov
2013-11-18 13:37:56 +04:00
parent 6b971fa50c
commit 72f4ae3a13
54 changed files with 527 additions and 505 deletions
@@ -0,0 +1,14 @@
<root>
<item
name='com.intellij.codeInsight.intention.IntentionAction boolean isAvailable(com.intellij.openapi.project.Project, com.intellij.openapi.editor.Editor, com.intellij.psi.PsiFile)'>
<annotation name='jet.runtime.typeinfo.KotlinSignature'>
<val name="value" val="&quot;fun isAvailable(project: Project, editor: Editor, file: PsiFile): Boolean&quot;"/>
</annotation>
</item>
<item
name='com.intellij.codeInsight.intention.IntentionAction void invoke(com.intellij.openapi.project.Project, com.intellij.openapi.editor.Editor, com.intellij.psi.PsiFile)'>
<annotation name='jet.runtime.typeinfo.KotlinSignature'>
<val name="value" val="&quot;fun invoke(project: Project, editor: Editor, file: PsiFile): Unit&quot;"/>
</annotation>
</item>
</root>
@@ -0,0 +1,5 @@
res = if (ok) {
"ok"
} else {
"failed"
}
@@ -0,0 +1,5 @@
if (ok) {
res = "ok"
} else {
res = "failed"
}
@@ -2,4 +2,4 @@
<body> <body>
This intention converts 'if' expression where each branch is terminated with assignment into a single assignment with 'if' expression as a right-hand side This intention converts 'if' expression where each branch is terminated with assignment into a single assignment with 'if' expression as a right-hand side
</body> </body>
</html> </html>
@@ -0,0 +1,3 @@
return if (ok) {
"ok"
} else "failed"
@@ -0,0 +1,4 @@
if (ok) {
return "ok"
}
return "failed"
@@ -2,4 +2,4 @@
<body> <body>
This intention converts single-branch 'if' expression immediately followed by 'return' into a single 'return' with 'if' expression as an argument This intention converts single-branch 'if' expression immediately followed by 'return' into a single 'return' with 'if' expression as an argument
</body> </body>
</html> </html>
@@ -0,0 +1,5 @@
return if (ok) {
"ok"
} else {
"failed"
}
@@ -0,0 +1,5 @@
if (ok) {
return "ok"
} else {
return "failed"
}
@@ -2,4 +2,4 @@
<body> <body>
This intention converts 'if' expression where each branch is terminated with 'return' into a single 'return' with 'if' expression as a right-hand side This intention converts 'if' expression where each branch is terminated with 'return' into a single 'return' with 'if' expression as a right-hand side
</body> </body>
</html> </html>
@@ -0,0 +1,5 @@
res = when (n) {
1 -> "one"
2 -> "two"
else -> "many"
}
@@ -0,0 +1,5 @@
when (n) {
1 -> res = "one"
2 -> res = "two"
else -> res = "many"
}
@@ -2,4 +2,4 @@
<body> <body>
This intention converts 'when' expression where each branch is terminated with assignment into a single assignment with 'when' expression as right-hand side This intention converts 'when' expression where each branch is terminated with assignment into a single assignment with 'when' expression as right-hand side
</body> </body>
</html> </html>
@@ -0,0 +1,5 @@
return when (n) {
1 -> "one"
2 -> "two"
else -> "many"
}
@@ -0,0 +1,5 @@
when (n) {
1 -> return "one"
2 -> return "two"
else -> return "many"
}
@@ -2,4 +2,4 @@
<body> <body>
This intention converts 'when' expression where each branch is terminated with 'return' into a single 'return' with 'when' expression as a right-hand side This intention converts 'when' expression where each branch is terminated with 'return' into a single 'return' with 'when' expression as a right-hand side
</body> </body>
</html> </html>
@@ -0,0 +1,5 @@
if (ok) {
res = "ok"
} else {
res = "failed"
}
@@ -0,0 +1,5 @@
res = if (ok) {
"ok"
} else {
"failed"
}
@@ -2,4 +2,4 @@
<body> <body>
This intention converts assignment with 'if' right-hand side to 'if' expression where each branch is terminated with assignment This intention converts assignment with 'if' right-hand side to 'if' expression where each branch is terminated with assignment
</body> </body>
</html> </html>
@@ -0,0 +1,5 @@
when (n) {
1 -> res = "one"
2 -> res = "two"
else -> res = "many"
}
@@ -0,0 +1,5 @@
res = when (n) {
1 -> "one"
2 -> "two"
else -> "many"
}
@@ -2,4 +2,4 @@
<body> <body>
This intention converts assignment with 'when' right-hand side to 'when' expression where each branch is terminated with assignment This intention converts assignment with 'when' right-hand side to 'when' expression where each branch is terminated with assignment
</body> </body>
</html> </html>
@@ -0,0 +1,6 @@
val res: String
if (ok) {
res = "ok"
} else {
res = "failed"
}
@@ -0,0 +1,5 @@
val res = if (ok) {
"ok"
} else {
"failed"
}
@@ -2,4 +2,4 @@
<body> <body>
This intention converts property with 'if' initializer to uninitialized property followed by 'if' expression where each branch is terminated with assignment This intention converts property with 'if' initializer to uninitialized property followed by 'if' expression where each branch is terminated with assignment
</body> </body>
</html> </html>
@@ -0,0 +1,6 @@
val res: String
when (n) {
1 -> res = "one"
2 -> res = "two"
else -> res = "many"
}
@@ -0,0 +1,5 @@
val res = when (n) {
1 -> "one"
2 -> "two"
else -> "many"
}
@@ -2,4 +2,4 @@
<body> <body>
This intention converts property with 'when' initializer to uninitialized property followed by 'when' expression where each branch is terminated with assignment This intention converts property with 'when' initializer to uninitialized property followed by 'when' expression where each branch is terminated with assignment
</body> </body>
</html> </html>
@@ -0,0 +1,5 @@
if (ok) {
return "ok"
} else {
return "failed"
}
@@ -0,0 +1,5 @@
return if (ok) {
"ok"
} else {
"failed"
}
@@ -2,4 +2,4 @@
<body> <body>
This intention converts 'return' with 'if' expression as a result to 'if' expression where each branch is terminated with 'return' This intention converts 'return' with 'if' expression as a result to 'if' expression where each branch is terminated with 'return'
</body> </body>
</html> </html>
@@ -0,0 +1,5 @@
when (n) {
1 -> return "one"
2 -> return "two"
else -> return "many"
}
@@ -0,0 +1,5 @@
return when (n) {
1 -> "one"
2 -> "two"
else -> "many"
}
@@ -2,4 +2,4 @@
<body> <body>
This intention converts 'return' with 'when' expression as a result to 'when' expression where each branch is terminated with 'return' This intention converts 'return' with 'when' expression as a result to 'when' expression where each branch is terminated with 'return'
</body> </body>
</html> </html>
+11 -11
View File
@@ -365,57 +365,57 @@
</intentionAction> </intentionAction>
<intentionAction> <intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.FoldBranchedExpressionIntention$FoldIfToAssignmentIntention</className> <className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.FoldIfToAssignmentIntention</className>
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
<intentionAction> <intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.FoldBranchedExpressionIntention$FoldIfToReturnAsymmetricallyIntention</className> <className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.FoldIfToReturnAsymmetricallyIntention</className>
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
<intentionAction> <intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.FoldBranchedExpressionIntention$FoldIfToReturnIntention</className> <className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.FoldIfToReturnIntention</className>
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
<intentionAction> <intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.FoldBranchedExpressionIntention$FoldWhenToAssignmentIntention</className> <className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.FoldWhenToAssignmentIntention</className>
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
<intentionAction> <intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.FoldBranchedExpressionIntention$FoldWhenToReturnIntention</className> <className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.FoldWhenToReturnIntention</className>
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
<intentionAction> <intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.UnfoldBranchedExpressionIntention$UnfoldAssignmentToIfIntention</className> <className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.UnfoldAssignmentToIfIntention</className>
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
<intentionAction> <intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.UnfoldBranchedExpressionIntention$UnfoldPropertyToIfIntention</className> <className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.UnfoldPropertyToIfIntention</className>
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
<intentionAction> <intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.UnfoldBranchedExpressionIntention$UnfoldAssignmentToWhenIntention</className> <className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.UnfoldAssignmentToWhenIntention</className>
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
<intentionAction> <intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.UnfoldBranchedExpressionIntention$UnfoldPropertyToWhenIntention</className> <className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.UnfoldPropertyToWhenIntention</className>
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
<intentionAction> <intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.UnfoldBranchedExpressionIntention$UnfoldReturnToIfIntention</className> <className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.UnfoldReturnToIfIntention</className>
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
<intentionAction> <intentionAction>
<className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.UnfoldBranchedExpressionIntention$UnfoldReturnToWhenIntention</className> <className>org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions.UnfoldReturnToWhenIntention</className>
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
@@ -1,68 +0,0 @@
/*
* 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.intentions;
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.util.IncorrectOperationException;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.jet.plugin.JetBundle;
public abstract class AbstractCodeTransformationIntention extends BaseIntentionAction {
private final Transformer transformer;
private final Function1<PsiElement, Boolean> isApplicable;
protected AbstractCodeTransformationIntention(@NotNull Transformer transformer, @NotNull Function1<PsiElement, Boolean> isApplicable) {
this.transformer = transformer;
this.isApplicable = isApplicable;
setText(JetBundle.message(transformer.getKey()));
}
@Nullable
private PsiElement getTarget(@NotNull Editor editor, @NotNull PsiFile file) {
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
return PsiUtilPackage.getParentByTypeAndPredicate(element, JetElement.class, false, isApplicable);
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message(transformer.getKey() + ".family");
}
@Override
public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
return getTarget(editor, file) != null;
}
@Override
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) throws IncorrectOperationException {
PsiElement target = getTarget(editor, file);
assert target != null : "Intention is not applicable";
transformer.transform(target, editor, (JetFile) file);
}
}
@@ -0,0 +1,59 @@
/*
* 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.intentions
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.util.IncorrectOperationException
import jet.Function1
import org.jetbrains.annotations.Nullable
import org.jetbrains.jet.lang.psi.JetElement
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.plugin.JetBundle
import org.jetbrains.jet.lang.psi.psiUtil.getParentByTypeAndPredicate
public abstract class JetSelfTargetingIntention<T: JetElement>(val key: String, val elementType: Class<T>) : BaseIntentionAction() {
{
setText(JetBundle.message(key))
}
protected abstract fun isApplicableTo(element: T): Boolean
protected abstract fun applyTo(element: T, editor: Editor)
private fun getTarget(editor: Editor, file: PsiFile): T? {
val offset = editor.getCaretModel().getOffset()
return file.findElementAt(offset)?.getParentByTypeAndPredicate(elementType) { element -> isApplicableTo(element) }
}
public override fun getFamilyName(): String {
return JetBundle.message(key + ".family")
}
public override fun isAvailable(project: Project, editor: Editor, file: PsiFile): Boolean {
return getTarget(editor, file) != null
}
public override fun invoke(project: Project, editor: Editor, file: PsiFile): Unit {
val target = getTarget(editor, file)
assert(target != null, "Intention is not applicable")
applyTo(target!!, editor)
}
}
@@ -1,38 +0,0 @@
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetWhenExpression;
import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationIntention;
import org.jetbrains.jet.plugin.intentions.branchedTransformations.WhenUtils;
import org.jetbrains.jet.plugin.intentions.Transformer;
public class EliminateWhenSubjectIntention extends AbstractCodeTransformationIntention {
private static final Transformer TRANSFORMER = new Transformer() {
@NotNull
@Override
public String getKey() {
return "eliminate.when.subject";
}
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, @NotNull JetFile file) {
WhenUtils.eliminateWhenSubject((JetWhenExpression) element);
}
};
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public Boolean invoke(@Nullable PsiElement input) {
return input instanceof JetWhenExpression && WhenUtils.checkEliminateWhenSubject((JetWhenExpression) input);
}
};
public EliminateWhenSubjectIntention() {
super(TRANSFORMER, IS_APPLICABLE);
}
}
@@ -0,0 +1,30 @@
/*
* 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.intentions.branchedTransformations.intentions
import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention
import org.jetbrains.jet.plugin.intentions.branchedTransformations.WhenUtils
import org.jetbrains.jet.lang.psi.JetWhenExpression
import com.intellij.openapi.editor.Editor
public class EliminateWhenSubjectIntention : JetSelfTargetingIntention<JetWhenExpression>("eliminate.when.subject", javaClass()) {
override fun isApplicableTo(element: JetWhenExpression): Boolean = WhenUtils.checkEliminateWhenSubject(element)
override fun applyTo(element: JetWhenExpression, editor: Editor) {
WhenUtils.eliminateWhenSubject(element)
}
}
@@ -1,38 +0,0 @@
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetWhenExpression;
import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationIntention;
import org.jetbrains.jet.plugin.intentions.branchedTransformations.WhenUtils;
import org.jetbrains.jet.plugin.intentions.Transformer;
public class FlattenWhenIntention extends AbstractCodeTransformationIntention {
private static final Transformer TRANSFORMER = new Transformer() {
@NotNull
@Override
public String getKey() {
return "flatten.when";
}
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, @NotNull JetFile file) {
WhenUtils.flattenWhen((JetWhenExpression) element);
}
};
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public Boolean invoke(@Nullable PsiElement input) {
return input instanceof JetWhenExpression && WhenUtils.checkFlattenWhen((JetWhenExpression) input);
}
};
public FlattenWhenIntention() {
super(TRANSFORMER, IS_APPLICABLE);
}
}
@@ -0,0 +1,30 @@
/*
* 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.intentions.branchedTransformations.intentions
import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention
import org.jetbrains.jet.plugin.intentions.branchedTransformations.WhenUtils
import org.jetbrains.jet.lang.psi.JetWhenExpression
import com.intellij.openapi.editor.Editor
public class FlattenWhenIntention : JetSelfTargetingIntention<JetWhenExpression>("flatten.when", javaClass()) {
override fun isApplicableTo(element: JetWhenExpression): Boolean = WhenUtils.checkFlattenWhen(element)
override fun applyTo(element: JetWhenExpression, editor: Editor) {
WhenUtils.flattenWhen(element)
}
}
@@ -1,70 +0,0 @@
/*
* 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.intentions.branchedTransformations.intentions;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationIntention;
import org.jetbrains.jet.plugin.intentions.branchedTransformations.BranchedFoldingUtils;
import org.jetbrains.jet.plugin.intentions.branchedTransformations.FoldableKind;
public abstract class FoldBranchedExpressionIntention extends AbstractCodeTransformationIntention {
protected FoldBranchedExpressionIntention(@NotNull final FoldableKind foldableKind) {
super(
foldableKind,
new Function1<PsiElement, Boolean>() {
@Override
public Boolean invoke(@Nullable PsiElement input) {
return (input instanceof JetExpression) && BranchedFoldingUtils.getFoldableExpressionKind((JetExpression) input) == foldableKind;
}
}
);
}
public static class FoldIfToAssignmentIntention extends FoldBranchedExpressionIntention {
public FoldIfToAssignmentIntention() {
super(FoldableKind.IF_TO_ASSIGNMENT);
}
}
public static class FoldIfToReturnAsymmetricallyIntention extends FoldBranchedExpressionIntention {
public FoldIfToReturnAsymmetricallyIntention() {
super(FoldableKind.IF_TO_RETURN_ASYMMETRICALLY);
}
}
public static class FoldIfToReturnIntention extends FoldBranchedExpressionIntention {
public FoldIfToReturnIntention() {
super(FoldableKind.IF_TO_RETURN);
}
}
public static class FoldWhenToAssignmentIntention extends FoldBranchedExpressionIntention {
public FoldWhenToAssignmentIntention() {
super(FoldableKind.WHEN_TO_ASSIGNMENT);
}
}
public static class FoldWhenToReturnIntention extends FoldBranchedExpressionIntention {
public FoldWhenToReturnIntention() {
super(FoldableKind.WHEN_TO_RETURN);
}
}
}
@@ -0,0 +1,57 @@
/*
* 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.intentions.branchedTransformations.intentions
import com.intellij.psi.PsiElement
import jet.Function1
import org.jetbrains.annotations.Nullable
import org.jetbrains.jet.lang.psi.JetExpression
import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention
import org.jetbrains.jet.plugin.intentions.branchedTransformations.BranchedFoldingUtils
import org.jetbrains.jet.plugin.intentions.branchedTransformations.FoldableKind
import org.jetbrains.jet.lang.psi.JetElement
import com.intellij.openapi.editor.Editor
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.psi.JetIfExpression
import org.jetbrains.jet.lang.psi.JetWhenExpression
public open class FoldBranchedExpressionIntention<T: JetExpression>(
val kind: FoldableKind, elementType: Class<T>
) : JetSelfTargetingIntention<T>(kind.getKey(), elementType) {
override fun isApplicableTo(element: T): Boolean = BranchedFoldingUtils.getFoldableExpressionKind(element) == kind
override fun applyTo(element: T, editor: Editor) {
val file = element.getContainingFile()
if (file is JetFile) {
kind.transform(element, editor, file)
}
}
}
public class FoldIfToAssignmentIntention : FoldBranchedExpressionIntention<JetIfExpression>(FoldableKind.IF_TO_ASSIGNMENT, javaClass())
public class FoldIfToReturnAsymmetricallyIntention : FoldBranchedExpressionIntention<JetIfExpression>(
FoldableKind.IF_TO_RETURN_ASYMMETRICALLY, javaClass()
)
public class FoldIfToReturnIntention : FoldBranchedExpressionIntention<JetIfExpression>(FoldableKind.IF_TO_RETURN, javaClass())
public class FoldWhenToAssignmentIntention : FoldBranchedExpressionIntention<JetWhenExpression>(
FoldableKind.WHEN_TO_ASSIGNMENT, javaClass()
)
public class FoldWhenToReturnIntention : FoldBranchedExpressionIntention<JetWhenExpression>(FoldableKind.WHEN_TO_RETURN, javaClass())
@@ -1,38 +0,0 @@
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetIfExpression;
import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationIntention;
import org.jetbrains.jet.plugin.intentions.branchedTransformations.IfWhenUtils;
import org.jetbrains.jet.plugin.intentions.Transformer;
public class IfToWhenIntention extends AbstractCodeTransformationIntention {
private static final Transformer TRANSFORMER = new Transformer() {
@NotNull
@Override
public String getKey() {
return "if.to.when";
}
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, @NotNull JetFile file) {
IfWhenUtils.transformIfToWhen((JetIfExpression) element);
}
};
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public Boolean invoke(@Nullable PsiElement input) {
return (input instanceof JetIfExpression) && IfWhenUtils.checkIfToWhen((JetIfExpression) input);
}
};
public IfToWhenIntention() {
super(TRANSFORMER, IS_APPLICABLE);
}
}
@@ -0,0 +1,31 @@
/*
* 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.intentions.branchedTransformations.intentions
import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention
import org.jetbrains.jet.plugin.intentions.branchedTransformations.IfWhenUtils
import org.jetbrains.jet.lang.psi.JetWhenExpression
import org.jetbrains.jet.lang.psi.JetIfExpression
import com.intellij.openapi.editor.Editor
public class IfToWhenIntention : JetSelfTargetingIntention<JetIfExpression>("if.to.when", javaClass()) {
override fun isApplicableTo(element: JetIfExpression): Boolean = IfWhenUtils.checkIfToWhen(element)
override fun applyTo(element: JetIfExpression, editor: Editor) {
IfWhenUtils.transformIfToWhen(element)
}
}
@@ -1,38 +0,0 @@
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetWhenExpression;
import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationIntention;
import org.jetbrains.jet.plugin.intentions.branchedTransformations.WhenUtils;
import org.jetbrains.jet.plugin.intentions.Transformer;
public class IntroduceWhenSubjectIntention extends AbstractCodeTransformationIntention {
private static final Transformer TRANSFORMER = new Transformer() {
@NotNull
@Override
public String getKey() {
return "introduce.when.subject";
}
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, @NotNull JetFile file) {
WhenUtils.introduceWhenSubject((JetWhenExpression) element);
}
};
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public Boolean invoke(@Nullable PsiElement input) {
return input instanceof JetWhenExpression && WhenUtils.checkIntroduceWhenSubject((JetWhenExpression) input);
}
};
public IntroduceWhenSubjectIntention() {
super(TRANSFORMER, IS_APPLICABLE);
}
}
@@ -0,0 +1,30 @@
/*
* 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.intentions.branchedTransformations.intentions
import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention
import org.jetbrains.jet.plugin.intentions.branchedTransformations.WhenUtils
import org.jetbrains.jet.lang.psi.JetWhenExpression
import com.intellij.openapi.editor.Editor
public class IntroduceWhenSubjectIntention : JetSelfTargetingIntention<JetWhenExpression>("introduce.when.subject", javaClass()) {
override fun isApplicableTo(element: JetWhenExpression): Boolean = WhenUtils.checkIntroduceWhenSubject(element)
override fun applyTo(element: JetWhenExpression, editor: Editor) {
WhenUtils.introduceWhenSubject(element)
}
}
@@ -1,75 +0,0 @@
/*
* 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.intentions.branchedTransformations.intentions;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.plugin.intentions.branchedTransformations.*;
import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationIntention;
public abstract class UnfoldBranchedExpressionIntention extends AbstractCodeTransformationIntention {
protected UnfoldBranchedExpressionIntention(@NotNull final UnfoldableKind unfoldableKind) {
super(
unfoldableKind,
new Function1<PsiElement, Boolean>() {
@Override
public Boolean invoke(@Nullable PsiElement input) {
return (input instanceof JetExpression) && BranchedUnfoldingUtils.getUnfoldableExpressionKind((JetExpression) input) == unfoldableKind;
}
}
);
}
public static class UnfoldAssignmentToIfIntention extends UnfoldBranchedExpressionIntention {
public UnfoldAssignmentToIfIntention() {
super(UnfoldableKind.ASSIGNMENT_TO_IF);
}
}
public static class UnfoldPropertyToIfIntention extends UnfoldBranchedExpressionIntention {
public UnfoldPropertyToIfIntention() {
super(UnfoldableKind.PROPERTY_TO_IF);
}
}
public static class UnfoldAssignmentToWhenIntention extends UnfoldBranchedExpressionIntention {
public UnfoldAssignmentToWhenIntention() {
super(UnfoldableKind.ASSIGNMENT_TO_WHEN);
}
}
public static class UnfoldPropertyToWhenIntention extends UnfoldBranchedExpressionIntention {
public UnfoldPropertyToWhenIntention() {
super(UnfoldableKind.PROPERTY_TO_WHEN);
}
}
public static class UnfoldReturnToIfIntention extends UnfoldBranchedExpressionIntention {
public UnfoldReturnToIfIntention() {
super(UnfoldableKind.RETURN_TO_IF);
}
}
public static class UnfoldReturnToWhenIntention extends UnfoldBranchedExpressionIntention {
public UnfoldReturnToWhenIntention() {
super(UnfoldableKind.RETURN_TO_WHEN);
}
}
}
@@ -0,0 +1,66 @@
/*
* 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.intentions.branchedTransformations.intentions
import com.intellij.psi.PsiElement
import jet.Function1
import org.jetbrains.annotations.Nullable
import org.jetbrains.jet.lang.psi.JetExpression
import org.jetbrains.jet.plugin.intentions.branchedTransformations.*
import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention
import com.intellij.openapi.editor.Editor
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.psi.JetBinaryExpression
import org.jetbrains.jet.lang.psi.JetReturnExpression
import org.jetbrains.jet.lang.psi.JetProperty
public open class UnfoldBranchedExpressionIntention<T: JetExpression>(
val kind: UnfoldableKind, elementType: Class<T>
) : JetSelfTargetingIntention<T>(kind.getKey(), elementType) {
override fun isApplicableTo(element: T): Boolean = BranchedUnfoldingUtils.getUnfoldableExpressionKind(element) == kind
override fun applyTo(element: T, editor: Editor) {
val file = element.getContainingFile()
if (file is JetFile) {
kind.transform(element, editor, file)
}
}
}
public class UnfoldAssignmentToIfIntention : UnfoldBranchedExpressionIntention<JetBinaryExpression>(
UnfoldableKind.ASSIGNMENT_TO_IF, javaClass()
)
public class UnfoldPropertyToIfIntention : UnfoldBranchedExpressionIntention<JetProperty>(
UnfoldableKind.PROPERTY_TO_IF, javaClass()
)
public class UnfoldAssignmentToWhenIntention : UnfoldBranchedExpressionIntention<JetBinaryExpression>(
UnfoldableKind.ASSIGNMENT_TO_WHEN, javaClass()
)
public class UnfoldPropertyToWhenIntention : UnfoldBranchedExpressionIntention<JetProperty>(
UnfoldableKind.PROPERTY_TO_WHEN, javaClass()
)
public class UnfoldReturnToIfIntention : UnfoldBranchedExpressionIntention<JetReturnExpression>(
UnfoldableKind.RETURN_TO_IF, javaClass()
)
public class UnfoldReturnToWhenIntention : UnfoldBranchedExpressionIntention<JetReturnExpression>(
UnfoldableKind.RETURN_TO_WHEN, javaClass()
)
@@ -1,54 +0,0 @@
/*
* 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.intentions.branchedTransformations.intentions;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetWhenExpression;
import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationIntention;
import org.jetbrains.jet.plugin.intentions.branchedTransformations.IfWhenUtils;
import org.jetbrains.jet.plugin.intentions.Transformer;
public class WhenToIfIntention extends AbstractCodeTransformationIntention {
private static final Transformer TRANSFORMER = new Transformer() {
@NotNull
@Override
public String getKey() {
return "when.to.if";
}
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, @NotNull JetFile file) {
IfWhenUtils.transformWhenToIf((JetWhenExpression) element);
}
};
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public Boolean invoke(@Nullable PsiElement input) {
return (input instanceof JetWhenExpression) && IfWhenUtils.checkWhenToIf((JetWhenExpression) input);
}
};
public WhenToIfIntention() {
super(TRANSFORMER, IS_APPLICABLE);
}
}
@@ -0,0 +1,31 @@
/*
* 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.intentions.branchedTransformations.intentions
import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention
import org.jetbrains.jet.plugin.intentions.branchedTransformations.IfWhenUtils
import org.jetbrains.jet.lang.psi.JetWhenExpression
import org.jetbrains.jet.lang.psi.JetIfExpression
import com.intellij.openapi.editor.Editor
public class WhenToIfIntention : JetSelfTargetingIntention<JetWhenExpression>("if.to.when", javaClass()) {
override fun isApplicableTo(element: JetWhenExpression): Boolean = IfWhenUtils.checkWhenToIf(element)
override fun applyTo(element: JetWhenExpression, editor: Editor) {
IfWhenUtils.transformWhenToIf(element)
}
}
@@ -1,53 +0,0 @@
/*
* 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.intentions.declarations;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.plugin.intentions.AbstractCodeTransformationIntention;
import org.jetbrains.jet.plugin.intentions.Transformer;
public class SplitPropertyDeclarationIntention extends AbstractCodeTransformationIntention {
private static final Transformer TRANSFORMER = new Transformer() {
@NotNull
@Override
public String getKey() {
return "split.property.declaration";
}
@Override
public void transform(@NotNull PsiElement element, @NotNull Editor editor, @NotNull JetFile file) {
DeclarationUtils.splitPropertyDeclaration((JetProperty) element, file);
}
};
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public Boolean invoke(@Nullable PsiElement input) {
return (input instanceof JetProperty) && DeclarationUtils.checkSplitProperty((JetProperty) input);
}
};
public SplitPropertyDeclarationIntention() {
super(TRANSFORMER, IS_APPLICABLE);
}
}
@@ -0,0 +1,37 @@
/*
* 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.intentions.declarations
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import jet.Function1
import org.jetbrains.annotations.Nullable
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.psi.JetProperty
import org.jetbrains.jet.plugin.intentions.JetSelfTargetingIntention
import org.jetbrains.jet.plugin.intentions.Transformer
public class SplitPropertyDeclarationIntention : JetSelfTargetingIntention<JetProperty>("split.property.declaration", javaClass()) {
override fun isApplicableTo(element: JetProperty): Boolean = DeclarationUtils.checkSplitProperty(element)
override fun applyTo(element: JetProperty, editor: Editor) {
val file = element.getContainingFile()
if (file is JetFile) {
DeclarationUtils.splitPropertyDeclaration(element, file)
}
}
}
@@ -31,47 +31,47 @@ import java.io.File;
public abstract class AbstractCodeTransformationTest extends LightCodeInsightTestCase { public abstract class AbstractCodeTransformationTest extends LightCodeInsightTestCase {
public void doTestFoldIfToAssignment(@NotNull String path) throws Exception { public void doTestFoldIfToAssignment(@NotNull String path) throws Exception {
doTestIntention(path, new FoldBranchedExpressionIntention.FoldIfToAssignmentIntention()); doTestIntention(path, new FoldIfToAssignmentIntention());
} }
public void doTestFoldIfToReturn(@NotNull String path) throws Exception { public void doTestFoldIfToReturn(@NotNull String path) throws Exception {
doTestIntention(path, new FoldBranchedExpressionIntention.FoldIfToReturnIntention()); doTestIntention(path, new FoldIfToReturnIntention());
} }
public void doTestFoldIfToReturnAsymmetrically(@NotNull String path) throws Exception { public void doTestFoldIfToReturnAsymmetrically(@NotNull String path) throws Exception {
doTestIntention(path, new FoldBranchedExpressionIntention.FoldIfToReturnAsymmetricallyIntention()); doTestIntention(path, new FoldIfToReturnAsymmetricallyIntention());
} }
public void doTestFoldWhenToAssignment(@NotNull String path) throws Exception { public void doTestFoldWhenToAssignment(@NotNull String path) throws Exception {
doTestIntention(path, new FoldBranchedExpressionIntention.FoldWhenToAssignmentIntention()); doTestIntention(path, new FoldWhenToAssignmentIntention());
} }
public void doTestFoldWhenToReturn(@NotNull String path) throws Exception { public void doTestFoldWhenToReturn(@NotNull String path) throws Exception {
doTestIntention(path, new FoldBranchedExpressionIntention.FoldWhenToReturnIntention()); doTestIntention(path, new FoldWhenToReturnIntention());
} }
public void doTestUnfoldAssignmentToIf(@NotNull String path) throws Exception { public void doTestUnfoldAssignmentToIf(@NotNull String path) throws Exception {
doTestIntention(path, new UnfoldBranchedExpressionIntention.UnfoldAssignmentToIfIntention()); doTestIntention(path, new UnfoldAssignmentToIfIntention());
} }
public void doTestUnfoldAssignmentToWhen(@NotNull String path) throws Exception { public void doTestUnfoldAssignmentToWhen(@NotNull String path) throws Exception {
doTestIntention(path, new UnfoldBranchedExpressionIntention.UnfoldAssignmentToWhenIntention()); doTestIntention(path, new UnfoldAssignmentToWhenIntention());
} }
public void doTestUnfoldPropertyToIf(@NotNull String path) throws Exception { public void doTestUnfoldPropertyToIf(@NotNull String path) throws Exception {
doTestIntention(path, new UnfoldBranchedExpressionIntention.UnfoldPropertyToIfIntention()); doTestIntention(path, new UnfoldPropertyToIfIntention());
} }
public void doTestUnfoldPropertyToWhen(@NotNull String path) throws Exception { public void doTestUnfoldPropertyToWhen(@NotNull String path) throws Exception {
doTestIntention(path, new UnfoldBranchedExpressionIntention.UnfoldPropertyToWhenIntention()); doTestIntention(path, new UnfoldPropertyToWhenIntention());
} }
public void doTestUnfoldReturnToIf(@NotNull String path) throws Exception { public void doTestUnfoldReturnToIf(@NotNull String path) throws Exception {
doTestIntention(path, new UnfoldBranchedExpressionIntention.UnfoldReturnToIfIntention()); doTestIntention(path, new UnfoldReturnToIfIntention());
} }
public void doTestUnfoldReturnToWhen(@NotNull String path) throws Exception { public void doTestUnfoldReturnToWhen(@NotNull String path) throws Exception {
doTestIntention(path, new UnfoldBranchedExpressionIntention.UnfoldReturnToWhenIntention()); doTestIntention(path, new UnfoldReturnToWhenIntention());
} }
public void doTestIfToWhen(@NotNull String path) throws Exception { public void doTestIfToWhen(@NotNull String path) throws Exception {