clear read-only status outside write action in Surround With

This commit is contained in:
peter
2016-11-28 21:57:51 +01:00
parent 512485600d
commit c36a7264ac
5 changed files with 30 additions and 62 deletions
@@ -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);
@@ -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);
}
@@ -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));
}
}
@@ -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)
}
}
@@ -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);
}
}