Search in sources :

Example 21 with WriteCommandAction

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

the class TreeIncrementalUpdateTest method testAddFixedElement.

public void testAddFixedElement() throws Throwable {
    final MyElement element = createPhysicalElement("<a>" + "<child/>" + "<child><child/></child>" + "<child/></a>");
    final MyElement child = element.getChild();
    final MyElement child2 = element.getChild2();
    final XmlTag leafTag = child2.getChild().getXmlTag();
    new WriteCommandAction(getProject()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            element.getXmlTag().addAfter(createTag("<child/>"), child.getXmlTag());
        }
    }.execute();
    assertNoCache(leafTag);
    final XmlTag[] subTags = element.getXmlTag().getSubTags();
    assertFalse(child2.isValid());
    assertEquals(child, element.getChild());
    assertFalse(child2.equals(element.getChild2()));
    assertCached(child, subTags[0]);
    assertNoCache(subTags[2]);
    assertNoCache(subTags[3]);
    putExpected(new DomEvent(element, false));
    putExpected(new DomEvent(element, false));
    assertResultsAndClear();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlTag(com.intellij.psi.xml.XmlTag) Result(com.intellij.openapi.application.Result) DomEvent(com.intellij.util.xml.events.DomEvent)

Example 22 with WriteCommandAction

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

the class TreeIncrementalUpdateTest method testRemoveAttributeParent.

public void testRemoveAttributeParent() throws Throwable {
    final XmlFile file = (XmlFile) createFile("file.xml", "<?xml version='1.0' encoding='UTF-8'?>\n" + "<!DOCTYPE ejb-jar PUBLIC \"-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN\" \"http://java.sun.com/dtd/ejb-jar_2_0.dtd\">\n" + "<a>\n" + " <child-element xxx=\"239\"/>\n" + "</a>");
    final DomFileElementImpl<MyElement> fileElement = getDomManager().getFileElement(file, MyElement.class, "a");
    myCallRegistry.clear();
    final MyElement rootElement = fileElement.getRootElement();
    final MyElement oldLeaf = rootElement.getChildElements().get(0);
    final GenericAttributeValue<String> xxx = oldLeaf.getXxx();
    final XmlTag oldLeafTag = oldLeaf.getXmlTag();
    new WriteCommandAction(getProject()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            oldLeafTag.delete();
        }
    }.execute();
    assertFalse(oldLeaf.isValid());
    assertFalse(xxx.isValid());
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlFile(com.intellij.psi.xml.XmlFile) XmlTag(com.intellij.psi.xml.XmlTag) Result(com.intellij.openapi.application.Result)

Example 23 with WriteCommandAction

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

the class TreeIncrementalUpdateTest method testTypeBeforeRootTag.

public void testTypeBeforeRootTag() throws Throwable {
    getDomManager().registerFileDescription(new DomFileDescription<>(MyElement.class, "a"), getTestRootDisposable());
    final XmlFile file = (XmlFile) createFile("file.xml", "<?xml version='1.0' encoding='UTF-8'?>\n" + "<a/>");
    assertTrue(getDomManager().isDomFile(file));
    final DomFileElementImpl<MyElement> fileElement = getDomManager().getFileElement(file, MyElement.class);
    assertTrue(fileElement.isValid());
    myCallRegistry.clear();
    putExpected(new DomEvent(fileElement, false));
    new WriteCommandAction(getProject()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            final Document document = getDocument(file);
            final int i = document.getText().indexOf("<a");
            document.insertString(i, "a");
            commitDocument(document);
        }
    }.execute();
    assertFalse(getDomManager().isDomFile(file));
    assertFalse(fileElement.isValid());
    assertResultsAndClear();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlFile(com.intellij.psi.xml.XmlFile) Document(com.intellij.openapi.editor.Document) DomEvent(com.intellij.util.xml.events.DomEvent) Result(com.intellij.openapi.application.Result)

Example 24 with WriteCommandAction

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

the class CollapseTagIntention method applyFix.

private static void applyFix(@NotNull final Project project, @NotNull final PsiElement tag) {
    if (!FileModificationService.getInstance().prepareFileForWrite(tag.getContainingFile())) {
        return;
    }
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    final ASTNode child = XmlChildRole.START_TAG_END_FINDER.findChild(tag.getNode());
    if (child == null)
        return;
    final int offset = child.getTextRange().getStartOffset();
    VirtualFile file = tag.getContainingFile().getVirtualFile();
    final Document document = FileDocumentManager.getInstance().getDocument(file);
    new WriteCommandAction(project) {

        @Override
        protected void run(@NotNull final Result result) throws Throwable {
            assert document != null;
            document.replaceString(offset, tag.getTextRange().getEndOffset(), "/>");
            PsiDocumentManager.getInstance(project).commitDocument(document);
            CodeStyleManager.getInstance(project).reformat(tag);
        }
    }.execute();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ASTNode(com.intellij.lang.ASTNode) Document(com.intellij.openapi.editor.Document) Result(com.intellij.openapi.application.Result)

Example 25 with WriteCommandAction

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

the class KotlinInplaceVariableIntroducer method runWriteActionAndRestartRefactoring.

protected final void runWriteActionAndRestartRefactoring(final Runnable runnable) {
    final Ref<Boolean> greedyToRight = new Ref<Boolean>();
    new WriteCommandAction(myProject, getCommandName(), getCommandName()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            PsiDocumentManager.getInstance(myProject).commitDocument(myEditor.getDocument());
            ASTNode identifier = myDeclaration.getNode().findChildByType(KtTokens.IDENTIFIER);
            if (identifier != null) {
                TextRange range = identifier.getTextRange();
                RangeHighlighter[] highlighters = myEditor.getMarkupModel().getAllHighlighters();
                for (RangeHighlighter highlighter : highlighters) {
                    if (highlighter.getStartOffset() == range.getStartOffset()) {
                        if (highlighter.getEndOffset() == range.getEndOffset()) {
                            greedyToRight.set(highlighter.isGreedyToRight());
                            highlighter.setGreedyToRight(false);
                        }
                    }
                }
            }
            runnable.run();
            TemplateState templateState = TemplateManagerImpl.getTemplateState(InjectedLanguageUtil.getTopLevelEditor(myEditor));
            if (templateState != null) {
                myEditor.putUserData(INTRODUCE_RESTART, true);
                templateState.cancelTemplate();
            }
        }
    }.execute();
    ApplicationManager.getApplication().runReadAction(new Runnable() {

        @Override
        public void run() {
            ASTNode identifier = myDeclaration.getNode().findChildByType(KtTokens.IDENTIFIER);
            if (identifier != null) {
                TextRange range = identifier.getTextRange();
                RangeHighlighter[] highlighters = myEditor.getMarkupModel().getAllHighlighters();
                for (RangeHighlighter highlighter : highlighters) {
                    if (highlighter.getStartOffset() == range.getStartOffset()) {
                        if (highlighter.getEndOffset() == range.getEndOffset()) {
                            highlighter.setGreedyToRight(greedyToRight.get());
                        }
                    }
                }
            }
        }
    });
    if (myEditor.getUserData(INTRODUCE_RESTART) == Boolean.TRUE) {
        myInitialName = myDeclaration.getName();
        performInplaceRefactoring(getSuggestionsForNextRun());
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) ASTNode(com.intellij.lang.ASTNode) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) 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