Search in sources :

Example 41 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.

the class CreateClassFromNewFix method invokeImpl.

@Override
protected void invokeImpl(PsiClass targetClass) {
    assert ApplicationManager.getApplication().isWriteAccessAllowed();
    final Project project = targetClass.getProject();
    TransactionGuard.getInstance().submitTransactionLater(project, () -> {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        final PsiNewExpression newExpression = getNewExpression();
        if (newExpression == null) {
            return;
        }
        final PsiJavaCodeReferenceElement referenceElement = getReferenceElement(newExpression);
        final PsiClass[] psiClass = new PsiClass[1];
        CommandProcessor.getInstance().executeCommand(newExpression.getProject(), () -> psiClass[0] = CreateFromUsageUtils.createClass(referenceElement, CreateClassKind.CLASS, null), getText(), getText());
        new WriteCommandAction(project, getText(), getText()) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                setupClassFromNewExpression(psiClass[0], newExpression);
            }
        }.execute();
    });
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) Result(com.intellij.openapi.application.Result)

Example 42 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.

the class ImplementAbstractClassMethodsFix method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") final Editor editor, @NotNull final PsiElement startElement, @NotNull PsiElement endElement) {
    if (editor == null)
        return;
    PsiJavaCodeReferenceElement classReference = ((PsiNewExpression) startElement).getClassReference();
    if (classReference == null)
        return;
    final PsiClass psiClass = (PsiClass) classReference.resolve();
    if (psiClass == null)
        return;
    final MemberChooser<PsiMethodMember> chooser = chooseMethodsToImplement(editor, startElement, psiClass, false);
    if (chooser == null)
        return;
    final List<PsiMethodMember> selectedElements = chooser.getSelectedElements();
    if (selectedElements == null || selectedElements.isEmpty())
        return;
    new WriteCommandAction(project, file) {

        @Override
        protected void run(@NotNull final Result result) throws Throwable {
            PsiNewExpression newExpression = (PsiNewExpression) JavaPsiFacade.getElementFactory(project).createExpressionFromText(startElement.getText() + "{}", startElement);
            newExpression = (PsiNewExpression) startElement.replace(newExpression);
            final PsiClass psiClass = newExpression.getAnonymousClass();
            if (psiClass == null)
                return;
            Map<PsiClass, PsiSubstitutor> subst = new HashMap<>();
            for (PsiMethodMember selectedElement : selectedElements) {
                final PsiClass baseClass = selectedElement.getElement().getContainingClass();
                if (baseClass != null) {
                    PsiSubstitutor substitutor = subst.get(baseClass);
                    if (substitutor == null) {
                        substitutor = TypeConversionUtil.getSuperClassSubstitutor(baseClass, psiClass, PsiSubstitutor.EMPTY);
                        subst.put(baseClass, substitutor);
                    }
                    selectedElement.setSubstitutor(substitutor);
                }
            }
            OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor, psiClass, selectedElements, chooser.isCopyJavadoc(), chooser.isInsertOverrideAnnotation());
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PsiMethodMember(com.intellij.codeInsight.generation.PsiMethodMember) Result(com.intellij.openapi.application.Result) HashMap(java.util.HashMap) Map(java.util.Map)

Example 43 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project intellij-community by JetBrains.

the class RangeMarkerTest method testRandomEdit_NoCommand.

public void testRandomEdit_NoCommand() {
    final int N = 100;
    final Random gen = new Random();
    int N_TRIES = Timings.adjustAccordingToMySpeed(7000, false);
    System.out.println("N_TRIES = " + N_TRIES);
    DocumentEx document = null;
    for (int tryn = 0; tryn < N_TRIES; tryn++) {
        ((UndoManagerImpl) UndoManager.getInstance(getProject())).flushCurrentCommandMerger();
        ((UndoManagerImpl) UndoManager.getGlobalInstance()).flushCurrentCommandMerger();
        if (document != null) {
            ((UndoManagerImpl) UndoManager.getInstance(getProject())).clearUndoRedoQueueInTests(document);
            ((UndoManagerImpl) UndoManager.getGlobalInstance()).clearUndoRedoQueueInTests(document);
        }
        if (tryn % 10000 == 0) {
            System.out.println(tryn);
        }
        document = (DocumentEx) EditorFactory.getInstance().createDocument(StringUtil.repeatSymbol(' ', N));
        final DocumentEx finalDocument = document;
        new WriteCommandAction(getProject()) {

            @Override
            protected void run(@NotNull Result result) throws Exception {
                List<Pair<RangeMarker, TextRange>> adds = new ArrayList<>();
                List<Pair<RangeMarker, TextRange>> dels = new ArrayList<>();
                List<Trinity<Integer, Integer, Integer>> edits = new ArrayList<>();
                try {
                    for (int i = 0; i < 30; i++) {
                        int x = gen.nextInt(N);
                        int y = x + gen.nextInt(N - x);
                        RangeMarkerEx r = (RangeMarkerEx) finalDocument.createRangeMarker(x, y);
                        adds.add(Pair.create(r, TextRange.create(r)));
                    }
                    for (int i = 0; i < 10; i++) {
                        int offset = gen.nextInt(finalDocument.getTextLength());
                        if (gen.nextBoolean()) {
                            int length = gen.nextInt(5);
                            edits.add(Trinity.create(offset, 0, length));
                            finalDocument.insertString(offset, StringUtil.repeatSymbol(' ', length));
                        } else {
                            int length = gen.nextInt(finalDocument.getTextLength() - offset);
                            edits.add(Trinity.create(offset, length, 0));
                            finalDocument.deleteString(offset, offset + length);
                        }
                    }
                    List<Pair<RangeMarker, TextRange>> candidates = new ArrayList<>(adds);
                    while (!candidates.isEmpty()) {
                        int size = candidates.size();
                        int x = gen.nextInt(size);
                        Pair<RangeMarker, TextRange> c = candidates.remove(x);
                        RangeMarkerEx r = (RangeMarkerEx) c.first;
                        assertEquals(size - 1, candidates.size());
                        dels.add(c);
                        r.dispose();
                    }
                } catch (AssertionError e) {
                    String s = "adds: ";
                    for (Pair<RangeMarker, TextRange> c : adds) {
                        TextRange t = c.second;
                        s += t.getStartOffset() + "," + t.getEndOffset() + ", ";
                    }
                    s += "\nedits: ";
                    for (Trinity<Integer, Integer, Integer> edit : edits) {
                        s += edit.first + "," + edit.second + "," + edit.third + ",  ";
                    }
                    s += "\ndels: ";
                    for (Pair<RangeMarker, TextRange> c : dels) {
                        int index = adds.indexOf(c);
                        assertSame(c, adds.get(index));
                        s += index + ", ";
                    }
                    System.err.println(s);
                    throw e;
                }
            }
        }.execute();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RangeMarkerEx(com.intellij.openapi.editor.ex.RangeMarkerEx) TextRange(com.intellij.openapi.util.TextRange) Result(com.intellij.openapi.application.Result) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) UndoManagerImpl(com.intellij.openapi.command.impl.UndoManagerImpl) WeakList(com.intellij.util.containers.WeakList) Pair(com.intellij.openapi.util.Pair)

Example 44 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.

the class ConstraintUtilities method saveModelToXML.

/**
   * Utility function to commit to the NlModel the current state of all widgets
   *
   * @param nlModel
   * @param commit  if false, the changes are only reflected in memory not saved to the XML file.
   */
static void saveModelToXML(@NotNull NlModel nlModel, boolean commit) {
    ConstraintModel model = ConstraintModel.getConstraintModel(nlModel);
    if (commit) {
        Project project = nlModel.getProject();
        XmlFile file = nlModel.getFile();
        String label = "Constraint";
        WriteCommandAction action = new WriteCommandAction(project, label, file) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                saveXmlWidgets(model, commit);
            }
        };
        action.execute();
    } else {
        saveXmlWidgets(model, commit);
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result)

Example 45 with WriteCommandAction

use of com.intellij.openapi.command.WriteCommandAction in project android by JetBrains.

the class AnchorTarget method mouseRelease.

/**
   * On mouseRelease, we can either disconnect the current anchor (if the mouse release is on ourselve)
   * or connect the anchor to a given target. Modifications are applied first in memory then commited
   * to the XML model.
   *
   * @param x
   * @param y
   * @param closestTarget
   */
@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
    myLastX = -1;
    myLastY = -1;
    if (myComponent.getParent() != null) {
        myComponent.getParent().setExpandTargetArea(false);
    }
    if (closestTarget != null && closestTarget instanceof AnchorTarget && !(((AnchorTarget) closestTarget).isConnected(this))) {
        NlComponent component = myComponent.getNlComponent();
        if (closestTarget == this) {
            disconnectMe(component);
        } else {
            String attribute = getAttribute(closestTarget);
            if (attribute != null) {
                AnchorTarget targetAnchor = (AnchorTarget) closestTarget;
                NlComponent targetComponent = targetAnchor.myComponent.getNlComponent();
                AttributesTransaction attributes = connectMe(component, attribute, targetComponent);
                NlModel nlModel = component.getModel();
                Project project = nlModel.getProject();
                XmlFile file = nlModel.getFile();
                String label = "Constraint Connected";
                WriteCommandAction action = new WriteCommandAction(project, label, file) {

                    @Override
                    protected void run(@NotNull Result result) throws Throwable {
                        attributes.commit();
                    }
                };
                action.execute();
                myComponent.getScene().needsLayout(Scene.ANIMATED_LAYOUT);
            }
        }
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) XmlFile(com.intellij.psi.xml.XmlFile) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull) AttributesTransaction(com.android.tools.idea.uibuilder.model.AttributesTransaction) Result(com.intellij.openapi.application.Result)

Aggregations

WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)176 Result (com.intellij.openapi.application.Result)175 NotNull (org.jetbrains.annotations.NotNull)62 Project (com.intellij.openapi.project.Project)45 VirtualFile (com.intellij.openapi.vfs.VirtualFile)29 XmlFile (com.intellij.psi.xml.XmlFile)28 XmlTag (com.intellij.psi.xml.XmlTag)23 Document (com.intellij.openapi.editor.Document)22 PsiFile (com.intellij.psi.PsiFile)16 Module (com.intellij.openapi.module.Module)14 Nullable (org.jetbrains.annotations.Nullable)12 NlModel (com.android.tools.idea.uibuilder.model.NlModel)11 AttributesTransaction (com.android.tools.idea.uibuilder.model.AttributesTransaction)10 Editor (com.intellij.openapi.editor.Editor)10 TextRange (com.intellij.openapi.util.TextRange)8 XmlAttribute (com.intellij.psi.xml.XmlAttribute)8 File (java.io.File)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)7