Merge branch 'liana/simplified_debugger'
[idea/community.git] / python / edu / src / com / jetbrains / python / edu / PyExecuteFileLineMarkerProvider.java
1 package com.jetbrains.python.edu;
2
3 import com.intellij.codeHighlighting.Pass;
4 import com.intellij.codeInsight.daemon.LineMarkerInfo;
5 import com.intellij.codeInsight.daemon.LineMarkerProvider;
6 import com.intellij.execution.actions.RunContextAction;
7 import com.intellij.execution.executors.DefaultRunExecutor;
8 import com.intellij.icons.AllIcons;
9 import com.intellij.openapi.actionSystem.ActionGroup;
10 import com.intellij.openapi.actionSystem.AnAction;
11 import com.intellij.openapi.actionSystem.DefaultActionGroup;
12 import com.intellij.openapi.application.ApplicationManager;
13 import com.intellij.openapi.editor.markup.GutterIconRenderer;
14 import com.intellij.psi.PsiElement;
15 import com.intellij.util.Function;
16 import org.jetbrains.annotations.NotNull;
17 import org.jetbrains.annotations.Nullable;
18
19 import java.util.Collection;
20 import java.util.List;
21
22 /**
23  * @author traff
24  */
25 public class PyExecuteFileLineMarkerProvider implements LineMarkerProvider {
26   @Nullable
27   @Override
28   public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) {
29     return null;
30   }
31
32   @Override
33   public void collectSlowLineMarkers(@NotNull List<PsiElement> elements, @NotNull Collection<LineMarkerInfo> result) {
34     if (elements.isEmpty()) {
35       return;
36     }
37     PsiElement element = elements.get(0).getContainingFile();
38     final RunContextAction runAction = new RunContextAction(DefaultRunExecutor.getRunExecutorInstance());
39     final LineMarkerInfo<PsiElement> markerInfo = new LineMarkerInfo<PsiElement>(
40       element, 0, AllIcons.Actions.Execute, Pass.UPDATE_OVERRIDEN_MARKERS,
41       new Function<PsiElement, String>() {
42         @Override
43         public String fun(PsiElement e) {
44           return "Execute '" + e.getContainingFile().getName() + "'";
45         }
46       }, null,
47       GutterIconRenderer.Alignment.RIGHT) {
48       @Nullable
49       @Override
50       public GutterIconRenderer createGutterRenderer() {
51         return new LineMarkerGutterIconRenderer<PsiElement>(this) {
52           @Override
53           public AnAction getClickAction() {
54             return runAction;
55           }
56
57           @Nullable
58           @Override
59           public ActionGroup getPopupMenuActions() {
60             final DefaultActionGroup group = new DefaultActionGroup();
61             group.add(runAction);
62             final PyExecuteFileExtensionPoint[] extensions =
63               ApplicationManager.getApplication().getExtensions(PyExecuteFileExtensionPoint.EP_NAME);
64             for (PyExecuteFileExtensionPoint extension : extensions) {
65               final AnAction action = extension.getRunAction();
66               if (action == null) {
67                 continue;
68               }
69               group.add(action);
70             }
71             return group;
72           }
73         };
74       }
75     };
76     result.add(markerInfo);
77   }
78 }