3c45e8e701e07ecf555ffa5ed7d33fb727aed88f
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / actions / StudyRefreshAnswerPlaceholder.java
1 package com.jetbrains.edu.learning.actions;
2
3 import com.intellij.icons.AllIcons;
4 import com.intellij.openapi.actionSystem.AnActionEvent;
5 import com.intellij.openapi.actionSystem.Presentation;
6 import com.intellij.openapi.application.ApplicationManager;
7 import com.intellij.openapi.command.CommandProcessor;
8 import com.intellij.openapi.editor.Document;
9 import com.intellij.openapi.editor.Editor;
10 import com.intellij.openapi.extensions.Extensions;
11 import com.intellij.openapi.project.DumbAwareAction;
12 import com.intellij.openapi.project.Project;
13 import com.intellij.openapi.util.TextRange;
14 import com.jetbrains.edu.learning.*;
15 import com.jetbrains.edu.learning.core.EduNames;
16 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
17 import com.jetbrains.edu.learning.courseFormat.Course;
18 import com.jetbrains.edu.learning.courseFormat.TaskFile;
19 import com.jetbrains.edu.learning.editor.StudyEditor;
20 import org.jetbrains.annotations.Nullable;
21
22 public class StudyRefreshAnswerPlaceholder extends DumbAwareAction {
23
24   public static final String NAME = "Refresh Answer Placeholder";
25
26   public StudyRefreshAnswerPlaceholder() {
27     super(NAME, NAME, AllIcons.Actions.Refresh);
28   }
29
30   @Override
31   public void actionPerformed(AnActionEvent e) {
32     Project project = e.getProject();
33     if (project == null) {
34       return;
35     }
36     for (StudyActionListener listener : Extensions.getExtensions(StudyActionListener.EP_NAME)) {
37       listener.beforeCheck(e);
38     }
39     final AnswerPlaceholder answerPlaceholder = getAnswerPlaceholder(e);
40     if (answerPlaceholder == null) {
41       return;
42     }
43     StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
44     final StudyState studyState = new StudyState(studyEditor);
45     if (answerPlaceholder.getTaskFile().getTask().hasSubtasks()) {
46       StudySubtaskUtils.refreshPlaceholder(project, studyState.getEditor(), answerPlaceholder);
47       return;
48     }
49     Document patternDocument = StudyUtils.getPatternDocument(answerPlaceholder.getTaskFile(), studyState.getVirtualFile().getName());
50     if (patternDocument == null) {
51       return;
52     }
53     AnswerPlaceholder.MyInitialState initialState = answerPlaceholder.getInitialState();
54     int startOffset = initialState.getOffset();
55     final String text = patternDocument.getText(new TextRange(startOffset, startOffset + initialState.getLength()));
56     CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
57       Document document = studyState.getEditor().getDocument();
58       int offset = answerPlaceholder.getOffset();
59       document.deleteString(offset, offset + answerPlaceholder.getRealLength());
60       document.insertString(offset, text);
61     }), NAME, null);
62   }
63
64   @Override
65   public void update(AnActionEvent e) {
66     Presentation presentation = e.getPresentation();
67     presentation.setEnabledAndVisible(false);
68     Project project = e.getProject();
69     if (project == null) {
70       return;
71     }
72     Course course = StudyTaskManager.getInstance(project).getCourse();
73     if (course == null) {
74       return;
75     }
76
77     if (!EduNames.STUDY.equals(course.getCourseMode())) {
78       presentation.setVisible(true);
79       return;
80     }
81
82     if (getAnswerPlaceholder(e) == null) {
83       presentation.setEnabledAndVisible(false);
84       return;
85     }
86     presentation.setEnabledAndVisible(true);
87   }
88
89   @Nullable
90   private static AnswerPlaceholder getAnswerPlaceholder(AnActionEvent e) {
91     final Project project = e.getProject();
92     if (project == null) {
93       return null;
94     }
95     StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
96     final StudyState studyState = new StudyState(studyEditor);
97     if (studyEditor == null || !studyState.isValid()) {
98       return null;
99     }
100     final Editor editor = studyState.getEditor();
101     final TaskFile taskFile = studyState.getTaskFile();
102     return taskFile.getAnswerPlaceholder(editor.getCaretModel().getOffset());
103   }
104 }