mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
reject to refactor when inplace refactoring is not ended
(cherry picked from commit 3fa8012435e9832dbd51fa8e67b903bbbe7dafdb)
This commit is contained in:
+6
@@ -26,6 +26,7 @@ import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.VariableKind;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.JavaRefactoringSettings;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
|
||||
import com.intellij.refactoring.util.occurrences.OccurrenceManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -222,6 +223,11 @@ public class InplaceIntroduceConstantPopup extends AbstractInplaceIntroduceField
|
||||
return super.performRefactoring();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean startsOnTheSameElement(RefactoringActionHandler handler, PsiElement element) {
|
||||
return super.startsOnTheSameElement(handler, element) && handler instanceof IntroduceConstantHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void performIntroduce() {
|
||||
final BaseExpressionToFieldHandler.Settings settings =
|
||||
|
||||
+6
@@ -27,6 +27,7 @@ import com.intellij.psi.codeStyle.VariableKind;
|
||||
import com.intellij.psi.util.PsiExpressionTrimRenderer;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.JavaRefactoringSettings;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
|
||||
import com.intellij.refactoring.util.occurrences.OccurrenceManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -164,6 +165,11 @@ public class InplaceIntroduceFieldPopup extends AbstractInplaceIntroduceFieldPop
|
||||
myIntroduceFieldPanel.saveFinalState();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean startsOnTheSameElement(RefactoringActionHandler handler, PsiElement element) {
|
||||
return super.startsOnTheSameElement(handler, element) && handler instanceof IntroduceFieldHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JComponent getComponent() {
|
||||
myIntroduceFieldPanel.addOccurrenceListener(new ItemListener() {
|
||||
|
||||
-4
@@ -117,10 +117,6 @@ public class IntroduceConstantHandler extends BaseExpressionToFieldHandler {
|
||||
|
||||
final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
|
||||
if (activeIntroducer != null) {
|
||||
if (!(activeIntroducer instanceof InplaceIntroduceConstantPopup) || !activeIntroducer.startsOnTheSameElement(expr, localVariable)) {
|
||||
AbstractInplaceIntroducer.unableToStartWarning(project, editor, activeIntroducer);
|
||||
return null;
|
||||
}
|
||||
activeIntroducer.stopIntroduce(editor);
|
||||
expr = (PsiExpression)activeIntroducer.getExpr();
|
||||
localVariable = (PsiLocalVariable)activeIntroducer.getLocalVariable();
|
||||
|
||||
@@ -88,10 +88,6 @@ public class IntroduceFieldHandler extends BaseExpressionToFieldHandler {
|
||||
String enteredName = null;
|
||||
boolean replaceAll = false;
|
||||
if (activeIntroducer != null) {
|
||||
if (!(activeIntroducer instanceof InplaceIntroduceFieldPopup) || !activeIntroducer.startsOnTheSameElement(expr, localVariable)) {
|
||||
AbstractInplaceIntroducer.unableToStartWarning(project, editor, activeIntroducer);
|
||||
return null;
|
||||
}
|
||||
activeIntroducer.stopIntroduce(editor);
|
||||
expr = (PsiExpression)activeIntroducer.getExpr();
|
||||
localVariable = (PsiLocalVariable)activeIntroducer.getLocalVariable();
|
||||
|
||||
+6
-2
@@ -29,15 +29,14 @@ import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.VariableKind;
|
||||
import com.intellij.psi.search.LocalSearchScope;
|
||||
import com.intellij.psi.search.SearchScope;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.JavaRefactoringSettings;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.intellij.refactoring.ui.TypeSelectorManagerImpl;
|
||||
import com.intellij.usageView.UsageInfo;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import gnu.trove.TIntArrayList;
|
||||
import gnu.trove.TIntProcedure;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -171,6 +170,11 @@ public class InplaceIntroduceParameterPopup extends AbstractJavaInplaceIntroduce
|
||||
return getParameter();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean startsOnTheSameElement(RefactoringActionHandler handler, PsiElement element) {
|
||||
return super.startsOnTheSameElement(handler, element) && handler instanceof IntroduceParameterHandler;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void saveSettings(@NotNull PsiVariable psiVariable) {
|
||||
|
||||
-4
@@ -388,10 +388,6 @@ public class IntroduceParameterHandler extends IntroduceHandlerBase {
|
||||
|
||||
final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(myEditor);
|
||||
if (activeIntroducer != null) {
|
||||
if (!(activeIntroducer instanceof InplaceIntroduceParameterPopup) || !activeIntroducer.startsOnTheSameElement(myExpr, myLocalVar)) {
|
||||
AbstractInplaceIntroducer.unableToStartWarning(myProject, myEditor, activeIntroducer);
|
||||
return;
|
||||
}
|
||||
activeIntroducer.stopIntroduce(myEditor);
|
||||
myExpr = (PsiExpression)activeIntroducer.getExpr();
|
||||
myLocalVar = (PsiLocalVariable)activeIntroducer.getLocalVariable();
|
||||
|
||||
@@ -29,6 +29,7 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiUtilBase;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.rename.inplace.InplaceRefactoring;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -81,6 +82,12 @@ public abstract class BaseRefactoringAction extends AnAction {
|
||||
"error.wrong.caret.position.symbol.to.refactor")), RefactoringBundle.getCannotRefactorMessage(null), null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!InplaceRefactoring.canStartAnotherRefactoring(editor, project, handler, elements)) {
|
||||
InplaceRefactoring.unableToStartWarning(project, editor);
|
||||
return;
|
||||
}
|
||||
|
||||
IdeEventQueue.getInstance().setEventCount(eventCount);
|
||||
if (editor != null) {
|
||||
final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
|
||||
|
||||
-8
@@ -42,9 +42,7 @@ import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.rename.inplace.InplaceRefactoring;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.ui.DottedBorder;
|
||||
import com.intellij.util.ui.PositionTracker;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -617,12 +615,6 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
|
||||
return isRestart();
|
||||
}
|
||||
|
||||
public static void unableToStartWarning(Project project, Editor editor, AbstractInplaceIntroducer introducer) {
|
||||
String message = RefactoringBundle
|
||||
.getCannotRefactorMessage(introducer.getCommandName() + " is not finished yet. Unable to start a refactoring");
|
||||
CommonRefactoringUtil.showErrorHint(project, editor, message, null, null);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static AbstractInplaceIntroducer getActiveIntroducer(@Nullable Editor editor) {
|
||||
if (editor == null) return null;
|
||||
|
||||
+35
-4
@@ -68,6 +68,8 @@ import com.intellij.psi.search.PsiSearchHelper;
|
||||
import com.intellij.psi.search.SearchScope;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.ui.awt.RelativePoint;
|
||||
import com.intellij.util.CommonProcessors;
|
||||
@@ -143,6 +145,21 @@ public abstract class InplaceRefactoring {
|
||||
}
|
||||
}
|
||||
|
||||
public static void unableToStartWarning(Project project, Editor editor) {
|
||||
final StartMarkAction startMarkAction = StartMarkAction.canStart(project);
|
||||
final String message = startMarkAction.getCommandName() + " is not finished yet.";
|
||||
final Document oldDocument = startMarkAction.getDocument();
|
||||
if (editor == null || oldDocument != editor.getDocument()) {
|
||||
final int exitCode = Messages.showYesNoDialog(project, message,
|
||||
RefactoringBundle.getCannotRefactorMessage(null),
|
||||
"Continue Started", "Cancel Started", Messages.getErrorIcon());
|
||||
navigateToStarted(oldDocument, project, exitCode);
|
||||
}
|
||||
else {
|
||||
CommonRefactoringUtil.showErrorHint(project, editor, message, RefactoringBundle.getCannotRefactorMessage(null), null);
|
||||
}
|
||||
}
|
||||
|
||||
public void setAdvertisementText(String advertisementText) {
|
||||
myAdvertisementText = advertisementText;
|
||||
}
|
||||
@@ -391,11 +408,15 @@ public abstract class InplaceRefactoring {
|
||||
}
|
||||
|
||||
protected void navigateToAlreadyStarted(Document oldDocument, int exitCode) {
|
||||
final PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(oldDocument);
|
||||
navigateToStarted(oldDocument, myProject, exitCode);
|
||||
}
|
||||
|
||||
private static void navigateToStarted(final Document oldDocument, final Project project, final int exitCode) {
|
||||
final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(oldDocument);
|
||||
if (file != null) {
|
||||
final VirtualFile virtualFile = file.getVirtualFile();
|
||||
if (virtualFile != null) {
|
||||
final FileEditor[] editors = FileEditorManager.getInstance(myProject).getEditors(virtualFile);
|
||||
final FileEditor[] editors = FileEditorManager.getInstance(project).getEditors(virtualFile);
|
||||
for (FileEditor editor : editors) {
|
||||
if (editor instanceof TextEditor) {
|
||||
final Editor textEditor = ((TextEditor)editor).getEditor();
|
||||
@@ -404,11 +425,11 @@ public abstract class InplaceRefactoring {
|
||||
if (exitCode == DialogWrapper.OK_EXIT_CODE) {
|
||||
final TextRange range = templateState.getVariableRange(PRIMARY_VARIABLE_NAME);
|
||||
if (range != null) {
|
||||
new OpenFileDescriptor(myProject, virtualFile, range.getStartOffset()).navigate(true);
|
||||
new OpenFileDescriptor(project, virtualFile, range.getStartOffset()).navigate(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
else if (exitCode > 0){
|
||||
templateState.gotoEnd();
|
||||
return;
|
||||
}
|
||||
@@ -666,6 +687,16 @@ public abstract class InplaceRefactoring {
|
||||
return isRestart != null && isRestart;
|
||||
}
|
||||
|
||||
public static boolean canStartAnotherRefactoring(Editor editor, Project project, RefactoringActionHandler handler, PsiElement... element) {
|
||||
final InplaceRefactoring inplaceRefactoring = editor != null ? editor.getUserData(INPLACE_RENAMER) : null;
|
||||
return StartMarkAction.canStart(project) == null ||
|
||||
(inplaceRefactoring != null && element.length == 1 && inplaceRefactoring.startsOnTheSameElement(handler, element[0]));
|
||||
}
|
||||
|
||||
protected boolean startsOnTheSameElement(RefactoringActionHandler handler, PsiElement element) {
|
||||
return getVariable() == element;
|
||||
}
|
||||
|
||||
protected void releaseResources() {
|
||||
}
|
||||
|
||||
|
||||
+6
@@ -35,6 +35,7 @@ import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
|
||||
import com.intellij.psi.util.PsiUtilCore;
|
||||
import com.intellij.refactoring.RefactoringActionHandler;
|
||||
import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.listeners.RefactoringElementListener;
|
||||
import com.intellij.refactoring.rename.AutomaticRenamingDialog;
|
||||
@@ -85,6 +86,11 @@ public class VariableInplaceRenamer extends InplaceRefactoring {
|
||||
super(editor, elementToRename, project, initialName, oldName);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean startsOnTheSameElement(RefactoringActionHandler handler, PsiElement element) {
|
||||
return super.startsOnTheSameElement(handler, element) && handler instanceof VariableInplaceRenameHandler;
|
||||
}
|
||||
|
||||
public boolean performInplaceRename() {
|
||||
return performInplaceRefactoring(null);
|
||||
}
|
||||
|
||||
@@ -65,6 +65,10 @@ public class StartMarkAction extends BasicUndoableAction {
|
||||
myCommandName = commandName;
|
||||
}
|
||||
|
||||
public Document getDocument() {
|
||||
return myDocument;
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
public static void checkCleared() {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user