Postfix: enabled new-expression template

This commit is contained in:
Alexander Zolotov
2013-12-24 15:26:06 +04:00
parent 7af3bf2746
commit 4d61bc17b9
4 changed files with 36 additions and 14 deletions
@@ -1,8 +1,26 @@
/*
* Copyright 2000-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 com.intellij.codeInsight.template.postfix.templates;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.ChildRole;
import com.intellij.psi.impl.source.tree.CompositeElement;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -45,7 +63,14 @@ public class NewExpressionPostfixTemplate extends PostfixTemplate {
PsiElementFactory factory = JavaPsiFacade.getInstance(context.getProject()).getElementFactory();
PsiNewExpression newExpression = (PsiNewExpression)factory.createExpressionFromText(template, context);
PsiElement replace = ((PsiJavaReference)ref).getElement().replace(newExpression);
replace.getNode().addLeaf(JavaTokenType.SEMICOLON, ";", null);
PsiStatement statement = PsiTreeUtil.getNonStrictParentOfType(replace, PsiStatement.class);
ASTNode existingSemicolon = statement instanceof CompositeElement
? ((CompositeElement)statement).findChildByRole(ChildRole.CLOSING_SEMICOLON)
: null;
if (statement == null || existingSemicolon == null) {
replace.getNode().addLeaf(JavaTokenType.SEMICOLON, ";", null);
}
int offset = calculateOffset((PsiNewExpression)replace, accessibility, typeRequiresRefinement);
editor.getCaretModel().moveToOffset(offset);
}
@@ -1,7 +1,7 @@
public class Foo {
public Foo() { }
void m() {
new Foo();<caret>
new Foo()<caret>;
_a = new Bar();
}
}
@@ -17,20 +17,17 @@ package com.intellij.codeInsight.template.postfix.templates;
import org.jetbrains.annotations.NotNull;
/**
* @author ignatov
*/
public class NewPostfixTemplateTest extends PostfixTemplateTestCase {
@NotNull
@Override
protected String getSuffix() { return "new"; }
public void _testNew01() { doTest(); }
public void _testNew02() { doTest(); }
public void _testNew03() { doTest(); }
public void _testNew04() { doTest(); }
public void _testNew05() { doTest(); }
public void _testNew06() { doTest(); }
public void _testNew07() { doTest(); }
public void _testNew08() { doTest(); }
public void testNew01() { doTest(); }
public void testNew02() { doTest(); }
public void testNew03() { doTest(); }
public void testNew04() { doTest(); }
public void testNew05() { doTest(); }
public void testNew06() { doTest(); }
public void testNew07() { doTest(); }
public void testNew08() { doTest(); }
}
+1 -1
View File
@@ -12,7 +12,7 @@
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.ForeachPostfixTemplate"/>
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.WhileStatementPostfixTemplate"/>
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.CastExpressionPostfixTemplate"/>
<!--<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.NewExpressionPostfixTemplate"/>-->
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.NewExpressionPostfixTemplate"/>
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.IfStatementPostfixTemplate"/>
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.ForAscendingPostfixTemplate"/>
<postfixTemplate implementation="com.intellij.codeInsight.template.postfix.templates.ForDescendingPostfixTemplate"/>