Search in sources :

Example 1 with SupportedLanguages

use of org.stepik.core.SupportedLanguages in project intellij-plugins by StepicOrg.

the class StudyToolWindow method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    if (stepNode == null) {
        return;
    }
    SupportedLanguages language = (SupportedLanguages) languageBox.getSelectedItem();
    if (language == null) {
        return;
    }
    final StepNode targetNode = stepNode;
    executor.execute(() -> {
        final SupportedLanguages[] selectedLang = new SupportedLanguages[1];
        ApplicationManager.getApplication().invokeAndWait(() -> selectedLang[0] = (SupportedLanguages) languageBox.getSelectedItem());
        if (selectedLang[0] != null) {
            ProgrammingLanguageUtils.switchProgrammingLanguage(project, targetNode, selectedLang[0]);
            if (selectedLang[0] != targetNode.getCurrentLang()) {
                ApplicationManager.getApplication().invokeLater(() -> languageBox.setSelectedItem(targetNode.getCurrentLang()));
            }
        }
    });
}
Also used : SupportedLanguages(org.stepik.core.SupportedLanguages) StepNode(org.stepik.core.courseFormat.StepNode)

Example 2 with SupportedLanguages

use of org.stepik.core.SupportedLanguages in project intellij-plugins by StepicOrg.

the class ProgrammingLanguageUtils method switchProgrammingLanguage.

public static void switchProgrammingLanguage(@NotNull Project project, @NotNull StepNode targetStepNode, @NotNull SupportedLanguages language) {
    if (!targetStepNode.getSupportedLanguages().contains(language)) {
        return;
    }
    SupportedLanguages currentLang = targetStepNode.getCurrentLang();
    String currentMainFileName = currentLang.getMainFileName();
    String mainFilePath = String.join("/", targetStepNode.getPath(), EduNames.SRC, currentMainFileName);
    VirtualFile mainFile = project.getBaseDir().findFileByRelativePath(mainFilePath);
    boolean mainFileExists = mainFile != null;
    if (currentLang == language && mainFileExists) {
        return;
    }
    if (currentMainFileName.equals(language.getMainFileName()) && mainFileExists) {
        targetStepNode.setCurrentLang(language);
        Metrics.switchLanguage(project, targetStepNode, SUCCESSFUL);
        return;
    }
    PsiDirectory src = getOrCreateSrcPsiDirectory(project, targetStepNode);
    if (src == null) {
        return;
    }
    PsiDirectory hide = getOrCreatePsiDirectory(project, src, EduNames.HIDE);
    if (hide == null) {
        return;
    }
    PsiFile second = findFile(src, language.getMainFileName());
    boolean moveSecond = second == null;
    if (moveSecond) {
        second = getOrCreateMainFile(project, hide.getVirtualFile(), language, targetStepNode);
        if (second == null) {
            logger.error("Can't create Main file: " + language.getMainFileName());
            return;
        }
    }
    PsiFile first = findFile(hide, currentMainFileName);
    boolean moveFirst = first == null;
    if (moveFirst) {
        first = findFile(src, currentMainFileName);
        moveFirst = !second.isEquivalentTo(first);
    }
    targetStepNode.setCurrentLang(language);
    ArrayList<VirtualFile> needClose = closeStepNodeFile(project, targetStepNode);
    PsiFile finalSecond = second;
    PsiFile finalFirst = first;
    boolean finalMoveFirst = moveFirst;
    ApplicationManager.getApplication().invokeAndWait(() -> {
        FileEditorManager.getInstance(project).openFile(finalSecond.getVirtualFile(), true);
        FileEditorManager editorManager = FileEditorManager.getInstance(project);
        needClose.forEach(editorManager::closeFile);
        exchangeFiles(src, hide, finalFirst, finalSecond, finalMoveFirst, moveSecond);
        ProjectView.getInstance(project).selectPsiElement(finalSecond, false);
    });
    Metrics.switchLanguage(project, targetStepNode, SUCCESSFUL);
}
Also used : SupportedLanguages(org.stepik.core.SupportedLanguages) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) ProjectFilesUtils.getOrCreateSrcPsiDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreateSrcPsiDirectory) ProjectFilesUtils.getOrCreatePsiDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreatePsiDirectory) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile)

Example 3 with SupportedLanguages

use of org.stepik.core.SupportedLanguages in project intellij-plugins by StepicOrg.

the class StepNode method getSupportedLanguages.

@NotNull
public List<SupportedLanguages> getSupportedLanguages() {
    if (supportedLanguages == null) {
        supportedLanguages = new ArrayList<>();
        BlockView block;
        Step data = getData();
        if (data == null) {
            return supportedLanguages;
        }
        block = data.getBlock();
        if (getType() == StepType.CODE) {
            BlockViewOptions options = block.getOptions();
            Map<String, String> templates = options.getCodeTemplates();
            templates.keySet().forEach(key -> {
                SupportedLanguages language = SupportedLanguages.langOfName(key);
                if (language != INVALID && !supportedLanguages.contains(language)) {
                    supportedLanguages.add(language);
                }
            });
        }
    }
    return supportedLanguages;
}
Also used : SupportedLanguages(org.stepik.core.SupportedLanguages) BlockView(org.stepik.api.objects.steps.BlockView) Step(org.stepik.api.objects.steps.Step) BlockViewOptions(org.stepik.api.objects.steps.BlockViewOptions) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with SupportedLanguages

use of org.stepik.core.SupportedLanguages in project intellij-plugins by StepicOrg.

the class StepikSendAction method getSubmissionId.

@Nullable
private static Long getSubmissionId(@NotNull Project project, @NotNull StepikApiClient stepikApiClient, @NotNull StepNode stepNode, long intAttemptId) {
    SupportedLanguages currentLang = stepNode.getCurrentLang();
    String code = getCode(project, stepNode, currentLang);
    if (code == null) {
        logger.info(String.format("Sending step failed: id=%s. Step content is null", stepNode.getId()));
        return null;
    }
    Submissions submissions;
    try {
        submissions = stepikApiClient.submissions().post().attempt(intAttemptId).language(currentLang.getName()).code(code).execute();
        if (submissions.isEmpty()) {
            notifyFailed(project, stepNode, "Submissions is empty", null);
            return null;
        }
    } catch (StepikClientException e) {
        notifyFailed(project, stepNode, "Failed post submission", e);
        return null;
    }
    return submissions.getFirst().getId();
}
Also used : SupportedLanguages(org.stepik.core.SupportedLanguages) Submissions(org.stepik.api.objects.submissions.Submissions) StepikClientException(org.stepik.api.exceptions.StepikClientException) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with SupportedLanguages

use of org.stepik.core.SupportedLanguages in project intellij-plugins by StepicOrg.

the class JavaWizardStep method onWizardFinished.

@Override
public void onWizardFinished() throws CommitStepException {
    if (!(valid && leaving)) {
        return;
    }
    SupportedLanguages selectedLang = panel.getLanguage();
    generator.setDefaultLang(selectedLang);
    StudyObject studyObject = panel.getSelectedStudyObject();
    generator.createCourseNodeUnderProgress(project, studyObject);
    long id = studyObject.getId();
    if (id == 0) {
        return;
    }
    boolean wasEnrollment = ProjectWizardUtils.enrollmentCourse(studyObject);
    if (wasEnrollment) {
        logger.warn("User didn't enrollment on course: " + id);
    }
    String messageTemplate = "Leaving step the project wizard with the selected study object: type=%s, id = %s, name = %s";
    logger.info(String.format(messageTemplate, studyObject.getClass().getSimpleName(), id, studyObject.getTitle()));
}
Also used : SupportedLanguages(org.stepik.core.SupportedLanguages) StudyObject(org.stepik.api.objects.StudyObject)

Aggregations

SupportedLanguages (org.stepik.core.SupportedLanguages)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 StepNode (org.stepik.core.courseFormat.StepNode)4 Document (com.intellij.openapi.editor.Document)3 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)3 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)2 Project (com.intellij.openapi.project.Project)2 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiFile (com.intellij.psi.PsiFile)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 StepikApiClient (org.stepik.api.client.StepikApiClient)2 StepikClientException (org.stepik.api.exceptions.StepikClientException)2 Submission (org.stepik.api.objects.submissions.Submission)2 Submissions (org.stepik.api.objects.submissions.Submissions)2 StudyNode (org.stepik.core.courseFormat.StudyNode)2 StepikAuthManager.authAndGetStepikApiClient (org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient)2 ProjectFilesUtils.getOrCreatePsiDirectory (org.stepik.core.utils.ProjectFilesUtils.getOrCreatePsiDirectory)2 ProjectView (com.intellij.ide.projectView.ProjectView)1 Application (com.intellij.openapi.application.Application)1