Search in sources :

Example 1 with PythonLanguage

use of com.jetbrains.python.PythonLanguage in project intellij-community by JetBrains.

the class PyBreakContinueGotoProvider method getGotoDeclarationTarget.

@Override
public PsiElement getGotoDeclarationTarget(@Nullable PsiElement source, Editor editor) {
    if (source != null && source.getLanguage() instanceof PythonLanguage) {
        final PyLoopStatement loop = PsiTreeUtil.getParentOfType(source, PyLoopStatement.class, false, PyFunction.class, PyClass.class);
        if (loop != null) {
            ASTNode node = source.getNode();
            if (node != null) {
                IElementType node_type = node.getElementType();
                if (node_type == PyTokenTypes.CONTINUE_KEYWORD) {
                    return loop;
                }
                if (node_type == PyTokenTypes.BREAK_KEYWORD) {
                    PsiElement outer_element = loop;
                    PsiElement after_cycle;
                    while (true) {
                        after_cycle = outer_element.getNextSibling();
                        if (after_cycle != null) {
                            if (after_cycle instanceof PsiWhiteSpace) {
                                after_cycle = after_cycle.getNextSibling();
                            }
                            if (after_cycle instanceof PyStatement)
                                return after_cycle;
                        }
                        outer_element = outer_element.getParent();
                        if (PyUtil.instanceOf(outer_element, PsiFile.class, PyFunction.class, PyClass.class)) {
                            break;
                        }
                    }
                    // cycle is the last statement in the flow of execution. its last element is our best bet.
                    return PsiTreeUtil.getDeepestLast(loop);
                }
            }
        }
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PythonLanguage(com.jetbrains.python.PythonLanguage) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

ASTNode (com.intellij.lang.ASTNode)1 PsiElement (com.intellij.psi.PsiElement)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 IElementType (com.intellij.psi.tree.IElementType)1 PythonLanguage (com.jetbrains.python.PythonLanguage)1