summaryrefslogtreecommitdiff
path: root/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/RunTestsLineMarker.java
blob: 93e0a706f367a21c2591990612ce0bb88f4ddfdc (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.coursecreator;

import com.intellij.codeHighlighting.Pass;
import com.intellij.codeInsight.daemon.GutterIconNavigationHandler;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.codeInsight.daemon.LineMarkerProvider;
import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.icons.AllIcons;
import com.intellij.ide.DataManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.util.Function;
import com.jetbrains.python.psi.PyFile;
import com.jetbrains.python.psi.PyImportStatement;
import com.jetbrains.python.psi.PyStatement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.List;

public class RunTestsLineMarker implements LineMarkerProvider {
  @Nullable
  @Override
  public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) {
    return null;
  }

  @Override
  public void collectSlowLineMarkers(@NotNull List<PsiElement> elements, @NotNull Collection<LineMarkerInfo> result) {
    for (PsiElement element : elements) {
      if (isFirstCodeLine(element)) {
        PsiFile psiFile = element.getContainingFile();
        if (psiFile == null || !psiFile.getName().contains(".answer")) {
          continue;
        }
        result.add(new LineMarkerInfo<PsiElement>(
          element, element.getTextRange(), AllIcons.Actions.Lightning, Pass.UPDATE_OVERRIDEN_MARKERS,
          new Function<PsiElement, String>() {
            @Override
            public String fun(PsiElement e) {
              return "Run tests from file '" + e.getContainingFile().getName() + "'";
            }
          },
          new GutterIconNavigationHandler<PsiElement>() {
            @Override
            public void navigate(MouseEvent e, PsiElement elt) {
              executeCurrentScript(elt);
            }
          },
          GutterIconRenderer.Alignment.RIGHT));
      }
    }
  }

  private static void executeCurrentScript(PsiElement elt) {
    Editor editor = PsiUtilBase.findEditor(elt);
    assert editor != null;

    final ConfigurationContext context =
      ConfigurationContext.getFromContext(DataManager.getInstance().getDataContext(editor.getComponent()));
      CCRunTests.run(context);
  }

  private static boolean isFirstCodeLine(PsiElement element) {
    return element instanceof PyStatement &&
           element.getParent() instanceof PyFile &&
           !isNothing(element) &&
           nothingBefore(element);
  }

  private static boolean nothingBefore(PsiElement element) {
    element = element.getPrevSibling();
    while (element != null) {
      if (!isNothing(element)) {
        return false;
      }
      element = element.getPrevSibling();
    }

    return true;
  }

  private static boolean isNothing(PsiElement element) {
    return (element instanceof PsiComment) || (element instanceof PyImportStatement) || (element instanceof PsiWhiteSpace);
  }
}