Add when surrounder
This commit is contained in:
@@ -257,6 +257,13 @@ public class GenerateTests {
|
||||
AbstractSurroundWithTest.class,
|
||||
testModel("idea/testData/codeInsight/surroundWith/stringTemplate", "doTestWithStringTemplateSurrounder")
|
||||
);
|
||||
|
||||
generateTest(
|
||||
"idea/tests/",
|
||||
"SurroundWithWhenTestGenerated",
|
||||
AbstractSurroundWithTest.class,
|
||||
testModel("idea/testData/codeInsight/surroundWith/when", "doTestWithWhenSurrounder")
|
||||
);
|
||||
}
|
||||
|
||||
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
|
||||
|
||||
@@ -117,3 +117,4 @@ change.to.property.name.family.name=Change to property name
|
||||
change.to.property.name.action=Change ''{0}'' to ''{1}''
|
||||
|
||||
surround.with.string.template="${expr}"
|
||||
surround.with.when.template=when (expr) {}
|
||||
|
||||
+2
-1
@@ -29,7 +29,8 @@ public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor {
|
||||
private static final Surrounder[] SURROUNDERS = {
|
||||
new KotlinNotSurrounder(),
|
||||
new KotlinStringTemplateSurrounder(),
|
||||
new KotlinParenthesesSurrounder()
|
||||
new KotlinParenthesesSurrounder(),
|
||||
new KotlinWhenSurrounder()
|
||||
};
|
||||
|
||||
@NotNull
|
||||
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassKind;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
import org.jetbrains.jet.plugin.codeInsight.surroundWith.KotlinSurrounderUtils;
|
||||
|
||||
public class KotlinWhenSurrounder extends KotlinExpressionSurrounder {
|
||||
@Override
|
||||
public String getTemplateDescription() {
|
||||
return JetBundle.message("surround.with.when.template");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(@NotNull JetExpression expression) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull JetExpression expression) {
|
||||
JetWhenExpression whenExpression = (JetWhenExpression)JetPsiFactory.createExpression(project, getCodeTemplate(expression));
|
||||
JetExpression subjectExpression = whenExpression.getSubjectExpression();
|
||||
assert subjectExpression != null : "JetExpression should exists for " + whenExpression.getText() + " expression";
|
||||
subjectExpression.replace(expression);
|
||||
|
||||
expression = (JetExpression) expression.replace(whenExpression);
|
||||
|
||||
CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression);
|
||||
|
||||
JetWhenEntry whenEntry = ((JetWhenExpression) expression).getEntries().get(0);
|
||||
JetWhenCondition whenEntryCondition = whenEntry.getConditions()[0];
|
||||
assert whenEntryCondition != null : "JetExpression for first entry should exists: " + expression.getText();
|
||||
TextRange whenRange = whenEntryCondition.getTextRange();
|
||||
editor.getDocument().deleteString(whenRange.getStartOffset(), whenRange.getEndOffset());
|
||||
int offset = whenRange.getStartOffset();
|
||||
return new TextRange(offset, offset);
|
||||
}
|
||||
|
||||
private String getCodeTemplate(JetExpression expression) {
|
||||
JetType type = KotlinSurrounderUtils.getExpressionType(expression);
|
||||
if (type != null) {
|
||||
ClassifierDescriptor descriptor = type.getConstructor().getDeclarationDescriptor();
|
||||
if (descriptor instanceof ClassDescriptor && ((ClassDescriptor) descriptor).getKind() == ClassKind.ENUM_CLASS) {
|
||||
return "when(a) { \nb -> {}\n}";
|
||||
}
|
||||
}
|
||||
return "when(a) { \nb -> {}\n else -> {}\n}";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
enum class MyEnum {
|
||||
FIRST
|
||||
SECOND
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
val a = MyEnum.FIRST
|
||||
<selection>a</selection>
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
enum class MyEnum {
|
||||
FIRST
|
||||
SECOND
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
val a = MyEnum.FIRST
|
||||
when(a) {
|
||||
<caret> -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
val a = "aa"
|
||||
<selection>a.capitalize()</selection>
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun foo() {
|
||||
val a = "aa"
|
||||
when(a.capitalize()) {
|
||||
<caret> -> {
|
||||
}
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
val a = "aa"
|
||||
<selection>a</selection>
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun foo() {
|
||||
val a = "aa"
|
||||
when(a) {
|
||||
<caret> -> {
|
||||
}
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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/when")
|
||||
public class SurroundWithWhenTestGenerated extends AbstractSurroundWithTest {
|
||||
public void testAllFilesPresentInWhen() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/when"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("enum.kt")
|
||||
public void testEnum() throws Exception {
|
||||
doTestWithWhenSurrounder("idea/testData/codeInsight/surroundWith/when/enum.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("multiExpression.kt")
|
||||
public void testMultiExpression() throws Exception {
|
||||
doTestWithWhenSurrounder("idea/testData/codeInsight/surroundWith/when/multiExpression.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("singleExpression.kt")
|
||||
public void testSingleExpression() throws Exception {
|
||||
doTestWithWhenSurrounder("idea/testData/codeInsight/surroundWith/when/singleExpression.kt");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user