Implement split/join property declaration
This commit is contained in:
@@ -830,4 +830,24 @@ public class JetPsiUtil {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiElement skipSiblingsBackwardByPredicate(@Nullable PsiElement element, Predicate<PsiElement> elementsToSkip) {
|
||||
if (element == null) return null;
|
||||
for (PsiElement e = element.getPrevSibling(); e != null; e = e.getPrevSibling()) {
|
||||
if (elementsToSkip.apply(e)) continue;
|
||||
return e;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiElement skipSiblingsForwardByPredicate(@Nullable PsiElement element, Predicate<PsiElement> elementsToSkip) {
|
||||
if (element == null) return null;
|
||||
for (PsiElement e = element.getNextSibling(); e != null; e = e.getNextSibling()) {
|
||||
if (elementsToSkip.apply(e)) continue;
|
||||
return e;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -345,7 +345,9 @@ public class GenerateTests {
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/ifWhen/whenToIf", "doTestWhenToIf"),
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/when/flatten", "doTestFlattenWhen"),
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/when/introduceSubject", "doTestIntroduceWhenSubject"),
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/when/eliminateSubject", "doTestEliminateWhenSubject")
|
||||
testModel("idea/testData/codeInsight/codeTransformations/branched/when/eliminateSubject", "doTestEliminateWhenSubject"),
|
||||
testModel("idea/testData/codeInsight/codeTransformations/declarations/split", "doTestSplitProperty"),
|
||||
testModel("idea/testData/codeInsight/codeTransformations/declarations/join", "doTestJoinProperty")
|
||||
);
|
||||
|
||||
generateTest(
|
||||
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
val n: Int
|
||||
n = a + b
|
||||
+1
@@ -0,0 +1 @@
|
||||
val n = a + b
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention splits property declaration with initializer and turns initializer to assignment.
|
||||
</body>
|
||||
</html>
|
||||
@@ -291,6 +291,8 @@
|
||||
implementation="org.jetbrains.jet.plugin.codeInsight.upDownMover.JetDeclarationMover"
|
||||
order="before jetExpression" />
|
||||
|
||||
<joinLinesHandler implementation="org.jetbrains.jet.plugin.codeInsight.codeTransformations.declarations.JetDeclarationJoinLinesHandler"/>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction</className>
|
||||
<category>Kotlin</category>
|
||||
@@ -386,6 +388,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.declarations.SplitPropertyDeclarationIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<project.converterProvider implementation="org.jetbrains.jet.plugin.converters.JetRunConfigurationSettingsFormatConverterProvider"/>
|
||||
|
||||
</extensions>
|
||||
|
||||
@@ -201,6 +201,8 @@ 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
|
||||
split.property.declaration=Split property declaration
|
||||
split.property.declaration.family=Split Property Declaration
|
||||
change.function.signature.action.single=Change function signature to ''{0}''
|
||||
change.function.signature.action.multiple=Change function signature...
|
||||
change.function.signature.family=Change function signature
|
||||
|
||||
+171
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* 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.declarations;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.psi.PsiComment;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiWhiteSpace;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.types.ErrorUtils;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening;
|
||||
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
|
||||
import org.jetbrains.jet.plugin.util.JetPsiMatcher;
|
||||
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
public class DeclarationUtils {
|
||||
private DeclarationUtils() {
|
||||
}
|
||||
|
||||
private static void assertNotNull(Object value) {
|
||||
assert value != null : "Expression must be checked before applying transformation";
|
||||
}
|
||||
|
||||
public static boolean checkSplitProperty(@NotNull JetProperty property) {
|
||||
return property.getInitializer() != null && property.isLocal();
|
||||
}
|
||||
|
||||
public static final Predicate<PsiElement> SKIP_DELIMITERS = new Predicate<PsiElement>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PsiElement input) {
|
||||
return input == null
|
||||
|| input instanceof PsiWhiteSpace || input instanceof PsiComment
|
||||
|| input.getNode().getElementType() == JetTokens.SEMICOLON;
|
||||
}
|
||||
};
|
||||
|
||||
@Nullable
|
||||
public static Pair<JetProperty, JetBinaryExpression> checkAndGetPropertyAndInitializer(@NotNull PsiElement element) {
|
||||
JetProperty property = null;
|
||||
JetExpression initializer = null;
|
||||
|
||||
if (element instanceof JetProperty) {
|
||||
PsiElement nextElement = JetPsiUtil.skipSiblingsForwardByPredicate(element, SKIP_DELIMITERS);
|
||||
if (nextElement instanceof JetExpression) {
|
||||
property = (JetProperty) element;
|
||||
initializer = (JetExpression) nextElement;
|
||||
}
|
||||
}
|
||||
|
||||
if (property == null) return null;
|
||||
if (property.getInitializer() != null) return null;
|
||||
if (!JetPsiUtil.isOrdinaryAssignment(initializer)) return null;
|
||||
|
||||
JetBinaryExpression assignment = (JetBinaryExpression) initializer;
|
||||
|
||||
if (!(assignment.getLeft() instanceof JetSimpleNameExpression)) return null;
|
||||
if (assignment.getRight() == null) return null;
|
||||
//noinspection ConstantConditions
|
||||
if (!JetPsiMatcher.checkIdentifierMatch(property.getNameIdentifier().getText(), assignment.getLeft().getText())) return null;
|
||||
|
||||
return new Pair<JetProperty, JetBinaryExpression>(property, assignment);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static JetType getPropertyTypeIfNeeded(@NotNull JetProperty property, @NotNull JetFile file) {
|
||||
if (property.getTypeRef() != null) return null;
|
||||
|
||||
JetType type = AnalyzerFacadeWithCache.analyzeFileWithCache(file).getBindingContext().get(
|
||||
BindingContext.EXPRESSION_TYPE,
|
||||
property.getInitializer()
|
||||
);
|
||||
|
||||
return type == null || ErrorUtils.isErrorType(type) ? null : type;
|
||||
}
|
||||
|
||||
// returns assignment which replaces initializer
|
||||
@NotNull
|
||||
public static JetBinaryExpression splitPropertyDeclaration(@NotNull JetProperty property, @NotNull JetFile file) {
|
||||
Project project = property.getProject();
|
||||
|
||||
PsiElement parent = property.getParent();
|
||||
assertNotNull(parent);
|
||||
|
||||
//noinspection unchecked
|
||||
JetExpression initializer = property.getInitializer();
|
||||
assertNotNull(initializer);
|
||||
|
||||
//noinspection ConstantConditions, unchecked
|
||||
JetBinaryExpression newInitializer = JetPsiFactory.createBinaryExpression(
|
||||
project, JetPsiFactory.createSimpleName(project, property.getName()), "=", initializer
|
||||
);
|
||||
|
||||
newInitializer = (JetBinaryExpression) parent.addAfter(newInitializer, property);
|
||||
parent.addAfter(JetPsiFactory.createNewLine(project), property);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
JetType inferredType = getPropertyTypeIfNeeded(property, file);
|
||||
|
||||
String typeStr = inferredType != null
|
||||
? DescriptorRenderer.TEXT.renderType(inferredType)
|
||||
: JetPsiUtil.getNullableText(property.getTypeRef());
|
||||
|
||||
//noinspection ConstantConditions
|
||||
property = (JetProperty) property.replace(
|
||||
JetPsiFactory.createProperty(project, property.getNameIdentifier().getText(), typeStr, property.isVar())
|
||||
);
|
||||
|
||||
if (inferredType != null) {
|
||||
ReferenceToClassesShortening.compactReferenceToClasses(Collections.singletonList(property.getTypeRef()));
|
||||
}
|
||||
|
||||
return newInitializer;
|
||||
}
|
||||
|
||||
// Returns joined property
|
||||
@NotNull
|
||||
public static JetProperty joinPropertyDeclarationWithInitializer(
|
||||
@NotNull Pair<JetProperty, JetBinaryExpression> propertyAndInitializer
|
||||
) {
|
||||
JetProperty property = propertyAndInitializer.first;
|
||||
assertNotNull(property);
|
||||
|
||||
JetBinaryExpression assignment = propertyAndInitializer.second;
|
||||
assertNotNull(assignment);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
JetProperty newProperty = JetPsiFactory.createProperty(
|
||||
property.getProject(),
|
||||
property.getNameIdentifier().getText(),
|
||||
JetPsiUtil.getNullableText(property.getTypeRef()),
|
||||
property.isVar(),
|
||||
JetPsiUtil.getNullableText(assignment.getRight())
|
||||
);
|
||||
|
||||
property.getParent().deleteChildRange(property.getNextSibling(), assignment);
|
||||
return (JetProperty) property.replace(newProperty);
|
||||
}
|
||||
|
||||
// Returns joined property
|
||||
@NotNull
|
||||
public static JetProperty joinPropertyDeclarationWithInitializer(@NotNull PsiElement element) {
|
||||
Pair<JetProperty, JetBinaryExpression> propertyAndInitializer = checkAndGetPropertyAndInitializer(element);
|
||||
assertNotNull(propertyAndInitializer);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
return joinPropertyDeclarationWithInitializer(propertyAndInitializer);
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package org.jetbrains.jet.plugin.codeInsight.codeTransformations.declarations;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.JetElement;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
|
||||
public class JetDeclarationJoinLinesHandler implements JoinRawLinesHandlerDelegate {
|
||||
private static final Predicate<PsiElement> IS_APPLICABLE = new Predicate<PsiElement>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PsiElement input) {
|
||||
return input != null && DeclarationUtils.checkAndGetPropertyAndInitializer(input) != null;
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public int tryJoinRawLines(Document document, PsiFile file, int start, int end) {
|
||||
PsiElement element = JetPsiUtil.skipSiblingsBackwardByPredicate(file.findElementAt(start), DeclarationUtils.SKIP_DELIMITERS);
|
||||
|
||||
PsiElement target = JetPsiUtil.getParentByTypeAndPredicate(element, JetElement.class, IS_APPLICABLE, false);
|
||||
if (target == null) return -1;
|
||||
|
||||
return DeclarationUtils.joinPropertyDeclarationWithInitializer(target).getTextRange().getStartOffset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int tryJoinLines(Document document, PsiFile file, int start, int end) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.declarations;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
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.codeInsight.codeTransformations.AbstractCodeTransformationIntention;
|
||||
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.Transformer;
|
||||
|
||||
public class SplitPropertyDeclarationIntention extends AbstractCodeTransformationIntention<Transformer> {
|
||||
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 Predicate<PsiElement> IS_APPLICABLE = new Predicate<PsiElement>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable PsiElement input) {
|
||||
return (input instanceof JetProperty) && DeclarationUtils.checkSplitProperty((JetProperty) input);
|
||||
}
|
||||
};
|
||||
|
||||
public SplitPropertyDeclarationIntention() {
|
||||
super(TRANSFORMER, IS_APPLICABLE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String
|
||||
x = if (n > 0)
|
||||
"> 0"
|
||||
else
|
||||
"<= 0"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String = if (n > 0)
|
||||
"> 0"
|
||||
else
|
||||
"<= 0"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo(n: Int) {
|
||||
val x: String<caret>
|
||||
x = if (n > 0)
|
||||
"> 0"
|
||||
else
|
||||
"<= 0"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String = if (n > 0)
|
||||
"> 0"
|
||||
else
|
||||
"<= 0"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String
|
||||
x = ""
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String = ""
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
val x: String<caret>
|
||||
x = ""
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String = ""
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String
|
||||
`x` = ""
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String = ""
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val `x`: String
|
||||
`x` = ""
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val `x`: String = ""
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val `x`: String
|
||||
x = ""
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val `x`: String = ""
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
fun foo(n: Int) {
|
||||
// foo3
|
||||
<caret>val /* foo */ x: String // foo2
|
||||
x = /* bar2 */ "" // bar3
|
||||
/* baz */
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun foo(n: Int) {
|
||||
// foo3
|
||||
<caret>val x: String = "" // bar3
|
||||
/* baz */
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
fun foo(n: Int) {
|
||||
// foo3
|
||||
val /* foo */ x: String // foo2<caret>
|
||||
x = /* bar2 */ "" // bar3
|
||||
/* baz */
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun foo(n: Int) {
|
||||
// foo3
|
||||
<caret>val x: String = "" // bar3
|
||||
/* baz */
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String; ;
|
||||
x = ""
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String = ""
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
val x: String; <caret>;
|
||||
x = ""
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String = ""
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun foo(n: Int) {
|
||||
val x: String;
|
||||
;<caret>
|
||||
x = ""
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String = ""
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: jet.String?
|
||||
x = ""
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: jet.String? = ""
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
val x: jet.String?<caret>
|
||||
x = ""
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: jet.String? = ""
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x =
|
||||
if (n > 0)
|
||||
"> 0"
|
||||
else
|
||||
"<= 0"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String
|
||||
x = if (n > 0)
|
||||
"> 0"
|
||||
else
|
||||
"<= 0"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>var x =
|
||||
if (n > 0)
|
||||
"> 0"
|
||||
else
|
||||
"<= 0"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>var x: String
|
||||
x = if (n > 0)
|
||||
"> 0"
|
||||
else
|
||||
"<= 0"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String
|
||||
x = ""
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(n: Int) {
|
||||
<caret>var x: String
|
||||
x = ""
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// IS_APPLICABLE: false
|
||||
val<caret> x = if (false) "0" else "1"
|
||||
@@ -0,0 +1,2 @@
|
||||
// IS_APPLICABLE: false
|
||||
var<caret> x = if (false) "0" else "1"
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x = ""
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: String
|
||||
x = ""
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>var x = ""
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>var x: String
|
||||
x = ""
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>var x = X<>()
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>var x
|
||||
x = X<>()
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x = X<>()
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x
|
||||
x = X<>()
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: jet.String? = ""
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>val x: jet.String?
|
||||
x = ""
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>var x: jet.String? = ""
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun foo(n: Int) {
|
||||
<caret>var x: jet.String?
|
||||
x = ""
|
||||
}
|
||||
+34
-21
@@ -16,85 +16,96 @@
|
||||
|
||||
package org.jetbrains.jet.plugin.codeInsight.codeTransformations;
|
||||
|
||||
import com.intellij.codeInsight.editorActions.JoinLinesHandler;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.InTextDirectivesUtils;
|
||||
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.intentions.*;
|
||||
import org.jetbrains.jet.plugin.codeInsight.codeTransformations.declarations.SplitPropertyDeclarationIntention;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public abstract class AbstractCodeTransformationTest extends LightCodeInsightTestCase {
|
||||
public void doTestFoldIfToAssignment(@NotNull String path) throws Exception {
|
||||
doTest(path, new FoldBranchedExpressionIntention.FoldIfToAssignmentIntention());
|
||||
doTestIntention(path, new FoldBranchedExpressionIntention.FoldIfToAssignmentIntention());
|
||||
}
|
||||
|
||||
public void doTestFoldIfToReturn(@NotNull String path) throws Exception {
|
||||
doTest(path, new FoldBranchedExpressionIntention.FoldIfToReturnIntention());
|
||||
doTestIntention(path, new FoldBranchedExpressionIntention.FoldIfToReturnIntention());
|
||||
}
|
||||
|
||||
public void doTestFoldIfToReturnAsymmetrically(@NotNull String path) throws Exception {
|
||||
doTest(path, new FoldBranchedExpressionIntention.FoldIfToReturnAsymmetricallyIntention());
|
||||
doTestIntention(path, new FoldBranchedExpressionIntention.FoldIfToReturnAsymmetricallyIntention());
|
||||
}
|
||||
|
||||
public void doTestFoldWhenToAssignment(@NotNull String path) throws Exception {
|
||||
doTest(path, new FoldBranchedExpressionIntention.FoldWhenToAssignmentIntention());
|
||||
doTestIntention(path, new FoldBranchedExpressionIntention.FoldWhenToAssignmentIntention());
|
||||
}
|
||||
|
||||
public void doTestFoldWhenToReturn(@NotNull String path) throws Exception {
|
||||
doTest(path, new FoldBranchedExpressionIntention.FoldWhenToReturnIntention());
|
||||
doTestIntention(path, new FoldBranchedExpressionIntention.FoldWhenToReturnIntention());
|
||||
}
|
||||
|
||||
public void doTestUnfoldAssignmentToIf(@NotNull String path) throws Exception {
|
||||
doTest(path, new UnfoldBranchedExpressionIntention.UnfoldAssignmentToIfIntention());
|
||||
doTestIntention(path, new UnfoldBranchedExpressionIntention.UnfoldAssignmentToIfIntention());
|
||||
}
|
||||
|
||||
public void doTestUnfoldAssignmentToWhen(@NotNull String path) throws Exception {
|
||||
doTest(path, new UnfoldBranchedExpressionIntention.UnfoldAssignmentToWhenIntention());
|
||||
doTestIntention(path, new UnfoldBranchedExpressionIntention.UnfoldAssignmentToWhenIntention());
|
||||
}
|
||||
|
||||
public void doTestUnfoldPropertyToIf(@NotNull String path) throws Exception {
|
||||
doTest(path, new UnfoldBranchedExpressionIntention.UnfoldPropertyToIfIntention());
|
||||
doTestIntention(path, new UnfoldBranchedExpressionIntention.UnfoldPropertyToIfIntention());
|
||||
}
|
||||
|
||||
public void doTestUnfoldPropertyToWhen(@NotNull String path) throws Exception {
|
||||
doTest(path, new UnfoldBranchedExpressionIntention.UnfoldPropertyToWhenIntention());
|
||||
doTestIntention(path, new UnfoldBranchedExpressionIntention.UnfoldPropertyToWhenIntention());
|
||||
}
|
||||
|
||||
public void doTestUnfoldReturnToIf(@NotNull String path) throws Exception {
|
||||
doTest(path, new UnfoldBranchedExpressionIntention.UnfoldReturnToIfIntention());
|
||||
doTestIntention(path, new UnfoldBranchedExpressionIntention.UnfoldReturnToIfIntention());
|
||||
}
|
||||
|
||||
public void doTestUnfoldReturnToWhen(@NotNull String path) throws Exception {
|
||||
doTest(path, new UnfoldBranchedExpressionIntention.UnfoldReturnToWhenIntention());
|
||||
doTestIntention(path, new UnfoldBranchedExpressionIntention.UnfoldReturnToWhenIntention());
|
||||
}
|
||||
|
||||
public void doTestIfToWhen(@NotNull String path) throws Exception {
|
||||
doTest(path, new IfToWhenIntention());
|
||||
doTestIntention(path, new IfToWhenIntention());
|
||||
}
|
||||
|
||||
public void doTestWhenToIf(@NotNull String path) throws Exception {
|
||||
doTest(path, new WhenToIfIntention());
|
||||
doTestIntention(path, new WhenToIfIntention());
|
||||
}
|
||||
|
||||
public void doTestFlattenWhen(@NotNull String path) throws Exception {
|
||||
doTest(path, new FlattenWhenIntention());
|
||||
doTestIntention(path, new FlattenWhenIntention());
|
||||
}
|
||||
|
||||
public void doTestIntroduceWhenSubject(@NotNull String path) throws Exception {
|
||||
doTest(path, new IntroduceWhenSubjectIntention());
|
||||
doTestIntention(path, new IntroduceWhenSubjectIntention());
|
||||
}
|
||||
|
||||
public void doTestEliminateWhenSubject(@NotNull String path) throws Exception {
|
||||
doTest(path, new EliminateWhenSubjectIntention());
|
||||
doTestIntention(path, new EliminateWhenSubjectIntention());
|
||||
}
|
||||
|
||||
public void doTestSplitProperty(@NotNull String path) throws Exception {
|
||||
doTestIntention(path, new SplitPropertyDeclarationIntention());
|
||||
}
|
||||
|
||||
public void doTestJoinProperty(@NotNull String path) throws Exception {
|
||||
doTestAction(path, new JoinLinesHandler(null));
|
||||
}
|
||||
|
||||
public void doTestRemoveUnnecessaryParentheses(@NotNull String path) throws Exception {
|
||||
doTest(path, new RemoveUnnecessaryParenthesesIntention());
|
||||
doTestIntention(path, new RemoveUnnecessaryParenthesesIntention());
|
||||
}
|
||||
|
||||
private void doTest(@NotNull String path, @NotNull IntentionAction intentionAction) throws Exception {
|
||||
private void doTestIntention(@NotNull String path, @NotNull IntentionAction intentionAction) throws Exception {
|
||||
configureByFile(path);
|
||||
|
||||
String fileText = FileUtil.loadFile(new File(path));
|
||||
@@ -104,12 +115,14 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes
|
||||
assert isApplicableExpected == intentionAction.isAvailable(getProject(), getEditor(), getFile())
|
||||
: "isAvailable() for " + intentionAction.getClass() + " should return " + isApplicableExpected;
|
||||
if (isApplicableExpected) {
|
||||
invokeAndCheck(path, intentionAction);
|
||||
intentionAction.invoke(getProject(), getEditor(), getFile());
|
||||
checkResultByFile(path + ".after");
|
||||
}
|
||||
}
|
||||
|
||||
private void invokeAndCheck(@NotNull String path, @NotNull IntentionAction intentionAction) {
|
||||
intentionAction.invoke(getProject(), getEditor(), getFile());
|
||||
private void doTestAction(@NotNull String path, @NotNull EditorActionHandler actionHandler) throws Exception {
|
||||
configureByFile(path);
|
||||
actionHandler.execute(getEditor(), getCurrentEditorDataContext());
|
||||
checkResultByFile(path + ".after");
|
||||
}
|
||||
|
||||
|
||||
+149
-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.IfToAssignment.class, CodeTransformationsTestGenerated.IfToReturn.class, CodeTransformationsTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationsTestGenerated.WhenToAssignment.class, CodeTransformationsTestGenerated.WhenToReturn.class, CodeTransformationsTestGenerated.AssignmentToIf.class, CodeTransformationsTestGenerated.AssignmentToWhen.class, CodeTransformationsTestGenerated.PropertyToIf.class, CodeTransformationsTestGenerated.PropertyToWhen.class, CodeTransformationsTestGenerated.ReturnToIf.class, CodeTransformationsTestGenerated.ReturnToWhen.class, CodeTransformationsTestGenerated.IfToWhen.class, CodeTransformationsTestGenerated.WhenToIf.class, CodeTransformationsTestGenerated.Flatten.class, CodeTransformationsTestGenerated.IntroduceSubject.class, CodeTransformationsTestGenerated.EliminateSubject.class})
|
||||
@InnerTestClasses({CodeTransformationsTestGenerated.IfToAssignment.class, CodeTransformationsTestGenerated.IfToReturn.class, CodeTransformationsTestGenerated.IfToReturnAsymmetrically.class, CodeTransformationsTestGenerated.WhenToAssignment.class, CodeTransformationsTestGenerated.WhenToReturn.class, CodeTransformationsTestGenerated.AssignmentToIf.class, CodeTransformationsTestGenerated.AssignmentToWhen.class, CodeTransformationsTestGenerated.PropertyToIf.class, CodeTransformationsTestGenerated.PropertyToWhen.class, CodeTransformationsTestGenerated.ReturnToIf.class, CodeTransformationsTestGenerated.ReturnToWhen.class, CodeTransformationsTestGenerated.IfToWhen.class, CodeTransformationsTestGenerated.WhenToIf.class, CodeTransformationsTestGenerated.Flatten.class, CodeTransformationsTestGenerated.IntroduceSubject.class, CodeTransformationsTestGenerated.EliminateSubject.class, CodeTransformationsTestGenerated.Split.class, CodeTransformationsTestGenerated.Join.class})
|
||||
public class CodeTransformationsTestGenerated extends AbstractCodeTransformationTest {
|
||||
@TestMetadata("idea/testData/codeInsight/codeTransformations/branched/folding/ifToAssignment")
|
||||
public static class IfToAssignment extends AbstractCodeTransformationTest {
|
||||
@@ -645,6 +645,152 @@ public class CodeTransformationsTestGenerated extends AbstractCodeTransformation
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/codeTransformations/declarations/split")
|
||||
public static class Split extends AbstractCodeTransformationTest {
|
||||
public void testAllFilesPresentInSplit() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/declarations/split"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("longInit.kt")
|
||||
public void testLongInit() throws Exception {
|
||||
doTestSplitProperty("idea/testData/codeInsight/codeTransformations/declarations/split/longInit.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("longInit2.kt")
|
||||
public void testLongInit2() throws Exception {
|
||||
doTestSplitProperty("idea/testData/codeInsight/codeTransformations/declarations/split/longInit2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("noInitializer.kt")
|
||||
public void testNoInitializer() throws Exception {
|
||||
doTestSplitProperty("idea/testData/codeInsight/codeTransformations/declarations/split/noInitializer.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("noInitializer2.kt")
|
||||
public void testNoInitializer2() throws Exception {
|
||||
doTestSplitProperty("idea/testData/codeInsight/codeTransformations/declarations/split/noInitializer2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nonLocalProperty.kt")
|
||||
public void testNonLocalProperty() throws Exception {
|
||||
doTestSplitProperty("idea/testData/codeInsight/codeTransformations/declarations/split/nonLocalProperty.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nonLocalProperty2.kt")
|
||||
public void testNonLocalProperty2() throws Exception {
|
||||
doTestSplitProperty("idea/testData/codeInsight/codeTransformations/declarations/split/nonLocalProperty2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInit.kt")
|
||||
public void testSimpleInit() throws Exception {
|
||||
doTestSplitProperty("idea/testData/codeInsight/codeTransformations/declarations/split/simpleInit.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInit2.kt")
|
||||
public void testSimpleInit2() throws Exception {
|
||||
doTestSplitProperty("idea/testData/codeInsight/codeTransformations/declarations/split/simpleInit2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithErrorType.kt")
|
||||
public void testSimpleInitWithErrorType() throws Exception {
|
||||
doTestSplitProperty("idea/testData/codeInsight/codeTransformations/declarations/split/simpleInitWithErrorType.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithErrorType2.kt")
|
||||
public void testSimpleInitWithErrorType2() throws Exception {
|
||||
doTestSplitProperty("idea/testData/codeInsight/codeTransformations/declarations/split/simpleInitWithErrorType2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithType.kt")
|
||||
public void testSimpleInitWithType() throws Exception {
|
||||
doTestSplitProperty("idea/testData/codeInsight/codeTransformations/declarations/split/simpleInitWithType.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithType2.kt")
|
||||
public void testSimpleInitWithType2() throws Exception {
|
||||
doTestSplitProperty("idea/testData/codeInsight/codeTransformations/declarations/split/simpleInitWithType2.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/codeTransformations/declarations/join")
|
||||
public static class Join extends AbstractCodeTransformationTest {
|
||||
public void testAllFilesPresentInJoin() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/declarations/join"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("longInit.kt")
|
||||
public void testLongInit() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/longInit.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("longInit2.kt")
|
||||
public void testLongInit2() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/longInit2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInit.kt")
|
||||
public void testSimpleInit() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/simpleInit.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInit2.kt")
|
||||
public void testSimpleInit2() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/simpleInit2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithBackticks.kt")
|
||||
public void testSimpleInitWithBackticks() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/simpleInitWithBackticks.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithBackticks2.kt")
|
||||
public void testSimpleInitWithBackticks2() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/simpleInitWithBackticks2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithBackticks3.kt")
|
||||
public void testSimpleInitWithBackticks3() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/simpleInitWithBackticks3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithComments.kt")
|
||||
public void testSimpleInitWithComments() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/simpleInitWithComments.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithComments2.kt")
|
||||
public void testSimpleInitWithComments2() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/simpleInitWithComments2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithSemicolons.kt")
|
||||
public void testSimpleInitWithSemicolons() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/simpleInitWithSemicolons.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithSemicolons2.kt")
|
||||
public void testSimpleInitWithSemicolons2() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/simpleInitWithSemicolons2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithSemicolons3.kt")
|
||||
public void testSimpleInitWithSemicolons3() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/simpleInitWithSemicolons3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithType.kt")
|
||||
public void testSimpleInitWithType() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/simpleInitWithType.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleInitWithType2.kt")
|
||||
public void testSimpleInitWithType2() throws Exception {
|
||||
doTestJoinProperty("idea/testData/codeInsight/codeTransformations/declarations/join/simpleInitWithType2.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite("CodeTransformationsTestGenerated");
|
||||
suite.addTestSuite(IfToAssignment.class);
|
||||
@@ -663,6 +809,8 @@ public class CodeTransformationsTestGenerated extends AbstractCodeTransformation
|
||||
suite.addTestSuite(Flatten.class);
|
||||
suite.addTestSuite(IntroduceSubject.class);
|
||||
suite.addTestSuite(EliminateSubject.class);
|
||||
suite.addTestSuite(Split.class);
|
||||
suite.addTestSuite(Join.class);
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user