1
0
forked from VimPlug/jedi

Move search_ancestor to jedi.parser.tree.

This commit is contained in:
Dave Halter
2017-04-27 21:41:24 +02:00
parent 800bf4bbe2
commit a846e687c3
6 changed files with 19 additions and 15 deletions

View File

@@ -4,7 +4,7 @@ are needed for name resolution.
""" """
from abc import abstractmethod from abc import abstractmethod
from jedi.parser.python.tree import search_ancestor from jedi.parser.tree import search_ancestor
from jedi.evaluate import flow_analysis from jedi.evaluate import flow_analysis
from jedi.common import to_list, unite from jedi.common import to_list, unite

View File

@@ -16,6 +16,7 @@ check for -> a is a string). There's big potential in these checks.
""" """
from jedi.parser.python import tree from jedi.parser.python import tree
from jedi.parser.tree import search_ancestor
from jedi import debug from jedi import debug
from jedi.common import unite from jedi.common import unite
from jedi import settings from jedi import settings
@@ -310,7 +311,7 @@ def _check_flow_information(context, flow, search_name, pos):
]) ])
for name in names: for name in names:
ass = tree.search_ancestor(name, 'assert_stmt') ass = search_ancestor(name, 'assert_stmt')
if ass is not None: if ass is not None:
result = _check_isinstance_type(context, ass.assertion, search_name) result = _check_isinstance_type(context, ass.assertion, search_name)
if result is not None: if result is not None:

View File

@@ -22,6 +22,7 @@ from jedi import settings
from jedi.common import source_to_unicode, unite from jedi.common import source_to_unicode, unite
from jedi.parser.python import parse from jedi.parser.python import parse
from jedi.parser.python import tree from jedi.parser.python import tree
from jedi.parser.tree import search_ancestor
from jedi.parser.cache import parser_cache from jedi.parser.cache import parser_cache
from jedi.evaluate import sys_path from jedi.evaluate import sys_path
from jedi.evaluate import helpers from jedi.evaluate import helpers
@@ -36,7 +37,7 @@ from jedi.evaluate.filters import AbstractNameDefinition
@memoize_default(default=set()) @memoize_default(default=set())
def infer_import(context, tree_name, is_goto=False): def infer_import(context, tree_name, is_goto=False):
module_context = context.get_root_context() module_context = context.get_root_context()
import_node = tree.search_ancestor(tree_name, ('import_name', 'import_from')) import_node = search_ancestor(tree_name, ('import_name', 'import_from'))
import_path = import_node.path_for_name(tree_name) import_path = import_node.path_for_name(tree_name)
from_import_name = None from_import_name = None
evaluator = context.evaluator evaluator = context.evaluator

View File

@@ -58,7 +58,8 @@ def parse(code=None, path=None, grammar=None, error_recovery=True,
:param code: A unicode string that contains Python code. :param code: A unicode string that contains Python code.
:param path: The path to the file you want to open. Only needed for caching. :param path: The path to the file you want to open. Only needed for caching.
:param grammar: A Python grammar file, created with load_grammar. :param grammar: A Python grammar file, created with load_grammar. You may
not specify it. In that case it's the current Python version.
:param error_recovery: If enabled, any code will be returned. If it is :param error_recovery: If enabled, any code will be returned. If it is
invalid, it will be returned as an error node. If disabled, you will invalid, it will be returned as an error node. If disabled, you will
get a ParseError when encountering syntax errors in your code. get a ParseError when encountering syntax errors in your code.

View File

@@ -30,17 +30,8 @@ See also :attr:`Scope.subscopes` and :attr:`Scope.statements`.
from itertools import chain from itertools import chain
from jedi._compatibility import utf8_repr, unicode from jedi._compatibility import utf8_repr, unicode
from jedi.parser.tree import Node, BaseNode, Leaf, ErrorNode, ErrorLeaf from jedi.parser.tree import Node, BaseNode, Leaf, ErrorNode, ErrorLeaf, \
search_ancestor
def search_ancestor(node, node_type_or_types):
if not isinstance(node_type_or_types, (list, tuple)):
node_type_or_types = (node_type_or_types,)
while True:
node = node.parent
if node is None or node.type in node_type_or_types:
return node
class DocstringMixin(object): class DocstringMixin(object):

View File

@@ -2,6 +2,16 @@ from abc import abstractmethod, abstractproperty
from jedi._compatibility import utf8_repr, encoding, is_py3 from jedi._compatibility import utf8_repr, encoding, is_py3
def search_ancestor(node, node_type_or_types):
if not isinstance(node_type_or_types, (list, tuple)):
node_type_or_types = (node_type_or_types,)
while True:
node = node.parent
if node is None or node.type in node_type_or_types:
return node
class NodeOrLeaf(object): class NodeOrLeaf(object):
""" """
The base class for nodes and leaves. The base class for nodes and leaves.