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