Search in sources :

Example 16 with StudyNode

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

the class StudyBasePluginConfigurator method getFileEditorManagerListener.

@NotNull
@Override
public FileEditorManagerListener getFileEditorManagerListener(@NotNull Project project) {
    return new FileEditorManagerAdapter() {

        @Override
        public void selectionChanged(@NotNull FileEditorManagerEvent event) {
            VirtualFile file = event.getNewFile();
            if (file == null) {
                return;
            }
            StudyNode stepNode = StudyUtils.getStudyNode(project, file);
            if (stepNode != null) {
                StepikProjectManager.setSelected(project, stepNode);
            }
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditorManagerAdapter(com.intellij.openapi.fileEditor.FileEditorManagerAdapter) FileEditorManagerEvent(com.intellij.openapi.fileEditor.FileEditorManagerEvent) NotNull(org.jetbrains.annotations.NotNull) StudyNode(org.stepik.core.courseFormat.StudyNode) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with StudyNode

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

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

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

the class ModuleUtils method createStepModule.

static void createStepModule(@NotNull Project project, @NotNull StepNode step, @NotNull ModifiableModuleModel moduleModel) {
    StudyNode lesson = step.getParent();
    if (lesson != null) {
        String moduleDir = String.join("/", project.getBasePath(), lesson.getPath());
        StepModuleBuilder stepModuleBuilder = new StepModuleBuilder(moduleDir, step);
        try {
            stepModuleBuilder.createModule(moduleModel);
        } catch (IOException | ModuleWithNameAlreadyExists | JDOMException | ConfigurationException e) {
            logger.warn("Cannot create step: " + step.getDirectory(), e);
        }
    }
}
Also used : ConfigurationException(com.intellij.openapi.options.ConfigurationException) ModuleWithNameAlreadyExists(com.intellij.openapi.module.ModuleWithNameAlreadyExists) IOException(java.io.IOException) JDOMException(org.jdom.JDOMException) StudyNode(org.stepik.core.courseFormat.StudyNode) StepModuleBuilder(org.stepik.plugin.projectWizard.idea.StepModuleBuilder)

Example 20 with StudyNode

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

the class FormListener method handle.

static void handle(@NotNull Project project, @NotNull StudyBrowserWindow browser, @NotNull HTMLFormElement form) {
    StudyNode root = StepikProjectManager.getProjectRoot(project);
    if (root == null) {
        return;
    }
    StudyNode node = StudyUtils.getStudyNode(root, form.getAction());
    if (!(node instanceof StepNode)) {
        return;
    }
    StepNode stepNode = (StepNode) node;
    Elements elements = new Elements(form.getElements());
    switch(elements.getAction()) {
        case "get_first_attempt":
        case "get_attempt":
            if (!elements.isLocked()) {
                getAttempt(project, stepNode);
            }
            break;
        case "submit":
            String typeStr = elements.getType();
            StepType type = StepType.of(typeStr);
            boolean isFromFile = elements.isFromFile();
            String data = isFromFile ? getDataFromFile(stepNode, project) : null;
            long attemptId = elements.getAttemptId();
            sendStep(project, stepNode, elements, type, attemptId, data);
            break;
        case "save_reply":
            typeStr = elements.getType();
            type = StepType.of(typeStr);
            getReply(stepNode, type, elements, null);
            break;
        case "login":
            String email = elements.getInputValue("email");
            String password = elements.getInputValue("password");
            browser.showLoadAnimation();
            StepikAuthManager.authentication(email, password).whenComplete((state, throwable) -> {
                if (state != StepikAuthState.AUTH) {
                    browser.callFunction("setErrorMessage", "Wrong email or password");
                }
                browser.hideLoadAnimation();
            });
            break;
        default:
            browser.hideLoadAnimation();
    }
}
Also used : StepNode(org.stepik.core.courseFormat.StepNode) StepType(org.stepik.core.courseFormat.StepType) StudyNode(org.stepik.core.courseFormat.StudyNode)

Aggregations

StudyNode (org.stepik.core.courseFormat.StudyNode)21 Project (com.intellij.openapi.project.Project)6 StepNode (org.stepik.core.courseFormat.StepNode)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Nullable (org.jetbrains.annotations.Nullable)4 StepikApiClient (org.stepik.api.client.StepikApiClient)4 StepikAuthManager.authAndGetStepikApiClient (org.stepik.core.stepik.StepikAuthManager.authAndGetStepikApiClient)4 StepikProjectManager (org.stepik.core.StepikProjectManager)3 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)2 NotNull (org.jetbrains.annotations.NotNull)2 StepikClientException (org.stepik.api.exceptions.StepikClientException)2 StudyObject (org.stepik.api.objects.StudyObject)2 SupportedLanguages (org.stepik.core.SupportedLanguages)2 PresentationData (com.intellij.ide.projectView.PresentationData)1 Presentation (com.intellij.openapi.actionSystem.Presentation)1 Application (com.intellij.openapi.application.Application)1 Document (com.intellij.openapi.editor.Document)1 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)1 FileEditorManagerAdapter (com.intellij.openapi.fileEditor.FileEditorManagerAdapter)1