Search in sources :

Example 26 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.

the class AbstractConvertLineSeparatorsAction method changeLineSeparators.

public static void changeLineSeparators(@NotNull final Project project, @NotNull final VirtualFile virtualFile, @NotNull final String newSeparator) {
    FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
    Document document = fileDocumentManager.getCachedDocument(virtualFile);
    if (document != null) {
        fileDocumentManager.saveDocument(document);
    }
    String currentSeparator = LoadTextUtil.detectLineSeparator(virtualFile, false);
    final String commandText;
    if (StringUtil.isEmpty(currentSeparator)) {
        commandText = "Changed line separators to " + LineSeparator.fromString(newSeparator);
    } else {
        commandText = String.format("Changed line separators from %s to %s", LineSeparator.fromString(currentSeparator), LineSeparator.fromString(newSeparator));
    }
    new WriteCommandAction(project, commandText) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            try {
                LoadTextUtil.changeLineSeparators(project, virtualFile, newSeparator, this);
            } catch (IOException e) {
                LOG.info(e);
            }
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) Result(com.intellij.openapi.application.Result)

Example 27 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-plugins by JetBrains.

the class TsLintExternalAnnotator method saveConfigFileAndReturnSkipProcessing.

public boolean saveConfigFileAndReturnSkipProcessing(@NotNull Project project, @NotNull VirtualFile config) {
    return ReadAction.compute(() -> {
        final FileDocumentManager manager = FileDocumentManager.getInstance();
        Document document = manager.getCachedDocument(config);
        if (document != null) {
            boolean unsaved = manager.isDocumentUnsaved(document);
            if (unsaved) {
                ApplicationManager.getApplication().invokeLater(() -> {
                    Document newDocument = manager.getCachedDocument(config);
                    if (newDocument != null) {
                        FileDocumentManager.getInstance().saveDocument(newDocument);
                    }
                    DaemonCodeAnalyzer.getInstance(project).restart();
                }, project.getDisposed());
            }
            return unsaved;
        }
        return false;
    });
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Example 28 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-plugins by JetBrains.

the class FlexCompilerConfigFileUtil method getInfoFromConfigFile.

@NotNull
public static InfoFromConfigFile getInfoFromConfigFile(final String configFilePath) {
    final VirtualFile configFile = configFilePath.isEmpty() ? null : LocalFileSystem.getInstance().findFileByPath(configFilePath);
    if (configFile == null) {
        return InfoFromConfigFile.DEFAULT;
    }
    Pair<Long, InfoFromConfigFile> data = configFile.getUserData(MOD_STAMP_AND_INFO_FROM_CONFIG_FILE);
    final FileDocumentManager documentManager = FileDocumentManager.getInstance();
    final Document cachedDocument = documentManager.getCachedDocument(configFile);
    final Long currentTimestamp = cachedDocument != null ? cachedDocument.getModificationStamp() : configFile.getModificationCount();
    final Long cachedTimestamp = data == null ? null : data.first;
    if (cachedTimestamp == null || !cachedTimestamp.equals(currentTimestamp)) {
        data = null;
        configFile.putUserData(MOD_STAMP_AND_INFO_FROM_CONFIG_FILE, data);
        final List<String> xmlElements = Arrays.asList(FILE_SPEC_ELEMENT, OUTPUT_ELEMENT, TARGET_PLAYER_ELEMENT);
        String mainClassPath = null;
        String outputPath = null;
        String targetPlayer = null;
        try {
            final InputStream inputStream = cachedDocument == null ? configFile.getInputStream() : new ByteArrayInputStream(cachedDocument.getText().getBytes());
            final Map<String, List<String>> map = FlexUtils.findXMLElements(inputStream, xmlElements);
            final List<String> fileSpecList = map.get(FILE_SPEC_ELEMENT);
            if (!fileSpecList.isEmpty()) {
                mainClassPath = fileSpecList.get(0).trim();
            }
            final List<String> outputList = map.get(OUTPUT_ELEMENT);
            if (!outputList.isEmpty()) {
                outputPath = outputList.get(0).trim();
                if (!FileUtil.isAbsolute(outputPath)) {
                    try {
                        outputPath = FileUtil.toSystemIndependentName(new File(configFile.getParent().getPath() + "/" + outputPath).getCanonicalPath());
                    } catch (IOException e) {
                        outputPath = FileUtil.toSystemIndependentName(new File(configFile.getParent().getPath() + "/" + outputPath).getAbsolutePath());
                    }
                }
            }
            final List<String> targetPlayerList = map.get(TARGET_PLAYER_ELEMENT);
            if (!targetPlayerList.isEmpty()) {
                targetPlayer = targetPlayerList.get(0).trim();
            }
        } catch (IOException ignore) {
        /*ignore*/
        }
        final String outputFileName = outputPath == null ? null : PathUtil.getFileName(outputPath);
        final String outputFolderPath = outputPath == null ? null : PathUtil.getParentPath(outputPath);
        data = Pair.create(currentTimestamp, new InfoFromConfigFile(configFile, mainClassPath, outputFileName, outputFolderPath, targetPlayer));
        configFile.putUserData(MOD_STAMP_AND_INFO_FROM_CONFIG_FILE, data);
    }
    return data.second;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) ByteArrayInputStream(java.io.ByteArrayInputStream) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager 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)

Example 30 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project Intellij-Discord-Integration by Almighty-Alpaca.

the class EditorMouseListener method mousePressed.

@Override
public void mousePressed(EditorMouseEvent event) {
    LOG.trace("EditorMouseListener#mousePressed({})", event);
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    Project project = event.getEditor().getProject();
    DiscordIntegrationProjectComponent component = DiscordIntegrationProjectComponent.getInstance(project);
    if (component != null)
        component.updateTimeAccessed(documentManager.getFile(event.getEditor().getDocument()));
}
Also used : Project(com.intellij.openapi.project.Project) DiscordIntegrationProjectComponent(com.almightyalpaca.jetbrains.plugins.discord.components.DiscordIntegrationProjectComponent) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager)

Aggregations

FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)63 Document (com.intellij.openapi.editor.Document)42 VirtualFile (com.intellij.openapi.vfs.VirtualFile)32 IOException (java.io.IOException)16 Editor (com.intellij.openapi.editor.Editor)10 Project (com.intellij.openapi.project.Project)9 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)7 File (java.io.File)7 Nullable (org.jetbrains.annotations.Nullable)7 DiscordIntegrationProjectComponent (com.almightyalpaca.jetbrains.plugins.discord.components.DiscordIntegrationProjectComponent)5 MockVirtualFile (com.intellij.mock.MockVirtualFile)5 PsiFile (com.intellij.psi.PsiFile)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)4 NotNull (org.jetbrains.annotations.NotNull)4 AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)3 ArrayList (java.util.ArrayList)3 DataContext (com.intellij.openapi.actionSystem.DataContext)2 ActionManagerEx (com.intellij.openapi.actionSystem.ex.ActionManagerEx)2 Application (com.intellij.openapi.application.Application)2