diff --git a/python/src/com/jetbrains/python/codeInsight/PyRunLineMarkerContributor.java b/python/src/com/jetbrains/python/codeInsight/PyRunLineMarkerContributor.java index 3365ce40f23c..8b96d65487a2 100644 --- a/python/src/com/jetbrains/python/codeInsight/PyRunLineMarkerContributor.java +++ b/python/src/com/jetbrains/python/codeInsight/PyRunLineMarkerContributor.java @@ -21,13 +21,14 @@ 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.psi.util.PsiTreeUtil; import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; import com.jetbrains.python.PyTokenTypes; +import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil; +import com.jetbrains.python.psi.PyFile; import com.jetbrains.python.psi.PyIfStatement; import com.jetbrains.python.psi.PyUtil; +import com.jetbrains.python.psi.impl.PyIfStatementNavigator; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -46,11 +47,10 @@ public class PyRunLineMarkerContributor extends RunLineMarkerContributor { private static boolean isMainClauseOnTopLevel(@NotNull PsiElement element) { if (element.getNode().getElementType() == PyTokenTypes.IF_KEYWORD) { - element = PsiTreeUtil.getParentOfType(element, PyIfStatement.class); - - return element != null && - element.getParent() instanceof PsiFile && - PyUtil.isIfNameEqualsMain(((PyIfStatement)element)); + PyIfStatement statement = PyIfStatementNavigator.getIfStatementByIfKeyword(element); + return statement != null && + ScopeUtil.getScopeOwner(element) instanceof PyFile && + PyUtil.isIfNameEqualsMain(statement); } else { return false; diff --git a/python/testData/codeInsight/runLineMarker/runnable_with_ifs.py b/python/testData/codeInsight/runLineMarker/runnable_with_ifs.py new file mode 100644 index 000000000000..77a4e0a6f17d --- /dev/null +++ b/python/testData/codeInsight/runLineMarker/runnable_with_ifs.py @@ -0,0 +1,5 @@ + + +if __name__ == "__main__": + result = [i for i in range(10) if i != 0] + result = [i for i in range(10) if i != 0] diff --git a/python/testSrc/com/jetbrains/python/codeInsight/runLineMarker/PyRunLineMarkerTest.java b/python/testSrc/com/jetbrains/python/codeInsight/runLineMarker/PyRunLineMarkerTest.java index 3bf3ae1f0ed5..162aef4939cb 100644 --- a/python/testSrc/com/jetbrains/python/codeInsight/runLineMarker/PyRunLineMarkerTest.java +++ b/python/testSrc/com/jetbrains/python/codeInsight/runLineMarker/PyRunLineMarkerTest.java @@ -32,6 +32,11 @@ public class PyRunLineMarkerTest extends PyTestCase { assertTrue(elementWithInfo.getParent().getText().startsWith("if")); } + public void testWithManyIfs() { + List infos = getInfos("runnable_with_ifs.py"); + assertEquals("There should be only one marker", 1, infos.size()); + } + public void testNotRunnable() { assertEquals(0, getInfos("not_runnable.py").size()); }