/** * 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()); }
@Nullable private HashMap<Character, Mark> getAllFileMarks(@NotNull final Document doc) { VirtualFile vf = FileDocumentManager.getInstance().getFile(doc); if (vf == null) { return null; } HashMap<Character, Mark> res = new HashMap<>(); FileMarks<Character, Mark> fileMarks = getFileMarks(doc); if (fileMarks != null) { res.putAll(fileMarks); } for (Character ch : globalMarks.keySet()) { Mark mark = globalMarks.get(ch); if (vf.getPath().equals(mark.getFilename())) { res.put(ch, mark); } } return res; }
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; }
/** * Gets the map of marks for the specified file * * @param doc The editor to get the marks for * @return The map of marks. The keys are <code>Character</code>s of the mark names, the values are * <code>Mark</code>s. */ @Nullable private FileMarks<Character, Mark> getFileMarks(@NotNull final Document doc) { VirtualFile vf = FileDocumentManager.getInstance().getFile(doc); if (vf == null) { return null; } return getFileMarks(vf.getPath()); }
@NotNull private static Optional<Integer> offset(@NotNull String path, @NotNull Matcher matcher) { VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(path); Optional<Integer> offset = Optional.empty(); if (virtualFile != null) { Document document = FileDocumentManager.getInstance().getDocument(virtualFile); if (document != null) { int lineNumber = Integer.parseInt(matcher.group("lineNumber")); int start = document.getLineStartOffset(lineNumber - 1); @Nullable String formattedColumnNumber = matcher.group("columnNumber"); if (formattedColumnNumber != null) { int column = Integer.parseInt(formattedColumnNumber) - 1; offset = Optional.of(start + column); } else { offset = Optional.of(start); } } } return offset; }
@Override public void editorCreated(@NotNull EditorFactoryEvent event) { Document document = event.getEditor().getDocument(); VirtualFile file = FileDocumentManager.getInstance().getFile(document); if (file != null && file.getFileType() == GoFileType.INSTANCE) { checkForUpdates(); } } };
/** * 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; }
private static boolean isLineBreakpointAvailable(@NotNull VirtualFile file, int line, @NotNull Project project) { Document document = FileDocumentManager.getInstance().getDocument(file); if (document == null || document.getLineEndOffset(line) == document.getLineStartOffset(line)) return false; Checker canPutAtChecker = new Checker(); XDebuggerUtil.getInstance().iterateLine(project, document, line, canPutAtChecker); return canPutAtChecker.isLineBreakpointAvailable(); }
private static void resetCursor(@NotNull Editor editor, boolean insert) { Document doc = editor.getDocument(); VirtualFile vf = FileDocumentManager.getInstance().getFile(doc); if (vf != null) { resetCursor(vf, editor.getProject(), insert); } else { editor.getSettings().setBlockCursor(!insert); } }
@Nullable @Override public RunContentDescriptor execute(@NotNull RunProfileState state, @NotNull ExecutionEnvironment env) throws ExecutionException { if (state instanceof GoApplicationRunningState) { FileDocumentManager.getInstance().saveAllDocuments(); ((GoApplicationRunningState)state).setHistoryProcessHandler(myHistoryProcessListener); ((GoApplicationRunningState)state).setOutputFilePath(myOutputFilePath); ((GoApplicationRunningState)state).setCompilationFailed(myCompilationFailed); ExecutionResult executionResult = state.execute(env.getExecutor(), GoBuildingRunner.this); return executionResult != null ? new RunContentBuilder(executionResult, env).showRunContent(env.getContentToReuse()) : null; } return null; } }
@Override public void actionPerformed(@NotNull AnActionEvent e) { Project project = e.getProject(); assert project != null; FileDocumentManager.getInstance().saveAllDocuments(); for (Module module : GoSdkUtil.getGoModules(project)) { for (VirtualFile file : ModuleRootManager.getInstance(module).getContentRoots()) { fmt(project, module, "go fmt " + file.getPath(), file); } } }
private static boolean isLineBreakpointAvailable(@NotNull VirtualFile file, int line, @NotNull Project project) { Document document = FileDocumentManager.getInstance().getDocument(file); if (document == null || document.getLineEndOffset(line) == document.getLineStartOffset(line)) { return false; } Checker canPutAtChecker = new Checker(); XDebuggerUtil.getInstance().iterateLine(project, document, line, canPutAtChecker); // Used to identify line comments, which cannot be correctly processed by the checker. if (document.getText(new TextRange(document.getLineStartOffset(line), document.getLineEndOffset(line))).trim() .startsWith(BALLERINA_LINE_COMMENT_PREFIX)) { return false; } return canPutAtChecker.isLineBreakpointAvailable(); }
private static void resetCursor(@NotNull VirtualFile virtualFile, Project proj, boolean insert) { logger.debug("resetCursor"); Document doc = FileDocumentManager.getInstance().getDocument(virtualFile); if (doc == null) return; // Must be no text editor (such as image) Editor[] editors = EditorFactory.getInstance().getEditors(doc, proj); if (logger.isDebugEnabled()) { logger.debug("There are " + editors.length + " editors for virtual file " + virtualFile.getName()); } for (Editor editor : editors) { editor.getSettings().setBlockCursor(!insert); } }
@Nullable @Override protected RunContentDescriptor doExecute(@NotNull RunProfileState state, @NotNull ExecutionEnvironment environment) throws ExecutionException { assert state instanceof GoTestRunningState; GoTestRunningState runningState = (GoTestRunningState)state; GoTestRunConfiguration runConfiguration = ObjectUtils.tryCast(environment.getRunProfile(), GoTestRunConfiguration.class); if (runConfiguration == null) { return null; } FileDocumentManager.getInstance().saveAllDocuments(); CoverageEnabledConfiguration coverageEnabledConfiguration = CoverageEnabledConfiguration.getOrCreate(runConfiguration); runningState.setCoverageFilePath(coverageEnabledConfiguration.getCoverageFilePath()); ExecutionResult executionResult = state.execute(environment.getExecutor(), this); if (executionResult == null) { return null; } CoverageHelper.attachToProcess(runConfiguration, executionResult.getProcessHandler(), environment.getRunnerSettings()); return new RunContentBuilder(executionResult, environment).showRunContent(environment.getContentToReuse()); } }
if (state instanceof GoApplicationRunningState) { int port = findFreePort(); FileDocumentManager.getInstance().saveAllDocuments(); ((GoApplicationRunningState)state).setHistoryProcessHandler(myHistoryProcessListener); ((GoApplicationRunningState)state).setOutputFilePath(myOutputFilePath);
@Override public ReturnResult beforeCheckin(@Nullable CommitExecutor executor, PairConsumer<Object, Object> additionalDataConsumer) { if (enabled(panel)) { Ref<Boolean> success = Ref.create(true); FileDocumentManager.getInstance().saveAllDocuments(); for (PsiFile file : getPsiFiles()) { VirtualFile virtualFile = file.getVirtualFile(); new GoFmtFileAction().doSomething(virtualFile, ModuleUtilCore.findModuleForPsiElement(file), file.getProject(), "Go fmt", true, result -> { if (!result) success.set(false); }); } if (!success.get()) { return showErrorMessage(executor); } } return super.beforeCheckin(); }
throws ExecutionException { File outputFile = getOutputFile(environment, (GoApplicationRunningState)state); FileDocumentManager.getInstance().saveAllDocuments();
@Override protected void postProcess(PsiFile createdElement, String templateName, Map<String, String> customProperties) { if (createdElement instanceof GoFile) { GoPackageClause packageClause = ((GoFile)createdElement).getPackage(); if (packageClause == null) { return; } Project project = createdElement.getProject(); Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor(); if (editor == null) { return; } VirtualFile virtualFile = createdElement.getContainingFile().getVirtualFile(); if (virtualFile == null) { return; } if (FileDocumentManager.getInstance().getDocument(virtualFile) == editor.getDocument()) { editor.getCaretModel().moveToOffset(packageClause.getTextRange().getEndOffset()); } } }