<commit_before>package com.jetbrains.edu.learning.actions;

import com.intellij.ide.projectView.ProjectView;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowId;
import com.intellij.openapi.wm.ToolWindowManager;
import com.jetbrains.edu.learning.StudyState;
import com.jetbrains.edu.learning.StudyUtils;
import com.jetbrains.edu.learning.core.EduNames;
import com.jetbrains.edu.learning.courseFormat.Task;
import com.jetbrains.edu.learning.courseFormat.TaskFile;
import com.jetbrains.edu.learning.editor.StudyEditor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.tree.TreePath;
import java.util.Map;


abstract public class StudyTaskNavigationAction extends StudyActionWithShortcut {
  public StudyTaskNavigationAction(@Nullable String text, @Nullable String description, @Nullable Icon icon) {
    super(text, description, icon);
  }

  public void navigateTask(@NotNull final Project project) {
    StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
    StudyState studyState = new StudyState(studyEditor);
    if (!studyState.isValid()) {
      return;
    }
    Task nextTask = getTargetTask(studyState.getTask());
    if (nextTask == null) {
      return;
    }
    for (VirtualFile file : FileEditorManager.getInstance(project).getOpenFiles()) {
      FileEditorManager.getInstance(project).closeFile(file);
    }
    int nextTaskIndex = nextTask.getIndex();
    int lessonIndex = nextTask.getLesson().getIndex();
    Map<String, TaskFile> nextTaskFiles = nextTask.getTaskFiles();
    VirtualFile projectDir = project.getBaseDir();
    String lessonDirName = EduNames.LESSON + String.valueOf(lessonIndex);
    if (projectDir == null) {
      return;
    }
    VirtualFile lessonDir = projectDir.findChild(lessonDirName);
    if (lessonDir == null) {
      return;
    }
    String taskDirName = EduNames.TASK + String.valueOf(nextTaskIndex);
    VirtualFile taskDir = lessonDir.findChild(taskDirName);
    if (taskDir == null) {
      return;
    }
    if (nextTaskFiles.isEmpty()) {
      ProjectView.getInstance(project).select(taskDir, taskDir, false);
      return;
    }

    VirtualFile shouldBeActive = getFileToActivate(project, nextTaskFiles, taskDir);
    JTree tree = ProjectView.getInstance(project).getCurrentProjectViewPane().getTree();
    TreePath path = tree.getSelectionPath();
    if (path != null) {
      TreePath oldSelectionPath = path.getParentPath();
      if (oldSelectionPath != null) {
        tree.collapsePath(oldSelectionPath);
        tree.fireTreeCollapsed(oldSelectionPath);
      }
    }
    if (shouldBeActive != null) {
      ProjectView.getInstance(project).select(shouldBeActive, shouldBeActive, false);
      FileEditorManager.getInstance(project).openFile(shouldBeActive, true);
    }

    ToolWindow runToolWindow = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.RUN);
    if (runToolWindow != null) {
      runToolWindow.hide(null);
    }
  }

  @Nullable
  protected VirtualFile getFileToActivate(@NotNull Project project, Map<String, TaskFile> nextTaskFiles, VirtualFile taskDir) {
    VirtualFile shouldBeActive = null;
    for (Map.Entry<String, TaskFile> entry : nextTaskFiles.entrySet()) {
      String name = entry.getKey();
      TaskFile taskFile = entry.getValue();
      VirtualFile srcDir = taskDir.findChild(EduNames.SRC);
      VirtualFile vf = srcDir == null ? taskDir.findChild(name) : srcDir.findChild(name);
      if (vf != null) {
        FileEditorManager.getInstance(project).openFile(vf, true);
        if (!taskFile.getAnswerPlaceholders().isEmpty()) {
          shouldBeActive = vf;
        }<commit_msg>EDU-536 Open task file with placeholders when new task opened<commit_after>package com.jetbrains.edu.learning.actions;

import com.intellij.ide.projectView.ProjectView;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowId;
import com.intellij.openapi.wm.ToolWindowManager;
import com.jetbrains.edu.learning.StudyState;
import com.jetbrains.edu.learning.StudyUtils;
import com.jetbrains.edu.learning.core.EduNames;
import com.jetbrains.edu.learning.courseFormat.Task;
import com.jetbrains.edu.learning.courseFormat.TaskFile;
import com.jetbrains.edu.learning.editor.StudyEditor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.tree.TreePath;
import java.util.Map;


abstract public class StudyTaskNavigationAction extends StudyActionWithShortcut {
  public StudyTaskNavigationAction(@Nullable String text, @Nullable String description, @Nullable Icon icon) {
    super(text, description, icon);
  }

  public void navigateTask(@NotNull final Project project) {
    StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
    StudyState studyState = new StudyState(studyEditor);
    if (!studyState.isValid()) {
      return;
    }
    Task nextTask = getTargetTask(studyState.getTask());
    if (nextTask == null) {
      return;
    }
    for (VirtualFile file : FileEditorManager.getInstance(project).getOpenFiles()) {
      FileEditorManager.getInstance(project).closeFile(file);
    }
    int nextTaskIndex = nextTask.getIndex();
    int lessonIndex = nextTask.getLesson().getIndex();
    Map<String, TaskFile> nextTaskFiles = nextTask.getTaskFiles();
    VirtualFile projectDir = project.getBaseDir();
    String lessonDirName = EduNames.LESSON + String.valueOf(lessonIndex);
    if (projectDir == null) {
      return;
    }
    VirtualFile lessonDir = projectDir.findChild(lessonDirName);
    if (lessonDir == null) {
      return;
    }
    String taskDirName = EduNames.TASK + String.valueOf(nextTaskIndex);
    VirtualFile taskDir = lessonDir.findChild(taskDirName);
    if (taskDir == null) {
      return;
    }
    if (nextTaskFiles.isEmpty()) {
      ProjectView.getInstance(project).select(taskDir, taskDir, false);
      return;
    }

    VirtualFile shouldBeActive = getFileToActivate(project, nextTaskFiles, taskDir);
    JTree tree = ProjectView.getInstance(project).getCurrentProjectViewPane().getTree();
    TreePath path = tree.getSelectionPath();
    if (path != null) {
      TreePath oldSelectionPath = path.getParentPath();
      if (oldSelectionPath != null) {
        tree.collapsePath(oldSelectionPath);
        tree.fireTreeCollapsed(oldSelectionPath);
      }
    }
    if (shouldBeActive != null) {
      ProjectView.getInstance(project).select(shouldBeActive, shouldBeActive, false);
      FileEditorManager.getInstance(project).openFile(shouldBeActive, true);
    }

    ToolWindow runToolWindow = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.RUN);
    if (runToolWindow != null) {
      runToolWindow.hide(null);
    }
  }

  @Nullable
  protected VirtualFile getFileToActivate(@NotNull Project project, Map<String, TaskFile> nextTaskFiles, VirtualFile taskDir) {
    VirtualFile shouldBeActive = null;
    for (Map.Entry<String, TaskFile> entry : nextTaskFiles.entrySet()) {
      String name = entry.getKey();
      TaskFile taskFile = entry.getValue();
      VirtualFile srcDir = taskDir.findChild(EduNames.SRC);
      VirtualFile vf = srcDir == null ? taskDir.findChild(name) : srcDir.findChild(name);
      if (vf != null) {
        if (shouldBeActive != null) {
          FileEditorManager.getInstance(project).openFile(vf, true);
        }
        if (shouldBeActive == null && !taskFile.getAnswerPlaceholders().isEmpty()) {
          shouldBeActive = vf;
        }
{'fraction_non_alphanumeric': 0.10770577933450087, 'fraction_numerical': 0.0003752814610958219, 'mean_word_length': 4.249507550886408, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7665849', 'n_tokens_mistral': 2337, 'n_tokens_neox': 2295, 'n_words': 438}