Delete dummy semicolon after postfix template expansion

This commit is contained in:
Alexander Zolotov
2014-05-07 22:37:31 +04:00
parent 75264a6a80
commit f7abe52af5
9 changed files with 82 additions and 3 deletions
@@ -102,6 +102,27 @@ public class JavaPostfixTemplateProvider implements PostfixTemplateProvider {
}
}
@Override
public void afterExpand(@NotNull final PsiFile file, @NotNull final Editor editor) {
final SmartPsiElementPointer<PsiElement> pointer = file.getUserData(ADDED_SEMICOLON);
if (pointer != null) {
final PsiElement addedSemicolon = pointer.getElement();
file.putUserData(ADDED_SEMICOLON, null);
if (addedSemicolon != null && addedSemicolon.isValid()) {
CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
addedSemicolon.delete();
}
});
}
});
}
}
}
@NotNull
@Override
public PsiFile preCheck(final @NotNull PsiFile copyFile, final @NotNull Editor realEditor, final int currentOffset) {
@@ -0,0 +1,5 @@
public class Foo {
void m(Object o) {
"test".cast<caret>.length()
}
}
@@ -0,0 +1,5 @@
public class Foo {
void m(Object o) {
(() "test")<caret>.length()
}
}
@@ -0,0 +1,7 @@
public class Foo {
void m() {
int[] xs = {1, 2, 3};
xs.for<caret>
xs = new int[0];
}
}
@@ -0,0 +1,9 @@
public class Foo {
void m() {
int[] xs = {1, 2, 3};
for (int x : xs) {
<caret>
}
xs = new int[0];
}
}
@@ -26,4 +26,8 @@ public class CastPostfixTemplateTest extends PostfixTemplateTestCase {
public void testVoidExpression() { doTest(); }
public void testSingleArgument() { doTest(); }
public void testInsideString() { doTest(); }
public void testChainCall() {
doTest();
}
}
@@ -1,9 +1,26 @@
/*
* 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 ForeachTemplateTest extends PostfixTemplateTestCase {
public void testInts() { doTest(); }
public void testBeforeAssignment() { doTest(); }
@NotNull
@Override
@@ -126,9 +126,14 @@ public class PostfixLiveTemplate extends CustomLiveTemplateBase {
final PsiFile file = callback.getContext().getContainingFile();
if (isApplicableTemplate(provider, key, file, editor)) {
int offset = deleteTemplateKey(file, editor, key);
provider.preExpand(file, editor);
PsiElement context = CustomTemplateCallback.getContext(file, positiveOffset(offset));
expandTemplate(postfixTemplate, editor, context);
try {
provider.preExpand(file, editor);
PsiElement context = CustomTemplateCallback.getContext(file, positiveOffset(offset));
expandTemplate(postfixTemplate, editor, context);
}
finally {
provider.afterExpand(file, editor);
}
}
// don't care about errors in multiCaret mode
else if (editor.getCaretModel().getAllCarets().size() == 1) {
@@ -46,6 +46,12 @@ public interface PostfixTemplateProvider {
*/
void preExpand(@NotNull PsiFile file, @NotNull Editor editor);
/**
* Invoked after template finished (doesn't matter if it finished successfully or not).
* E.g. java postfix template use this method for deleting inserted semicolon.
*/
void afterExpand(@NotNull PsiFile file, @NotNull Editor editor);
/**
* Prepare file for checking availability of templates.
* Almost the same as {@link this#preExpand(com.intellij.psi.PsiFile, com.intellij.openapi.editor.Editor)} with several differences: