postfix template: introduce field: if configured, start inplace

This commit is contained in:
Anna.Kozlova
2016-10-14 09:57:28 +02:00
parent 614deb2e24
commit 30d5ae89d0
3 changed files with 14 additions and 7 deletions
@@ -35,7 +35,7 @@ public class IntroduceFieldPostfixTemplate extends PostfixTemplateWithExpression
protected void expandForChooseExpression(@NotNull PsiElement expression, @NotNull Editor editor) {
IntroduceFieldHandler handler =
ApplicationManager.getApplication().isUnitTestMode() ? getMockHandler(expression) : new IntroduceFieldHandler();
handler.invoke(expression.getProject(), new PsiElement[]{expression}, null);
handler.invoke(expression.getProject(), expression, editor);
}
@NotNull
@@ -27,6 +27,7 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.extractMethod.ExtractMethodHandler;
import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
/**
@@ -64,11 +65,15 @@ public abstract class IntroduceHandlerBase implements RefactoringActionHandler,
else {
editor = null;
}
if (tempExpr instanceof PsiExpression) {
invokeImpl(project, (PsiExpression)tempExpr, editor);
invoke(project, tempExpr, editor);
}
public void invoke(@NotNull Project project, PsiElement element, @Nullable Editor editor) {
if (element instanceof PsiExpression) {
invokeImpl(project, (PsiExpression)element, editor);
}
else if(tempExpr instanceof PsiLocalVariable) {
invokeImpl(project, (PsiLocalVariable)tempExpr, editor);
else if(element instanceof PsiLocalVariable) {
invokeImpl(project, (PsiLocalVariable)element, editor);
}
else {
LOG.error("elements[0] should be PsiExpression or PsiLocalVariable");
@@ -84,7 +84,7 @@ public class InplaceIntroduceFieldPopup extends AbstractInplaceIntroduceFieldPop
protected PsiField createFieldToStartTemplateOn(final String[] names,
final PsiType defaultType) {
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(myProject);
return ApplicationManager.getApplication().runWriteAction(new Computable<PsiField>() {
final PsiField field = ApplicationManager.getApplication().runWriteAction(new Computable<PsiField>() {
@Override
public PsiField compute() {
PsiField field = elementFactory.createField(chooseName(names, myParentClass.getLanguage()), defaultType);
@@ -97,10 +97,12 @@ public class InplaceIntroduceFieldPopup extends AbstractInplaceIntroduceFieldPop
if (visibility != null) {
PsiUtil.setModifierProperty(field, visibility, true);
}
myFieldRangeStart = myEditor.getDocument().createRangeMarker(field.getTextRange());
myFieldRangeStart = myEditor.getDocument().createRangeMarker(field.getTextRange());
return field;
}
});
PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myEditor.getDocument());
return field;
}
@Override