diff --git a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java index 572221b8cf78..ca51d31a50c9 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * 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. @@ -18,6 +18,7 @@ package com.intellij.refactoring.introduceVariable; import com.intellij.codeInsight.CodeInsightUtil; import com.intellij.codeInsight.completion.JavaCompletionUtil; import com.intellij.codeInsight.highlighting.HighlightManager; +import com.intellij.codeInsight.intention.impl.TypeExpression; import com.intellij.codeInsight.lookup.LookupManager; import com.intellij.codeInsight.unwrap.ScopeHighlighter; import com.intellij.featureStatistics.FeatureUsageTracker; @@ -36,7 +37,10 @@ import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.markup.TextAttributes; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.*; +import com.intellij.openapi.util.Key; +import com.intellij.openapi.util.Pass; +import com.intellij.openapi.util.Ref; +import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.registry.Registry; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.wm.WindowManager; @@ -73,7 +77,6 @@ import com.intellij.util.containers.MultiMap; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.annotations.TestOnly; import java.util.*; @@ -88,7 +91,6 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { protected static final String REFACTORING_NAME = RefactoringBundle.message("introduce.variable.title"); public static final Key NEED_PARENTHESIS = Key.create("NEED_PARENTHESIS"); - private JavaVariableInplaceIntroducer myInplaceIntroducer; public static SuggestedNameInfo getSuggestedName(@Nullable PsiType type, @NotNull final PsiExpression expression) { return getSuggestedName(type, expression, expression); @@ -643,74 +645,76 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { final Pass callback = new Pass() { @Override public void pass(final OccurrencesChooser.ReplaceChoice choice) { - if (choice != null) { - final boolean replaceAll = choice == OccurrencesChooser.ReplaceChoice.ALL || choice == OccurrencesChooser.ReplaceChoice.NO_WRITE; - typeSelectorManager.setAllOccurrences(replaceAll); + final boolean allOccurences = choice == OccurrencesChooser.ReplaceChoice.ALL || choice == OccurrencesChooser.ReplaceChoice.NO_WRITE; + final Ref> variable = new Ref>(); - final PsiElement chosenAnchor = - chooseAnchor(replaceAll, choice == OccurrencesChooser.ReplaceChoice.NO_WRITE, nonWrite, anchorStatementIfAll, anchorStatement); - final IntroduceVariableSettings settings = - getSettings(project, editor, expr, occurrences, typeSelectorManager, inFinalContext, hasWriteAccess, validator, chosenAnchor, choice); - - final boolean cantChangeFinalModifier = (hasWriteAccess || inFinalContext) && choice == OccurrencesChooser.ReplaceChoice.ALL; - - final boolean noWrite = choice == OccurrencesChooser.ReplaceChoice.NO_WRITE; - final List allOccurrences = new ArrayList(); - for (PsiExpression occurrence : occurrences) { - if (expr.equals(occurrence) && expr.getParent() instanceof PsiExpressionStatement) continue; - if (choice == OccurrencesChooser.ReplaceChoice.ALL || (noWrite && !PsiUtil.isAccessedForWriting(occurrence)) || expr.equals(occurrence)) { - allOccurrences.add(occurrence); - } - } - myInplaceIntroducer = new JavaVariableInplaceIntroducer(project, - settings, - chosenAnchor, - editor, expr, cantChangeFinalModifier, - allOccurrences.toArray(new PsiExpression[allOccurrences.size()]), - typeSelectorManager, - REFACTORING_NAME); - if (myInplaceIntroducer.startInplaceIntroduceTemplate()) { - return; - } + final Editor topLevelEditor; + if (!InjectedLanguageManager.getInstance(project).isInjectedFragment(anchorStatement.getContainingFile())) { + topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(editor); + } else { + topLevelEditor = editor; } + final IntroduceVariableSettings settings; + final PsiElement chosenAnchor; + if (choice != null) { + chosenAnchor = chooseAnchor(allOccurences, choice == OccurrencesChooser.ReplaceChoice.NO_WRITE, nonWrite, anchorStatementIfAll, anchorStatement); + settings = getSettings(project, topLevelEditor, expr, occurrences, typeSelectorManager, inFinalContext, hasWriteAccess, validator, chosenAnchor, choice); + } + else { + settings = getSettings(project, topLevelEditor, expr, occurrences, typeSelectorManager, inFinalContext, hasWriteAccess, validator, anchorStatement, choice); + chosenAnchor = chooseAnchor(settings.isReplaceAllOccurrences(), hasWriteAccess, nonWrite, anchorStatementIfAll, anchorStatement); + } + if (!settings.isOK()) { + wasSucceed[0] = false; + return; + } + typeSelectorManager.setAllOccurrences(allOccurences); + final TypeExpression expression = new TypeExpression(project, allOccurences ? typeSelectorManager.getTypesForAll() : typeSelectorManager.getTypesForOne()); + final RangeMarker exprMarker = topLevelEditor.getDocument().createRangeMarker(expr.getTextRange()); + final SuggestedNameInfo suggestedName = getSuggestedName(settings.getSelectedType(), expr, chosenAnchor); + final List occurrenceMarkers = new ArrayList(); + final boolean noWrite = choice == OccurrencesChooser.ReplaceChoice.NO_WRITE; + for (PsiExpression occurrence : occurrences) { + if (allOccurences || (noWrite && !PsiUtil.isAccessedForWriting(occurrence))) { + occurrenceMarkers.add(topLevelEditor.getDocument().createRangeMarker(occurrence.getTextRange())); + } + } + final RefactoringEventData beforeData = new RefactoringEventData(); + beforeData.addElement(expr); + project.getMessageBus() + .syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(REFACTORING_ID, beforeData); + final String expressionText = expr.getText(); + final Runnable runnable = introduce(project, expr, topLevelEditor, chosenAnchor, occurrences, settings, variable); CommandProcessor.getInstance().executeCommand( project, new Runnable() { public void run() { - final Editor topLevelEditor ; - if (!InjectedLanguageManager.getInstance(project).isInjectedFragment(anchorStatement.getContainingFile())) { - topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(editor); - } else { - topLevelEditor = editor; - } - - PsiVariable variable = null; try { - final IntroduceVariableSettings settings = - getSettings(project, topLevelEditor, expr, occurrences, typeSelectorManager, inFinalContext, hasWriteAccess, validator, anchorStatement, choice); - if (!settings.isOK()) { - wasSucceed[0] = false; - return; - } - - final RefactoringEventData beforeData = new RefactoringEventData(); - beforeData.addElement(expr); - project.getMessageBus() - .syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(REFACTORING_ID, beforeData); - - final PsiElement chosenAnchor = - chooseAnchor(settings.isReplaceAllOccurrences(), hasWriteAccess, nonWrite, anchorStatementIfAll, anchorStatement); - - variable = ApplicationManager.getApplication().runWriteAction( - introduce(project, expr, topLevelEditor, chosenAnchor, occurrences, settings)); + ApplicationManager.getApplication().runWriteAction(runnable); } finally { final RefactoringEventData afterData = new RefactoringEventData(); - afterData.addElement(variable); + final SmartPsiElementPointer pointer = variable.get(); + afterData.addElement(pointer != null ? pointer.getElement() : null); project.getMessageBus() .syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(REFACTORING_ID, afterData); } + + if (isInplaceAvailableOnDataContext) { + final PsiVariable elementToRename = variable.get().getElement(); + if (elementToRename != null) { + topLevelEditor.getCaretModel().moveToOffset(elementToRename.getTextOffset()); + final boolean cantChangeFinalModifier = (hasWriteAccess || inFinalContext) && choice == OccurrencesChooser.ReplaceChoice.ALL; + final JavaVariableInplaceIntroducer renamer = + new JavaVariableInplaceIntroducer(project, expression, topLevelEditor, elementToRename, cantChangeFinalModifier, + typeSelectorManager.getTypesForAll().length > 1, exprMarker, occurrenceMarkers, + REFACTORING_NAME); + renamer.initInitialText(expressionText); + PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(topLevelEditor.getDocument()); + renamer.performInplaceRefactoring(new LinkedHashSet(Arrays.asList(suggestedName.names))); + } + } } }, REFACTORING_NAME, null); } @@ -720,25 +724,16 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { callback.pass(null); } else { - OccurrencesChooser.ReplaceChoice choice = getOccurrencesChoice(); - if (choice != null) { - callback.pass(choice); - } else { - OccurrencesChooser.simpleChooser(editor).showChooser(callback, occurrencesMap); - } + OccurrencesChooser.simpleChooser(editor).showChooser(callback, occurrencesMap); } return wasSucceed[0]; } - - protected OccurrencesChooser.ReplaceChoice getOccurrencesChoice() { - return null; - } - protected static PsiElement chooseAnchor(boolean allOccurences, - boolean hasWriteAccess, - List nonWrite, - PsiElement anchorStatementIfAll, - PsiElement anchorStatement) { + protected PsiElement chooseAnchor(boolean allOccurences, + boolean hasWriteAccess, + List nonWrite, + PsiElement anchorStatementIfAll, + PsiElement anchorStatement) { if (allOccurences) { if (hasWriteAccess) { return RefactoringUtil.getAnchorElementForMultipleExpressions(nonWrite.toArray(new PsiExpression[nonWrite.size()]), null); @@ -797,12 +792,13 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { return parent3 instanceof JspHolderMethod; } - public static Computable introduce(final Project project, - final PsiExpression expr, - final Editor editor, - final PsiElement anchorStatement, - final PsiExpression[] occurrences, - final IntroduceVariableSettings settings) { + private static Runnable introduce(final Project project, + final PsiExpression expr, + final Editor editor, + final PsiElement anchorStatement, + final PsiExpression[] occurrences, + final IntroduceVariableSettings settings, + final Ref> variable) { final PsiElement container = anchorStatement.getParent(); PsiElement child = anchorStatement; if (!RefactoringUtil.isLoopOrIf(container)) { @@ -841,9 +837,8 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { final PsiCodeBlock newDeclarationScope = PsiTreeUtil.getParentOfType(container, PsiCodeBlock.class, false); final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(settings.getEnteredName(), newDeclarationScope); - return new Computable() { - @Override - public PsiVariable compute() { + return new Runnable() { + public void run() { try { PsiStatement statement = null; final boolean isInsideLoop = RefactoringUtil.isLoopOrIf(container); @@ -918,12 +913,11 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { declaration = (PsiDeclarationStatement)JavaCodeStyleManager.getInstance(project).shortenClassReferences(declaration); PsiVariable var = (PsiVariable) declaration.getDeclaredElements()[0]; PsiUtil.setModifierProperty(var, PsiModifier.FINAL, settings.isDeclareFinal()); + variable.set(SmartPointerManager.getInstance(project).createSmartPsiElementPointer(var)); fieldConflictsResolver.fix(); - return var; } catch (IncorrectOperationException e) { LOG.error(e); } - return null; } private PsiDeclarationStatement addDeclaration(PsiDeclarationStatement declaration, PsiExpression initializer) { @@ -1171,6 +1165,6 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { @Override public AbstractInplaceIntroducer getInplaceIntroducer() { - return myInplaceIntroducer; + return null; } } diff --git a/java/java-impl/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java b/java/java-impl/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java index 0682e250244b..f9e88de162cf 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/JavaVariableInplaceIntroducer.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * 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. @@ -21,6 +21,7 @@ import com.intellij.openapi.actionSystem.Shortcut; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.Result; import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.command.impl.StartMarkAction; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.RangeMarker; @@ -29,66 +30,87 @@ import com.intellij.openapi.keymap.Keymap; import com.intellij.openapi.keymap.KeymapManager; import com.intellij.openapi.keymap.KeymapUtil; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Comparing; +import com.intellij.openapi.vfs.ReadonlyStatusHandler; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.codeStyle.JavaCodeStyleManager; -import com.intellij.psi.codeStyle.VariableKind; +import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; import com.intellij.psi.scope.processor.VariablesProcessor; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.TypeConversionUtil; import com.intellij.refactoring.JavaRefactoringSettings; +import com.intellij.refactoring.introduce.inplace.InplaceVariableIntroducer; import com.intellij.refactoring.introduceParameter.AbstractJavaInplaceIntroducer; import com.intellij.refactoring.rename.ResolveSnapshotProvider; import com.intellij.refactoring.rename.inplace.VariableInplaceRenamer; import com.intellij.refactoring.ui.TypeSelectorManagerImpl; import com.intellij.ui.NonFocusableCheckBox; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.ArrayList; import java.util.List; /** * User: anna * Date: 12/8/10 */ -public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer { - - private SmartPsiElementPointer myPointer; +public class JavaVariableInplaceIntroducer extends InplaceVariableIntroducer { + protected final Project myProject; + private final SmartPsiElementPointer myPointer; private JCheckBox myCanBeFinalCb; - private IntroduceVariableSettings mySettings; - private SmartPsiElementPointer myChosenAnchor; + private final boolean myCantChangeFinalModifier; - private boolean myHasTypeSuggestion; + private final String myTitle; + private String myExpressionText; + protected final SmartTypePointer myDefaultType; + protected final TypeExpression myExpression; + private ResolveSnapshotProvider.ResolveSnapshot myConflictResolver; - private TypeExpression myExpression; - private boolean myReplaceSelf; - private boolean myDeleteSelf = true; public JavaVariableInplaceIntroducer(final Project project, - IntroduceVariableSettings settings, PsiElement chosenAnchor, final Editor editor, - final PsiExpression expr, + final TypeExpression expression, + final Editor editor, + @NotNull final PsiVariable elementToRename, final boolean cantChangeFinalModifier, - final PsiExpression[] occurrences, - final TypeSelectorManagerImpl selectorManager, + final boolean hasTypeSuggestion, + final RangeMarker exprMarker, + final List occurrenceMarkers, final String title) { - super(project, editor, expr, null, occurrences, selectorManager, title); - mySettings = settings; - myChosenAnchor = SmartPointerManager.getInstance(project).createSmartPsiElementPointer(chosenAnchor); + super(elementToRename, editor, project, title, new PsiExpression[0], null); + myProject = project; myCantChangeFinalModifier = cantChangeFinalModifier; - myHasTypeSuggestion = selectorManager.getTypesForAll().length > 1; myTitle = title; - myExpression = new TypeExpression(myProject, isReplaceAllOccurrences() - ? myTypeSelectorManager.getTypesForAll() - : myTypeSelectorManager.getTypesForOne()); + setExprMarker(exprMarker); + setOccurrenceMarkers(occurrenceMarkers); + final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(elementToRename, PsiDeclarationStatement.class); + myPointer = declarationStatement != null ? SmartPointerManager.getInstance(project).createSmartPsiElementPointer(declarationStatement) : null; + editor.putUserData(ReassignVariableUtil.DECLARATION_KEY, myPointer); + if (occurrenceMarkers != null) { + final ArrayList rangeMarkers = new ArrayList(occurrenceMarkers); + rangeMarkers.add(exprMarker); + editor.putUserData(ReassignVariableUtil.OCCURRENCES_KEY, + rangeMarkers.toArray(new RangeMarker[rangeMarkers.size()])); + } + myExpression = expression; + final PsiType defaultType = elementToRename.getType(); + myDefaultType = SmartTypePointerManager.getInstance(project).createSmartTypePointer(defaultType); + setAdvertisementText(getAdvertisementText(declarationStatement, defaultType, hasTypeSuggestion)); + } - final List rangeMarkers = getOccurrenceMarkers(); - editor.putUserData(ReassignVariableUtil.OCCURRENCES_KEY, - rangeMarkers.toArray(new RangeMarker[rangeMarkers.size()])); - myReplaceSelf = myExpr.getParent() instanceof PsiExpressionStatement; + public void initInitialText(String text) { + myExpressionText = text; + } + + @Override + protected StartMarkAction startRename() throws StartMarkAction.AlreadyStartedException { + return StartMarkAction.start(myEditor, myProject, getCommandName()); } @Override @@ -100,7 +122,7 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer @Nullable protected PsiVariable getVariable() { - final PsiDeclarationStatement declarationStatement = myPointer != null ? myPointer.getElement() : null; + final PsiDeclarationStatement declarationStatement = myPointer.getElement(); if (declarationStatement != null) { PsiElement[] declaredElements = declarationStatement.getDeclaredElements(); return declaredElements.length == 0 ? null : (PsiVariable)declaredElements[0]; @@ -109,89 +131,101 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer } @Override - protected String getActionName() { - return "IntroduceVariable"; - } - - @Override - protected void restoreState(PsiVariable psiField) { - if (myDeleteSelf) return; - super.restoreState(psiField); - } - - @Override - protected boolean ensureValid() { - final PsiVariable variable = getVariable(); - return variable != null && isIdentifier(getInputName(), variable.getLanguage()); - } - - @Override - protected void performCleanup() { - super.performCleanup(); - super.restoreState(getVariable()); - } - - @Override - protected void deleteTemplateField(PsiVariable psiField) { - if (!myDeleteSelf) return; - if (myReplaceSelf) { - psiField.replace(psiField.getInitializer()); - } else { - super.deleteTemplateField(psiField); - } - } - - @Override - protected void performIntroduce() { - final PsiVariable psiVariable = getVariable(); - if (psiVariable == null) { - return; - } - - TypeSelectorManagerImpl.typeSelected(psiVariable.getType(), myTypeSelectorManager.getDefaultType()); - if (myCanBeFinalCb != null) { - JavaRefactoringSettings.getInstance().INTRODUCE_LOCAL_CREATE_FINALS = psiVariable.hasModifierProperty(PsiModifier.FINAL); - } - - final Document document = myEditor.getDocument(); - LOG.assertTrue(psiVariable.isValid()); - adjustLine(psiVariable, document); - - int startOffset = getExprMarker() != null && getExprMarker().isValid() ? getExprMarker().getStartOffset() : psiVariable.getTextOffset(); - final PsiFile file = psiVariable.getContainingFile(); - final PsiReference referenceAt = file.findReferenceAt(startOffset); - if (referenceAt != null && referenceAt.resolve() instanceof PsiVariable) { - startOffset = referenceAt.getElement().getTextRange().getEndOffset(); - } - else { - final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(psiVariable, PsiDeclarationStatement.class); - if (declarationStatement != null) { - startOffset = declarationStatement.getTextRange().getEndOffset(); - } - } - - myEditor.getCaretModel().moveToOffset(startOffset); - myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); - - ApplicationManager.getApplication().runWriteAction(new Runnable() { - public void run() { - if (psiVariable.getInitializer() != null) { - appendTypeCasts(getOccurrenceMarkers(), file, myProject, psiVariable); + protected void moveOffsetAfter(boolean success) { + try { + if (success) { + final Document document = myEditor.getDocument(); + @Nullable final PsiVariable psiVariable = getVariable(); + if (psiVariable == null) { + return; } - if (myConflictResolver != null && myInsertedName != null && isIdentifier(myInsertedName, psiVariable.getLanguage())) { - myConflictResolver.apply(psiVariable.getName()); + LOG.assertTrue(psiVariable.isValid()); + TypeSelectorManagerImpl.typeSelected(psiVariable.getType(), myDefaultType.getType()); + if (myCanBeFinalCb != null) { + JavaRefactoringSettings.getInstance().INTRODUCE_LOCAL_CREATE_FINALS = psiVariable.hasModifierProperty(PsiModifier.FINAL); + } + adjustLine(psiVariable, document); + + int startOffset = getExprMarker() != null && getExprMarker().isValid() ? getExprMarker().getStartOffset() : psiVariable.getTextOffset(); + final PsiFile file = psiVariable.getContainingFile(); + final PsiReference referenceAt = file.findReferenceAt(startOffset); + if (referenceAt != null && referenceAt.resolve() instanceof PsiVariable) { + startOffset = referenceAt.getElement().getTextRange().getEndOffset(); + } + else { + final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(psiVariable, PsiDeclarationStatement.class); + if (declarationStatement != null) { + startOffset = declarationStatement.getTextRange().getEndOffset(); + } + } + myEditor.getCaretModel().moveToOffset(startOffset); + myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); + ApplicationManager.getApplication().runWriteAction(new Runnable() { + public void run() { + if (psiVariable.getInitializer() != null) { + appendTypeCasts(getOccurrenceMarkers(), file, myProject, psiVariable); + } + if (myConflictResolver != null && myInsertedName != null && isIdentifier(myInsertedName, psiVariable.getLanguage())) { + myConflictResolver.apply(psiVariable.getName()); + } + } + }); + } + else { + RangeMarker exprMarker = getExprMarker(); + if (exprMarker != null && exprMarker.isValid()) { + myEditor.getCaretModel().moveToOffset(exprMarker.getStartOffset()); + myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); + } + if (myExpressionText != null) { + if (!ReadonlyStatusHandler.ensureDocumentWritable(myProject, InjectedLanguageUtil.getTopLevelEditor(myEditor).getDocument())) return; + ApplicationManager.getApplication().runWriteAction(new Runnable() { + public void run() { + final PsiDeclarationStatement element = myPointer.getElement(); + if (element != null) { + final PsiElement[] vars = element.getDeclaredElements(); + if (vars.length > 0 && vars[0] instanceof PsiVariable) { + final PsiFile containingFile = element.getContainingFile(); + //todo pull up method restore state + final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(myProject); + final RangeMarker exprMarker = getExprMarker(); + if (exprMarker != null) { + myExpr = AbstractJavaInplaceIntroducer.restoreExpression(containingFile, (PsiVariable)vars[0], elementFactory, exprMarker, myExpressionText); + if (myExpr != null && myExpr.isPhysical()) { + myExprMarker = createMarker(myExpr); + } + } + List markers = getOccurrenceMarkers(); + for (RangeMarker occurrenceMarker : markers) { + if (getExprMarker() != null && occurrenceMarker.getStartOffset() == getExprMarker().getStartOffset() && myExpr != null) { + continue; + } + AbstractJavaInplaceIntroducer + .restoreExpression(containingFile, (PsiVariable)vars[0], elementFactory, occurrenceMarker, myExpressionText); + } + final PsiExpression initializer = ((PsiVariable)vars[0]).getInitializer(); + if (initializer != null && Comparing.strEqual(initializer.getText(), myExpressionText) && myExpr == null) { + element.replace(JavaPsiFacade.getInstance(myProject).getElementFactory().createStatementFromText(myExpressionText, element)); + } else { + element.delete(); + } + } + } + } + }); } } - }); + } + finally { + myEditor.putUserData(ReassignVariableUtil.DECLARATION_KEY, null); + for (RangeMarker occurrenceMarker : getOccurrenceMarkers()) { + occurrenceMarker.dispose(); + } + myEditor.putUserData(ReassignVariableUtil.OCCURRENCES_KEY, null); + if (getExprMarker() != null) getExprMarker().dispose(); + } } - @Override - public boolean isReplaceAllOccurrences() { - return mySettings.isReplaceAllOccurrences(); - } - - @Override - public void setReplaceAllOccurrences(boolean allOccurrences) {} @Nullable protected JComponent getComponent() { @@ -231,13 +265,8 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer } protected void addAdditionalVariables(TemplateBuilderImpl builder) { - final PsiVariable variable = getVariable(); - if (variable != null) { - final PsiTypeElement typeElement = variable.getTypeElement(); - if (typeElement != null) { - builder.replaceElement(typeElement, "Variable_Type", AbstractJavaInplaceIntroducer.createExpression(myExpression, typeElement.getText()), true, true); - } - } + final PsiTypeElement typeElement = getVariable().getTypeElement(); + builder.replaceElement(typeElement, "Variable_Type", AbstractJavaInplaceIntroducer.createExpression(myExpression, typeElement.getText()), true, true); } private static void appendTypeCasts(List occurrenceMarkers, @@ -324,52 +353,21 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer }); } - @Override - protected PsiVariable createFieldToStartTemplateOn(String[] names, PsiType psiType) { - final PsiVariable variable = ApplicationManager.getApplication().runWriteAction( - IntroduceVariableBase.introduce(myProject, myExpr, myEditor, myChosenAnchor.getElement(), getOccurrences(), mySettings)); - PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myEditor.getDocument()); - final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(variable, PsiDeclarationStatement.class); - myPointer = declarationStatement != null ? SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(declarationStatement) : null; - myEditor.putUserData(ReassignVariableUtil.DECLARATION_KEY, myPointer); - setAdvertisementText(getAdvertisementText(declarationStatement, variable.getType(), myHasTypeSuggestion)); - final PsiIdentifier identifier = variable.getNameIdentifier(); - if (identifier != null) { - myEditor.getCaretModel().moveToOffset(identifier.getTextOffset()); - } - try { - myDeleteSelf = false; - restoreState(variable); - } - finally { - myDeleteSelf = true; - } - initOccurrencesMarkers(); - return variable; + + protected String getTitle() { + return myTitle; } - @Override - protected void correctExpression() {} - @Override - protected int getCaretOffset() { - final PsiVariable variable = getVariable(); - if (variable != null) { - final PsiIdentifier identifier = variable.getNameIdentifier(); - if (identifier != null) { - return identifier.getTextOffset(); + @Nullable + private static String getAdvertisementText(final boolean hasTypeSuggestion) { + final Keymap keymap = KeymapManager.getInstance().getActiveKeymap(); + if (hasTypeSuggestion) { + final Shortcut[] shortcuts = keymap.getShortcuts("PreviousTemplateVariable"); + if (shortcuts.length > 0) { + return "Press " + shortcuts[0] + " to change type"; } } - return super.getCaretOffset(); - } - - @Override - protected String[] suggestNames(PsiType defaultType, String propName) { - return IntroduceVariableBase.getSuggestedName(defaultType, myExpr).names; - } - - @Override - protected VariableKind getVariableKind() { - return VariableKind.LOCAL_VARIABLE; + return null; } } diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/allIncomplete.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/allIncomplete.java deleted file mode 100644 index 377da56e3975..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/allIncomplete.java +++ /dev/null @@ -1,6 +0,0 @@ -class C { - { - new C(); - new C(); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/allIncomplete_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/allIncomplete_after.java deleted file mode 100644 index f83b8e61cd52..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/allIncomplete_after.java +++ /dev/null @@ -1,6 +0,0 @@ -class C { - { - C c = new C(); - c; - } -} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/allInsertFinal.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/allInsertFinal.java deleted file mode 100644 index 476e9111cdfb..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/allInsertFinal.java +++ /dev/null @@ -1,11 +0,0 @@ -class C { - { - C c = new C(); - Runnable r = new Runnable() { - @Override - public void run() { - new C(); - } - }; - } -} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/allInsertFinal_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/allInsertFinal_after.java deleted file mode 100644 index c6f2df805826..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/allInsertFinal_after.java +++ /dev/null @@ -1,12 +0,0 @@ -class C { - { - final C c1 = new C(); - C c = c1; - Runnable r = new Runnable() { - @Override - public void run() { - c1; - } - }; - } -} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/cast.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/cast.java deleted file mode 100644 index f175eb2b1b32..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/cast.java +++ /dev/null @@ -1,5 +0,0 @@ -class C { - { - C c = new C(); - } -} diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/castToObject.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/castToObject.java deleted file mode 100644 index f175eb2b1b32..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/castToObject.java +++ /dev/null @@ -1,5 +0,0 @@ -class C { - { - C c = new C(); - } -} diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/castToObject_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/castToObject_after.java deleted file mode 100644 index 0e98b27791c3..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/castToObject_after.java +++ /dev/null @@ -1,6 +0,0 @@ -class C { - { - Object c1 = new C(); - C c = (C) c1; - } -} diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/cast_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/cast_after.java deleted file mode 100644 index 04af17ef8d89..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/cast_after.java +++ /dev/null @@ -1,6 +0,0 @@ -class C { - { - Integer c1 = (Integer) new C(); - C c = (C) c1; - } -} diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/escapePosition.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/escapePosition.java deleted file mode 100644 index f175eb2b1b32..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/escapePosition.java +++ /dev/null @@ -1,5 +0,0 @@ -class C { - { - C c = new C(); - } -} diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/escapePositionIfTyped.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/escapePositionIfTyped.java deleted file mode 100644 index f175eb2b1b32..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/escapePositionIfTyped.java +++ /dev/null @@ -1,5 +0,0 @@ -class C { - { - C c = new C(); - } -} diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/escapePositionIfTyped_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/escapePositionIfTyped_after.java deleted file mode 100644 index 2cbdef758fe1..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/escapePositionIfTyped_after.java +++ /dev/null @@ -1,5 +0,0 @@ -class C { - { - C c = new C(); - } -} diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/escapePosition_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/escapePosition_after.java deleted file mode 100644 index 2cbdef758fe1..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/escapePosition_after.java +++ /dev/null @@ -1,5 +0,0 @@ -class C { - { - C c = new C(); - } -} diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromExpression.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromExpression.java deleted file mode 100644 index ea322fae5609..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromExpression.java +++ /dev/null @@ -1,5 +0,0 @@ -class C { - { - C c = new C(); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromExpression_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromExpression_after.java deleted file mode 100644 index f9a85d873dd3..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/fromExpression_after.java +++ /dev/null @@ -1,6 +0,0 @@ -class C { - { - C expr = new C(); - C c = expr; - } -} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/noWritable.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/noWritable.java deleted file mode 100644 index f577e27bde64..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/noWritable.java +++ /dev/null @@ -1,7 +0,0 @@ -class C { - { - int[] a = new int[1]; - a[1] = 42; - System.out.println(a[1]); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/noWritable_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/noWritable_after.java deleted file mode 100644 index 803af2c8f1da..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/noWritable_after.java +++ /dev/null @@ -1,8 +0,0 @@ -class C { - { - int[] a = new int[1]; - a[1] = 42; - int x = a[1]; - System.out.println(x); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/stopEditing.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/stopEditing.java deleted file mode 100644 index ea322fae5609..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/stopEditing.java +++ /dev/null @@ -1,5 +0,0 @@ -class C { - { - C c = new C(); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/stopEditing_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/stopEditing_after.java deleted file mode 100644 index 26a73045f173..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/stopEditing_after.java +++ /dev/null @@ -1,8 +0,0 @@ -class C { - { - C - - c1 = new C(); - C c = c1; - } -} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/writable.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/writable.java deleted file mode 100644 index f577e27bde64..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/writable.java +++ /dev/null @@ -1,7 +0,0 @@ -class C { - { - int[] a = new int[1]; - a[1] = 42; - System.out.println(a[1]); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/writable_after.java b/java/java-tests/testData/refactoring/inplaceIntroduceVariable/writable_after.java deleted file mode 100644 index 66a0280705f0..000000000000 --- a/java/java-tests/testData/refactoring/inplaceIntroduceVariable/writable_after.java +++ /dev/null @@ -1,8 +0,0 @@ -class C { - { - int[] a = new int[1]; - int x = a[1]; - x = 42; - System.out.println(x); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/introduceVariable/ReturnNonExportedArray.after.java b/java/java-tests/testData/refactoring/introduceVariable/ReturnNonExportedArray.after.java index bcf896651037..ebd5a17ad231 100644 --- a/java/java-tests/testData/refactoring/introduceVariable/ReturnNonExportedArray.after.java +++ b/java/java-tests/testData/refactoring/introduceVariable/ReturnNonExportedArray.after.java @@ -3,6 +3,6 @@ import java.io.File; class Test { java.io.File[] get() {return null;} { - File[] i = get(); + File[] files = get(); } } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/AbstractJavaInplaceIntroduceTest.java b/java/java-tests/testSrc/com/intellij/refactoring/AbstractJavaInplaceIntroduceTest.java index 9cd5d069c875..4f2cb602af67 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/AbstractJavaInplaceIntroduceTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/AbstractJavaInplaceIntroduceTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * 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. @@ -72,10 +72,6 @@ public abstract class AbstractJavaInplaceIntroduceTest extends AbstractInplaceIn @Override protected AbstractInplaceIntroducer invokeRefactoring() { final MyIntroduceHandler introduceHandler = createIntroduceHandler(); - return invokeRefactoring(introduceHandler); - } - - protected AbstractInplaceIntroducer invokeRefactoring(MyIntroduceHandler introduceHandler) { final PsiExpression expression = getExpressionFromEditor(); if (expression != null) { introduceHandler.invokeImpl(LightPlatformTestCase.getProject(), expression, getEditor()); diff --git a/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java b/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java deleted file mode 100644 index 104e3051ae2e..000000000000 --- a/java/java-tests/testSrc/com/intellij/refactoring/InplaceIntroduceVariableTest.java +++ /dev/null @@ -1,233 +0,0 @@ -/* - * 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.refactoring; - -import com.intellij.codeInsight.template.impl.TemplateManagerImpl; -import com.intellij.codeInsight.template.impl.TemplateState; -import com.intellij.openapi.actionSystem.IdeActions; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.editor.actionSystem.EditorActionManager; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Pass; -import com.intellij.psi.PsiExpression; -import com.intellij.psi.PsiLiteralExpression; -import com.intellij.psi.PsiLocalVariable; -import com.intellij.psi.util.PsiTreeUtil; -import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer; -import com.intellij.refactoring.introduce.inplace.OccurrencesChooser; -import com.intellij.refactoring.introduceVariable.IntroduceVariableHandler; -import com.intellij.testFramework.MapDataContext; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTest { - - @Nullable - @Override - protected PsiExpression getExpressionFromEditor() { - final PsiExpression expression = super.getExpressionFromEditor(); - if (expression != null) { - return expression; - } - final PsiExpression expr = PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getCaretModel().getOffset()), PsiExpression.class); - return expr instanceof PsiLiteralExpression ? expr : null; - } - - - public void testFromExpression() throws Exception { - doTest(new Pass() { - @Override - public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) { - type("expr"); - } - }); - } - - public void testCast() throws Exception { - doTestTypeChange("Integer"); - } - - public void testCastToObject() throws Exception { - doTestTypeChange("Object"); - } - - public void testEscapePosition() { - doTestStopEditing(new Pass() { - @Override - public void pass(AbstractInplaceIntroducer introducer) { - invokeEditorAction(IdeActions.ACTION_EDITOR_ESCAPE); - invokeEditorAction(IdeActions.ACTION_EDITOR_ESCAPE); - } - }); - } - - public void testEscapePositionIfTyped() { - doTestStopEditing(new Pass() { - @Override - public void pass(AbstractInplaceIntroducer introducer) { - type("fooBar"); - invokeEditorAction(IdeActions.ACTION_EDITOR_ESCAPE); - } - }); - } - - public void testWritable() throws Exception { - doTestReplaceChoice(OccurrencesChooser.ReplaceChoice.ALL); - } - - public void testNoWritable() throws Exception { - doTestReplaceChoice(OccurrencesChooser.ReplaceChoice.NO_WRITE); - } - - public void testAllInsertFinal() throws Exception { - doTestReplaceChoice(OccurrencesChooser.ReplaceChoice.ALL); - } - - public void testAllIncomplete() throws Exception { - doTestReplaceChoice(OccurrencesChooser.ReplaceChoice.ALL); - } - - public void testStopEditing() { - doTestStopEditing(new Pass() { - @Override - public void pass(AbstractInplaceIntroducer introducer) { - invokeEditorAction(IdeActions.ACTION_EDITOR_MOVE_CARET_LEFT); - invokeEditorAction(IdeActions.ACTION_EDITOR_ENTER); - invokeEditorAction(IdeActions.ACTION_EDITOR_ENTER); - } - }); - } - - private void doTestStopEditing(Pass pass) { - String name = getTestName(true); - configureByFile(getBasePath() + name + getExtension()); - final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled(); - try { - TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - getEditor().getSettings().setVariableInplaceRenameEnabled(true); - - final AbstractInplaceIntroducer introducer = invokeRefactoring(); - pass.pass(introducer); - checkResultByFile(getBasePath() + name + "_after" + getExtension()); - } - finally { - TemplateState state = TemplateManagerImpl.getTemplateState(getEditor()); - if (state != null) { - state.gotoEnd(true); - } - getEditor().getSettings().setVariableInplaceRenameEnabled(enabled); - } - } - - private void doTestTypeChange(final String newType) { - final Pass typeChanger = new Pass() { - @Override - public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) { - type(newType); - } - }; - String name = getTestName(true); - configureByFile(getBasePath() + name + getExtension()); - final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled(); - try { - TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - getEditor().getSettings().setVariableInplaceRenameEnabled(true); - - final AbstractInplaceIntroducer introducer = invokeRefactoring(); - TemplateState state = TemplateManagerImpl.getTemplateState(getEditor()); - assert state != null; - state.previousTab(); - typeChanger.pass(introducer); - state.gotoEnd(false); - checkResultByFile(getBasePath() + name + "_after" + getExtension()); - } - finally { - getEditor().getSettings().setVariableInplaceRenameEnabled(enabled); - } - } - - private void doTestReplaceChoice(OccurrencesChooser.ReplaceChoice choice) { - doTestReplaceChoice(choice, null); - } - - private void doTestReplaceChoice(OccurrencesChooser.ReplaceChoice choice, Pass pass) { - String name = getTestName(true); - configureByFile(getBasePath() + name + getExtension()); - final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled(); - try { - TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - getEditor().getSettings().setVariableInplaceRenameEnabled(true); - - MyIntroduceHandler handler = createIntroduceHandler(); - ((MyIntroduceVariableHandler)handler).setChoice(choice); - final AbstractInplaceIntroducer introducer = invokeRefactoring(handler); - if (pass != null) { - pass.pass(introducer); - } - TemplateState state = TemplateManagerImpl.getTemplateState(getEditor()); - assert state != null; - state.gotoEnd(false); - checkResultByFile(getBasePath() + name + "_after" + getExtension()); - } - finally { - getEditor().getSettings().setVariableInplaceRenameEnabled(enabled); - } - } - - private static void invokeEditorAction(String actionId) { - EditorActionManager.getInstance().getActionHandler(actionId) - .execute(getEditor(), getEditor().getCaretModel().getCurrentCaret(), new MapDataContext()); - } - - - @Override - protected String getBasePath() { - return "/refactoring/inplaceIntroduceVariable/"; - } - - @Override - protected MyIntroduceHandler createIntroduceHandler() { - return new MyIntroduceVariableHandler(); - } - - public static class MyIntroduceVariableHandler extends IntroduceVariableHandler implements MyIntroduceHandler { - private OccurrencesChooser.ReplaceChoice myChoice = null; - - public void setChoice(OccurrencesChooser.ReplaceChoice choice) { - myChoice = choice; - } - - @Override - public boolean invokeImpl(Project project, @NotNull PsiExpression selectedExpr, Editor editor) { - return super.invokeImpl(project, selectedExpr, editor); - } - - @Override - public boolean invokeImpl(Project project, PsiLocalVariable localVariable, Editor editor) { - return super.invokeImpl(project, localVariable, editor); - } - - @Override - protected OccurrencesChooser.ReplaceChoice getOccurrencesChoice() { - return myChoice; - } - - @Override - protected boolean isInplaceAvailableInTestMode() { - return true; - } - } -} diff --git a/platform/lang-impl/src/com/intellij/refactoring/introduce/inplace/AbstractInplaceIntroducer.java b/platform/lang-impl/src/com/intellij/refactoring/introduce/inplace/AbstractInplaceIntroducer.java index f239aba31daa..d16183c7d069 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/introduce/inplace/AbstractInplaceIntroducer.java +++ b/platform/lang-impl/src/com/intellij/refactoring/introduce/inplace/AbstractInplaceIntroducer.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * 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. @@ -510,7 +510,6 @@ public abstract class AbstractInplaceIntroducer pass) { String name = getTestName(true); configureByFile(getBasePath() + name + getExtension()); final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled(); @@ -41,10 +37,7 @@ public abstract class AbstractInplaceIntroduceTest extends LightPlatformCodeInsi TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); getEditor().getSettings().setVariableInplaceRenameEnabled(true); - final AbstractInplaceIntroducer introducer = invokeRefactoring(); - if (pass != null) { - pass.pass(introducer); - } + invokeRefactoring(); TemplateState state = TemplateManagerImpl.getTemplateState(getEditor()); assert state != null; state.gotoEnd(true);