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;
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");
}
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);
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;
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;
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);
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;
}
}
}
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
@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));
}
}
* 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
/**
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)
}
}
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;
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);
}
}