Search in sources :

Example 1 with DocumentRunnable

use of com.intellij.openapi.editor.DocumentRunnable in project intellij-community by JetBrains.

the class PsiDocumentManagerBase method finishCommit.

// public for Upsource
public boolean finishCommit(@NotNull final Document document, @NotNull final List<Processor<Document>> finishProcessors, final boolean synchronously, @NotNull final Object reason) {
    assert !myProject.isDisposed() : "Already disposed";
    ApplicationManager.getApplication().assertIsDispatchThread();
    final boolean[] ok = { true };
    Runnable runnable = new DocumentRunnable(document, myProject) {

        @Override
        public void run() {
            ok[0] = finishCommitInWriteAction(document, finishProcessors, synchronously);
        }
    };
    if (synchronously) {
        runnable.run();
    } else {
        ApplicationManager.getApplication().runWriteAction(runnable);
    }
    if (ok[0]) {
        // otherwise changes maybe not synced to the document yet, and injectors will crash
        if (!mySynchronizer.isDocumentAffectedByTransactions(document)) {
            InjectedLanguageManager.getInstance(myProject).startRunInjectors(document, synchronously);
        }
        // run after commit actions outside write action
        runAfterCommitActions(document);
        if (DebugUtil.DO_EXPENSIVE_CHECKS && !ApplicationInfoImpl.isInStressTest()) {
            checkAllElementsValid(document, reason);
        }
    }
    return ok[0];
}
Also used : DocumentRunnable(com.intellij.openapi.editor.DocumentRunnable) DocumentRunnable(com.intellij.openapi.editor.DocumentRunnable)

Example 2 with DocumentRunnable

use of com.intellij.openapi.editor.DocumentRunnable in project ServiceStack.Java by ServiceStack.

the class IDEAPomFileHelper method PomAppendDependency.

private void PomAppendDependency(final Module module, final File pomFile, String groupId, String packageId, String version) throws ParserConfigurationException, IOException, SAXException, TransformerException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    final Document doc = docBuilder.parse(pomFile);
    Node root = doc.getFirstChild();
    if (!pomHasDependenciesNode(doc)) {
        root.appendChild(doc.createElement("dependencies"));
    }
    Node dependenciesNode = getMavenDependenciesNode(doc);
    Node newDepNode = doc.createElement("dependency");
    Node groupNode = doc.createElement("groupId");
    groupNode.appendChild(doc.createTextNode(groupId));
    Node artifactNode = doc.createElement("artifactId");
    artifactNode.appendChild(doc.createTextNode(packageId));
    Node versionNode = doc.createElement("version");
    versionNode.appendChild(doc.createTextNode(version));
    newDepNode.appendChild(groupNode);
    newDepNode.appendChild(artifactNode);
    newDepNode.appendChild(versionNode);
    dependenciesNode.appendChild(newDepNode);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    final StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    final Project project = module.getProject();
    final VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(pomFile);
    final com.intellij.openapi.editor.Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
    final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
    // when file is edited and editor is closed, it is needed to save the text
    fileDocumentManager.saveDocument(document);
    PsiDocumentManager.getInstance(project).commitDocument(document);
    PsiDocumentManager.getInstance(module.getProject()).commitAllDocuments();
    ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(document, null) {

        @Override
        public void run() {
            PsiDocumentManager.getInstance(module.getProject()).doPostponedOperationsAndUnblockDocument(document);
            document.setText(writer.getBuffer().toString().replaceAll("\r\n", "\n"));
            fileDocumentManager.saveDocument(document);
            PsiDocumentManager.getInstance(project).commitDocument(document);
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(org.w3c.dom.Document) Project(com.intellij.openapi.project.Project) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DocumentRunnable(com.intellij.openapi.editor.DocumentRunnable)

Aggregations

DocumentRunnable (com.intellij.openapi.editor.DocumentRunnable)2 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 StringWriter (java.io.StringWriter)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1