Search in sources :

Example 16 with AnswerPlaceholder

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

the class StudyRefreshAnswerPlaceholder method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();
    if (project == null) {
        return;
    }
    for (StudyActionListener listener : Extensions.getExtensions(StudyActionListener.EP_NAME)) {
        listener.beforeCheck(e);
    }
    final AnswerPlaceholder answerPlaceholder = getAnswerPlaceholder(e);
    if (answerPlaceholder == null) {
        return;
    }
    StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
    final StudyState studyState = new StudyState(studyEditor);
    if (answerPlaceholder.getTaskFile().getTask().hasSubtasks()) {
        StudySubtaskUtils.refreshPlaceholder(studyState.getEditor(), answerPlaceholder);
        return;
    }
    Document patternDocument = StudyUtils.getPatternDocument(answerPlaceholder.getTaskFile(), studyState.getVirtualFile().getName());
    if (patternDocument == null) {
        return;
    }
    AnswerPlaceholder.MyInitialState initialState = answerPlaceholder.getInitialState();
    int startOffset = initialState.getOffset();
    final String text = patternDocument.getText(new TextRange(startOffset, startOffset + initialState.getLength()));
    CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
        Document document = studyState.getEditor().getDocument();
        int offset = answerPlaceholder.getOffset();
        document.deleteString(offset, offset + answerPlaceholder.getRealLength());
        document.insertString(offset, text);
    }), NAME, null);
}
Also used : Project(com.intellij.openapi.project.Project) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) StudyEditor(com.jetbrains.edu.learning.editor.StudyEditor) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document)

Example 17 with AnswerPlaceholder

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

the class StudyShowHintAction method showHint.

public void showHint(Project project) {
    Course course = StudyTaskManager.getInstance(project).getCourse();
    if (course == null) {
        return;
    }
    StudyState studyState = new StudyState(StudyUtils.getSelectedStudyEditor(project));
    if (!studyState.isValid()) {
        return;
    }
    PsiFile file = PsiManager.getInstance(project).findFile(studyState.getVirtualFile());
    final Editor editor = studyState.getEditor();
    int offset = editor.getCaretModel().getOffset();
    AnswerPlaceholder answerPlaceholder = studyState.getTaskFile().getAnswerPlaceholder(offset);
    if (file == null) {
        return;
    }
    EduUsagesCollector.hintShown();
    final StudyToolWindow hintComponent = getHint(project, answerPlaceholder).getStudyToolWindow();
    hintComponent.setPreferredSize(new Dimension(400, 150));
    showHintPopUp(project, studyState, editor, hintComponent);
}
Also used : AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) StudyState(com.jetbrains.edu.learning.StudyState) PsiFile(com.intellij.psi.PsiFile) StudyToolWindow(com.jetbrains.edu.learning.ui.StudyToolWindow) Course(com.jetbrains.edu.learning.courseFormat.Course) Editor(com.intellij.openapi.editor.Editor) StudyHint(com.jetbrains.edu.learning.ui.StudyHint)

Example 18 with AnswerPlaceholder

use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder 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;
}
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) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) EduDocumentListener(com.jetbrains.edu.learning.core.EduDocumentListener)

Example 19 with AnswerPlaceholder

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

the class StudyCheckUtils method runSmartTestProcess.

public static void runSmartTestProcess(@NotNull final VirtualFile taskDir, @NotNull final StudyTestRunner testRunner, @NotNull final String taskFileName, @NotNull final TaskFile taskFile, @NotNull final Project project) {
    final VirtualFile virtualFile = taskDir.findFileByRelativePath(taskFileName);
    if (virtualFile == null) {
        return;
    }
    Pair<VirtualFile, TaskFile> pair = getCopyWithAnswers(taskDir, virtualFile, taskFile);
    if (pair == null) {
        return;
    }
    VirtualFile answerFile = pair.getFirst();
    TaskFile answerTaskFile = pair.getSecond();
    try {
        for (final AnswerPlaceholder answerPlaceholder : answerTaskFile.getActivePlaceholders()) {
            final Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
            if (document == null) {
                continue;
            }
            StudySmartChecker.smartCheck(answerPlaceholder, project, answerFile, answerTaskFile, taskFile, testRunner, virtualFile, document);
        }
    } finally {
        StudyUtils.deleteFile(answerFile);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) Document(com.intellij.openapi.editor.Document)

Example 20 with AnswerPlaceholder

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

the class EduDocumentListener method beforeDocumentChange.

@Override
public void beforeDocumentChange(DocumentEvent e) {
    if (!myTaskFile.isTrackChanges()) {
        return;
    }
    myTaskFile.setHighlightErrors(true);
    myAnswerPlaceholders.clear();
    for (AnswerPlaceholder answerPlaceholder : myTaskFile.getAnswerPlaceholders()) {
        int twStart = answerPlaceholder.getOffset();
        int length = answerPlaceholder.getRealLength();
        int twEnd = twStart + length;
        myAnswerPlaceholders.add(new AnswerPlaceholderWrapper(answerPlaceholder, twStart, twEnd));
    }
}
Also used : AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)

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