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);
}
}
};
}
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));
}
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);
}
}
}
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);
}
}
}
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();
}
}
Aggregations