Search in sources :

Example 81 with Document

use of com.intellij.openapi.editor.Document 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());
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) TextRange(com.intellij.openapi.util.TextRange) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) ProcessOutput(com.intellij.execution.process.ProcessOutput) Course(com.jetbrains.edu.learning.courseFormat.Course) ExecutionException(com.intellij.execution.ExecutionException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) File(java.io.File) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler) EduDocumentListener(com.jetbrains.edu.learning.core.EduDocumentListener)

Example 82 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class EduAnswerPlaceholderPainter method createGuardedBlocks.

public static void createGuardedBlocks(@NotNull final Editor editor, AnswerPlaceholder placeholder) {
    Document document = editor.getDocument();
    if (document instanceof DocumentImpl) {
        DocumentImpl documentImpl = (DocumentImpl) document;
        List<RangeMarker> blocks = documentImpl.getGuardedBlocks();
        Pair<Integer, Integer> offsets = StudyUtils.getPlaceholderOffsets(placeholder, editor.getDocument());
        Integer start = offsets.first;
        Integer end = offsets.second;
        if (start != 0) {
            createGuardedBlock(editor, blocks, start - 1, start);
        }
        if (end != document.getTextLength()) {
            createGuardedBlock(editor, blocks, end, end + 1);
        }
    }
}
Also used : RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl)

Example 83 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class EduDocumentListener method documentChanged.

@Override
public void documentChanged(DocumentEvent e) {
    if (!myTaskFile.isTrackChanges()) {
        return;
    }
    if (myAnswerPlaceholders.isEmpty())
        return;
    if (e instanceof DocumentEventImpl) {
        DocumentEventImpl event = (DocumentEventImpl) e;
        Document document = e.getDocument();
        int offset = e.getOffset();
        int change = event.getNewLength() - event.getOldLength();
        for (AnswerPlaceholderWrapper answerPlaceholderWrapper : myAnswerPlaceholders) {
            int twStart = answerPlaceholderWrapper.getTwStart();
            if (twStart > offset) {
                twStart += change;
            }
            int twEnd = answerPlaceholderWrapper.getTwEnd();
            if (twEnd >= offset) {
                twEnd += change;
            }
            AnswerPlaceholder answerPlaceholder = answerPlaceholderWrapper.getAnswerPlaceholder();
            int length = twEnd - twStart;
            answerPlaceholder.setOffset(twStart);
            if (myTrackLength) {
                if (answerPlaceholder.getUseLength()) {
                    answerPlaceholder.setLength(length);
                } else {
                    if (answerPlaceholder.isActive() && myTaskFile.isTrackLengths()) {
                        answerPlaceholder.setPossibleAnswer(document.getText(TextRange.create(twStart, twStart + length)));
                    }
                }
            }
        }
    }
}
Also used : DocumentEventImpl(com.intellij.openapi.editor.impl.event.DocumentEventImpl) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) Document(com.intellij.openapi.editor.Document)

Example 84 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class EduUtils method flushWindows.

@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
@Nullable
public static VirtualFile flushWindows(@NotNull final TaskFile taskFile, @NotNull final VirtualFile file) {
    final VirtualFile taskDir = file.getParent();
    VirtualFile fileWindows = null;
    final Document document = FileDocumentManager.getInstance().getDocument(file);
    if (document == null) {
        LOG.debug("Couldn't flush windows");
        return null;
    }
    if (taskDir != null) {
        final String name = file.getNameWithoutExtension() + EduNames.WINDOWS_POSTFIX;
        deleteWindowsFile(taskDir, name);
        PrintWriter printWriter = null;
        try {
            fileWindows = taskDir.createChildData(taskFile, name);
            printWriter = new PrintWriter(new FileOutputStream(fileWindows.getPath()));
            for (AnswerPlaceholder answerPlaceholder : taskFile.getActivePlaceholders()) {
                int length = answerPlaceholder.getRealLength();
                int start = answerPlaceholder.getOffset();
                final String windowDescription = document.getText(new TextRange(start, start + length));
                printWriter.println("#educational_plugin_window = " + windowDescription);
            }
            ApplicationManager.getApplication().runWriteAction(() -> FileDocumentManager.getInstance().saveDocument(document));
        } catch (IOException e) {
            LOG.error(e);
        } finally {
            if (printWriter != null) {
                printWriter.close();
            }
            synchronize();
        }
    }
    return fileWindows;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileOutputStream(java.io.FileOutputStream) TextRange(com.intellij.openapi.util.TextRange) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) PrintWriter(java.io.PrintWriter) Nullable(org.jetbrains.annotations.Nullable)

Example 85 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class EduUtils method copyFile.

public static VirtualFile copyFile(Object requestor, VirtualFile toDir, VirtualFile file) {
    Document document = FileDocumentManager.getInstance().getDocument(file);
    if (document != null) {
        FileDocumentManager.getInstance().saveDocument(document);
    }
    String taskRelativePath = StudyUtils.pathRelativeToTask(file);
    try {
        VirtualFile userFile = toDir.findFileByRelativePath(taskRelativePath);
        if (userFile != null) {
            userFile.delete(requestor);
        }
        return VfsUtil.copyFileRelative(requestor, file, toDir, taskRelativePath);
    } catch (IOException e) {
        LOG.info("Failed to create file " + taskRelativePath + "  in folder " + toDir.getPath(), e);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document)

Aggregations

Document (com.intellij.openapi.editor.Document)1075 VirtualFile (com.intellij.openapi.vfs.VirtualFile)246 PsiFile (com.intellij.psi.PsiFile)191 Project (com.intellij.openapi.project.Project)164 NotNull (org.jetbrains.annotations.NotNull)138 Nullable (org.jetbrains.annotations.Nullable)129 TextRange (com.intellij.openapi.util.TextRange)122 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)117 PsiElement (com.intellij.psi.PsiElement)107 Editor (com.intellij.openapi.editor.Editor)104 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)56 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)45 IncorrectOperationException (com.intellij.util.IncorrectOperationException)43 IOException (java.io.IOException)42 RangeMarker (com.intellij.openapi.editor.RangeMarker)35 MockVirtualFile (com.intellij.mock.MockVirtualFile)33 File (java.io.File)32 ArrayList (java.util.ArrayList)32 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)30 List (java.util.List)29