Search in sources :

Example 6 with WriteCommandAction

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

the class XmlEventsTest method testBulkUpdate.

public void testBulkUpdate() throws Exception {
    final Listener listener = addPomListener();
    final PsiFile file = createFile("a.xml", "<a/>");
    new WriteCommandAction(getProject()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
            DocumentUtil.executeInBulk(document, true, () -> {
                document.insertString(0, " ");
                commitDocument(document);
            });
        }
    }.execute();
    assertEquals("(Xml document changed)", listener.getEventString().trim());
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PomModelListener(com.intellij.pom.event.PomModelListener) Document(com.intellij.openapi.editor.Document) Result(com.intellij.openapi.application.Result)

Example 7 with WriteCommandAction

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

the class XmlChooseColorIntentionAction method chooseColor.

public static void chooseColor(JComponent editorComponent, PsiElement element) {
    String caption = CodeInsightBundle.message("intention.color.chooser.dialog");
    final XmlAttributeValue literal = PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class, false);
    if (literal == null)
        return;
    final String text = StringUtil.unquoteString(literal.getValue());
    Color oldColor;
    try {
        oldColor = Color.decode(text);
    } catch (NumberFormatException e) {
        oldColor = JBColor.GRAY;
    }
    Color color = ColorChooser.chooseColor(editorComponent, caption, oldColor, true);
    if (color == null)
        return;
    if (!Comparing.equal(color, oldColor)) {
        if (!FileModificationService.getInstance().preparePsiElementForWrite(element))
            return;
        final String newText = "#" + ColorUtil.toHex(color);
        final PsiManager manager = literal.getManager();
        final XmlAttribute newAttribute = XmlElementFactory.getInstance(manager.getProject()).createAttribute("name", newText, element);
        final Runnable replaceRunnable = () -> {
            final XmlAttributeValue valueElement = newAttribute.getValueElement();
            assert valueElement != null;
            literal.replace(valueElement);
        };
        new WriteCommandAction(element.getProject(), caption) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                replaceRunnable.run();
            }
        }.execute();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlAttribute(com.intellij.psi.xml.XmlAttribute) JBColor(com.intellij.ui.JBColor) PsiManager(com.intellij.psi.PsiManager) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Result(com.intellij.openapi.application.Result)

Example 8 with WriteCommandAction

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

the class MavenOpenOrCreateFilesAction method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    final Project project = MavenActionUtil.getProject(e.getDataContext());
    if (project == null)
        return;
    final List<File> files = getFiles(e);
    final List<VirtualFile> virtualFiles = collectVirtualFiles(files);
    if (files.size() == 1 && virtualFiles.isEmpty()) {
        new WriteCommandAction(project, e.getPresentation().getText()) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                File file = files.get(0);
                try {
                    final VirtualFile virtualFile = VfsUtil.createDirectoryIfMissing(file.getParent());
                    if (virtualFile != null) {
                        VirtualFile newFile = virtualFile.createChildData(this, file.getName());
                        virtualFiles.add(newFile);
                        MavenUtil.runFileTemplate(project, newFile, getFileTemplate());
                    }
                } catch (IOException ex) {
                    MavenUtil.showError(project, "Cannot create " + file.getName(), ex);
                }
            }
        }.execute();
        return;
    }
    for (VirtualFile each : virtualFiles) {
        new OpenFileDescriptor(project, each).navigate(true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Result(com.intellij.openapi.application.Result)

Example 9 with WriteCommandAction

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

the class MavenTestCase method createModule.

protected Module createModule(final String name, final ModuleType type) throws IOException {
    return new WriteCommandAction<Module>(myProject) {

        @Override
        protected void run(@NotNull Result<Module> moduleResult) throws Throwable {
            VirtualFile f = createProjectSubFile(name + "/" + name + ".iml");
            Module module = ModuleManager.getInstance(myProject).newModule(f.getPath(), type.getId());
            PsiTestUtil.addContentRoot(module, f.getParent());
            moduleResult.setResult(module);
        }
    }.execute().getResultObject();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result)

Example 10 with WriteCommandAction

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

the class DomNamespacesTest method testCopyFromHonorsNamespaces.

public void testCopyFromHonorsNamespaces() throws Throwable {
    final MyElement element = createElement("<a xmlns=\"foo\" xmlns:bar=\"bar\"/>", MyElement.class);
    registerNamespacePolicies(element);
    final MyElement element2 = createElement("<f:a xmlns:f=\"foo1\" xmlns:b=\"bar1\" xmlns=\"foo1\">" + "<foo-child/>" + "<b:bar-child/>" + "<f:some-child/>" + "<f:foo-element attr-2=\"239\" attr=\"42\"/>" + "<f:foo-element/>" + "<f:bool/>" + "<sys:aaa/>" + "</f:a>", MyElement.class);
    registerNamespacePolicies(element2, "foo1", "bar1");
    new WriteCommandAction(getProject()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            element.copyFrom(element2);
        }
    }.execute();
    assertEquals("<a xmlns=\"foo\" xmlns:bar=\"bar\">" + "<bar:bar-child/>" + "<bool/>" + "<foo-child/>" + "<some-child/>" + "<sys:aaa/>" + "<foo-element attr=\"42\" attr-2=\"239\"/>" + "<foo-element/>" + "</a>", element.getXmlTag().getText());
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) 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