Search in sources :

Example 41 with FileDocumentManager

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

the class JSConditionalCompilationDefinitionsProviderImpl method getDefinitionsFromConfigFile.

private Collection<Pair<String, String>> getDefinitionsFromConfigFile(final String configFilePath) {
    if (StringUtil.isEmptyOrSpaces(configFilePath))
        return Collections.emptyList();
    final VirtualFile configFile = LocalFileSystem.getInstance().findFileByPath(configFilePath);
    if (configFile == null || configFile.isDirectory())
        return Collections.emptyList();
    final FileDocumentManager documentManager = FileDocumentManager.getInstance();
    final Document cachedDocument = documentManager.getCachedDocument(configFile);
    final Long currentTimestamp = cachedDocument != null ? cachedDocument.getModificationStamp() : configFile.getModificationCount();
    final Long cachedTimestamp = configFileToTimestamp.get(configFile);
    if (cachedTimestamp == null || !cachedTimestamp.equals(currentTimestamp)) {
        configFileToTimestamp.remove(configFile);
        configFileToConditionalCompilerDefinitions.remove(configFile);
        try {
            final Element rootElement = cachedDocument == null ? JDOMUtil.load(configFile.getInputStream()) : JDOMUtil.load(cachedDocument.getCharsSequence());
            final Collection<Pair<String, String>> result = new ArrayList<>();
            if (rootElement.getName().equals(FlexCompilerConfigFileUtilBase.FLEX_CONFIG)) {
                // noinspection unchecked
                for (Element compilerElement : rootElement.getChildren(FlexCompilerConfigFileUtilBase.COMPILER, rootElement.getNamespace())) {
                    // noinspection unchecked
                    for (Element defineElement : compilerElement.getChildren(DEFINE, rootElement.getNamespace())) {
                        final String name = defineElement.getChildText(NAME, rootElement.getNamespace());
                        final String value = defineElement.getChildText(VALUE, rootElement.getNamespace());
                        if (!StringUtil.isEmpty(name) && value != null) {
                            result.add(Pair.create(name, value));
                        }
                    }
                }
            }
            configFileToTimestamp.put(configFile, currentTimestamp);
            configFileToConditionalCompilerDefinitions.put(configFile, result);
            return result;
        } catch (JDOMException ignored) {
        /*ignore*/
        } catch (IOException ignored) {
        /*ignore*/
        }
        return Collections.emptyList();
    } else {
        return configFileToConditionalCompilerDefinitions.get(configFile);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Element(org.jdom.Element) ArrayList(java.util.ArrayList) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) JDOMException(org.jdom.JDOMException) Pair(com.intellij.openapi.util.Pair)

Example 42 with FileDocumentManager

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

the class ComplexRenderAction method collectChanges.

private void collectChanges(List<DocumentFactoryManager.DocumentInfo> documentInfos, THashMap<ModuleInfo, List<LocalStyleHolder>> localStyleSources) {
    final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
    final DocumentFactoryManager documentFactoryManager = DocumentFactoryManager.getInstance();
    final Client client = Client.getInstance();
    for (Document document : documents) {
        final VirtualFile file = fileDocumentManager.getFile(document);
        if (file == null) {
            continue;
        }
        boolean isMxml = JavaScriptSupportLoader.isFlexMxmFile(file);
        if (isMxml || file.getFileType() == CssFileType.INSTANCE) {
            if (!collectChangedLocalStyleSources(localStyleSources, file) && onlyStyle) {
                // if onlyStyle and we didn't find changed local style sources, so, it is new style source - we must collect style sources for appropriate module
                Project p = ProjectUtil.guessProjectForFile(file);
                if (p != null) {
                    ModuleInfo info = client.getRegisteredModules().getNullableInfo(ModuleUtilCore.findModuleForFile(file, p));
                    if (info != null) {
                        localStyleSources.put(info, Collections.emptyList());
                    }
                }
            }
        }
        final DocumentFactoryManager.DocumentInfo info = isMxml ? documentFactoryManager.getNullableInfo(file) : null;
        if (info == null) {
            continue;
        } else if (onlyStyle) {
            info.documentModificationStamp = document.getModificationStamp();
            continue;
        }
        if (info.documentModificationStamp == document.getModificationStamp()) {
            continue;
        }
        final Project project = ProjectUtil.guessProjectForFile(file);
        if (project == null) {
            continue;
        }
        final Module module = ModuleUtilCore.findModuleForFile(file, project);
        if (module == null) {
            continue;
        }
        final XmlFile psiFile;
        final AccessToken token = ReadAction.start();
        try {
            psiFile = (XmlFile) PsiDocumentManager.getInstance(project).getPsiFile(document);
            if (psiFile == null) {
                continue;
            }
        } finally {
            token.finish();
        }
        if (client.updateDocumentFactory(info.getId(), module, psiFile, reportProblems)) {
            info.documentModificationStamp = document.getModificationStamp();
            documentInfos.add(info);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) AccessToken(com.intellij.openapi.application.AccessToken) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) Module(com.intellij.openapi.module.Module)

Example 43 with FileDocumentManager

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

the class ChangeFileEncodingAction method checkEnabled.

private boolean checkEnabled(@NotNull VirtualFile virtualFile) {
    if (allowDirectories && virtualFile.isDirectory())
        return true;
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    Document document = documentManager.getDocument(virtualFile);
    if (document == null)
        return false;
    return EncodingUtil.checkCanConvert(virtualFile) == null || EncodingUtil.checkCanReload(virtualFile).second == null;
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Example 44 with FileDocumentManager

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

the class EncodingUtil method saveIn.

static void saveIn(@NotNull final Document document, final Editor editor, @NotNull final VirtualFile virtualFile, @NotNull final Charset charset) {
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    documentManager.saveDocument(document);
    final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
    boolean writable = project == null ? virtualFile.isWritable() : ReadonlyStatusHandler.ensureFilesWritable(project, virtualFile);
    if (!writable) {
        CommonRefactoringUtil.showErrorHint(project, editor, "Cannot save the file " + virtualFile.getPresentableUrl(), "Unable to Save", null);
        return;
    }
    EncodingProjectManagerImpl.suppressReloadDuring(() -> {
        EncodingManager.getInstance().setEncoding(virtualFile, charset);
        try {
            ApplicationManager.getApplication().runWriteAction((ThrowableComputable<Object, IOException>) () -> {
                virtualFile.setCharset(charset);
                LoadTextUtil.write(project, virtualFile, virtualFile, document.getText(), document.getModificationStamp());
                return null;
            });
        } catch (IOException io) {
            Messages.showErrorDialog(project, io.getMessage(), "Error Writing File");
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException)

Example 45 with FileDocumentManager

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

the class FileContentUtilCore method saveOrReload.

private static void saveOrReload(VirtualFile file, @NotNull Collection<VFilePropertyChangeEvent> events) {
    if (file == null || file.isDirectory() || !file.isValid()) {
        return;
    }
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    if (documentManager.isFileModified(file)) {
        Document document = documentManager.getDocument(file);
        if (document != null) {
            // this can be called e.g. in context of undo, so we shouldn't modify document
            documentManager.saveDocumentAsIs(document);
        }
    }
    events.add(new VFilePropertyChangeEvent(FORCE_RELOAD_REQUESTOR, file, VirtualFile.PROP_NAME, file.getName(), file.getName(), false));
}
Also used : VFilePropertyChangeEvent(com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

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