/** * Gets the editor for the virtual file within the editor manager. * * @param manager The file editor manager * @param file The virtual file get the editor for * @return The matching editor or null if no match was found */ @Nullable public static Editor getEditor(@NotNull final FileEditorManager manager, @Nullable final VirtualFile file) { if (file == null) { return null; } final Document doc = FileDocumentManager.getInstance().getDocument(file); if (doc == null) { return null; } final Editor[] editors = EditorFactory.getInstance().getEditors(doc, manager.getProject()); if (editors.length > 0) { return editors[0]; } return null; }
/** * Saves all files in the project. */ public void saveFiles() { FileDocumentManager.getInstance().saveAllDocuments(); }
/** * Gets the virtual file associated with this editor * * @param editor The editor * @return The virtual file for the editor */ @Nullable public static VirtualFile getVirtualFile(@NotNull Editor editor) { return FileDocumentManager.getInstance().getFile(editor.getDocument()); }
/** * Saves specific file in the project. */ public void saveFile(@NotNull Editor editor) { FileDocumentManager.getInstance().saveDocument(editor.getDocument()); }
protected boolean doSomething(@NotNull VirtualFile virtualFile, @Nullable Module module, @NotNull Project project, @NotNull String title, boolean withProgress, @NotNull Consumer<Boolean> consumer) { Document document = FileDocumentManager.getInstance().getDocument(virtualFile); if (document != null) { FileDocumentManager.getInstance().saveDocument(document); } else { FileDocumentManager.getInstance().saveAllDocuments(); } createExecutor(project, module, title, virtualFile).executeWithProgress(withProgress, result -> { consumer.consume(result); VfsUtil.markDirtyAndRefresh(true, true, true, virtualFile); }); return true; }
private boolean documentIsModifiedAndUnsaved(final PsiFile file) { final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance(); return virtualFileOf(file).filter(fileDocumentManager::isFileModified).map(fileDocumentManager::getDocument) .map(fileDocumentManager::isDocumentUnsaved).orElse(false); }
FileDocumentManager docMgr = FileDocumentManager.getInstance(); Document doc = docMgr.getDocument(grammarFile); if ( doc==null ) return; boolean unsaved = !psiMgr.isCommitted(doc) || docMgr.isDocumentUnsaved(doc); if ( unsaved ) { docMgr.saveDocument(doc);
/** * Must be called from EDT */ private static boolean saveFile(final VirtualFile virtualFile) { ApplicationManager.getApplication().assertIsDispatchThread(); final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance(); if (fileDocumentManager.isFileModified(virtualFile)) { final Document document = fileDocumentManager.getDocument(virtualFile); if (document != null) { fileDocumentManager.saveDocument(document); return true; } } return false; }
@Override public void actionPerformed(final AnActionEvent e) { if ( e.getProject()==null ) { LOG.error("actionPerformed no project for "+e); return; // whoa! } VirtualFile grammarFile = MyActionUtils.getGrammarFileFromEvent(e); if ( grammarFile==null ) return; LOG.info("actionPerformed "+grammarFile); ANTLRv4PluginController controller = ANTLRv4PluginController.getInstance(e.getProject()); controller.getPreviewWindow().show(null); ParserRuleRefNode r = MyActionUtils.getParserRuleSurroundingRef(e); if ( r==null ) { return; // weird. no rule name. } String ruleName = r.getText(); FileDocumentManager docMgr = FileDocumentManager.getInstance(); Document doc = docMgr.getDocument(grammarFile); if ( doc!=null ) { docMgr.saveDocument(doc); } controller.setStartRuleNameEvent(grammarFile, ruleName); }
@Nullable @Override public State doAnnotate(@NotNull final PsiFile file) { // Force all files to save to ensure annotations are in sync with the file system. if (FileDocumentManager.getInstance().getUnsavedDocuments().length != 0) ApplicationManager .getApplication() .invokeAndWait(() -> FileDocumentManager.getInstance().saveAllDocuments(), ApplicationManager.getApplication().getDefaultModalityState()); final State state = new State(); state.dScannerProblems = new DScanner().checkFileSyntax(file); state.syntaxProblems = new CompileCheck().checkFileSyntax(file); return state; }
@Override public PsiFile compute() { final PsiManager mgr = PsiManager.getInstance(project); // // NB: ensure we're not eating stale cache // ((PsiManagerImpl) mgr).getFileManager().cleanupForNextTest(); final PsiFile file = mgr.findFile(virtual); if (file == null) return null; // ensure the doc is up to date as well final DocumentEx doc = VimDocument.getInstance(file); FileDocumentManager.getInstance().reloadFromDisk(doc); PsiDocumentManager.getInstance(project).commitDocument(doc); mgr.reloadFromDisk(file); PsiUtilCore.ensureValid(file); return file; } }));
private static void commitChanges(final Document doc) { final FileDocumentManager manager = FileDocumentManager.getInstance(); if (!manager.isDocumentUnsaved(doc) && (doc instanceof VimDocument)) { manager.saveDocument(doc);
msg.append("[RO] "); else if (FileDocumentManager.getInstance().isDocumentUnsaved(doc)) { msg.append("[+] ");
private void beforeDocumentsSaving() { Document[] unsavedDocuments = FileDocumentManager.getInstance().getUnsavedDocuments(); LOGGER.info("[+] Start SaveActionManager#beforeAllDocumentsSaving"); LOGGER.info("Unsaved documents (" + unsavedDocuments.length + "): " + Arrays.asList(unsavedDocuments)); Map<Project, Set<PsiFile>> projectPsiFiles = new HashMap<>(); stream(unsavedDocuments) .forEach(document -> stream(ProjectManager.getInstance().getOpenProjects()) .forEach(project -> Optional .ofNullable(PsiDocumentManager.getInstance(project).getPsiFile(document)) .map(psiFile -> { Set<PsiFile> psiFiles = projectPsiFiles.getOrDefault(project, new HashSet<>()); projectPsiFiles.put(project, psiFiles); return psiFiles.add(psiFile); }))); projectPsiFiles.forEach(((project, psiFiles) -> { processPsiFilesIfNecessary(project, psiFiles, activate, normal); })); LOGGER.info("End SaveActionManager#beforeAllDocumentsSaving processed " + unsavedDocuments.length + " documents"); }
@Override public void documentChanged(DocumentEvent event) { if (!globalSettings.isAutoTrigger()) { return; } VirtualFile file = docManager.getFile(event.getDocument()); if (file == null) { return; } Project project = utils.guessProjectForFile(file); if (project == null || !project.equals(myProject)) { return; } eventMap.put(file, System.currentTimeMillis()); }
@CheckForNull private ClientInputFile createClientInputFile(Module module, VirtualFile virtualFile, VirtualFileTestPredicate testPredicate) { boolean test = testPredicate.test(virtualFile); Charset charset = getEncoding(virtualFile); String relativePath = appUtils.getRelativePathForAnalysis(module, virtualFile); if (relativePath != null) { if (fileDocumentManager.isFileModified(virtualFile)) { return new DefaultClientInputFile(virtualFile, relativePath, test, charset, fileDocumentManager.getDocument(virtualFile)); } else { return new DefaultClientInputFile(virtualFile, relativePath, test, charset); } } return null; }
private void setupEditor(Project project) { ApplicationManager.getApplication().runWriteAction(() -> { try { VirtualFile file = FileUtil.getScratchFile(project, "Neo4jGraphDbConsoleParametersPanel.json"); Document document = FILE_DOCUMENT_MANAGER.getDocument(file); globalParamEditor = createEditor(project, document); JLabel jLabel = new JLabel("<html>Global parameters:</html>"); jLabel.setIcon(ICON_HELP); jLabel.setToolTipText("Enter parameters in JSON format. Will be applied to any data source when executed"); globalParamEditor.setHeaderComponent(jLabel); setInitialContent(document); initializeUi(); } catch (Throwable e) { Throwables.throwIfUnchecked(e); throw new RuntimeException(e); } }); }
public void cherryPickChange(final Project project, final ChangeInfo changeInfo, final String revisionId) { fileDocumentManager.saveAllDocuments(); platformFacade.getChangeListManager(project).blockModalNotifications();
public static void commitAndSaveDocument(PsiDocumentManager psiDocumentManager, Document document) { if (document != null) { psiDocumentManager.doPostponedOperationsAndUnblockDocument(document); psiDocumentManager.commitDocument(document); FileDocumentManager.getInstance().saveDocument(document); } } }
@Override protected void configure() { bind(Logger.class).toInstance(LOG); bind(Application.class).toInstance(ApplicationManager.getApplication()); bind(DiffManager.class).toInstance(DiffManager.getInstance()); bind(LocalFileSystem.class).toInstance(LocalFileSystem.getInstance()); bind(Git.class).toInstance(ServiceManager.getService(Git.class)); bind(GitPlatformFacade.class).toInstance(ServiceManager.getService(GitPlatformFacade.class)); bind(FileDocumentManager.class).toInstance(FileDocumentManager.getInstance()); bind(VirtualFileManager.class).toInstance(VirtualFileManager.getInstance()); bind(ShowSettingsUtil.class).toInstance(ShowSettingsUtil.getInstance()); bind(DataManager.class).toInstance(DataManager.getInstance()); bind(JBPopupFactory.class).toInstance(JBPopupFactory.getInstance()); } }