Add parentheses surrounder

This commit is contained in:
Natalia.Ukhorskaya
2013-02-14 13:37:20 +04:00
parent eeb158a5c8
commit 856bffb324
11 changed files with 130 additions and 1 deletions
@@ -243,6 +243,13 @@ public class GenerateTests {
AbstractSurroundWithTest.class,
testModel("idea/testData/codeInsight/surroundWith/not", "doTestWithNotSurrounder")
);
generateTest(
"idea/tests/",
"SurroundWithParenthesesTestGenerated",
AbstractSurroundWithTest.class,
testModel("idea/testData/codeInsight/surroundWith/parentheses", "doTestWithParenthesesSurrounder")
);
}
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
@@ -27,7 +27,8 @@ import org.jetbrains.jet.plugin.codeInsight.CodeInsightUtils;
public class KotlinExpressionSurroundDescriptor implements SurroundDescriptor {
private static final Surrounder[] SURROUNDERS = {
new KotlinNotSurrounder()
new KotlinNotSurrounder(),
new KotlinParenthesesSurrounder()
};
@NotNull
@@ -0,0 +1,40 @@
package org.jetbrains.jet.plugin.codeInsight.surroundWith.expression;
import com.intellij.codeInsight.CodeInsightBundle;
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.JetExpression;
import org.jetbrains.jet.lang.psi.JetParenthesizedExpression;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
public class KotlinParenthesesSurrounder extends KotlinExpressionSurrounder {
@Override
public String getTemplateDescription() {
return CodeInsightBundle.message("surround.with.parenthesis.template");
}
@Override
public boolean isApplicable(@NotNull JetExpression expression) {
return true;
}
@Nullable
@Override
public TextRange surroundExpression( @NotNull Project project, @NotNull Editor editor, @NotNull JetExpression expression) {
JetParenthesizedExpression parenthesizedExpression = (JetParenthesizedExpression)JetPsiFactory.createExpression(project, "(a)");
JetExpression expressionWithoutParentheses = parenthesizedExpression.getExpression();
assert expressionWithoutParentheses != null : "JetExpression should exists for " + parenthesizedExpression.getText() + " expression";
expressionWithoutParentheses.replace(expression);
expression = (JetExpression) expression.replace(parenthesizedExpression);
CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression);
int offset = expression.getTextRange().getEndOffset();
return new TextRange(offset, offset);
}
}
@@ -0,0 +1,3 @@
fun foo() {
<caret>true
}
@@ -0,0 +1,3 @@
fun foo() {
(true)<caret>
}
@@ -0,0 +1,5 @@
fun foo() {
val a = "test"
if (<selection>a == "t"</selection>) {}
}
@@ -0,0 +1,5 @@
fun foo() {
val a = "test"
if ((a == "t")<caret>) {}
}
@@ -0,0 +1,3 @@
fun foo() {
val a = <selection>1 + 2</selection>
}
@@ -0,0 +1,3 @@
fun foo() {
val a = (1 + 2)<caret>
}
@@ -21,6 +21,7 @@ import com.intellij.lang.surroundWith.Surrounder;
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.statement.KotlinIfElseSurrounder;
import org.jetbrains.jet.plugin.codeInsight.surroundWith.statement.KotlinIfSurrounder;
@@ -38,6 +39,10 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase
doTest(path, new KotlinNotSurrounder());
}
public void doTestWithParenthesesSurrounder(String path) throws Exception {
doTest(path, new KotlinParenthesesSurrounder());
}
private void doTest(String path, Surrounder surrounder) throws Exception{
configureByFile(path);
SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder);
@@ -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/parentheses")
public class SurroundWithParenthesesTestGenerated extends AbstractSurroundWithTest {
public void testAllFilesPresentInParentheses() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/parentheses"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("expr.kt")
public void testExpr() throws Exception {
doTestWithParenthesesSurrounder("idea/testData/codeInsight/surroundWith/parentheses/expr.kt");
}
@TestMetadata("inIf.kt")
public void testInIf() throws Exception {
doTestWithParenthesesSurrounder("idea/testData/codeInsight/surroundWith/parentheses/inIf.kt");
}
@TestMetadata("partOfExpr.kt")
public void testPartOfExpr() throws Exception {
doTestWithParenthesesSurrounder("idea/testData/codeInsight/surroundWith/parentheses/partOfExpr.kt");
}
}