use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class StudyRunAction method run.
public void run(@NotNull final Project project) {
if (myHandler != null && !myHandler.isProcessTerminated())
return;
Editor selectedEditor = StudyUtils.getSelectedEditor(project);
FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
assert selectedEditor != null;
VirtualFile openedFile = fileDocumentManager.getFile(selectedEditor.getDocument());
if (openedFile != null && openedFile.getCanonicalPath() != null) {
String filePath = openedFile.getCanonicalPath();
executeFile(project, openedFile, filePath);
}
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class StudyWindowNavigationAction method navigateToPlaceholder.
private void navigateToPlaceholder(@NotNull final Project project) {
final Editor selectedEditor = StudyUtils.getSelectedEditor(project);
if (selectedEditor != null) {
final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
final VirtualFile openedFile = fileDocumentManager.getFile(selectedEditor.getDocument());
if (openedFile != null) {
final TaskFile selectedTaskFile = StudyUtils.getTaskFile(project, openedFile);
if (selectedTaskFile != null) {
final int offset = selectedEditor.getCaretModel().getOffset();
final AnswerPlaceholder targetPlaceholder = getTargetPlaceholder(selectedTaskFile, offset);
if (targetPlaceholder == null) {
return;
}
StudyNavigator.navigateToAnswerPlaceholder(selectedEditor, targetPlaceholder);
}
}
}
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class StudySmartChecker method smartCheck.
public static void smartCheck(@NotNull final AnswerPlaceholder placeholder, @NotNull final Project project, @NotNull final VirtualFile answerFile, @NotNull final TaskFile answerTaskFile, @NotNull final TaskFile usersTaskFile, @NotNull final StudyTestRunner testRunner, @NotNull final VirtualFile virtualFile, @NotNull final Document usersDocument) {
VirtualFile fileWindows = null;
File resourceFile = null;
VirtualFile windowCopy = null;
try {
final int index = placeholder.getIndex();
String windowCopyName = answerFile.getNameWithoutExtension() + index + EduNames.WINDOW_POSTFIX + answerFile.getExtension();
windowCopy = answerFile.copy(project, answerFile.getParent(), windowCopyName);
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document windowDocument = documentManager.getDocument(windowCopy);
if (windowDocument != null) {
resourceFile = StudyUtils.copyResourceFile(virtualFile.getName(), windowCopy.getName(), project, usersTaskFile.getTask());
TaskFile windowTaskFile = answerTaskFile.getTask().copy().getTaskFile(StudyUtils.pathRelativeToTask(virtualFile));
if (windowTaskFile == null) {
return;
}
EduDocumentListener listener = new EduDocumentListener(windowTaskFile);
windowDocument.addDocumentListener(listener);
int start = placeholder.getOffset();
int end = start + placeholder.getRealLength();
final AnswerPlaceholder userAnswerPlaceholder = usersTaskFile.getAnswerPlaceholders().get(placeholder.getIndex());
int userStart = userAnswerPlaceholder.getOffset();
int userEnd = userStart + userAnswerPlaceholder.getRealLength();
String text = usersDocument.getText(new TextRange(userStart, userEnd));
windowDocument.replaceString(start, end, text);
ApplicationManager.getApplication().runWriteAction(() -> documentManager.saveDocument(windowDocument));
fileWindows = EduUtils.flushWindows(windowTaskFile, windowCopy);
Process smartTestProcess = testRunner.createCheckProcess(project, windowCopy.getPath());
final CapturingProcessHandler handler = new CapturingProcessHandler(smartTestProcess, null, windowCopy.getPath());
final ProcessOutput output = handler.runProcess();
final Course course = StudyTaskManager.getInstance(project).getCourse();
if (course != null) {
boolean res = StudyTestsOutputParser.getTestsOutput(output, course.isAdaptive()).isSuccess();
StudyTaskManager.getInstance(project).setStatus(userAnswerPlaceholder, res ? StudyStatus.Solved : StudyStatus.Failed);
}
}
} catch (ExecutionException | IOException e) {
LOG.error(e);
} finally {
StudyUtils.deleteFile(windowCopy);
StudyUtils.deleteFile(fileWindows);
if (resourceFile != null && resourceFile.exists() && !resourceFile.delete()) {
LOG.error("failed to delete", resourceFile.getPath());
}
}
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class VirtualFileDiffElement method refreshFile.
public static void refreshFile(boolean userInitiated, VirtualFile virtualFile) {
if (userInitiated) {
final List<Document> docsToSave = new ArrayList<>();
final FileDocumentManager manager = FileDocumentManager.getInstance();
for (Document document : manager.getUnsavedDocuments()) {
VirtualFile file = manager.getFile(document);
if (file != null && VfsUtilCore.isAncestor(virtualFile, file, false)) {
docsToSave.add(document);
}
}
if (!docsToSave.isEmpty()) {
new WriteAction() {
@Override
protected void run(@NotNull Result result) throws Throwable {
for (Document document : docsToSave) {
manager.saveDocument(document);
}
}
}.execute();
}
ModalityState modalityState = ProgressManager.getInstance().getProgressIndicator().getModalityState();
VfsUtil.markDirty(true, true, virtualFile);
RefreshQueue.getInstance().refresh(false, true, null, modalityState, virtualFile);
}
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class BaseDiffAction method actionPerformed.
public void actionPerformed(AnActionEvent e) {
DiffRequest diffData = getDiffData(e.getDataContext());
if (diffData == null)
return;
final DiffContent[] contents = diffData.getContents();
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
ApplicationManager.getApplication().runWriteAction(() -> {
for (DiffContent content : contents) {
Document document = content.getDocument();
if (document != null) {
documentManager.saveDocument(document);
}
}
});
DiffManager.getInstance().getDiffTool().show(diffData);
}
Aggregations