PY-22042 Gutter icon for running main clause

This commit is contained in:
Liana.Bakradze
2016-12-30 14:14:55 +03:00
parent d4ac966f03
commit 7084f40683
5 changed files with 108 additions and 0 deletions
@@ -160,6 +160,7 @@
<runConfigurationProducer implementation="com.jetbrains.python.testing.attest.PythonAtTestConfigurationProducer"/>
<codeInsight.lineMarkerProvider language="Python" implementationClass="com.jetbrains.python.codeInsight.PyLineMarkerProvider"/>
<runLineMarkerContributor language="Python" implementationClass="com.jetbrains.python.codeInsight.PyRunLineMarkerContributor"/>
<definitionsSearch implementation="com.jetbrains.python.psi.search.PyDefinitionsSearch"/>
<gotoTargetRendererProvider implementation="com.jetbrains.python.codeInsight.PyGotoTargetRendererProvider"/>
@@ -0,0 +1,48 @@
/*
* Copyright 2000-2016 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 com.jetbrains.python.codeInsight;
import com.intellij.execution.lineMarker.ExecutorAction;
import com.intellij.execution.lineMarker.RunLineMarkerContributor;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import com.jetbrains.python.psi.PyIfStatement;
import com.jetbrains.python.psi.PyUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class PyRunLineMarkerContributor extends RunLineMarkerContributor {
@Nullable
@Override
public Info getInfo(@NotNull PsiElement element) {
if (isMainClauseOnTopLevel(element)) {
final AnAction[] actions = ExecutorAction.getActions();
Function<PsiElement, String> tooltipProvider =
psiElement -> StringUtil.join(ContainerUtil.mapNotNull(actions, action -> getText(action, psiElement)), "\n");
return new Info(AllIcons.RunConfigurations.TestState.Run, tooltipProvider, actions);
}
return null;
}
private static boolean isMainClauseOnTopLevel(@NotNull PsiElement element) {
return element instanceof PyIfStatement && element.getParent() instanceof PsiFile && PyUtil.isIfNameEqualsMain(((PyIfStatement)element));
}
}
@@ -0,0 +1,10 @@
def f():
def g():
pass
if __name__ == '__main__':
g()
print(1)
f()
@@ -0,0 +1,5 @@
def f():
print(1)
if __name__ == "__main__":
f()
@@ -0,0 +1,44 @@
/*
* Copyright 2000-2016 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 com.jetbrains.python.codeInsight.runLineMarker;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.fixtures.PyTestCase;
import java.util.List;
public class PyRunLineMarkerTest extends PyTestCase {
public void testRunnableMain() {
List<LineMarkerInfo> infos = getInfos("runnable.py");
assertEquals("Wrong number of line markers", 1, infos.size());
LineMarkerInfo lineMarkerInfo = infos.get(0);
PsiElement elementWithInfo = lineMarkerInfo.getElement();
assertNotNull(elementWithInfo);
assertTrue(elementWithInfo.getText().startsWith("if __name__ == \"__main__\""));
}
public void testNotRunnable() {
assertEquals(0, getInfos("not_runnable.py").size());
}
private List<LineMarkerInfo> getInfos(String fileName) {
myFixture.configureByFile(getTestDataPath() + "/codeInsight/runLineMarker/" + fileName);
myFixture.doHighlighting();
return DaemonCodeAnalyzerImpl.getLineMarkers(myFixture.getEditor().getDocument(), myFixture.getProject());
}
}