use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class StudyCheckUtils method getCopyWithAnswers.
private static Pair<VirtualFile, TaskFile> getCopyWithAnswers(@NotNull final VirtualFile taskDir, @NotNull final VirtualFile file, @NotNull final TaskFile source) {
try {
VirtualFile answerFile = file.copy(taskDir, taskDir, file.getNameWithoutExtension() + EduNames.ANSWERS_POSTFIX + "." + file.getExtension());
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document document = documentManager.getDocument(answerFile);
if (document != null) {
TaskFile answerTaskFile = source.getTask().copy().getTaskFile(StudyUtils.pathRelativeToTask(file));
if (answerTaskFile == null) {
return null;
}
EduDocumentListener listener = new EduDocumentListener(answerTaskFile);
document.addDocumentListener(listener);
for (AnswerPlaceholder answerPlaceholder : answerTaskFile.getActivePlaceholders()) {
final int start = answerPlaceholder.getOffset();
final int end = start + answerPlaceholder.getRealLength();
final String text = answerPlaceholder.getPossibleAnswer();
document.replaceString(start, end, text);
}
ApplicationManager.getApplication().runWriteAction(() -> documentManager.saveDocument(document));
return Pair.create(answerFile, answerTaskFile);
}
} catch (IOException e) {
LOG.error(e);
}
return null;
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project Intellij-Discord-Integration by Almighty-Alpaca.
the class DocumentListener method documentChanged.
@Override
public void documentChanged(DocumentEvent event) {
LOG.trace("DocumentListener#documentChanged({})", event.getDocument());
Document document = event.getDocument();
Editor[] editors = EditorFactory.getInstance().getEditors(document);
FileDocumentManager documentManager = FileDocumentManager.getInstance();
for (Editor editor : editors) {
Project project = editor.getProject();
DiscordIntegrationProjectComponent component = DiscordIntegrationProjectComponent.getInstance(project);
if (component != null)
component.updateTimeAccessed(documentManager.getFile(document));
}
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project Intellij-Discord-Integration by Almighty-Alpaca.
the class VisibleAreaListener method visibleAreaChanged.
@Override
public void visibleAreaChanged(VisibleAreaEvent event) {
LOG.trace("VisibleAreaListener#visibleAreaChanged({})", 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()));
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project sonarlint-intellij by SonarSource.
the class SonarLintUtils method getSelectedFile.
/**
* FileEditorManager#getSelectedFiles does not work as expected. In split editors, the order of the files does not change depending
* on which one of the split editors is selected.
* This seems to work well with split editors.
*/
@CheckForNull
public static VirtualFile getSelectedFile(Project project) {
if (project.isDisposed()) {
return null;
}
FileEditorManager editorManager = FileEditorManager.getInstance(project);
Editor editor = editorManager.getSelectedTextEditor();
if (editor != null) {
Document doc = editor.getDocument();
FileDocumentManager docManager = FileDocumentManager.getInstance();
return docManager.getFile(doc);
}
return null;
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project sonarlint-intellij by SonarSource.
the class SonarLintUtils method saveFile.
/**
* 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;
}
Aggregations