Add surround with function literal
This commit is contained in:
@@ -285,6 +285,13 @@ public class GenerateTests {
|
||||
AbstractSurroundWithTest.class,
|
||||
testModel("idea/testData/codeInsight/surroundWith/tryFinally", "doTestWithTryFinallySurrounder")
|
||||
);
|
||||
|
||||
generateTest(
|
||||
"idea/tests/",
|
||||
"SurroundWithFunctionLiteralTestGenerated",
|
||||
AbstractSurroundWithTest.class,
|
||||
testModel("idea/testData/codeInsight/surroundWith/functionLiteral", "doTestWithFunctionLiteralSurrounder")
|
||||
);
|
||||
}
|
||||
|
||||
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
|
||||
|
||||
@@ -118,3 +118,4 @@ change.to.property.name.action=Change ''{0}'' to ''{1}''
|
||||
|
||||
surround.with.string.template="${expr}"
|
||||
surround.with.when.template=when (expr) {}
|
||||
surround.with.function.template={ }
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.statement;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiElement;
|
||||
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;
|
||||
|
||||
public class KotlinFunctionLiteralSurrounder extends KotlinStatementsSurrounder {
|
||||
@Nullable
|
||||
@Override
|
||||
protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) {
|
||||
// TODO extract variables declaration
|
||||
|
||||
JetCallExpression callExpression = (JetCallExpression) JetPsiFactory.createExpression(project, "run {\n}");
|
||||
callExpression = (JetCallExpression) container.addAfter(callExpression, statements[statements.length - 1]);
|
||||
container.addBefore(JetPsiFactory.createWhiteSpace(project), callExpression);
|
||||
|
||||
JetFunctionLiteralExpression bodyExpression = (JetFunctionLiteralExpression) callExpression.getFunctionLiteralArguments().get(0);
|
||||
assert bodyExpression != null : "Body expression should exists for " + callExpression.getText();
|
||||
JetBlockExpression blockExpression = bodyExpression.getBodyExpression();
|
||||
assert blockExpression != null : "JetBlockExpression should exists for " + callExpression.getText();
|
||||
//Add statements in function literal block
|
||||
KotlinSurrounderUtils.addStatementsInBlock(blockExpression, statements);
|
||||
|
||||
//Delete statements from original code
|
||||
container.deleteChildRange(statements[0], statements[statements.length - 1]);
|
||||
|
||||
callExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(callExpression);
|
||||
|
||||
JetExpression literalName = callExpression.getCalleeExpression();
|
||||
assert literalName != null : "Run expression should have callee expression " + callExpression.getText();
|
||||
return literalName.getTextRange();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTemplateDescription() {
|
||||
return JetBundle.message("surround.with.function.template");
|
||||
}
|
||||
}
|
||||
+1
@@ -28,6 +28,7 @@ public class KotlinStatementSurroundDescriptor implements SurroundDescriptor {
|
||||
private static final Surrounder[] SURROUNDERS = {
|
||||
new KotlinIfSurrounder(),
|
||||
new KotlinIfElseSurrounder(),
|
||||
new KotlinFunctionLiteralSurrounder(),
|
||||
new KotlinTryFinallySurrounder(),
|
||||
new KotlinTryCatchFinallySurrounder(),
|
||||
new KotlinTryCatchSurrounder()
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
<selection>"aaa"
|
||||
"aaa"</selection>
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo() {
|
||||
<selection>run</selection> {
|
||||
"aaa"
|
||||
"aaa"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
<caret>"aaa"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
<selection>run</selection> {
|
||||
"aaa"
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -64,7 +64,11 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase
|
||||
doTest(path, new KotlinTryFinallySurrounder());
|
||||
}
|
||||
|
||||
private void doTest(String path, Surrounder surrounder) throws Exception{
|
||||
public void doTestWithFunctionLiteralSurrounder(String path) throws Exception {
|
||||
doTest(path, new KotlinFunctionLiteralSurrounder());
|
||||
}
|
||||
|
||||
private void doTest(String path, Surrounder surrounder) throws Exception {
|
||||
configureByFile(path);
|
||||
SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder);
|
||||
checkResultByFile(path + ".after");
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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/functionLiteral")
|
||||
public class SurroundWithFunctionLiteralTestGenerated extends AbstractSurroundWithTest {
|
||||
public void testAllFilesPresentInFunctionLiteral() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/functionLiteral"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("multiStatement.kt")
|
||||
public void testMultiStatement() throws Exception {
|
||||
doTestWithFunctionLiteralSurrounder("idea/testData/codeInsight/surroundWith/functionLiteral/multiStatement.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("singleStatement.kt")
|
||||
public void testSingleStatement() throws Exception {
|
||||
doTestWithFunctionLiteralSurrounder("idea/testData/codeInsight/surroundWith/functionLiteral/singleStatement.kt");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user