<commit_before>package ru.compscicenter.edide;

import com.intellij.execution.RunManager;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.configurations.ConfigurationFactory;
import com.intellij.execution.configurations.ConfigurationTypeUtil;
import com.intellij.facet.ui.ValidationResult;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Log;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.platform.DirectoryProjectGenerator;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.*;

/**
 * User: lia
 */
public class StudyDirectoryProjectGenerator implements DirectoryProjectGenerator {
    private RunManager runManager;
    private RunnerAndConfigurationSettings runConfiguration;

    @Nls
    @NotNull
    @Override
    public String getName() {
        return "Study project";
    }

    @Nullable
    @Override
    public Object showGenerationSettings(VirtualFile baseDir) throws ProcessCanceledException {
        return null;
    }

    private void makeRunConfiguration(@NotNull Project project, @NotNull final VirtualFile baseDir) {
        runManager = RunManager.getInstance(project);
        StudyConfigurationType configurationType = ConfigurationTypeUtil.findConfigurationType(StudyConfigurationType.class);
        ConfigurationFactory[] factories = configurationType.getConfigurationFactories();
        runConfiguration = runManager.createRunConfiguration("Study test configuration", factories[0]);

        StudyUnitTestRunConfiguration configuration = (StudyUnitTestRunConfiguration) runConfiguration.getConfiguration();

        try {
            configuration.setScriptName(baseDir.findChild("task1").findChild("task1_tests.py").getCanonicalPath()); //TODO: get current task name
            //configuration.setScriptName(baseDir.findChild("task3").findChild("task3_tests.py").getCanonicalPath());
        } catch (Exception e) {
            Log.print("Can not find test script for run configuration");
        }

        runManager.addConfiguration(runConfiguration, true);
        runManager.setSelectedConfiguration(runConfiguration);
    }

    public void createFile(String name, VirtualFile directory) throws IOException {
        VirtualFile currentFile = directory.createChildData(this, name);
        currentFile.setWritable(true);
        InputStream ip = StudyDirectoryProjectGenerator.class.getResourceAsStream(name);
        BufferedReader bf = new BufferedReader(new InputStreamReader(ip));
        OutputStream os = currentFile.getOutputStream(this);
        PrintWriter printWriter = new PrintWriter(os);
        while (bf.ready()) {
            printWriter.println(bf.readLine());
        }
        bf.close();
        printWriter.close();
    }

    @Override
    public void generateProject(@NotNull Project project, @NotNull final VirtualFile baseDir,
                                @Nullable Object settings, @NotNull Module module) {

        ApplicationManager.getApplication().runWriteAction(new Runnable() {
            @Override
            public void run() {
                try {
                    TaskManager taskManager = TaskManager.getInstance();
                    taskManager.setCurrentTask(0);
                    int tasksNumber = taskManager.getTasksNum();
                    for (int task = 0; task < tasksNumber; task++) {
                        VirtualFile taskDirectory = baseDir.createChildDirectory(this, "task" + (task + 1));
                        for (int file = 0; file < taskManager.getTaskFileNum(task); file++) {
                            final String curFileName = taskManager.getFileName(task, file);
                            createFile(curFileName, taskDirectory);
                        }

                    }
                    createFile("task1_tests.py", baseDir.findChild("task1"));
                    createFile("task2_tests.py", baseDir.findChild("task2"));
                    createFile("study_utrunner.py", baseDir.findChild(".idea"));
                    createFile("study_tcunittest.py", baseDir.findChild(".idea"));

                } catch (IOException e) {
                    Log.print("Problems with creating files");
                    Log.print(e.toString());
                    Log.flush();
                }

            }
        });
        makeRunConfiguration(project, baseDir);

    }

    @NotNull
    @Override
    public ValidationResult validate(@NotNull String s) {
        return ValidationResult.OK;
    }
}<commit_msg>It seems that IDE  doesn't crush now.<commit_after>package ru.compscicenter.edide;

import com.intellij.execution.RunManager;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.configurations.ConfigurationFactory;
import com.intellij.execution.configurations.ConfigurationTypeUtil;
import com.intellij.facet.ui.ValidationResult;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Log;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.platform.DirectoryProjectGenerator;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.*;

/**
 * User: lia
 */
public class StudyDirectoryProjectGenerator implements DirectoryProjectGenerator {
    private RunManager runManager;
    private RunnerAndConfigurationSettings runConfiguration;

    @Nls
    @NotNull
    @Override
    public String getName() {
        return "Study project";
    }

    @Nullable
    @Override
    public Object showGenerationSettings(VirtualFile baseDir) throws ProcessCanceledException {
        return null;
    }

    private void makeRunConfiguration(@NotNull Project project, @NotNull final VirtualFile baseDir) {
        runManager = RunManager.getInstance(project);
        StudyConfigurationType configurationType = ConfigurationTypeUtil.findConfigurationType(StudyConfigurationType.class);
        ConfigurationFactory[] factories = configurationType.getConfigurationFactories();
        runConfiguration = runManager.createRunConfiguration("Study test configuration", factories[0]);

        StudyUnitTestRunConfiguration configuration = (StudyUnitTestRunConfiguration) runConfiguration.getConfiguration();

        try {
            configuration.setScriptName(baseDir.findChild("task1").findChild("task1_tests.py").getCanonicalPath()); //TODO: get current task name
            //configuration.setScriptName(baseDir.findChild("task3").findChild("task3_tests.py").getCanonicalPath());
        } catch (Exception e) {
            Log.print("Can not find test script for run configuration");
        }

        runManager.addConfiguration(runConfiguration, true);
        runManager.setSelectedConfiguration(runConfiguration);
    }

    public void createFile(String name, VirtualFile directory) throws IOException {
        VirtualFile currentFile = directory.createChildData(this, name);
        currentFile.setWritable(true);
        InputStream ip = StudyDirectoryProjectGenerator.class.getResourceAsStream(name);
        BufferedReader bf = new BufferedReader(new InputStreamReader(ip));
        OutputStream os = currentFile.getOutputStream(this);
        PrintWriter printWriter = new PrintWriter(os);
        while (bf.ready()) {
            printWriter.println(bf.readLine());
        }
        bf.close();
        printWriter.close();
    }

    @Override
    public void generateProject(@NotNull Project project, @NotNull final VirtualFile baseDir,
                                @Nullable Object settings, @NotNull Module module) {

        ApplicationManager.getApplication().invokeLater(
                new Runnable() {
                    @Override
                    public void run() {
                        ApplicationManager.getApplication().runWriteAction(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    TaskManager taskManager = TaskManager.getInstance();
                                    taskManager.setCurrentTask(0);
                                    int tasksNumber = taskManager.getTasksNum();
                                    for (int task = 0; task < tasksNumber; task++) {
                                        VirtualFile taskDirectory = baseDir.createChildDirectory(this, "task" + (task + 1));
                                        for (int file = 0; file < taskManager.getTaskFileNum(task); file++) {
                                            final String curFileName = taskManager.getFileName(task, file);
                                            createFile(curFileName, taskDirectory);
                                        }

                                    }
                                    createFile("task1_tests.py", baseDir.findChild("task1"));
                                    createFile("task2_tests.py", baseDir.findChild("task2"));
                                    createFile("study_utrunner.py", baseDir.findChild(".idea"));
                                    createFile("study_tcunittest.py", baseDir.findChild(".idea"));

                                } catch (IOException e) {
                                    Log.print("Problems with creating files");
                                    Log.print(e.toString());
                                    Log.flush();
                                }

                            }
                        });
                    }
                }
        );
        makeRunConfiguration(project, baseDir);

    }

    @NotNull
    @Override
    public ValidationResult validate(@NotNull String s) {
        return ValidationResult.OK;
    }
}
{'fraction_non_alphanumeric': 0.08843343770756239, 'fraction_numerical': 0.002467027232185217, 'mean_word_length': 2.4388254486133767, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '533453', 'n_tokens_mistral': 2554, 'n_tokens_neox': 2428, 'n_words': 489}