Add "try" live postfix template

This commit is contained in:
Andrey Starovoyt
2014-04-16 14:45:35 +04:00
parent 5686958fe6
commit fffcd492d5
11 changed files with 163 additions and 0 deletions
@@ -0,0 +1,66 @@
/*
* Copyright 2000-2014 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 com.intellij.codeInsight.template.postfix.templates;
import com.intellij.codeInsight.generation.surroundWith.JavaWithTryCatchSurrounder;
import com.intellij.codeInsight.template.postfix.util.PostfixTemplatesUtils;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
public class TryStatementPostfixTemplate extends PostfixTemplate {
protected TryStatementPostfixTemplate() {
super("try", "Insert statement in try-catch block", "try { exp } catch (Exception e) { e.printStackTrace(); }");
}
@Override
public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) {
return null != PsiTreeUtil.getNonStrictParentOfType(context, PsiStatement.class);
}
@Override
public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
PsiStatement statement = PsiTreeUtil.getNonStrictParentOfType(context, PsiStatement.class);
assert statement != null;
PsiFile file = statement.getContainingFile();
Project project = context.getProject();
JavaWithTryCatchSurrounder surrounder = new JavaWithTryCatchSurrounder();
TextRange range = surrounder.surroundElements(project, editor, new PsiElement[]{statement});
if (range == null) {
PostfixTemplatesUtils.showErrorHint(project, editor);
return;
}
PsiElement element = file.findElementAt(range.getStartOffset());
PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(element, PsiTryStatement.class);
assert tryStatement != null;
PsiCodeBlock block = tryStatement.getTryBlock();
assert block != null;
PsiStatement statementInTry = ArrayUtil.getFirstElement(block.getStatements());
if (null != statementInTry) {
editor.getCaretModel().moveToOffset(statementInTry.getTextRange().getEndOffset());
}
}
}
@@ -0,0 +1,7 @@
public class Foo {
void m() {
somevalue.try<caret>
int i=0;
}
}
@@ -0,0 +1,11 @@
public class Foo {
void m() {
try {
somevalue<caret>
} catch (Exception e) {
e.printStackTrace();
}
int i=0;
}
}
@@ -0,0 +1,3 @@
public class Foo.try<caret> {
}
@@ -0,0 +1,3 @@
public class Foo.try <caret> {
}
@@ -0,0 +1,5 @@
public class Foo {
void m() {
somevalue.try<caret>
}
}
@@ -0,0 +1,9 @@
public class Foo {
void m() {
try {
somevalue<caret>
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,5 @@
public class Foo {
void m() {
Object o = new Object().try<caret>
}
}
@@ -0,0 +1,9 @@
public class Foo {
void m() {
try {
Object o = new Object()<caret>
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,44 @@
/*
* Copyright 2000-2014 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 com.intellij.codeInsight.template.postfix.templates;
import org.jetbrains.annotations.NotNull;
public class TryPostfixTemplateTest extends PostfixTemplateTestCase {
@NotNull
@Override
protected String getSuffix() {
return "try";
}
public void testSimple() {
doTest();
}
public void testStatement() {
doTest();
}
public void testMultiStatement() {
doTest();
}
public void testNotStatement() {
doTest();
}
}
@@ -25,6 +25,7 @@
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.InstanceofExpressionPostfixTemplate"/>
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.SoutPostfixTemplate"/>
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.FormatPostfixTemplate"/>
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.TryStatementPostfixTemplate"/>
</extensions>
<extensions defaultExtensionNs="com.intellij">