Search in sources :

Example 11 with StepNode

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

the class StudyNodeTest method setUp.

@Before
public void setUp() throws Exception {
    courseNode = new CourseNode();
    sectionNode = new SectionNode();
    sectionNode.setId(1);
    sectionNode.setParent(courseNode);
    lessonNode = new LessonNode();
    lessonNode.setId(1);
    lessonNode.setParent(sectionNode);
    stepNode = new StepNode();
    stepNode.setId(1);
    stepNode.setParent(lessonNode);
    courseNode.getChildren().add(sectionNode);
    sectionNode.getChildren().add(lessonNode);
    lessonNode.getChildren().add(stepNode);
}
Also used : StepNode(org.stepik.core.courseFormat.StepNode) LessonNode(org.stepik.core.courseFormat.LessonNode) CourseNode(org.stepik.core.courseFormat.CourseNode) SectionNode(org.stepik.core.courseFormat.SectionNode) Before(org.junit.Before)

Example 12 with StepNode

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

the class CodeQuizAction method update.

@Override
public void update(AnActionEvent e) {
    super.update(e);
    Presentation presentation = e.getPresentation();
    StudyNode<?, ?> selectedNode = StepikProjectManager.getSelected(e.getProject());
    boolean enabled = presentation.isEnabled();
    boolean canEnabled = (selectedNode instanceof StepNode) && (((StepNode) selectedNode).getType() == CODE);
    presentation.setEnabled(enabled && canEnabled);
}
Also used : StepNode(org.stepik.core.courseFormat.StepNode) Presentation(com.intellij.openapi.actionSystem.Presentation)

Example 13 with StepNode

use of org.stepik.core.courseFormat.StepNode 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 14 with StepNode

use of org.stepik.core.courseFormat.StepNode 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 15 with StepNode

use of org.stepik.core.courseFormat.StepNode 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

StepNode (org.stepik.core.courseFormat.StepNode)17 StudyNode (org.stepik.core.courseFormat.StudyNode)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 StepikApiClient (org.stepik.api.client.StepikApiClient)4 SupportedLanguages (org.stepik.core.SupportedLanguages)4 StepikAuthManager.authAndGetStepikApiClient (org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient)4 Document (com.intellij.openapi.editor.Document)3 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)3 Project (com.intellij.openapi.project.Project)3 File (java.io.File)3 NotNull (org.jetbrains.annotations.NotNull)3 Submission (org.stepik.api.objects.submissions.Submission)3 StepikProjectManager (org.stepik.core.StepikProjectManager)3 Application (com.intellij.openapi.application.Application)2 Logger (com.intellij.openapi.diagnostic.Logger)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Task (com.intellij.openapi.progress.Task)2 IOException (java.io.IOException)2 List (java.util.List)2 Nullable (org.jetbrains.annotations.Nullable)2