summaryrefslogtreecommitdiff
path: root/python/edu/learn-python/src/com/jetbrains/python/edu/actions/StudyShowHintAction.java
blob: 2952486274cf9f39fc18b290ecf367a9708633cd (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
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.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.util.Disposer;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.jetbrains.python.edu.StudyState;
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.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) {
    final Project project = e.getProject();
    if (project == null) {
      return;
    }
    Course course = StudyTaskManager.getInstance(project).getCourse();
    if (course == null) {
      return;
    }
    StudyState studyState = new StudyState(StudyEditor.getSelectedStudyEditor(project));
    if (!studyState.isValid()) {
      return;
    }
    PsiFile file = PsiManager.getInstance(project).findFile(studyState.getVirtualFile());
    final Editor editor = studyState.getEditor();
    LogicalPosition pos = editor.getCaretModel().getLogicalPosition();
    TaskWindow taskWindow = studyState.getTaskFile().getTaskWindow(editor.getDocument(), pos);
    if (file == null || taskWindow == null) {
      return;
    }
    String hint = taskWindow.getHint();
    if (hint == null) {
      return;
    }
    File resourceFile = new File(course.getResourcePath());
    File resourceRoot = resourceFile.getParentFile();
    if (resourceRoot == null || !resourceRoot.exists()) {
      return;
    }
    File hintsDir = new File(resourceRoot, Course.HINTS_DIR);
    if (hintsDir.exists()) {
      String hintText = StudyUtils.getFileText(hintsDir.getAbsolutePath(), hint, true);
      int offset = editor.getDocument().getLineStartOffset(pos.line) + pos.column;
      PsiElement element = file.findElementAt(offset);
      if (hintText == null || element == null) {
        return;
      }

      DocumentationManager documentationManager = DocumentationManager.getInstance(project);
      DocumentationComponent component = new DocumentationComponent(documentationManager);
      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(editor);
      Disposer.dispose(component);
    }
  }

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