Add string template surrounder
This commit is contained in:
@@ -250,6 +250,13 @@ public class GenerateTests {
|
||||
AbstractSurroundWithTest.class,
|
||||
testModel("idea/testData/codeInsight/surroundWith/parentheses", "doTestWithParenthesesSurrounder")
|
||||
);
|
||||
|
||||
generateTest(
|
||||
"idea/tests/",
|
||||
"SurroundWithStringTemplateTestGenerated",
|
||||
AbstractSurroundWithTest.class,
|
||||
testModel("idea/testData/codeInsight/surroundWith/stringTemplate", "doTestWithStringTemplateSurrounder")
|
||||
);
|
||||
}
|
||||
|
||||
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
|
||||
|
||||
@@ -115,3 +115,5 @@ move.when.else.branch.to.the.end.action=Move else branch to the end
|
||||
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.string.template="${expr}"
|
||||
|
||||
+1
@@ -28,6 +28,7 @@ public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor {
|
||||
|
||||
private static final Surrounder[] SURROUNDERS = {
|
||||
new KotlinNotSurrounder(),
|
||||
new KotlinStringTemplateSurrounder(),
|
||||
new KotlinParenthesesSurrounder()
|
||||
};
|
||||
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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.expression;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
|
||||
public class KotlinStringTemplateSurrounder extends KotlinExpressionSurrounder {
|
||||
@Override
|
||||
public String getTemplateDescription() {
|
||||
return JetBundle.message("surround.with.string.template");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(@NotNull JetExpression expression) {
|
||||
return !(expression instanceof JetStringTemplateExpression);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull JetExpression expression) {
|
||||
JetStringTemplateExpression stringTemplateExpression = (JetStringTemplateExpression)JetPsiFactory.createExpression(project, getCodeTemplate(expression));
|
||||
JetStringTemplateEntry templateEntry = stringTemplateExpression.getEntries()[0];
|
||||
JetExpression innerExpression = templateEntry.getExpression();
|
||||
assert innerExpression != null : "JetExpression should exists for " + stringTemplateExpression.toString();
|
||||
innerExpression.replace(expression);
|
||||
|
||||
expression = (JetExpression) expression.replace(stringTemplateExpression);
|
||||
|
||||
CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression);
|
||||
|
||||
int offset = expression.getTextRange().getEndOffset();
|
||||
return new TextRange(offset, offset);
|
||||
}
|
||||
|
||||
private String getCodeTemplate(JetExpression expression) {
|
||||
if (expression.getChildren().length > 0 ||
|
||||
expression instanceof JetConstantExpression) {
|
||||
return "\"${a}\"";
|
||||
}
|
||||
return "\"$a\"";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo() {
|
||||
val a = 1
|
||||
val b = 2
|
||||
|
||||
<selection>a + b</selection>
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo() {
|
||||
val a = 1
|
||||
val b = 2
|
||||
|
||||
"${a + b}"<caret>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
val a = <selection>1 * 3</selection>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
val a = "${1 * 3}"<caret>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
val a = <selection>1</selection>
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
val a = "${1}"<caret>
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
val a = "ddd"
|
||||
<selection>a</selection>
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
val a = "ddd"
|
||||
"$a"<caret>
|
||||
}
|
||||
+5
@@ -22,6 +22,7 @@ import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.expression.KotlinNotSurrounder;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.expression.KotlinParenthesesSurrounder;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.expression.KotlinStringTemplateSurrounder;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.statement.KotlinIfElseSurrounder;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.statement.KotlinIfSurrounder;
|
||||
|
||||
@@ -43,6 +44,10 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase
|
||||
doTest(path, new KotlinParenthesesSurrounder());
|
||||
}
|
||||
|
||||
public void doTestWithStringTemplateSurrounder(String path) throws Exception {
|
||||
doTest(path, new KotlinStringTemplateSurrounder());
|
||||
}
|
||||
|
||||
private void doTest(String path, Surrounder surrounder) throws Exception{
|
||||
configureByFile(path);
|
||||
SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder);
|
||||
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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/stringTemplate")
|
||||
public class SurroundWithStringTemplateTestGenerated extends AbstractSurroundWithTest {
|
||||
public void testAllFilesPresentInStringTemplate() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/stringTemplate"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("multiExpression.kt")
|
||||
public void testMultiExpression() throws Exception {
|
||||
doTestWithStringTemplateSurrounder("idea/testData/codeInsight/surroundWith/stringTemplate/multiExpression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("multiExpressionConstant.kt")
|
||||
public void testMultiExpressionConstant() throws Exception {
|
||||
doTestWithStringTemplateSurrounder("idea/testData/codeInsight/surroundWith/stringTemplate/multiExpressionConstant.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("singleConstant.kt")
|
||||
public void testSingleConstant() throws Exception {
|
||||
doTestWithStringTemplateSurrounder("idea/testData/codeInsight/surroundWith/stringTemplate/singleConstant.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("singleExpression.kt")
|
||||
public void testSingleExpression() throws Exception {
|
||||
doTestWithStringTemplateSurrounder("idea/testData/codeInsight/surroundWith/stringTemplate/singleExpression.kt");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user