Search in sources :

Example 31 with TaskFile

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

the class CCVirtualFileListener method fileCreated.

@Override
public void fileCreated(@NotNull VirtualFileEvent event) {
    VirtualFile createdFile = event.getFile();
    if (createdFile.isDirectory()) {
        return;
    }
    if (createdFile.getPath().contains(CCUtils.GENERATED_FILES_FOLDER)) {
        return;
    }
    Project project = ProjectUtil.guessProjectForFile(createdFile);
    if (project == null) {
        return;
    }
    if (project.getBasePath() != null && !FileUtil.isAncestor(project.getBasePath(), createdFile.getPath(), true)) {
        return;
    }
    Course course = StudyTaskManager.getInstance(project).getCourse();
    if (course == null || !CCUtils.isCourseCreator(project)) {
        return;
    }
    TaskFile taskFile = StudyUtils.getTaskFile(project, createdFile);
    if (taskFile != null) {
        return;
    }
    String taskRelativePath = StudyUtils.pathRelativeToTask(createdFile);
    CCLanguageManager manager = CCUtils.getStudyLanguageManager(course);
    if (manager != null && manager.doNotPackFile(new File(createdFile.getPath()))) {
        return;
    }
    if (CCUtils.isTestsFile(project, createdFile) || StudyUtils.isTaskDescriptionFile(createdFile.getName()) || taskRelativePath.contains(EduNames.WINDOW_POSTFIX) || taskRelativePath.contains(EduNames.WINDOWS_POSTFIX) || taskRelativePath.contains(EduNames.ANSWERS_POSTFIX)) {
        return;
    }
    VirtualFile taskVF = StudyUtils.getTaskDir(createdFile);
    if (taskVF == null) {
        return;
    }
    Task task = StudyUtils.getTask(project, taskVF);
    if (task == null) {
        return;
    }
    CCUtils.createResourceFile(createdFile, course, taskVF);
    task.addTaskFile(taskRelativePath, 1);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Project(com.intellij.openapi.project.Project) Task(com.jetbrains.edu.learning.courseFormat.Task) Course(com.jetbrains.edu.learning.courseFormat.Course) VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) File(java.io.File)

Example 32 with TaskFile

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

the class StudyEditorFactoryListener method editorCreated.

@Override
public void editorCreated(@NotNull final EditorFactoryEvent event) {
    final Editor editor = event.getEditor();
    final Project project = editor.getProject();
    if (project == null) {
        return;
    }
    final Document document = editor.getDocument();
    final VirtualFile openedFile = FileDocumentManager.getInstance().getFile(document);
    if (openedFile != null) {
        final TaskFile taskFile = StudyUtils.getTaskFile(project, openedFile);
        if (taskFile != null) {
            WolfTheProblemSolver.getInstance(project).clearProblems(openedFile);
            final ToolWindow studyToolWindow = ToolWindowManager.getInstance(project).getToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW);
            if (studyToolWindow != null) {
                StudyUtils.updateToolWindows(project);
                studyToolWindow.show(null);
            }
            Course course = StudyTaskManager.getInstance(project).getCourse();
            if (course == null) {
                return;
            }
            StudyEditor.addDocumentListener(document, new EduDocumentListener(taskFile, true));
            if (!taskFile.getAnswerPlaceholders().isEmpty()) {
                StudyNavigator.navigateToFirstAnswerPlaceholder(editor, taskFile);
                boolean isStudyProject = EduNames.STUDY.equals(course.getCourseMode());
                StudyUtils.drawAllAnswerPlaceholders(editor, taskFile);
                if (isStudyProject) {
                    editor.addEditorMouseListener(new WindowSelectionListener(taskFile));
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Project(com.intellij.openapi.project.Project) ToolWindow(com.intellij.openapi.wm.ToolWindow) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) Course(com.jetbrains.edu.learning.courseFormat.Course) EduDocumentListener(com.jetbrains.edu.learning.core.EduDocumentListener)

Example 33 with TaskFile

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

the class StudyRefreshAnswerPlaceholder method getAnswerPlaceholder.

@Nullable
private static AnswerPlaceholder getAnswerPlaceholder(AnActionEvent e) {
    final Project project = e.getProject();
    if (project == null) {
        return null;
    }
    StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
    final StudyState studyState = new StudyState(studyEditor);
    if (studyEditor == null || !studyState.isValid()) {
        return null;
    }
    final Editor editor = studyState.getEditor();
    final TaskFile taskFile = studyState.getTaskFile();
    return taskFile.getAnswerPlaceholder(editor.getCaretModel().getOffset());
}
Also used : TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Project(com.intellij.openapi.project.Project) StudyEditor(com.jetbrains.edu.learning.editor.StudyEditor) StudyEditor(com.jetbrains.edu.learning.editor.StudyEditor) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable)

Example 34 with TaskFile

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

the class StudyCheckUtils method drawAllPlaceholders.

public static void drawAllPlaceholders(@NotNull final Project project, @NotNull final Task task) {
    VirtualFile taskDir = task.getTaskDir(project);
    if (taskDir == null) {
        return;
    }
    for (Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
        String name = entry.getKey();
        TaskFile taskFile = entry.getValue();
        VirtualFile virtualFile = taskDir.findFileByRelativePath(name);
        if (virtualFile == null) {
            continue;
        }
        FileEditor fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(virtualFile);
        if (fileEditor instanceof StudyEditor) {
            StudyEditor studyEditor = (StudyEditor) fileEditor;
            StudyUtils.drawAllAnswerPlaceholders(studyEditor.getEditor(), taskFile);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) StudyEditor(com.jetbrains.edu.learning.editor.StudyEditor) Map(java.util.Map)

Example 35 with TaskFile

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

Aggregations

TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)44 VirtualFile (com.intellij.openapi.vfs.VirtualFile)30 Project (com.intellij.openapi.project.Project)14 Editor (com.intellij.openapi.editor.Editor)11 AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)11 Map (java.util.Map)10 Course (com.jetbrains.edu.learning.courseFormat.Course)9 Task (com.jetbrains.edu.learning.courseFormat.Task)8 Document (com.intellij.openapi.editor.Document)7 PsiFile (com.intellij.psi.PsiFile)7 StudyEditor (com.jetbrains.edu.learning.editor.StudyEditor)7 File (java.io.File)6 Nullable (org.jetbrains.annotations.Nullable)6 FileEditor (com.intellij.openapi.fileEditor.FileEditor)5 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)4 Lesson (com.jetbrains.edu.learning.courseFormat.Lesson)4 Presentation (com.intellij.openapi.actionSystem.Presentation)3 EduDocumentListener (com.jetbrains.edu.learning.core.EduDocumentListener)3 AnswerPlaceholderSubtaskInfo (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo)3 ExecutionException (com.intellij.execution.ExecutionException)2