import textwrap
from inspect import cleandoc

from jedi._compatibility import literal_eval, is_py3
from parso.python import tree

_EXECUTE_NODES = set([
    'funcdef', 'classdef', 'import_from', 'import_name', 'test', 'or_test',
    'and_test', 'not_test', 'comparison', 'expr', 'xor_expr', 'and_expr',
    'shift_expr', 'arith_expr', 'atom_expr', 'term', 'factor', 'power', 'atom'
])

_FLOW_KEYWORDS = (
    'try', 'except', 'finally', 'else', 'if', 'elif', 'with', 'for', 'while'
)


def get_executable_nodes(node, last_added=False):
    """
    For static analysis.
    """
    result = []
    typ = node.type
    if typ == 'name':
        next_leaf = node.get_next_leaf()
        if last_added is False and node.parent.type != 'param' and next_leaf != '=':
            result.append(node)
    elif typ == 'expr_stmt':
        # I think evaluating the statement (and possibly returned arrays),
        # should be enough for static analysis.
        result.append(node)
        for child in node.children:
            result += get_executable_nodes(child, last_added=True)
    elif typ == 'decorator':
        # decorator
        if node.children[-2] == ')':
            node = node.children[-3]