Add surround with if
This commit is contained in:
@@ -29,6 +29,7 @@ import org.jetbrains.jet.jvm.compiler.*;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveDescriptorRendererTest;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveNamespaceComparingTest;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveTest;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.AbstractSurroundWithTest;
|
||||
import org.jetbrains.jet.plugin.highlighter.AbstractDeprecatedHighlightingTest;
|
||||
import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixMultiFileTest;
|
||||
import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixTest;
|
||||
@@ -221,6 +222,13 @@ public class GenerateTests {
|
||||
AbstractDeprecatedHighlightingTest.class,
|
||||
testModel("idea/testData/highlighter/deprecated")
|
||||
);
|
||||
|
||||
generateTest(
|
||||
"idea/tests/",
|
||||
"SurroundWithIfTestGenerated",
|
||||
AbstractSurroundWithTest.class,
|
||||
testModel("idea/testData/codeInsight/surroundWith/if", "doTestWithIfSurrounder")
|
||||
);
|
||||
}
|
||||
|
||||
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
|
||||
|
||||
@@ -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.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.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 String getTemplateDescription() {
|
||||
return CodeInsightBundle.message("surround.with.if.template");
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -26,7 +26,7 @@ import org.jetbrains.jet.plugin.refactoring.JetRefactoringUtil;
|
||||
public class KotlinStatementSurroundDescriptor implements SurroundDescriptor {
|
||||
|
||||
private static final Surrounder[] SURROUNDERS = {
|
||||
|
||||
new KotlinIfSurrounder()
|
||||
};
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.jetbrains.jet.plugin.codeInsight.surroundWith;
|
||||
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetBlockExpression;
|
||||
|
||||
public class KotlinSurrounderUtils {
|
||||
|
||||
public static void addStatementsInBlock(
|
||||
@NotNull JetBlockExpression block,
|
||||
@NotNull PsiElement[] statements
|
||||
) {
|
||||
PsiElement lBrace = block.getFirstChild();
|
||||
block.addRangeAfter(statements[0], statements[statements.length - 1], lBrace);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
val a: String? = null
|
||||
|
||||
<selection>if (a != null) {
|
||||
a.length()
|
||||
}</selection>
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun foo() {
|
||||
val a: String? = null
|
||||
|
||||
if (<caret>) {
|
||||
if (a != null) {
|
||||
a.length()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
<selection>"".capitalize()
|
||||
"".capitalize()</selection>
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo() {
|
||||
if (<caret>) {
|
||||
"".capitalize()
|
||||
"".capitalize()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
<selection>"".capitalize()</selection>
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
if (<caret>) {
|
||||
"".capitalize()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
"".capi<caret>talize()
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
if (<caret>) {
|
||||
"".capitalize()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
val a = ""
|
||||
|
||||
<caret>a
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
val a = ""
|
||||
|
||||
if (<caret>) {
|
||||
a
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.generation.surroundWith.SurroundWithHandler;
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase {
|
||||
|
||||
public void doTestWithIfSurrounder(String path) throws Exception {
|
||||
configureByFile(path);
|
||||
SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), new KotlinIfSurrounder());
|
||||
checkResultByFile(path + ".after");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
+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/if")
|
||||
public class SurroundWithIfTestGenerated 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);
|
||||
}
|
||||
|
||||
@TestMetadata("block.kt")
|
||||
public void testBlock() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/block.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("severalStatements.kt")
|
||||
public void testSeveralStatements() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/severalStatements.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("singleStatement.kt")
|
||||
public void testSingleStatement() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/singleStatement.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("singleStatementAtCaret.kt")
|
||||
public void testSingleStatementAtCaret() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/singleStatementAtCaret.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("variable.kt")
|
||||
public void testVariable() throws Exception {
|
||||
doTestWithIfSurrounder("idea/testData/codeInsight/surroundWith/if/variable.kt");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user