inplace rename: no need to revert to continue editing non-identifier; no need to...
authoranna <Anna.Kozlova@jetbrains.com>
Wed, 18 Jan 2012 13:04:19 +0000 (14:04 +0100)
committeranna <Anna.Kozlova@jetbrains.com>
Wed, 18 Jan 2012 13:16:05 +0000 (14:16 +0100)
EA-33094 - assert: InplaceRefactoring$$.run

platform/lang-impl/src/com/intellij/refactoring/introduce/inplace/AbstractInplaceIntroducer.java
platform/lang-impl/src/com/intellij/refactoring/introduce/inplace/InplaceVariableIntroducer.java
platform/lang-impl/src/com/intellij/refactoring/rename/inplace/InplaceRefactoring.java
platform/lang-impl/src/com/intellij/refactoring/rename/inplace/MemberInplaceRenamer.java
platform/lang-impl/src/com/intellij/refactoring/rename/inplace/VariableInplaceRenamer.java

index fe3dcfdfb65211c31545b0fdcc045410c176dc02..90b723124a23c8b31286808c385d970ee9532901 100644 (file)
@@ -246,7 +246,7 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
         }
         result.set(started);
         if (!started) {
-          finish();
+          finish(true);
         }
       }
 
@@ -316,7 +316,7 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
 
 
   @Override
-  public void finish() {
+  public void finish(boolean success) {
     myFinished = true;
     final TemplateState templateState = TemplateManagerImpl.getTemplateState(myEditor);
     if (templateState != null) {
@@ -328,13 +328,15 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
     if (myBalloon == null) {
       releaseIfNotRestart();
     }
-    super.finish();
-    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
-    final V variable = getVariable();
-    if (variable == null) {
-      return;
+    super.finish(success);
+    if (success) {
+      PsiDocumentManager.getInstance(myProject).commitAllDocuments();
+      final V variable = getVariable();
+      if (variable == null) {
+        return;
+      }
+      restoreState(variable);
     }
-    restoreState(variable);
   }
 
   @Override
@@ -547,7 +549,7 @@ public abstract class AbstractInplaceIntroducer<V extends PsiNameIdentifierOwner
 
   @Override
   protected void navigateToAlreadyStarted(Document oldDocument, int exitCode) {
-    finish();
+    finish(true);
     super.navigateToAlreadyStarted(oldDocument, exitCode);
   }
 
index fe80dc840697bc808fd5ff2dcc9824b0c4838f13..db458a9b5b269a7f02da66d7fab73835ea234272 100644 (file)
@@ -62,7 +62,7 @@ public abstract class InplaceVariableIntroducer<E extends PsiElement> extends In
   protected Balloon myBalloon;
   protected String myTitle;
   protected RelativePoint myTarget;
-  private RangeMarker myCaretRangeMarker;
 
   public InplaceVariableIntroducer(PsiNamedElement elementToRename,
                                    Editor editor,
@@ -88,11 +88,6 @@ public abstract class InplaceVariableIntroducer<E extends PsiElement> extends In
   }
 
   @Override
-  protected int restoreCaretOffset(int offset) {
-    return myCaretRangeMarker.isValid() ? myCaretRangeMarker.getStartOffset() : offset;
-  }
-
-  @Override
   protected StartMarkAction startRename() throws StartMarkAction.AlreadyStartedException {
     return null;
   }
@@ -148,12 +143,6 @@ public abstract class InplaceVariableIntroducer<E extends PsiElement> extends In
   }
 
   @Override
-  protected void beforeTemplateStart() {
-    myCaretRangeMarker = myEditor.getDocument()
-      .createRangeMarker(new TextRange(myEditor.getCaretModel().getOffset(), myEditor.getCaretModel().getOffset()));
-  }
-
-  @Override
   protected void collectAdditionalElementsToRename(List<Pair<PsiElement, TextRange>> stringUsages) {
   }
 
@@ -181,9 +170,6 @@ public abstract class InplaceVariableIntroducer<E extends PsiElement> extends In
         marker.dispose();
       }
     }
-    if (myCaretRangeMarker != null) {
-      myCaretRangeMarker.dispose();
-    }
     if (myExprMarker != null && !isRestart()) {
       myExprMarker.dispose();
     }
@@ -233,8 +219,8 @@ public abstract class InplaceVariableIntroducer<E extends PsiElement> extends In
   }
 
   @Override
-  public void finish() {
-    super.finish();
+  public void finish(boolean success) {
+    super.finish(success);
     if (myBalloon != null) {
       final Boolean isRestart = myEditor.getUserData(INTRODUCE_RESTART);
       if (isRestart == null || !isRestart.booleanValue()) {
index 52c35c5c8e920c2600da370f2bf763b161409dcc..08a6cbf6eee0391b56d5caf766caca4ad32b9990 100644 (file)
@@ -101,6 +101,8 @@ public abstract class InplaceRefactoring {
 
   protected StartMarkAction myMarkAction;
   protected PsiElement myScope;
+  
+  private RangeMarker myCaretRangeMarker;
 
   public InplaceRefactoring(Editor editor, PsiNamedElement elementToRename, Project project) {
     this(editor, elementToRename, project, elementToRename != null ? elementToRename.getName() : null,
@@ -256,7 +258,10 @@ public abstract class InplaceRefactoring {
     return true;
   }
 
-  protected abstract void beforeTemplateStart();
+  protected void beforeTemplateStart() {
+    myCaretRangeMarker = myEditor.getDocument()
+          .createRangeMarker(new TextRange(myEditor.getCaretModel().getOffset(), myEditor.getCaretModel().getOffset()));
+  }
 
   private void startTemplate(final TemplateBuilderImpl builder) {
 
@@ -343,7 +348,7 @@ public abstract class InplaceRefactoring {
   }
 
   protected int restoreCaretOffset(int offset) {
-    return offset;
+    return myCaretRangeMarker.isValid() ? myCaretRangeMarker.getStartOffset() : offset;
   }
 
   protected void navigateToAlreadyStarted(Document oldDocument, int exitCode) {
@@ -415,6 +420,9 @@ public abstract class InplaceRefactoring {
    * @param success true if the refactoring was accepted, false if it was cancelled (by undo or Esc)
    */
   protected void moveOffsetAfter(boolean success) {
+    if (myCaretRangeMarker != null) {
+      myCaretRangeMarker.dispose();
+    }
   }
 
   protected void addAdditionalVariables(TemplateBuilderImpl builder) {
@@ -480,7 +488,7 @@ public abstract class InplaceRefactoring {
    */
   protected abstract String getCommandName();
 
-  public void finish() {
+  public void finish(boolean success) {
     if (!ourRenamersStack.isEmpty() && ourRenamersStack.peek() == this) {
       ourRenamersStack.pop();
     }
@@ -672,7 +680,7 @@ public abstract class InplaceRefactoring {
         if (myBeforeRevert != null) {
           myBeforeRevert.setGreedyToRight(true);
         }
-        finish();
+        finish(true);
       }
       finally {
         restoreDaemonUpdateState();
@@ -703,7 +711,7 @@ public abstract class InplaceRefactoring {
       try {
         final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
         documentManager.commitAllDocuments();
-        finish();
+        finish(false);
         moveOffsetAfter(false);
         ApplicationManager.getApplication().runWriteAction(new Runnable() {
           public void run() {
index 58bf29a516cb9501f68341b757ffd3dc6ab6e6d0..39358467cd7442940f2a73763fb9b0f882a68116 100644 (file)
@@ -220,9 +220,11 @@ public class MemberInplaceRenamer extends VariableInplaceRenamer {
   }
 
   @Override
-  public void finish() {
-    super.finish();
-    revertState();
+  public void finish(boolean success) {
+    super.finish(success);
+    if (success) {
+      revertState();
+    }
   }
 
   @Nullable
index 0c5fdc8cb06251c64308aefcff615308e3f511ed..e93ced975855b84a23aeca8a9fdb5ca67c1b84be 100644 (file)
@@ -132,6 +132,7 @@ public class VariableInplaceRenamer extends InplaceRefactoring {
 
   @Override
   protected void beforeTemplateStart() {
+    super.beforeTemplateStart();
     myLanguage = myScope.getLanguage();
     final ResolveSnapshotProvider resolveSnapshotProvider = INSTANCE.forLanguage(myLanguage);
     mySnapshot = resolveSnapshotProvider != null ? resolveSnapshotProvider.createSnapshot(myScope) : null;
@@ -163,14 +164,18 @@ public class VariableInplaceRenamer extends InplaceRefactoring {
   }
 
   protected void performOnInvalidIdentifier(final String newName, final LinkedHashSet<String> nameSuggestions) {
-    revertState();
-    JBPopupFactory.getInstance()
-      .createConfirmation("Inserted identifier is not valid", "Continue editing", "Cancel", new Runnable() {
-        @Override
-        public void run() {
-          createInplaceRenamerToRestart(getVariable(), myEditor, newName).performInplaceRefactoring(nameSuggestions);
-        }
-      }, 0).showInBestPositionFor(myEditor);
+    final PsiNamedElement variable = getVariable();
+    if (variable != null) {
+      final int offset = variable.getTextOffset();
+      restoreCaretOffset(offset);
+      JBPopupFactory.getInstance()
+        .createConfirmation("Inserted identifier is not valid", "Continue editing", "Cancel", new Runnable() {
+          @Override
+          public void run() {
+            createInplaceRenamerToRestart(variable, myEditor, newName).performInplaceRefactoring(nameSuggestions);
+          }
+        }, 0).showInBestPositionFor(myEditor);
+    }
   }
 
   protected void performRefactoringRename(final String newName,