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