Add surround with if /else
This commit is contained in:
@@ -229,6 +229,13 @@ public class GenerateTests {
|
|||||||
AbstractSurroundWithTest.class,
|
AbstractSurroundWithTest.class,
|
||||||
testModel("idea/testData/codeInsight/surroundWith/if", "doTestWithIfSurrounder")
|
testModel("idea/testData/codeInsight/surroundWith/if", "doTestWithIfSurrounder")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
generateTest(
|
||||||
|
"idea/tests/",
|
||||||
|
"SurroundWithIfElseTestGenerated",
|
||||||
|
AbstractSurroundWithTest.class,
|
||||||
|
testModel("idea/testData/codeInsight/surroundWith/ifElse", "doTestWithIfElseSurrounder")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
|
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
|
||||||
|
|||||||
+35
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* 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.codeInsight.CodeInsightBundle;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public class KotlinIfElseSurrounder extends KotlinIfSurrounderBase {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getTemplateDescription() {
|
||||||
|
return CodeInsightBundle.message("surround.with.ifelse.template");
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
protected String getCodeTemplate() {
|
||||||
|
return "if (a) { \n} else { \n}";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,69 +18,18 @@ package org.jetbrains.jet.plugin.codeInsight.surroundWith;
|
|||||||
|
|
||||||
|
|
||||||
import com.intellij.codeInsight.CodeInsightBundle;
|
import com.intellij.codeInsight.CodeInsightBundle;
|
||||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
|
||||||
import com.intellij.lang.surroundWith.Surrounder;
|
|
||||||
import com.intellij.openapi.editor.Editor;
|
|
||||||
import com.intellij.openapi.project.Project;
|
|
||||||
import com.intellij.openapi.util.TextRange;
|
|
||||||
import com.intellij.psi.PsiElement;
|
|
||||||
import com.intellij.util.IncorrectOperationException;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.jet.lang.psi.JetBlockExpression;
|
|
||||||
import org.jetbrains.jet.lang.psi.JetExpression;
|
|
||||||
import org.jetbrains.jet.lang.psi.JetIfExpression;
|
|
||||||
import org.jetbrains.jet.lang.psi.JetPsiFactory;
|
|
||||||
|
|
||||||
public class KotlinIfSurrounder implements Surrounder {
|
public class KotlinIfSurrounder extends KotlinIfSurrounderBase {
|
||||||
|
|
||||||
|
@Override
|
||||||
public String getTemplateDescription() {
|
public String getTemplateDescription() {
|
||||||
return CodeInsightBundle.message("surround.with.if.template");
|
return CodeInsightBundle.message("surround.with.if.template");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public boolean isApplicable(@NotNull PsiElement[] elements) {
|
protected String getCodeTemplate() {
|
||||||
return true;
|
return "if (a) { \n}";
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public TextRange surroundElements(
|
|
||||||
@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements
|
|
||||||
) throws IncorrectOperationException {
|
|
||||||
PsiElement container = elements[0].getParent();
|
|
||||||
if (container == null) return null;
|
|
||||||
return surroundStatements (project, editor, container, elements);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException{
|
|
||||||
// TODO extract variables declaration
|
|
||||||
|
|
||||||
if (statements.length == 0){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
String text = "if (a) { \n}";
|
|
||||||
JetIfExpression ifExpression = (JetIfExpression) JetPsiFactory.createExpression(project, text);
|
|
||||||
ifExpression = (JetIfExpression) container.addAfter(ifExpression, statements[statements.length - 1]);
|
|
||||||
|
|
||||||
// TODO move a comment for first statement
|
|
||||||
|
|
||||||
final JetBlockExpression thenBranch = (JetBlockExpression) ifExpression.getThen();
|
|
||||||
assert thenBranch != null : "Then branch should exist for created if expression: " + ifExpression.getText();
|
|
||||||
// Add statements in then branch of created if
|
|
||||||
KotlinSurrounderUtils.addStatementsInBlock(thenBranch, statements);
|
|
||||||
|
|
||||||
// Delete statements from original code
|
|
||||||
container.deleteChildRange(statements[0], statements[statements.length - 1]);
|
|
||||||
|
|
||||||
ifExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(ifExpression);
|
|
||||||
|
|
||||||
JetExpression condition = ifExpression.getCondition();
|
|
||||||
assert condition != null : "Condition should exists for created if expression: " + ifExpression.getText();
|
|
||||||
// Delete condition from created if
|
|
||||||
TextRange range = condition.getTextRange();
|
|
||||||
TextRange textRange = new TextRange(range.getStartOffset(), range.getStartOffset());
|
|
||||||
editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
|
|
||||||
return textRange;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+86
@@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* 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.codeInsight.CodeInsightUtilBase;
|
||||||
|
import com.intellij.lang.surroundWith.Surrounder;
|
||||||
|
import com.intellij.openapi.editor.Editor;
|
||||||
|
import com.intellij.openapi.project.Project;
|
||||||
|
import com.intellij.openapi.util.TextRange;
|
||||||
|
import com.intellij.psi.PsiElement;
|
||||||
|
import com.intellij.util.IncorrectOperationException;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.jet.lang.psi.JetBlockExpression;
|
||||||
|
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||||
|
import org.jetbrains.jet.lang.psi.JetIfExpression;
|
||||||
|
import org.jetbrains.jet.lang.psi.JetPsiFactory;
|
||||||
|
|
||||||
|
import java.lang.String;
|
||||||
|
|
||||||
|
public abstract class KotlinIfSurrounderBase implements Surrounder {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isApplicable(@NotNull PsiElement[] elements) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public TextRange surroundElements(
|
||||||
|
@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements
|
||||||
|
) throws IncorrectOperationException {
|
||||||
|
PsiElement container = elements[0].getParent();
|
||||||
|
if (container == null) return null;
|
||||||
|
return surroundStatements (project, editor, container, elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException{
|
||||||
|
// TODO extract variables declaration
|
||||||
|
|
||||||
|
if (statements.length == 0){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
JetIfExpression ifExpression = (JetIfExpression) JetPsiFactory.createExpression(project, getCodeTemplate());
|
||||||
|
ifExpression = (JetIfExpression) container.addAfter(ifExpression, statements[statements.length - 1]);
|
||||||
|
|
||||||
|
// TODO move a comment for first statement
|
||||||
|
|
||||||
|
final JetBlockExpression thenBranch = (JetBlockExpression) ifExpression.getThen();
|
||||||
|
assert thenBranch != null : "Then branch should exist for created if expression: " + ifExpression.getText();
|
||||||
|
// Add statements in then branch of created if
|
||||||
|
KotlinSurrounderUtils.addStatementsInBlock(thenBranch, statements);
|
||||||
|
|
||||||
|
// Delete statements from original code
|
||||||
|
container.deleteChildRange(statements[0], statements[statements.length - 1]);
|
||||||
|
|
||||||
|
ifExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(ifExpression);
|
||||||
|
|
||||||
|
JetExpression condition = ifExpression.getCondition();
|
||||||
|
assert condition != null : "Condition should exists for created if expression: " + ifExpression.getText();
|
||||||
|
// Delete condition from created if
|
||||||
|
TextRange range = condition.getTextRange();
|
||||||
|
TextRange textRange = new TextRange(range.getStartOffset(), range.getStartOffset());
|
||||||
|
editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
|
||||||
|
return textRange;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
protected abstract String getCodeTemplate();
|
||||||
|
}
|
||||||
+2
-1
@@ -26,7 +26,8 @@ import org.jetbrains.jet.plugin.refactoring.JetRefactoringUtil;
|
|||||||
public class KotlinStatementSurroundDescriptor implements SurroundDescriptor {
|
public class KotlinStatementSurroundDescriptor implements SurroundDescriptor {
|
||||||
|
|
||||||
private static final Surrounder[] SURROUNDERS = {
|
private static final Surrounder[] SURROUNDERS = {
|
||||||
new KotlinIfSurrounder()
|
new KotlinIfSurrounder(),
|
||||||
|
new KotlinIfElseSurrounder()
|
||||||
};
|
};
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fun foo() {
|
||||||
|
val a: String? = null
|
||||||
|
|
||||||
|
<selection>if (a != null) {
|
||||||
|
a.length()
|
||||||
|
}</selection>
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
fun foo() {
|
||||||
|
val a: String? = null
|
||||||
|
|
||||||
|
if (<caret>) {
|
||||||
|
if (a != null) {
|
||||||
|
a.length()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fun foo() {
|
||||||
|
<selection>"".capitalize()
|
||||||
|
"".capitalize()</selection>
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fun foo() {
|
||||||
|
if (<caret>) {
|
||||||
|
"".capitalize()
|
||||||
|
"".capitalize()
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo() {
|
||||||
|
<selection>"".capitalize()</selection>
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
fun foo() {
|
||||||
|
if (<caret>) {
|
||||||
|
"".capitalize()
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo() {
|
||||||
|
"".capi<caret>talize()
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
fun foo() {
|
||||||
|
if (<caret>) {
|
||||||
|
"".capitalize()
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
fun foo() {
|
||||||
|
val a = ""
|
||||||
|
|
||||||
|
<caret>a
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
fun foo() {
|
||||||
|
val a = ""
|
||||||
|
|
||||||
|
if (<caret>) {
|
||||||
|
a
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
@@ -28,6 +28,12 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase
|
|||||||
checkResultByFile(path + ".after");
|
checkResultByFile(path + ".after");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void doTestWithIfElseSurrounder(String path) throws Exception {
|
||||||
|
configureByFile(path);
|
||||||
|
SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), new KotlinIfElseSurrounder());
|
||||||
|
checkResultByFile(path + ".after");
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
protected String getTestDataPath() {
|
protected String getTestDataPath() {
|
||||||
|
|||||||
+64
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2013 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.jet.plugin.codeInsight.surroundWith;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import org.jetbrains.jet.JetTestUtils;
|
||||||
|
import org.jetbrains.jet.test.InnerTestClasses;
|
||||||
|
import org.jetbrains.jet.test.TestMetadata;
|
||||||
|
|
||||||
|
import org.jetbrains.jet.plugin.codeInsight.surroundWith.AbstractSurroundWithTest;
|
||||||
|
|
||||||
|
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
@TestMetadata("idea/testData/codeInsight/surroundWith/ifElse")
|
||||||
|
public class SurroundWithIfElseTestGenerated extends AbstractSurroundWithTest {
|
||||||
|
public void testAllFilesPresentInIfElse() throws Exception {
|
||||||
|
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/ifElse"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("block.kt")
|
||||||
|
public void testBlock() throws Exception {
|
||||||
|
doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/block.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("severalStatements.kt")
|
||||||
|
public void testSeveralStatements() throws Exception {
|
||||||
|
doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/severalStatements.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("singleStatement.kt")
|
||||||
|
public void testSingleStatement() throws Exception {
|
||||||
|
doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/singleStatement.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("singleStatementAtCaret.kt")
|
||||||
|
public void testSingleStatementAtCaret() throws Exception {
|
||||||
|
doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/singleStatementAtCaret.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("variable.kt")
|
||||||
|
public void testVariable() throws Exception {
|
||||||
|
doTestWithIfElseSurrounder("idea/testData/codeInsight/surroundWith/ifElse/variable.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user