Search in sources :

Example 1 with AnswerPlaceholder

use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.

the class StudyNextWindowAction method getTargetPlaceholder.

@Override
protected AnswerPlaceholder getTargetPlaceholder(@NotNull final TaskFile taskFile, int offset) {
    final AnswerPlaceholder selectedAnswerPlaceholder = taskFile.getAnswerPlaceholder(offset);
    final List<AnswerPlaceholder> placeholders = taskFile.getActivePlaceholders();
    if (selectedAnswerPlaceholder == null) {
        for (AnswerPlaceholder placeholder : placeholders) {
            if (placeholder.getOffset() > offset) {
                return placeholder;
            }
        }
    } else {
        int index = selectedAnswerPlaceholder.getIndex();
        if (StudyUtils.indexIsValid(index, placeholders)) {
            int newIndex = index + 1;
            return placeholders.get(newIndex == placeholders.size() ? 0 : newIndex);
        }
    }
    return null;
}
Also used : AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)

Example 2 with AnswerPlaceholder

use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.

the class StudyPrevWindowAction method getTargetPlaceholder.

@Nullable
@Override
protected AnswerPlaceholder getTargetPlaceholder(@NotNull final TaskFile taskFile, int offset) {
    final AnswerPlaceholder selectedAnswerPlaceholder = taskFile.getAnswerPlaceholder(offset);
    final List<AnswerPlaceholder> placeholders = taskFile.getActivePlaceholders();
    if (selectedAnswerPlaceholder == null) {
        for (int i = placeholders.size() - 1; i >= 0; i--) {
            final AnswerPlaceholder placeholder = placeholders.get(i);
            if (placeholder.getOffset() < offset) {
                return placeholder;
            }
        }
    } else {
        int prevIndex = selectedAnswerPlaceholder.getIndex() - 1;
        if (StudyUtils.indexIsValid(prevIndex, placeholders)) {
            return placeholders.get(prevIndex);
        }
    }
    return null;
}
Also used : AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with AnswerPlaceholder

use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder 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);
            }
        }
    }
}
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) Editor(com.intellij.openapi.editor.Editor)

Example 4 with AnswerPlaceholder

use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder 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 5 with AnswerPlaceholder

use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder 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)

Aggregations

AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)25 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)12 Document (com.intellij.openapi.editor.Document)9 Editor (com.intellij.openapi.editor.Editor)7 Project (com.intellij.openapi.project.Project)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 AnswerPlaceholderSubtaskInfo (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo)4 UnexpectedUndoException (com.intellij.openapi.command.undo.UnexpectedUndoException)3 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)3 TextRange (com.intellij.openapi.util.TextRange)3 PsiFile (com.intellij.psi.PsiFile)3 File (java.io.File)3 BasicUndoableAction (com.intellij.openapi.command.undo.BasicUndoableAction)2 StudyState (com.jetbrains.edu.learning.StudyState)2 EduDocumentListener (com.jetbrains.edu.learning.core.EduDocumentListener)2 Course (com.jetbrains.edu.learning.courseFormat.Course)2 StudyEditor (com.jetbrains.edu.learning.editor.StudyEditor)2 IOException (java.io.IOException)2 Nullable (org.jetbrains.annotations.Nullable)2 Gson (com.google.gson.Gson)1