Search in sources :

Example 6 with SupportedLanguages

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

the class DownloadSubmission method downloadSubmission.

private void downloadSubmission(@Nullable Project project) {
    if (project == null) {
        return;
    }
    StudyNode<?, ?> studyNode = StepikProjectManager.getSelected(project);
    if (!(studyNode instanceof StepNode)) {
        return;
    }
    StepNode stepNode = (StepNode) studyNode;
    String title = "Download submission";
    StepikApiClient stepikApiClient = authAndGetStepikApiClient(true);
    if (!isAuthenticated()) {
        return;
    }
    List<Submission> submissions = ProgressManager.getInstance().run(new Task.WithResult<List<Submission>, RuntimeException>(project, title, true) {

        @Override
        protected List<Submission> compute(@NotNull ProgressIndicator progressIndicator) throws RuntimeException {
            progressIndicator.setIndeterminate(true);
            StudyNode parent = stepNode.getParent();
            String lessonName = parent != null ? parent.getName() : "";
            progressIndicator.setText(lessonName);
            progressIndicator.setText2(stepNode.getName());
            List<Submission> submissions = getSubmissions(stepikApiClient, stepNode);
            if (Utils.isCanceled()) {
                Metrics.downloadAction(project, stepNode, USER_CANCELED);
                return null;
            }
            if (submissions == null) {
                Metrics.downloadAction(project, stepNode, DATA_NOT_LOADED);
                return Collections.emptyList();
            }
            SupportedLanguages currentLang = stepNode.getCurrentLang();
            return filterSubmissions(submissions, currentLang);
        }
    });
    if (submissions == null) {
        return;
    }
    ApplicationManager.getApplication().invokeAndWait(() -> showPopup(project, stepNode, submissions));
}
Also used : StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) Task(com.intellij.openapi.progress.Task) Submission(org.stepik.api.objects.submissions.Submission) StudyNode(org.stepik.core.courseFormat.StudyNode) SupportedLanguages(org.stepik.core.SupportedLanguages) StepNode(org.stepik.core.courseFormat.StepNode) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List)

Example 7 with SupportedLanguages

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

the class DownloadSubmission method loadSubmission.

private void loadSubmission(@NotNull Project project, @NotNull StepNode stepNode, @NotNull Submission submission) {
    String fileName = stepNode.getCurrentLang().getMainFileName();
    VirtualFile src = getOrCreateSrcDirectory(project, stepNode, true);
    if (src == null) {
        Metrics.downloadAction(project, stepNode, TARGET_NOT_FOUND);
        return;
    }
    VirtualFile mainFile = src.findChild(fileName);
    if (mainFile == null) {
        Metrics.downloadAction(project, stepNode, TARGET_NOT_FOUND);
        return;
    }
    final String finalCode = submission.getReply().getCode();
    CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
        FileDocumentManager documentManager = FileDocumentManager.getInstance();
        Document document = documentManager.getDocument(mainFile);
        if (document != null) {
            SupportedLanguages language = langOfName(submission.getReply().getLanguage());
            if (containsDirectives(finalCode, language)) {
                String text = uncommentAmbientCode(finalCode, language);
                document.setText(text);
            } else {
                String code = replaceCode(document.getText(), finalCode, language);
                document.setText(code);
            }
            StudyStatus status = StudyStatus.of(submission.getStatus());
            stepNode.setStatus(status);
            FileEditorManager.getInstance(project).openFile(mainFile, true);
            ProjectView.getInstance(project).refresh();
            Metrics.downloadAction(project, stepNode, SUCCESSFUL);
        }
    }), "Download submission", "Download submission");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SupportedLanguages(org.stepik.core.SupportedLanguages) StudyStatus(org.stepik.core.courseFormat.StudyStatus) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Example 8 with SupportedLanguages

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

the class InsertStepikDirectives method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();
    if (project == null) {
        return;
    }
    StudyNode selectedNode = StepikProjectManager.getSelected(project);
    if (!(selectedNode instanceof StepNode) || ((StepNode) selectedNode).getType() != CODE) {
        return;
    }
    StepNode targetStepNode = (StepNode) selectedNode;
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    for (VirtualFile file : FileEditorManager.getInstance(project).getOpenFiles()) {
        Document document = documentManager.getDocument(file);
        if (document != null)
            documentManager.saveDocument(document);
    }
    SupportedLanguages currentLang = targetStepNode.getCurrentLang();
    VirtualFile src = getOrCreateSrcDirectory(project, targetStepNode, true);
    if (src == null) {
        return;
    }
    VirtualFile file = src.findChild(currentLang.getMainFileName());
    if (file == null) {
        return;
    }
    String text = getFileText(file);
    StepikProjectManager projectManager = StepikProjectManager.getInstance(project);
    boolean showHint = projectManager != null && projectManager.getShowHint();
    boolean needInsert = !containsDirectives(text, currentLang);
    if (needInsert) {
        text = insertAmbientCode(text, currentLang, showHint);
        Metrics.insertAmbientCodeAction(project, targetStepNode, SUCCESSFUL);
    } else {
        text = removeAmbientCode(text, showHint, currentLang, true);
        Metrics.removeAmbientCodeAction(project, targetStepNode, SUCCESSFUL);
    }
    writeInToFile(text, file, project);
    if (needInsert) {
        final Document document = FileDocumentManager.getInstance().getDocument(file);
        if (document != null) {
            ReformatUtils.reformatSelectedEditor(project, document);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SupportedLanguages(org.stepik.core.SupportedLanguages) Project(com.intellij.openapi.project.Project) StepikProjectManager(org.stepik.core.StepikProjectManager) StepNode(org.stepik.core.courseFormat.StepNode) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) StudyNode(org.stepik.core.courseFormat.StudyNode)

Example 9 with SupportedLanguages

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

the class ProgrammingLanguageUtils method getOrCreateMainFile.

@Nullable
private static PsiFile getOrCreateMainFile(@NotNull Project project, @NotNull VirtualFile parent, @NotNull SupportedLanguages language, @NotNull StepNode stepNode) {
    String fileName = language.getMainFileName();
    final VirtualFile[] file = { parent.findChild(fileName) };
    Application application = ApplicationManager.getApplication();
    if (file[0] == null) {
        application.invokeAndWait(() -> application.runWriteAction(() -> {
            try {
                file[0] = parent.createChildData(null, fileName);
                String template = null;
                StepikApiClient stepikApiClient = authAndGetStepikApiClient();
                User user = getCurrentUser();
                if (!user.isGuest()) {
                    try {
                        Submissions submissions = stepikApiClient.submissions().get().user(user.getId()).order(Order.DESC).step(stepNode.getId()).execute();
                        if (!submissions.isEmpty()) {
                            Optional<Submission> lastSubmission = submissions.getItems().stream().filter(submission -> SupportedLanguages.langOfName(submission.getReply().getLanguage()) == language).limit(1).findFirst();
                            if (lastSubmission.isPresent()) {
                                template = lastSubmission.get().getReply().getCode();
                            }
                        }
                    } catch (StepikClientException e) {
                        logger.warn(e);
                    }
                }
                if (template == null) {
                    template = stepNode.getTemplate(language);
                }
                file[0].setBinaryContent(template.getBytes());
            } catch (IOException e) {
                file[0] = null;
            }
        }));
    }
    return application.runReadAction((Computable<PsiFile>) () -> PsiManager.getInstance(project).findFile(file[0]));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) Application(com.intellij.openapi.application.Application) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) ProjectFilesUtils.getOrCreateSrcPsiDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreateSrcPsiDirectory) Computable(com.intellij.openapi.util.Computable) StepikClientException(org.stepik.api.exceptions.StepikClientException) User(org.stepik.api.objects.users.User) PsiManager(com.intellij.psi.PsiManager) ArrayList(java.util.ArrayList) ProjectView(com.intellij.ide.projectView.ProjectView) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) ProjectFilesUtils.getOrCreatePsiDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreatePsiDirectory) Metrics(org.stepik.core.metrics.Metrics) StepikAuthManager.getCurrentUser(org.stepik.core.stepik.StepikAuthManager.getCurrentUser) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) SUCCESSFUL(org.stepik.core.metrics.MetricsStatus.SUCCESSFUL) Submission(org.stepik.api.objects.submissions.Submission) SupportedLanguages(org.stepik.core.SupportedLanguages) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) StepNode(org.stepik.core.courseFormat.StepNode) IOException(java.io.IOException) StudyUtils(org.stepik.core.StudyUtils) MoveFilesOrDirectoriesUtil(com.intellij.refactoring.move.moveFilesOrDirectories.MoveFilesOrDirectoriesUtil) Nullable(org.jetbrains.annotations.Nullable) EduNames(org.stepik.core.core.EduNames) Submissions(org.stepik.api.objects.submissions.Submissions) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Optional(java.util.Optional) PsiDirectory(com.intellij.psi.PsiDirectory) StepikApiClient(org.stepik.api.client.StepikApiClient) NotNull(org.jetbrains.annotations.NotNull) Order(org.stepik.api.queries.Order) StepikAuthManager.authAndGetStepikApiClient(org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient) StepikApiClient(org.stepik.api.client.StepikApiClient) User(org.stepik.api.objects.users.User) StepikAuthManager.getCurrentUser(org.stepik.core.stepik.StepikAuthManager.getCurrentUser) Optional(java.util.Optional) PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) Application(com.intellij.openapi.application.Application) Submissions(org.stepik.api.objects.submissions.Submissions) StepikClientException(org.stepik.api.exceptions.StepikClientException) Nullable(org.jetbrains.annotations.Nullable)

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