Surround with: move declaration out from block
This commit is contained in:
@@ -116,8 +116,10 @@ move.when.else.branch.to.the.end.family.name=Move Else Branch to the End
|
||||
change.to.property.name.family.name=Change to property name
|
||||
change.to.property.name.action=Change ''{0}'' to ''{1}''
|
||||
|
||||
surround.with=Surround with
|
||||
surround.with.string.template="${expr}"
|
||||
surround.with.when.template=when (expr) {}
|
||||
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}''
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.jetbrains.jet.plugin.codeInsight.surroundWith;
|
||||
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -10,9 +12,13 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
import org.jetbrains.jet.plugin.codeInsight.CodeInsightUtils;
|
||||
import org.jetbrains.jet.plugin.project.WholeProjectAnalyzerFacade;
|
||||
|
||||
public class KotlinSurrounderUtils {
|
||||
public static String SURROUND_WITH = JetBundle.message("surround.with");
|
||||
public static String SURROUND_WITH_ERROR = JetBundle.message("surround.with.cannot.perform.action");
|
||||
|
||||
public static void addStatementsInBlock(
|
||||
@NotNull JetBlockExpression block,
|
||||
@@ -28,4 +34,8 @@ public class KotlinSurrounderUtils {
|
||||
BindingContext expressionBindingContext = ResolveSessionUtils.resolveToExpression(resolveSession, expression);
|
||||
return expressionBindingContext.get(BindingContext.EXPRESSION_TYPE, expression);
|
||||
}
|
||||
|
||||
public static void showErrorHint(@NotNull Project project, @NotNull Editor editor, @NotNull String message) {
|
||||
CodeInsightUtils.showErrorHint(project, editor, message, KotlinSurrounderUtils.SURROUND_WITH, null);
|
||||
}
|
||||
}
|
||||
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* 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.surroundWith;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import com.intellij.psi.search.LocalSearchScope;
|
||||
import com.intellij.psi.search.SearchScope;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiUtilCore;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
|
||||
import org.jetbrains.jet.lang.types.ErrorUtils;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.plugin.codeInsight.CodeInsightUtils;
|
||||
import org.jetbrains.jet.plugin.codeInsight.ReferenceToClassesShortening;
|
||||
import org.jetbrains.jet.plugin.project.WholeProjectAnalyzerFacade;
|
||||
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.jet.lang.psi.JetPsiFactory.*;
|
||||
|
||||
public class MoveDeclarationsOutHelper {
|
||||
|
||||
public static PsiElement[] move(@NotNull PsiElement container, @NotNull PsiElement[] statements, boolean generateDefaultInitializers) {
|
||||
if (statements.length == 0) {
|
||||
return statements;
|
||||
}
|
||||
|
||||
Project project = container.getProject();
|
||||
|
||||
List<PsiElement> resultStatements = new ArrayList<PsiElement>();
|
||||
List<JetProperty> propertiesDeclarations = new ArrayList<JetProperty>();
|
||||
|
||||
// Dummy element to add new declarations at the beginning
|
||||
PsiElement dummyFirstStatement = container.addBefore(createExpression(project, "dummyStatement "), statements[0]);
|
||||
|
||||
try {
|
||||
SearchScope scope = new LocalSearchScope(container);
|
||||
int lastStatementOffset = statements[statements.length - 1].getTextRange().getEndOffset();
|
||||
|
||||
for (PsiElement statement : statements) {
|
||||
if (needToDeclareOut(statement, lastStatementOffset, scope)) {
|
||||
if (statement instanceof JetProperty && ((JetProperty) statement).getInitializer() != null) {
|
||||
JetProperty property = (JetProperty) statement;
|
||||
JetProperty declaration = createVariableDeclaration(property, generateDefaultInitializers);
|
||||
declaration = (JetProperty) container.addBefore(declaration, dummyFirstStatement);
|
||||
propertiesDeclarations.add(declaration);
|
||||
container.addAfter(createNewLine(project), declaration);
|
||||
|
||||
JetBinaryExpression assignment = createVariableAssignment(property);
|
||||
resultStatements.add(property.replace(assignment));
|
||||
}
|
||||
else {
|
||||
PsiElement newStatement = container.addBefore(statement, dummyFirstStatement);
|
||||
container.addAfter(createNewLine(project), newStatement);
|
||||
container.deleteChildRange(statement, statement);
|
||||
}
|
||||
}
|
||||
else {
|
||||
resultStatements.add(statement);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
dummyFirstStatement.delete();
|
||||
}
|
||||
|
||||
ReferenceToClassesShortening.compactReferenceToClasses(propertiesDeclarations);
|
||||
|
||||
return PsiUtilCore.toPsiElementArray(resultStatements);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static JetBinaryExpression createVariableAssignment(@NotNull JetProperty property) {
|
||||
String propertyName = property.getName();
|
||||
assert propertyName != null : "Property should have a name " + property.getText();
|
||||
JetBinaryExpression assignment = (JetBinaryExpression) createExpression(property.getProject(), propertyName + " = x");
|
||||
JetExpression right = assignment.getRight();
|
||||
assert right != null : "Created binary expression should have a right part " + assignment.getText();
|
||||
JetExpression initializer = property.getInitializer();
|
||||
assert initializer != null : "Initializer should exist for property " + property.getText();
|
||||
right.replace(initializer);
|
||||
return assignment;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static JetProperty createVariableDeclaration(@NotNull JetProperty property, boolean generateDefaultInitializers) {
|
||||
JetType propertyType = getPropertyType(property);
|
||||
String defaultInitializer = null;
|
||||
if (generateDefaultInitializers && property.isVar()) {
|
||||
defaultInitializer = CodeInsightUtils.defaultInitializer(propertyType);
|
||||
}
|
||||
return createProperty(property, propertyType, defaultInitializer);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static JetType getPropertyType(@NotNull JetProperty property) {
|
||||
ResolveSession resolveSession = WholeProjectAnalyzerFacade.getLazyResolveSessionForFile((JetFile) property.getContainingFile());
|
||||
BindingContext expressionBindingContext = ResolveSessionUtils.resolveToExpression(resolveSession, property);
|
||||
|
||||
VariableDescriptor propertyDescriptor = expressionBindingContext.get(BindingContext.VARIABLE, property);
|
||||
assert propertyDescriptor != null : "Couldn't resolve property to property descriptor " + property.getText();
|
||||
return propertyDescriptor.getType();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static JetProperty createProperty(@NotNull JetProperty property, @NotNull JetType propertyType, @Nullable String initializer) {
|
||||
JetTypeReference typeRef = property.getTypeRef();
|
||||
String typeString = null;
|
||||
if (typeRef != null) {
|
||||
typeString = typeRef.getText();
|
||||
}
|
||||
else if (!ErrorUtils.isErrorType(propertyType)) {
|
||||
typeString = DescriptorRenderer.TEXT.renderType(propertyType);
|
||||
}
|
||||
|
||||
return JetPsiFactory.createProperty(property.getProject(), property.getName(), typeString, property.isVar(), initializer);
|
||||
}
|
||||
|
||||
private static boolean needToDeclareOut(@NotNull PsiElement element, int lastStatementOffset, @NotNull SearchScope scope) {
|
||||
if (element instanceof JetProperty ||
|
||||
element instanceof JetClassOrObject ||
|
||||
element instanceof JetFunction) {
|
||||
|
||||
// Descriptor for local object is linked with JetObjectNameDeclaration
|
||||
if (element instanceof JetObjectDeclaration) {
|
||||
JetObjectDeclarationName declarationName = ((JetObjectDeclaration) element).getNameAsDeclaration();
|
||||
if (declarationName != null) {
|
||||
element = declarationName;
|
||||
}
|
||||
}
|
||||
|
||||
PsiReference[] refs = ReferencesSearch.search(element, scope, false).toArray(PsiReference.EMPTY_ARRAY);
|
||||
if (refs.length > 0) {
|
||||
PsiReference lastRef = refs[refs.length - 1];
|
||||
if (lastRef.getElement().getTextOffset() > lastStatementOffset) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private MoveDeclarationsOutHelper() {
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -25,12 +25,18 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.KotlinSurrounderUtils;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.MoveDeclarationsOutHelper;
|
||||
|
||||
public class KotlinFunctionLiteralSurrounder extends KotlinStatementsSurrounder {
|
||||
@Nullable
|
||||
@Override
|
||||
protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) {
|
||||
// TODO extract variables declaration
|
||||
statements = MoveDeclarationsOutHelper.move(container, statements, true);
|
||||
|
||||
if (statements.length == 0) {
|
||||
KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR);
|
||||
return null;
|
||||
}
|
||||
|
||||
JetCallExpression callExpression = (JetCallExpression) JetPsiFactory.createExpression(project, "run {\n}");
|
||||
callExpression = (JetCallExpression) container.addAfter(callExpression, statements[statements.length - 1]);
|
||||
|
||||
+5
@@ -32,4 +32,9 @@ public class KotlinIfElseSurrounder extends KotlinIfSurrounderBase {
|
||||
protected String getCodeTemplate() {
|
||||
return "if (a) { \n} else { \n}";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isGenerateDefaultInitializers() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+5
@@ -32,4 +32,9 @@ public class KotlinIfSurrounder extends KotlinIfSurrounderBase {
|
||||
protected String getCodeTemplate() {
|
||||
return "if (a) { \n}";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isGenerateDefaultInitializers() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+9
-3
@@ -29,15 +29,19 @@ import org.jetbrains.jet.lang.psi.JetExpression;
|
||||
import org.jetbrains.jet.lang.psi.JetIfExpression;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiFactory;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.KotlinSurrounderUtils;
|
||||
|
||||
import java.lang.String;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.MoveDeclarationsOutHelper;
|
||||
|
||||
public abstract class KotlinIfSurrounderBase extends KotlinStatementsSurrounder {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) {
|
||||
// TODO extract variables declaration
|
||||
statements = MoveDeclarationsOutHelper.move(container, statements, isGenerateDefaultInitializers());
|
||||
|
||||
if (statements.length == 0) {
|
||||
KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR);
|
||||
return null;
|
||||
}
|
||||
|
||||
JetIfExpression ifExpression = (JetIfExpression) JetPsiFactory.createExpression(project, getCodeTemplate());
|
||||
ifExpression = (JetIfExpression) container.addAfter(ifExpression, statements[statements.length - 1]);
|
||||
@@ -65,4 +69,6 @@ public abstract class KotlinIfSurrounderBase extends KotlinStatementsSurrounder
|
||||
|
||||
@NotNull
|
||||
protected abstract String getCodeTemplate();
|
||||
|
||||
protected abstract boolean isGenerateDefaultInitializers();
|
||||
}
|
||||
+3
-1
@@ -38,7 +38,9 @@ public class KotlinTryFinallySurrounder extends KotlinTrySurrounderBase {
|
||||
@Override
|
||||
protected TextRange surroundStatements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement container, @NotNull PsiElement[] statements) {
|
||||
TextRange textRange = super.surroundStatements(project, editor, container, statements);
|
||||
assert textRange != null;
|
||||
if (textRange == null) {
|
||||
return null;
|
||||
}
|
||||
// Delete dummy "b" element for caret
|
||||
editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset());
|
||||
return new TextRange(textRange.getStartOffset(), textRange.getStartOffset());
|
||||
|
||||
+7
-1
@@ -25,13 +25,19 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.KotlinSurrounderUtils;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.MoveDeclarationsOutHelper;
|
||||
|
||||
public abstract class KotlinTrySurrounderBase extends KotlinStatementsSurrounder {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected TextRange surroundStatements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement container, @NotNull PsiElement[] statements) {
|
||||
// TODO extract variables declaration
|
||||
statements = MoveDeclarationsOutHelper.move(container, statements, true);
|
||||
|
||||
if (statements.length == 0) {
|
||||
KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR);
|
||||
return null;
|
||||
}
|
||||
|
||||
JetTryExpression tryExpression = (JetTryExpression) JetPsiFactory.createExpression(project, getCodeTemplate());
|
||||
tryExpression = (JetTryExpression) container.addAfter(tryExpression, statements[statements.length - 1]);
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
<selection>val a = "aaa"
|
||||
val b = "aaa"</selection>
|
||||
|
||||
a.charAt(1)
|
||||
b.charAt(1)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fun foo() {
|
||||
val a: String
|
||||
val b: String
|
||||
<selection>run</selection> {
|
||||
a = "aaa"
|
||||
b = "aaa"
|
||||
}
|
||||
|
||||
a.charAt(1)
|
||||
b.charAt(1)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo() {
|
||||
<selection>class A {}
|
||||
"bbb"</selection>
|
||||
|
||||
val myClass: A? = null
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
class A {}
|
||||
if (<caret>) {
|
||||
"bbb"
|
||||
}
|
||||
|
||||
val myClass: A? = null
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo() {
|
||||
<selection>class A {}
|
||||
"bbb"</selection>
|
||||
|
||||
val myClass = A()
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
class A {}
|
||||
if (<caret>) {
|
||||
"bbb"
|
||||
}
|
||||
|
||||
val myClass = A()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<selection>fun test() {}
|
||||
"aaa"</selection>
|
||||
test()
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
fun test() {}
|
||||
if (<caret>) {
|
||||
"aaa"
|
||||
}
|
||||
test()
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
<selection>"aaa"
|
||||
fun test() {}
|
||||
"aaa"</selection>
|
||||
|
||||
test()
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
fun foo() {
|
||||
fun test() {}
|
||||
if (<caret>) {
|
||||
"aaa"
|
||||
"aaa"
|
||||
}
|
||||
|
||||
test()
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
fun foo() {
|
||||
<selection>
|
||||
fun test() {}
|
||||
"aaa"
|
||||
test()
|
||||
</selection>
|
||||
|
||||
val a = "ss"
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fun foo() {
|
||||
|
||||
if (<caret>) {
|
||||
fun test() {}
|
||||
"aaa"
|
||||
test()
|
||||
}
|
||||
|
||||
|
||||
val a = "ss"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo() {
|
||||
<selection>object A {}
|
||||
"bbb"</selection>
|
||||
|
||||
val myClass = A
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
object A {}
|
||||
if (<caret>) {
|
||||
"bbb"
|
||||
}
|
||||
|
||||
val myClass = A
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun foo() {
|
||||
<selection>"start"
|
||||
open class A {}
|
||||
class B: A() {}
|
||||
"end"</selection>
|
||||
|
||||
val c = B()
|
||||
val d = A()
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fun foo() {
|
||||
open class A {}
|
||||
class B: A() {}
|
||||
if (<caret>) {
|
||||
"start"
|
||||
"end"
|
||||
}
|
||||
|
||||
val c = B()
|
||||
val d = A()
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun foo() {
|
||||
<selection>class A {
|
||||
fun test() {}
|
||||
}
|
||||
val d: A = A()</selection>
|
||||
|
||||
d.test()
|
||||
A()
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fun foo() {
|
||||
class A {
|
||||
fun test() {}
|
||||
}
|
||||
val d: A
|
||||
if (<caret>) {
|
||||
d = A()
|
||||
}
|
||||
|
||||
d.test()
|
||||
A()
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
<selection>val a: String = "aaa"
|
||||
val b = a</selection>
|
||||
|
||||
a.charAt(1)
|
||||
b.charAt(1)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fun foo() {
|
||||
val a: String
|
||||
val b: String
|
||||
if (<caret>) {
|
||||
a = "aaa"
|
||||
b = a
|
||||
}
|
||||
|
||||
a.charAt(1)
|
||||
b.charAt(1)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>val a: kotlin.test.Asserter? = null
|
||||
|
||||
a?.charAt(1)
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
val a: kotlin.test.Asserter?
|
||||
if (<caret>) {
|
||||
a = null
|
||||
}
|
||||
|
||||
a?.charAt(1)
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package test
|
||||
|
||||
class A {}
|
||||
|
||||
fun foo() {
|
||||
<caret>val a = test.A()
|
||||
|
||||
a.hashCode()
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package test
|
||||
|
||||
class A {}
|
||||
|
||||
fun foo() {
|
||||
val a: A
|
||||
if (<caret>) {
|
||||
a = test.A()
|
||||
}
|
||||
|
||||
a.hashCode()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>val a: String = "aaa"
|
||||
|
||||
a.charAt(1)
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
val a: String
|
||||
if () {
|
||||
a = "aaa"
|
||||
}
|
||||
|
||||
a.charAt(1)
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fun foo() {
|
||||
<selection>val a: String
|
||||
a = "a"
|
||||
a.capitalize()
|
||||
val b: String
|
||||
b = "b"
|
||||
b.capitalize()</selection>
|
||||
|
||||
a.charAt(1)
|
||||
b.charAt(1)
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
fun foo() {
|
||||
val a: String
|
||||
val b: String
|
||||
if () {
|
||||
a = "a"
|
||||
a.capitalize()
|
||||
b = "b"
|
||||
b.capitalize()
|
||||
}
|
||||
|
||||
a.charAt(1)
|
||||
b.charAt(1)
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>val a = "aaa"
|
||||
|
||||
a.charAt(1)
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
val a: String
|
||||
if () {
|
||||
a = "aaa"
|
||||
}
|
||||
|
||||
a.charAt(1)
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
<caret>var a: Boolean = true
|
||||
a = true
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
var a: Boolean = false
|
||||
if (<caret>) {
|
||||
a = true
|
||||
}
|
||||
a = true
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
<caret>var a: String? = "aaa"
|
||||
a = "bbb"
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
var a: String? = null
|
||||
if (<caret>) {
|
||||
a = "aaa"
|
||||
}
|
||||
a = "bbb"
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
fun foo() {
|
||||
<selection>var a: Int = 1
|
||||
var b: Char = 1
|
||||
var c: Byte = 1
|
||||
var d: Long = 1
|
||||
var e: Short = 1
|
||||
var f: Double = 1
|
||||
var g: Float = 1</selection>
|
||||
a = 2
|
||||
b = 2
|
||||
c = 2
|
||||
d = 2
|
||||
e = 2
|
||||
f = 2
|
||||
g = 2
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
fun foo() {
|
||||
var a: Int = 0
|
||||
var b: Char = 0
|
||||
var c: Byte = 0
|
||||
var d: Long = 0
|
||||
var e: Short = 0
|
||||
var f: Double = 0
|
||||
var g: Float = 0
|
||||
if (<caret>) {
|
||||
a = 1
|
||||
b = 1
|
||||
c = 1
|
||||
d = 1
|
||||
e = 1
|
||||
f = 1
|
||||
g = 1
|
||||
}
|
||||
a = 2
|
||||
b = 2
|
||||
c = 2
|
||||
d = 2
|
||||
e = 2
|
||||
f = 2
|
||||
g = 2
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>var a: String = "aaa"
|
||||
|
||||
a.charAt(1)
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
var a: String
|
||||
if () {
|
||||
a = "aaa"
|
||||
}
|
||||
|
||||
a.charAt(1)
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
fun foo() {
|
||||
<selection>var a: String
|
||||
a.capitalize()</selection>
|
||||
|
||||
a.charAt(1)
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
var a: String
|
||||
if () {
|
||||
a.capitalize()
|
||||
}
|
||||
|
||||
a.charAt(1)
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<caret>var a = "aaa"
|
||||
|
||||
a.charAt(1)
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
var a: String
|
||||
if () {
|
||||
a = "aaa"
|
||||
}
|
||||
|
||||
a.charAt(1)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
<caret>val a: String? = "aaa"
|
||||
a.toString()
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
val a: String?
|
||||
if (<caret>) {
|
||||
a = "aaa"
|
||||
} else {
|
||||
}
|
||||
a.toString()
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
<caret>var a: String? = "aaa"
|
||||
a = "bbb"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
var a: String?
|
||||
if (<caret>) {
|
||||
a = "aaa"
|
||||
} else {
|
||||
}
|
||||
a = "bbb"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
<selection>val a = "aaa"
|
||||
val b = "aaa"</selection>
|
||||
|
||||
a.charAt(1)
|
||||
b.charAt(1)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fun foo() {
|
||||
val a: String
|
||||
val b: String
|
||||
try {
|
||||
a = "aaa"
|
||||
b = "aaa"
|
||||
} catch(e: <selection>Exception</selection>) {
|
||||
}
|
||||
|
||||
a.charAt(1)
|
||||
b.charAt(1)
|
||||
}
|
||||
+211
-1
@@ -33,6 +33,7 @@ import org.jetbrains.jet.plugin.codeInsight.surroundWith.AbstractSurroundWithTes
|
||||
@InnerTestClasses({SurroundWithTestGenerated.If.class, SurroundWithTestGenerated.IfElse.class, SurroundWithTestGenerated.Not.class, SurroundWithTestGenerated.Parentheses.class, SurroundWithTestGenerated.StringTemplate.class, SurroundWithTestGenerated.When.class, SurroundWithTestGenerated.TryCatch.class, SurroundWithTestGenerated.TryCatchFinally.class, SurroundWithTestGenerated.TryFinally.class, SurroundWithTestGenerated.FunctionLiteral.class})
|
||||
public class SurroundWithTestGenerated extends AbstractSurroundWithTest {
|
||||
@TestMetadata("idea/testData/codeInsight/surroundWith/if")
|
||||
@InnerTestClasses({If.MoveDeclarationsOut.class})
|
||||
public static class If extends AbstractSurroundWithTest {
|
||||
public void testAllFilesPresentInIf() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/if"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
@@ -63,6 +64,195 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/variable.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut")
|
||||
@InnerTestClasses({MoveDeclarationsOut.Class.class, MoveDeclarationsOut.Function.class, MoveDeclarationsOut.Object.class, MoveDeclarationsOut.Order.class, MoveDeclarationsOut.Val.class, MoveDeclarationsOut.Var.class})
|
||||
public static class MoveDeclarationsOut extends AbstractSurroundWithTest {
|
||||
public void testAllFilesPresentInMoveDeclarationsOut() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/class")
|
||||
public static class Class extends AbstractSurroundWithTest {
|
||||
public void testAllFilesPresentInClass() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/class"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("classInType.kt")
|
||||
public void testClassInType() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/class/classInType.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("localClass.kt")
|
||||
public void testLocalClass() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/class/localClass.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/function")
|
||||
public static class Function extends AbstractSurroundWithTest {
|
||||
public void testAllFilesPresentInFunction() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/function"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("firstChildLocalFun.kt")
|
||||
public void testFirstChildLocalFun() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/function/firstChildLocalFun.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("localFun.kt")
|
||||
public void testLocalFun() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/function/localFun.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("unusedLocalFun.kt")
|
||||
public void testUnusedLocalFun() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/function/unusedLocalFun.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/object")
|
||||
public static class Object extends AbstractSurroundWithTest {
|
||||
public void testAllFilesPresentInObject() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/object"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("localObject.kt")
|
||||
public void testLocalObject() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/object/localObject.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/order")
|
||||
public static class Order extends AbstractSurroundWithTest {
|
||||
public void testAllFilesPresentInOrder() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/order"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("twoClasses.kt")
|
||||
public void testTwoClasses() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/order/twoClasses.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("valAndClass.kt")
|
||||
public void testValAndClass() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/order/valAndClass.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("valOrder.kt")
|
||||
public void testValOrder() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/order/valOrder.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/val")
|
||||
public static class Val extends AbstractSurroundWithTest {
|
||||
public void testAllFilesPresentInVal() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/val"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("fullQualifiedType.kt")
|
||||
public void testFullQualifiedType() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/val/fullQualifiedType.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fullQualifiedTypeWithoutTypeRef.kt")
|
||||
public void testFullQualifiedTypeWithoutTypeRef() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/val/fullQualifiedTypeWithoutTypeRef.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("valWithTypeWithInitializer.kt")
|
||||
public void testValWithTypeWithInitializer() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/val/valWithTypeWithInitializer.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("valWithTypeWoInitializer.kt")
|
||||
public void testValWithTypeWoInitializer() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/val/valWithTypeWoInitializer.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("valWoTypeWithInitializer.kt")
|
||||
public void testValWoTypeWithInitializer() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/val/valWoTypeWithInitializer.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/var")
|
||||
@InnerTestClasses({Var.DefaultValue.class})
|
||||
public static class Var extends AbstractSurroundWithTest {
|
||||
public void testAllFilesPresentInVar() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/var"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("varWithNotNullableTypeWithInitializer.kt")
|
||||
public void testVarWithNotNullableTypeWithInitializer() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/var/varWithNotNullableTypeWithInitializer.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("varWithTypeWoInitializer.kt")
|
||||
public void testVarWithTypeWoInitializer() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/var/varWithTypeWoInitializer.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("varWoTypeWithInitializer.kt")
|
||||
public void testVarWoTypeWithInitializer() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/var/varWoTypeWithInitializer.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/var/defaultValue")
|
||||
public static class DefaultValue extends AbstractSurroundWithTest {
|
||||
public void testAllFilesPresentInDefaultValue() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/var/defaultValue"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("boolean.kt")
|
||||
public void testBoolean() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/var/defaultValue/boolean.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("nullable.kt")
|
||||
public void testNullable() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/var/defaultValue/nullable.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("primitiveNumbers.kt")
|
||||
public void testPrimitiveNumbers() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/moveDeclarationsOut/var/defaultValue/primitiveNumbers.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Test innerSuite() {
|
||||
TestSuite suite = new TestSuite("Var");
|
||||
suite.addTestSuite(Var.class);
|
||||
suite.addTestSuite(DefaultValue.class);
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
public static Test innerSuite() {
|
||||
TestSuite suite = new TestSuite("MoveDeclarationsOut");
|
||||
suite.addTestSuite(MoveDeclarationsOut.class);
|
||||
suite.addTestSuite(Class.class);
|
||||
suite.addTestSuite(Function.class);
|
||||
suite.addTestSuite(Object.class);
|
||||
suite.addTestSuite(Order.class);
|
||||
suite.addTestSuite(Val.class);
|
||||
suite.addTest(Var.innerSuite());
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
public static Test innerSuite() {
|
||||
TestSuite suite = new TestSuite("If");
|
||||
suite.addTestSuite(If.class);
|
||||
suite.addTest(MoveDeclarationsOut.innerSuite());
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/codeInsight/surroundWith/ifElse")
|
||||
@@ -76,6 +266,16 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest {
|
||||
doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/block.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("moveDeclarationsOutVal.kt")
|
||||
public void testMoveDeclarationsOutVal() throws Exception {
|
||||
doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/moveDeclarationsOutVal.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("moveDeclarationsOutVar.kt")
|
||||
public void testMoveDeclarationsOutVar() throws Exception {
|
||||
doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/moveDeclarationsOutVar.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("severalStatements.kt")
|
||||
public void testSeveralStatements() throws Exception {
|
||||
doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/severalStatements.kt");
|
||||
@@ -281,6 +481,11 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/tryCatch"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("moveDeclarationsOut.kt")
|
||||
public void testMoveDeclarationsOut() throws Exception {
|
||||
doTestWithTryCatchSurrounder("idea/testData/codeInsight/surroundWith/tryCatch/moveDeclarationsOut.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("multiExpression.kt")
|
||||
public void testMultiExpression() throws Exception {
|
||||
doTestWithTryCatchSurrounder("idea/testData/codeInsight/surroundWith/tryCatch/multiExpression.kt");
|
||||
@@ -335,6 +540,11 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/functionLiteral"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("moveDeclarationsOut.kt")
|
||||
public void testMoveDeclarationsOut() throws Exception {
|
||||
doTestWithFunctionLiteralSurrounder("idea/testData/codeInsight/surroundWith/functionLiteral/moveDeclarationsOut.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("multiStatement.kt")
|
||||
public void testMultiStatement() throws Exception {
|
||||
doTestWithFunctionLiteralSurrounder("idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt");
|
||||
@@ -349,7 +559,7 @@ public class SurroundWithTestGenerated extends AbstractSurroundWithTest {
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite("SurroundWithTestGenerated");
|
||||
suite.addTestSuite(If.class);
|
||||
suite.addTest(If.innerSuite());
|
||||
suite.addTestSuite(IfElse.class);
|
||||
suite.addTestSuite(Not.class);
|
||||
suite.addTest(Parentheses.innerSuite());
|
||||
|
||||
Reference in New Issue
Block a user