clear read-only status outside write action in Surround With
authorpeter <peter@jetbrains.com>
Mon, 28 Nov 2016 20:55:43 +0000 (21:55 +0100)
committerpeter <peter@jetbrains.com>
Mon, 28 Nov 2016 20:57:51 +0000 (21:57 +0100)
java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java
json/tests/test/com/intellij/json/JsonSurroundWithTest.java
platform/lang-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithHandler.java
plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundTestCase.groovy
python/testSrc/com/jetbrains/python/PySurroundWithTest.java

index 5fa1417cb1c41eaede3ba60be8250116c803f20c..9477d56dfee465f5e974b140edd37083cddc6955 100644 (file)
@@ -25,7 +25,6 @@ import com.intellij.lang.LanguageSurrounders;
 import com.intellij.lang.java.JavaLanguage;
 import com.intellij.lang.surroundWith.SurroundDescriptor;
 import com.intellij.lang.surroundWith.Surrounder;
-import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.editor.SelectionModel;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.psi.PsiElement;
@@ -221,7 +220,7 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase {
     PsiElement[] elements = item.getElementsToSurround(getFile(), selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
     assertTrue(surrounder.isApplicable(elements));
 
-    ApplicationManager.getApplication().runWriteAction(() -> SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder));
+    SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder);
 
     checkResultByFile(BASE_PATH + fileName + "_after.java");
   }
@@ -229,7 +228,7 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase {
   private void doTestWithTemplateFinish(@NotNull String fileName, Surrounder surrounder, @Nullable String textToType) {
     TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
     configureByFile(BASE_PATH + fileName + ".java");
-    ApplicationManager.getApplication().runWriteAction(() -> SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder));
+    SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder);
 
     if (textToType != null) {
       type(textToType);
index 9502ff6e51fc939313c53a1d1fdf8f0b935ac6aa..59d9cf9e0048efe45a7faa55a51a27cc7e14706e 100644 (file)
@@ -2,7 +2,6 @@ package com.intellij.json;
 
 import com.intellij.codeInsight.generation.surroundWith.SurroundWithHandler;
 import com.intellij.json.surroundWith.JsonWithObjectLiteralSurrounder;
-import com.intellij.openapi.command.WriteCommandAction;
 
 /**
  * @author Mikhail Golubev
@@ -10,12 +9,7 @@ import com.intellij.openapi.command.WriteCommandAction;
 public class JsonSurroundWithTest extends JsonTestCase {
   private void doTest() {
     myFixture.configureByFile("/surround/" + getTestName(false) + ".json");
-    new WriteCommandAction.Simple(myFixture.getProject()) {
-      @Override
-      protected void run() {
-        SurroundWithHandler.invoke(myFixture.getProject(), myFixture.getEditor(), myFixture.getFile(), new JsonWithObjectLiteralSurrounder());
-      }
-    }.execute();
+    SurroundWithHandler.invoke(myFixture.getProject(), myFixture.getEditor(), myFixture.getFile(), new JsonWithObjectLiteralSurrounder());
     myFixture.checkResultByFile("/surround/" + getTestName(false) + "_after.json", true);
   }
 
index 5455740aa70340faf927dd95546edf358a4b2129..7e89da532788ea45f26fed2f73fa3dd75bec948d 100644 (file)
@@ -34,9 +34,7 @@ import com.intellij.lang.surroundWith.SurroundDescriptor;
 import com.intellij.lang.surroundWith.Surrounder;
 import com.intellij.openapi.actionSystem.*;
 import com.intellij.openapi.application.ApplicationManager;
-import com.intellij.openapi.application.Result;
 import com.intellij.openapi.command.WriteCommandAction;
-import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.editor.LogicalPosition;
 import com.intellij.openapi.editor.ScrollType;
@@ -52,7 +50,6 @@ import com.intellij.psi.PsiDocumentManager;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiFile;
 import com.intellij.refactoring.rename.inplace.InplaceRefactoring;
-import com.intellij.util.IncorrectOperationException;
 import com.intellij.util.containers.ContainerUtil;
 import com.intellij.util.ui.UIUtil;
 import org.jetbrains.annotations.NotNull;
@@ -61,7 +58,6 @@ import org.jetbrains.annotations.Nullable;
 import java.util.*;
 
 public class SurroundWithHandler implements CodeInsightActionHandler {
-  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.generation.surroundWith.SurroundWithHandler");
   private static final String CHOOSER_TITLE = CodeInsightBundle.message("surround.with.chooser.title");
   public static final TextRange CARET_IS_OK = new TextRange(0, 0);
 
@@ -172,7 +168,7 @@ public class SurroundWithHandler implements CodeInsightActionHandler {
       if (elements.length > 0) {
         for (Surrounder descriptorSurrounder : descriptor.getSurrounders()) {
           if (surrounder.getClass().equals(descriptorSurrounder.getClass())) {
-            doSurround(project, editor, surrounder, elements);
+            WriteCommandAction.runWriteCommandAction(project, () -> doSurround(project, editor, surrounder, elements));
             return;
           }
         }
@@ -188,37 +184,28 @@ public class SurroundWithHandler implements CodeInsightActionHandler {
   }
 
   static void doSurround(final Project project, final Editor editor, final Surrounder surrounder, final PsiElement[] elements) {
-    if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) {
-      return;
+    PsiDocumentManager.getInstance(project).commitAllDocuments();
+    int col = editor.getCaretModel().getLogicalPosition().column;
+    int line = editor.getCaretModel().getLogicalPosition().line;
+    if (!editor.getCaretModel().supportsMultipleCarets()) {
+      LogicalPosition pos = new LogicalPosition(0, 0);
+      editor.getCaretModel().moveToLogicalPosition(pos);
     }
-
-    try {
-      PsiDocumentManager.getInstance(project).commitAllDocuments();
-      int col = editor.getCaretModel().getLogicalPosition().column;
-      int line = editor.getCaretModel().getLogicalPosition().line;
-      if (!editor.getCaretModel().supportsMultipleCarets()) {
-        LogicalPosition pos = new LogicalPosition(0, 0);
-        editor.getCaretModel().moveToLogicalPosition(pos);
+    TextRange range = surrounder.surroundElements(project, editor, elements);
+    if (range != CARET_IS_OK) {
+      if (TemplateManager.getInstance(project).getActiveTemplate(editor) == null &&
+          InplaceRefactoring.getActiveInplaceRenamer(editor) == null) {
+        LogicalPosition pos1 = new LogicalPosition(line, col);
+        editor.getCaretModel().moveToLogicalPosition(pos1);
       }
-      TextRange range = surrounder.surroundElements(project, editor, elements);
-      if (range != CARET_IS_OK) {
-        if (TemplateManager.getInstance(project).getActiveTemplate(editor) == null &&
-            InplaceRefactoring.getActiveInplaceRenamer(editor) == null) {
-          LogicalPosition pos1 = new LogicalPosition(line, col);
-          editor.getCaretModel().moveToLogicalPosition(pos1);
-        }
-        if (range != null) {
-          int offset = range.getStartOffset();
-          editor.getCaretModel().removeSecondaryCarets();
-          editor.getCaretModel().moveToOffset(offset);
-          editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
-          editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
-        }
+      if (range != null) {
+        int offset = range.getStartOffset();
+        editor.getCaretModel().removeSecondaryCarets();
+        editor.getCaretModel().moveToOffset(offset);
+        editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
+        editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
       }
     }
-    catch (IncorrectOperationException e) {
-      LOG.error(e);
-    }
   }
 
   @Nullable
@@ -293,12 +280,11 @@ public class SurroundWithHandler implements CodeInsightActionHandler {
 
     @Override
     public void actionPerformed(AnActionEvent e) {
-      new WriteCommandAction(myProject) {
-        @Override
-        protected void run(@NotNull Result result) throws Exception {
-          doSurround(myProject, myEditor, mySurrounder, myElements);
-        }
-      }.execute();
+      if (!FileDocumentManager.getInstance().requestWriting(myEditor.getDocument(), myProject)) {
+        return;
+      }
+
+      WriteCommandAction.runWriteCommandAction(myProject, () -> doSurround(myProject, myEditor, mySurrounder, myElements));
     }
   }
 
index d88187b5b46ffe5896dafaf655d0e827efdb57bb..5a5406ca4025a1ef6c5c50b0d9bc97a1d3e95560 100644 (file)
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 package org.jetbrains.plugins.groovy.lang.surroundWith
+
 import com.intellij.codeInsight.generation.surroundWith.SurroundWithHandler
 import com.intellij.lang.surroundWith.Surrounder
-import com.intellij.openapi.command.WriteCommandAction
 import org.jetbrains.plugins.groovy.LightGroovyTestCase
 import org.jetbrains.plugins.groovy.util.TestUtils
 /**
@@ -30,12 +30,7 @@ abstract class SurroundTestCase extends LightGroovyTestCase {
 
   protected void doTest(final Surrounder surrounder, String textBefore, String textAfter) {
     myFixture.configureByText("a.groovy", textBefore)
-
-    WriteCommandAction.runWriteCommandAction project, {
-      SurroundWithHandler.invoke(project, myFixture.editor, myFixture.file, surrounder)
-      doPostponedFormatting(project)
-    }
-
+    SurroundWithHandler.invoke(project, myFixture.editor, myFixture.file, surrounder)
     myFixture.checkResult(textAfter)
   }
 }
index 2af16ff92edcc893a09f692ebb824f3e55b23a0e..af9e2e6ee2d3743960adb486b295e36d69beb260 100644 (file)
@@ -18,7 +18,6 @@ package com.jetbrains.python;
 import com.intellij.codeInsight.generation.surroundWith.SurroundWithHandler;
 import com.intellij.lang.folding.CustomFoldingSurroundDescriptor;
 import com.intellij.lang.surroundWith.Surrounder;
-import com.intellij.openapi.command.WriteCommandAction;
 import com.intellij.openapi.util.Condition;
 import com.intellij.util.containers.ContainerUtil;
 import com.jetbrains.python.fixtures.PyTestCase;
@@ -87,12 +86,7 @@ public class PySurroundWithTest extends PyTestCase {
   private void doTest(final Surrounder surrounder) throws Exception {
     String baseName = "/surround/" + getTestName(false);
     myFixture.configureByFile(baseName + ".py");
-    new WriteCommandAction.Simple(myFixture.getProject()) {
-      @Override
-      protected void run() throws Throwable {
-        SurroundWithHandler.invoke(myFixture.getProject(), myFixture.getEditor(), myFixture.getFile(), surrounder);
-      }
-    }.execute();
+    SurroundWithHandler.invoke(myFixture.getProject(), myFixture.getEditor(), myFixture.getFile(), surrounder);
     myFixture.checkResultByFile(baseName + "_after.py", true);
   }
 }