diff --git a/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceConstantPopup.java b/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceConstantPopup.java index e207ed7aef03..00297ce6e6db 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceConstantPopup.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceConstantPopup.java @@ -300,11 +300,12 @@ public class InplaceIntroduceConstantPopup { }); } - public void performInplaceIntroduce() { - startIntroduceTemplate(false, null); + public boolean performInplaceIntroduce() { + return startIntroduceTemplate(false, null); } - private void startIntroduceTemplate(final boolean replaceAllOccurrences, @Nullable final PsiType fieldDefaultType) { + private boolean startIntroduceTemplate(final boolean replaceAllOccurrences, @Nullable final PsiType fieldDefaultType) { + final Ref result = new Ref(); CommandProcessor.getInstance().executeCommand(myProject, new Runnable() { public void run() { myTypeSelectorManager.setAllOccurences(replaceAllOccurrences); @@ -325,6 +326,7 @@ public class InplaceIntroduceConstantPopup { IntroduceConstantDialog.createNameSuggestionGenerator(propName, myExpr, JavaCodeStyleManager.getInstance(myProject)) .getSuggestedNameInfo(defaultType).names; final PsiField field = createFieldToStartTemplateOn(names, defaultType); + boolean started = false; if (field != null) { myEditor.getCaretModel().moveToOffset(field.getTextOffset()); myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); @@ -332,10 +334,20 @@ public class InplaceIntroduceConstantPopup { nameSuggestions.add(field.getName()); nameSuggestions.addAll(Arrays.asList(names)); final VariableInplaceRenamer renamer = new FieldInplaceIntroducer(field); - renamer.performInplaceRename(false, nameSuggestions); + started = renamer.performInplaceRename(false, nameSuggestions); + } + result.set(started); + if (!started && field != null) { + ApplicationManager.getApplication().runWriteAction(new Runnable() { + @Override + public void run() { + field.delete(); + } + }); } } }, IntroduceConstantHandler.REFACTORING_NAME, IntroduceConstantHandler.REFACTORING_NAME); + return result.get(); } private PsiField createFieldToStartTemplateOn(final String[] names, final PsiType psiType) { @@ -343,22 +355,19 @@ public class InplaceIntroduceConstantPopup { return ApplicationManager.getApplication().runWriteAction(new Computable() { @Override public PsiField compute() { - final Ref ref = new Ref(); - final Runnable runnable = new Runnable() { - public void run() { - PsiField field = elementFactory.createFieldFromText(psiType.getCanonicalText() + " " + (myConstantName != null ? myConstantName : names[0]) + " = " + myInitializerText + ";", myParentClass); - PsiUtil.setModifierProperty(field, PsiModifier.FINAL, true); - PsiUtil.setModifierProperty(field, PsiModifier.STATIC, true); - final String visibility = getSelectedVisibility(); - if (visibility != null) { - PsiUtil.setModifierProperty(field, visibility, true); - } - field = BaseExpressionToFieldHandler.ConvertToFieldRunnable.appendField(myExpr, BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION, myParentClass, myParentClass, myAnchorElementIfAll, field); - ref.set(field); - } - }; - PostprocessReformattingAspect.getInstance(myProject).postponeFormattingInside(runnable); - return ref.get(); + + PsiField field = elementFactory.createFieldFromText( + psiType.getCanonicalText() + " " + (myConstantName != null ? myConstantName : names[0]) + " = " + myInitializerText + ";", + myParentClass); + PsiUtil.setModifierProperty(field, PsiModifier.FINAL, true); + PsiUtil.setModifierProperty(field, PsiModifier.STATIC, true); + final String visibility = getSelectedVisibility(); + if (visibility != null) { + PsiUtil.setModifierProperty(field, visibility, true); + } + return BaseExpressionToFieldHandler.ConvertToFieldRunnable + .appendField(myExpr, BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION, myParentClass, myParentClass, + myAnchorElementIfAll, field); } }); } diff --git a/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceFieldPopup.java b/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceFieldPopup.java index d137c9bdffc4..77b1c27dca0a 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceFieldPopup.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceField/InplaceIntroduceFieldPopup.java @@ -151,11 +151,12 @@ public class InplaceIntroduceFieldPopup { return myOccurrenceMarkers; } - public void startTemplate() { - startTemplate(false, null); + public boolean startTemplate() { + return startTemplate(false, null); } - public void startTemplate(final boolean replaceAllOccurrences, @Nullable final PsiType fieldDefaultType) { + public boolean startTemplate(final boolean replaceAllOccurrences, @Nullable final PsiType fieldDefaultType) { + final Ref result = new Ref(); CommandProcessor.getInstance().executeCommand(myProject, new Runnable() { public void run() { myTypeSelectorManager.setAllOccurences(replaceAllOccurrences); @@ -176,6 +177,7 @@ public class InplaceIntroduceFieldPopup { IntroduceFieldDialog.createGenerator(myStatic, myLocalVariable, myInitializerExpression, myLocalVariable != null) .getSuggestedNameInfo(defaultType); + boolean started = false; final PsiField field = createFieldToStartTemplateOn(suggestedNameInfo.names, defaultType); if (field != null) { myEditor.getCaretModel().moveToOffset(field.getTextOffset()); @@ -184,10 +186,20 @@ public class InplaceIntroduceFieldPopup { nameSuggestions.add(field.getName()); nameSuggestions.addAll(Arrays.asList(suggestedNameInfo.names)); final VariableInplaceRenamer renamer = new FieldInplaceIntroducer(field); - renamer.performInplaceRename(false, nameSuggestions); + started = renamer.performInplaceRename(false, nameSuggestions); + } + result.set(started); + if (!started && field != null) { + ApplicationManager.getApplication().runWriteAction(new Runnable() { + @Override + public void run() { + field.delete(); + } + }); } } }, IntroduceFieldHandler.REFACTORING_NAME, IntroduceFieldHandler.REFACTORING_NAME); + return result.get(); } private PsiField createFieldToStartTemplateOn(final String[] names, @@ -196,22 +208,14 @@ public class InplaceIntroduceFieldPopup { return ApplicationManager.getApplication().runWriteAction(new Computable() { @Override public PsiField compute() { - final Ref ref = new Ref(); - PostprocessReformattingAspect.getInstance(myProject).postponeFormattingInside(new Runnable() { - @Override - public void run() { - PsiField field = elementFactory.createField(myFieldName != null ? myFieldName : names[0], defaultType); - field = (PsiField)myParentClass.add(field); - PsiUtil.setModifierProperty(field, PsiModifier.FINAL, myIntroduceFieldPanel.isDeclareFinal()); - final String visibility = myIntroduceFieldPanel.getFieldVisibility(); - if (visibility != null) { - PsiUtil.setModifierProperty(field, visibility, true); - } - ref.set(field); - } - }); - - return ref.get(); + PsiField field = elementFactory.createField(myFieldName != null ? myFieldName : names[0], defaultType); + field = (PsiField)myParentClass.add(field); + PsiUtil.setModifierProperty(field, PsiModifier.FINAL, myIntroduceFieldPanel.isDeclareFinal()); + final String visibility = myIntroduceFieldPanel.getFieldVisibility(); + if (visibility != null) { + PsiUtil.setModifierProperty(field, visibility, true); + } + return field; } }); } diff --git a/java/java-impl/src/com/intellij/refactoring/introduceField/IntroduceConstantHandler.java b/java/java-impl/src/com/intellij/refactoring/introduceField/IntroduceConstantHandler.java index 2f230c9def78..cf86a545362b 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceField/IntroduceConstantHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceField/IntroduceConstantHandler.java @@ -147,10 +147,11 @@ public class IntroduceConstantHandler extends BaseExpressionToFieldHandler { final TypeSelectorManagerImpl typeSelectorManager = new TypeSelectorManagerImpl(project, type, containingMethod, expr, occurences); if (editor != null && editor.getSettings().isVariableInplaceRenameEnabled()) { - new InplaceIntroduceConstantPopup(project, editor, parentClass, expr, localVariable, occurences, typeSelectorManager, + if (new InplaceIntroduceConstantPopup(project, editor, parentClass, expr, localVariable, occurences, typeSelectorManager, anchorElement, anchorElementIfAll, - expr != null ? createOccurenceManager(expr, parentClass) : null).performInplaceIntroduce(); - return null; + expr != null ? createOccurenceManager(expr, parentClass) : null).performInplaceIntroduce() ){ + return null; + } } diff --git a/java/java-impl/src/com/intellij/refactoring/introduceField/IntroduceFieldHandler.java b/java/java-impl/src/com/intellij/refactoring/introduceField/IntroduceFieldHandler.java index bc69a59642aa..d617bb670d7b 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceField/IntroduceFieldHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceField/IntroduceFieldHandler.java @@ -109,8 +109,9 @@ public class IntroduceFieldHandler extends BaseExpressionToFieldHandler { myInplaceIntroduceFieldPopup = new InplaceIntroduceFieldPopup(localVariable, parentClass, declareStatic, currentMethodConstructor, occurences, expr, typeSelectorManager, editor, allowInitInMethod, allowInitInMethodIfAll, anchorElement, anchorElementIfAll, expr != null ? createOccurenceManager(expr, parentClass) : null); - myInplaceIntroduceFieldPopup.startTemplate(); - return null; + if (myInplaceIntroduceFieldPopup.startTemplate()) { + return null; + } } IntroduceFieldDialog dialog = new IntroduceFieldDialog( diff --git a/java/java-impl/src/com/intellij/refactoring/introduceParameter/InplaceIntroduceParameterPopup.java b/java/java-impl/src/com/intellij/refactoring/introduceParameter/InplaceIntroduceParameterPopup.java index 4bc1422a9a7a..7578f7b67c53 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceParameter/InplaceIntroduceParameterPopup.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceParameter/InplaceIntroduceParameterPopup.java @@ -194,16 +194,17 @@ class InplaceIntroduceParameterPopup extends IntroduceParameterSettingsUI { return myReplaceFieldsCb!= null ? (Integer)myReplaceFieldsCb.getSelectedItem() : IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE; } - void inplaceIntroduceParameter() { - startIntroduceTemplate(false); + boolean inplaceIntroduceParameter() { + return startIntroduceTemplate(false); } - private void startIntroduceTemplate(final boolean replaceAllOccurrences) { - startIntroduceTemplate(replaceAllOccurrences, hasFinalModifier()); + private boolean startIntroduceTemplate(final boolean replaceAllOccurrences) { + return startIntroduceTemplate(replaceAllOccurrences, hasFinalModifier()); } - private void startIntroduceTemplate(final boolean replaceAllOccurrences, + private boolean startIntroduceTemplate(final boolean replaceAllOccurrences, final boolean hasFinalModifier) { + final Ref result = new Ref(); CommandProcessor.getInstance().executeCommand(myProject, new Runnable() { public void run() { myTypeSelectorManager.setAllOccurences(replaceAllOccurrences); @@ -213,6 +214,7 @@ class InplaceIntroduceParameterPopup extends IntroduceParameterSettingsUI { final String[] names = IntroduceParameterHandler.createNameSuggestionGenerator(myExpr, propName, myProject) .getSuggestedNameInfo(defaultType).names; final PsiParameter parameter = createParameterToStartTemplateOn(names, defaultType, hasFinalModifier); + boolean started = false; if (parameter != null) { myParameterIndex = myMethod.getParameterList().getParameterIndex(parameter); myEditor.getCaretModel().moveToOffset(parameter.getTextOffset()); @@ -222,10 +224,19 @@ class InplaceIntroduceParameterPopup extends IntroduceParameterSettingsUI { nameSuggestions.addAll(Arrays.asList(names)); final VariableInplaceRenamer renamer = new ParameterInplaceIntroducer(parameter); LOG.assertTrue(parameter.isPhysical()); - renamer.performInplaceRename(false, nameSuggestions); + started = renamer.performInplaceRename(false, nameSuggestions); + } + result.set(started); + if (!started && parameter != null) { + ApplicationManager.getApplication().runWriteAction(new Runnable() { + public void run() { + parameter.delete(); + } + }); } } }, IntroduceParameterHandler.REFACTORING_NAME, IntroduceParameterHandler.REFACTORING_NAME); + return result.get(); } @Override diff --git a/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterHandler.java b/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterHandler.java index 2e0787fc34f4..215534815684 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceParameter/IntroduceParameterHandler.java @@ -419,21 +419,20 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase implements R NameSuggestionsGenerator nameSuggestionsGenerator = createNameSuggestionGenerator(myExpr, propName, myProject); boolean isInplaceAvailableOnDataContext = myEditor != null && myEditor.getSettings().isVariableInplaceRenameEnabled(); - if (!isInplaceAvailableOnDataContext) { - if (myEditor != null) { - RefactoringUtil.highlightAllOccurences(myProject, occurences, myEditor); - } - new IntroduceParameterDialog(myProject, classMemberRefs, occurences, myLocalVar, myExpr, nameSuggestionsGenerator, - typeSelectorManager, methodToSearchFor, method, parametersToRemove, mustBeFinal).show(); - if (myEditor != null) { - myEditor.getSelectionModel().removeSelection(); - } + if (isInplaceAvailableOnDataContext && new InplaceIntroduceParameterPopup(myProject, myEditor, classMemberRefs, + typeSelectorManager, + myExpr, myLocalVar, method, methodToSearchFor, occurences, + parametersToRemove, + mustBeFinal).inplaceIntroduceParameter()) { + return; } - else { - new InplaceIntroduceParameterPopup(myProject, myEditor, classMemberRefs, - typeSelectorManager, - myExpr, myLocalVar, method, methodToSearchFor, occurences, parametersToRemove, - mustBeFinal).inplaceIntroduceParameter(); + if (myEditor != null) { + RefactoringUtil.highlightAllOccurences(myProject, occurences, myEditor); + } + new IntroduceParameterDialog(myProject, classMemberRefs, occurences, myLocalVar, myExpr, nameSuggestionsGenerator, + typeSelectorManager, methodToSearchFor, method, parametersToRemove, mustBeFinal).show(); + if (myEditor != null) { + myEditor.getSelectionModel().removeSelection(); } } } diff --git a/java/java-impl/src/com/intellij/refactoring/introduceVariable/VariableInplaceIntroducer.java b/java/java-impl/src/com/intellij/refactoring/introduceVariable/VariableInplaceIntroducer.java index da8472d0ddbd..c5951cf994a2 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/VariableInplaceIntroducer.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/VariableInplaceIntroducer.java @@ -169,7 +169,9 @@ public class VariableInplaceIntroducer extends VariableInplaceRenamer { @Override public boolean performInplaceRename(boolean processTextOccurrences, LinkedHashSet nameSuggestions) { final boolean result = super.performInplaceRename(processTextOccurrences, nameSuggestions); - showBalloon(); + if (result) { + showBalloon(); + } return result; }