Add try-catch, try-finally, try-catch-finally surrounders

This commit is contained in:
Natalia.Ukhorskaya
2013-02-14 18:15:34 +04:00
parent ae6a2d3820
commit 7789062c7c
22 changed files with 462 additions and 3 deletions
@@ -264,6 +264,27 @@ public class GenerateTests {
AbstractSurroundWithTest.class,
testModel("idea/testData/codeInsight/surroundWith/when", "doTestWithWhenSurrounder")
);
generateTest(
"idea/tests/",
"SurroundWithTryCatchTestGenerated",
AbstractSurroundWithTest.class,
testModel("idea/testData/codeInsight/surroundWith/tryCatch", "doTestWithTryCatchSurrounder")
);
generateTest(
"idea/tests/",
"SurroundWithTryCatchFinallyTestGenerated",
AbstractSurroundWithTest.class,
testModel("idea/testData/codeInsight/surroundWith/tryCatchFinally", "doTestWithTryCatchFinallySurrounder")
);
generateTest(
"idea/tests/",
"SurroundWithTryFinallyTestGenerated",
AbstractSurroundWithTest.class,
testModel("idea/testData/codeInsight/surroundWith/tryFinally", "doTestWithTryFinallySurrounder")
);
}
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
@@ -27,7 +27,10 @@ public class KotlinStatementSurroundDescriptor implements SurroundDescriptor {
private static final Surrounder[] SURROUNDERS = {
new KotlinIfSurrounder(),
new KotlinIfElseSurrounder()
new KotlinIfElseSurrounder(),
new KotlinTryFinallySurrounder(),
new KotlinTryCatchFinallySurrounder(),
new KotlinTryCatchSurrounder()
};
@NotNull
@@ -0,0 +1,41 @@
/*
* 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.CodeInsightBundle;
import com.intellij.openapi.util.TextRange;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetTryExpression;
public class KotlinTryCatchFinallySurrounder extends KotlinTrySurrounderBase {
@Override
protected String getCodeTemplate() {
return "try { \n} catch(e: Exception) {\n} finally {\n}";
}
@NotNull
@Override
protected TextRange getTextRangeForCaret(@NotNull JetTryExpression expression) {
return getCatchTypeParameterTextRange(expression);
}
@Override
public String getTemplateDescription() {
return CodeInsightBundle.message("surround.with.try.catch.finally.template");
}
}
@@ -0,0 +1,41 @@
/*
* 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.CodeInsightBundle;
import com.intellij.openapi.util.TextRange;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetTryExpression;
public class KotlinTryCatchSurrounder extends KotlinTrySurrounderBase {
@Override
protected String getCodeTemplate() {
return "try { \n} catch(e: Exception) {\n}";
}
@NotNull
@Override
protected TextRange getTextRangeForCaret(@NotNull JetTryExpression expression) {
return getCatchTypeParameterTextRange(expression);
}
@Override
public String getTemplateDescription() {
return CodeInsightBundle.message("surround.with.try.catch.template");
}
}
@@ -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.CodeInsightBundle;
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.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetFinallySection;
import org.jetbrains.jet.lang.psi.JetTryExpression;
public class KotlinTryFinallySurrounder extends KotlinTrySurrounderBase {
@Override
protected String getCodeTemplate() {
return "try { \n} finally {\nb\n}";
}
@Nullable
@Override
protected TextRange surroundStatements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement container, @NotNull PsiElement[] statements) {
TextRange textRange = super.surroundStatements(project, editor, container, statements);
assert textRange != null;
// Delete dummy "b" element for caret
editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset());
return new TextRange(textRange.getStartOffset(), textRange.getStartOffset());
}
@NotNull
@Override
protected TextRange getTextRangeForCaret(@NotNull JetTryExpression expression) {
JetFinallySection block = expression.getFinallyBlock();
assert block != null : "Finally block should exists for " + expression.getText();
JetElement blockExpression = block.getFinalExpression().getStatements().get(0);
return blockExpression.getTextRange();
}
@Override
public String getTemplateDescription() {
return CodeInsightBundle.message("surround.with.try.finally.template");
}
}
@@ -0,0 +1,66 @@
/*
* 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.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.plugin.codeInsight.surroundWith.KotlinSurrounderUtils;
public abstract class KotlinTrySurrounderBase extends KotlinStatementsSurrounder {
@Nullable
@Override
protected TextRange surroundStatements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement container, @NotNull PsiElement[] statements) {
// TODO extract variables declaration
JetTryExpression tryExpression = (JetTryExpression) JetPsiFactory.createExpression(project, getCodeTemplate());
tryExpression = (JetTryExpression) container.addAfter(tryExpression, statements[statements.length - 1]);
// TODO move a comment for first statement
final JetBlockExpression tryBlock = tryExpression.getTryBlock();
// Add statements in try block of created try - catch - finally
KotlinSurrounderUtils.addStatementsInBlock(tryBlock, statements);
// Delete statements from original code
container.deleteChildRange(statements[0], statements[statements.length - 1]);
tryExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(tryExpression);
return getTextRangeForCaret(tryExpression);
}
protected abstract String getCodeTemplate();
@NotNull
protected abstract TextRange getTextRangeForCaret(@NotNull JetTryExpression expression);
protected static TextRange getCatchTypeParameterTextRange(@NotNull JetTryExpression expression) {
JetParameter parameter = expression.getCatchClauses().get(0).getCatchParameter();
assert parameter != null : "Catch parameter should exists for " + expression.getText();
JetElement typeReference = parameter.getTypeReference();
assert typeReference != null : "Type reference for parameter should exists for " + expression.getText();
return typeReference.getTextRange();
}
}
@@ -0,0 +1,4 @@
fun foo() {
<selection>"aaa"
"aaa"</selection>
}
@@ -0,0 +1,7 @@
fun foo() {
try {
"aaa"
"aaa"
} catch(e: <selection>Exception</selection>) {
}
}
@@ -0,0 +1,3 @@
fun foo() {
<selection>"aaa"</selection>
}
@@ -0,0 +1,6 @@
fun foo() {
try {
"aaa"
} catch(e: <selection>Exception</selection>) {
}
}
@@ -0,0 +1,4 @@
fun foo() {
<selection>"aaa"
"aaa"</selection>
}
@@ -0,0 +1,8 @@
fun foo() {
try {
"aaa"
"aaa"
} catch(e: <selection>Exception</selection>) {
} finally {
}
}
@@ -0,0 +1,3 @@
fun foo() {
<selection>"aaa"</selection>
}
@@ -0,0 +1,7 @@
fun foo() {
try {
"aaa"
} catch(e: <selection>Exception</selection>) {
} finally {
}
}
@@ -0,0 +1,4 @@
fun foo() {
<selection>"aaa"
"aaa"</selection>
}
@@ -0,0 +1,8 @@
fun foo() {
try {
"aaa"
"aaa"
} finally {
<caret>
}
}
@@ -0,0 +1,3 @@
fun foo() {
<selection>"aaa"</selection>
}
@@ -0,0 +1,7 @@
fun foo() {
try {
"aaa"
} finally {
<caret>
}
}
@@ -23,8 +23,8 @@ 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;
import org.jetbrains.jet.plugin.codeInsight.surroundWith.expression.KotlinWhenSurrounder;
import org.jetbrains.jet.plugin.codeInsight.surroundWith.statement.*;
public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase {
@@ -47,6 +47,22 @@ public abstract class AbstractSurroundWithTest extends LightCodeInsightTestCase
public void doTestWithStringTemplateSurrounder(String path) throws Exception {
doTest(path, new KotlinStringTemplateSurrounder());
}
public void doTestWithWhenSurrounder(String path) throws Exception {
doTest(path, new KotlinWhenSurrounder());
}
public void doTestWithTryCatchSurrounder(String path) throws Exception {
doTest(path, new KotlinTryCatchSurrounder());
}
public void doTestWithTryCatchFinallySurrounder(String path) throws Exception {
doTest(path, new KotlinTryCatchFinallySurrounder());
}
public void doTestWithTryFinallySurrounder(String path) throws Exception {
doTest(path, new KotlinTryFinallySurrounder());
}
private void doTest(String path, Surrounder surrounder) throws Exception{
configureByFile(path);
@@ -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/tryCatchFinally")
public class SurroundWithTryCatchFinallyTestGenerated extends AbstractSurroundWithTest {
public void testAllFilesPresentInTryCatchFinally() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/tryCatchFinally"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("multiExpression.kt")
public void testMultiExpression() throws Exception {
doTestWithTryCatchFinallySurrounder("idea/testData/codeInsight/surroundWith/tryCatchFinally/multiExpression.kt");
}
@TestMetadata("singleExpression.kt")
public void testSingleExpression() throws Exception {
doTestWithTryCatchFinallySurrounder("idea/testData/codeInsight/surroundWith/tryCatchFinally/singleExpression.kt");
}
}
@@ -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/tryCatch")
public class SurroundWithTryCatchTestGenerated extends AbstractSurroundWithTest {
public void testAllFilesPresentInTryCatch() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/tryCatch"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("multiExpression.kt")
public void testMultiExpression() throws Exception {
doTestWithTryCatchSurrounder("idea/testData/codeInsight/surroundWith/tryCatch/multiExpression.kt");
}
@TestMetadata("singleExpression.kt")
public void testSingleExpression() throws Exception {
doTestWithTryCatchSurrounder("idea/testData/codeInsight/surroundWith/tryCatch/singleExpression.kt");
}
}
@@ -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/tryFinally")
public class SurroundWithTryFinallyTestGenerated extends AbstractSurroundWithTest {
public void testAllFilesPresentInTryFinally() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/surroundWith/tryFinally"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("multiExpression.kt")
public void testMultiExpression() throws Exception {
doTestWithTryFinallySurrounder("idea/testData/codeInsight/surroundWith/tryFinally/multiExpression.kt");
}
@TestMetadata("singleExpression.kt")
public void testSingleExpression() throws Exception {
doTestWithTryFinallySurrounder("idea/testData/codeInsight/surroundWith/tryFinally/singleExpression.kt");
}
}