summaryrefslogtreecommitdiff
path: root/python/edu/learn-python/src/com/jetbrains/python/edu/actions/StudyShowHintAction.java
blob: 1efa90889449ddc89c4506e59ee3e37b306c2140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.jetbrains.python.edu.actions;

import com.intellij.codeInsight.documentation.DocumentationComponent;
import com.intellij.codeInsight.documentation.DocumentationManager;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.jetbrains.python.edu.StudyTaskManager;
import com.jetbrains.python.edu.StudyUtils;
import com.jetbrains.python.edu.course.Course;
import com.jetbrains.python.edu.course.TaskFile;
import com.jetbrains.python.edu.course.TaskWindow;
import com.jetbrains.python.edu.editor.StudyEditor;
import icons.StudyIcons;

import java.io.File;

public class StudyShowHintAction extends DumbAwareAction {
  public static final String ACTION_ID = "ShowHintAction";
  public static final String SHORTCUT = "ctrl pressed 7";

  public StudyShowHintAction() {
    super("Show hint", "Show hint", StudyIcons.ShowHint);
  }

  public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();
    if (project != null) {
      DocumentationManager documentationManager = DocumentationManager.getInstance(project);
      DocumentationComponent component = new DocumentationComponent(documentationManager);
      Editor selectedEditor = StudyEditor.getSelectedEditor(project);
      FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
      assert selectedEditor != null;
      VirtualFile openedFile = fileDocumentManager.getFile(selectedEditor.getDocument());
      if (openedFile != null) {
        StudyTaskManager taskManager = StudyTaskManager.getInstance(e.getProject());
        TaskFile taskFile = taskManager.getTaskFile(openedFile);
        if (taskFile != null) {
          PsiFile file = PsiManager.getInstance(project).findFile(openedFile);
          if (file != null) {
            LogicalPosition pos = selectedEditor.getCaretModel().getLogicalPosition();
            TaskWindow taskWindow = taskFile.getTaskWindow(selectedEditor.getDocument(), pos);
            if (taskWindow != null) {
              String hint = taskWindow.getHint();
              if (hint == null) {
                return;
              }
              Course course = taskManager.getCourse();
              if (course != null) {
                File resourceFile = new File(course.getResourcePath());
                File resourceRoot = resourceFile.getParentFile();
                if (resourceRoot != null && resourceRoot.exists()) {
                  File hintsDir = new File(resourceRoot, Course.HINTS_DIR);
                  if (hintsDir.exists()) {
                    String hintText = StudyUtils.getFileText(hintsDir.getAbsolutePath(), hint, true);
                    if (hintText != null) {
                      int offset = selectedEditor.getDocument().getLineStartOffset(pos.line) + pos.column;
                      PsiElement element = file.findElementAt(offset);
                      if (element != null) {
                        component.setData(element, hintText, true, null);
                        final JBPopup popup =
                          JBPopupFactory.getInstance().createComponentPopupBuilder(component, component)
                            .setDimensionServiceKey(project, DocumentationManager.JAVADOC_LOCATION_AND_SIZE, false)
                            .setResizable(true)
                            .setMovable(true)
                            .setRequestFocus(true)
                            .createPopup();
                        component.setHint(popup);
                        popup.showInBestPositionFor(selectedEditor);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  @Override
  public void update(AnActionEvent e) {
    StudyUtils.updateAction(e);
  }
}