forked from VimPlug/jedi
Change parser and api to use tree instead of pr.
This commit is contained in:
@@ -15,7 +15,7 @@ from itertools import chain
|
|||||||
from jedi._compatibility import unicode, builtins
|
from jedi._compatibility import unicode, builtins
|
||||||
from jedi.parser import Parser, load_grammar
|
from jedi.parser import Parser, load_grammar
|
||||||
from jedi.parser.tokenize import source_tokens
|
from jedi.parser.tokenize import source_tokens
|
||||||
from jedi.parser import tree as pr
|
from jedi.parser import tree
|
||||||
from jedi.parser.user_context import UserContext, UserContextParser
|
from jedi.parser.user_context import UserContext, UserContextParser
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi import settings
|
from jedi import settings
|
||||||
@@ -159,13 +159,13 @@ class Script(object):
|
|||||||
else:
|
else:
|
||||||
return keywords.keyword_names('import')
|
return keywords.keyword_names('import')
|
||||||
|
|
||||||
if isinstance(user_stmt, pr.Import):
|
if isinstance(user_stmt, tree.Import):
|
||||||
module = self._parser.module()
|
module = self._parser.module()
|
||||||
completion_names += imports.completion_names(self._evaluator,
|
completion_names += imports.completion_names(self._evaluator,
|
||||||
user_stmt, self._pos)
|
user_stmt, self._pos)
|
||||||
return completion_names
|
return completion_names
|
||||||
|
|
||||||
if names is None and not isinstance(user_stmt, pr.Import):
|
if names is None and not isinstance(user_stmt, tree.Import):
|
||||||
if not path and not dot:
|
if not path and not dot:
|
||||||
# add keywords
|
# add keywords
|
||||||
completion_names += keywords.keyword_names(all=True)
|
completion_names += keywords.keyword_names(all=True)
|
||||||
@@ -210,7 +210,7 @@ class Script(object):
|
|||||||
if settings.case_insensitive_completion \
|
if settings.case_insensitive_completion \
|
||||||
and n.lower().startswith(like.lower()) \
|
and n.lower().startswith(like.lower()) \
|
||||||
or n.startswith(like):
|
or n.startswith(like):
|
||||||
if isinstance(c.parent, (pr.Function, pr.Class)):
|
if isinstance(c.parent, (tree.Function, tree.Class)):
|
||||||
# TODO I think this is a hack. It should be an
|
# TODO I think this is a hack. It should be an
|
||||||
# er.Function/er.Class before that.
|
# er.Function/er.Class before that.
|
||||||
c = self._evaluator.wrap(c.parent).name
|
c = self._evaluator.wrap(c.parent).name
|
||||||
@@ -272,7 +272,7 @@ class Script(object):
|
|||||||
# matched to much.
|
# matched to much.
|
||||||
return []
|
return []
|
||||||
|
|
||||||
if isinstance(user_stmt, pr.Import):
|
if isinstance(user_stmt, tree.Import):
|
||||||
i, _ = helpers.get_on_import_stmt(self._evaluator, self._user_context,
|
i, _ = helpers.get_on_import_stmt(self._evaluator, self._user_context,
|
||||||
user_stmt, is_completion)
|
user_stmt, is_completion)
|
||||||
if i is None:
|
if i is None:
|
||||||
@@ -392,7 +392,7 @@ class Script(object):
|
|||||||
"""
|
"""
|
||||||
definitions = set(defs)
|
definitions = set(defs)
|
||||||
for d in defs:
|
for d in defs:
|
||||||
if isinstance(d.parent, pr.Import) \
|
if isinstance(d.parent, tree.Import) \
|
||||||
and d.start_pos == (0, 0):
|
and d.start_pos == (0, 0):
|
||||||
i = imports.ImportWrapper(self._evaluator, d.parent).follow(is_goto=True)
|
i = imports.ImportWrapper(self._evaluator, d.parent).follow(is_goto=True)
|
||||||
definitions.remove(d)
|
definitions.remove(d)
|
||||||
@@ -417,7 +417,7 @@ class Script(object):
|
|||||||
|
|
||||||
if last_name is None:
|
if last_name is None:
|
||||||
last_name = stmt
|
last_name = stmt
|
||||||
while not isinstance(last_name, pr.Name):
|
while not isinstance(last_name, tree.Name):
|
||||||
try:
|
try:
|
||||||
last_name = last_name.children[-1]
|
last_name = last_name.children[-1]
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
@@ -428,7 +428,7 @@ class Script(object):
|
|||||||
# The cursor is on a class/function name.
|
# The cursor is on a class/function name.
|
||||||
user_scope = self._parser.user_scope()
|
user_scope = self._parser.user_scope()
|
||||||
definitions = set([user_scope.name])
|
definitions = set([user_scope.name])
|
||||||
elif isinstance(user_stmt, pr.Import):
|
elif isinstance(user_stmt, tree.Import):
|
||||||
s, name = helpers.get_on_import_stmt(self._evaluator,
|
s, name = helpers.get_on_import_stmt(self._evaluator,
|
||||||
self._user_context, user_stmt)
|
self._user_context, user_stmt)
|
||||||
|
|
||||||
@@ -450,8 +450,8 @@ class Script(object):
|
|||||||
# The Evaluator.goto function checks for definitions, but since we
|
# The Evaluator.goto function checks for definitions, but since we
|
||||||
# use a reverse tokenizer, we have new name_part objects, so we
|
# use a reverse tokenizer, we have new name_part objects, so we
|
||||||
# have to check the user_stmt here for positions.
|
# have to check the user_stmt here for positions.
|
||||||
if isinstance(user_stmt, pr.ExprStmt) \
|
if isinstance(user_stmt, tree.ExprStmt) \
|
||||||
and isinstance(last_name.parent, pr.ExprStmt):
|
and isinstance(last_name.parent, tree.ExprStmt):
|
||||||
for name in user_stmt.get_defined_names():
|
for name in user_stmt.get_defined_names():
|
||||||
if name.start_pos <= self._pos <= name.end_pos:
|
if name.start_pos <= self._pos <= name.end_pos:
|
||||||
return [name]
|
return [name]
|
||||||
@@ -476,7 +476,7 @@ class Script(object):
|
|||||||
try:
|
try:
|
||||||
user_stmt = self._parser.user_stmt()
|
user_stmt = self._parser.user_stmt()
|
||||||
definitions = self._goto(add_import_name=True)
|
definitions = self._goto(add_import_name=True)
|
||||||
if not definitions and isinstance(user_stmt, pr.Import):
|
if not definitions and isinstance(user_stmt, tree.Import):
|
||||||
# For not defined imports (goto doesn't find something, we take
|
# For not defined imports (goto doesn't find something, we take
|
||||||
# the name as a definition. This is enough, because every name
|
# the name as a definition. This is enough, because every name
|
||||||
# points to it.
|
# points to it.
|
||||||
@@ -486,7 +486,7 @@ class Script(object):
|
|||||||
# Without a definition for a name we cannot find references.
|
# Without a definition for a name we cannot find references.
|
||||||
return []
|
return []
|
||||||
|
|
||||||
if not isinstance(user_stmt, pr.Import):
|
if not isinstance(user_stmt, tree.Import):
|
||||||
# import case is looked at with add_import_name option
|
# import case is looked at with add_import_name option
|
||||||
definitions = usages.usages_add_import_modules(self._evaluator,
|
definitions = usages.usages_add_import_modules(self._evaluator,
|
||||||
definitions)
|
definitions)
|
||||||
@@ -610,7 +610,7 @@ class Interpreter(Script):
|
|||||||
def _simple_complete(self, path, dot, like):
|
def _simple_complete(self, path, dot, like):
|
||||||
user_stmt = self._parser.user_stmt_with_whitespace()
|
user_stmt = self._parser.user_stmt_with_whitespace()
|
||||||
is_simple_path = not path or re.search('^[\w][\w\d.]*$', path)
|
is_simple_path = not path or re.search('^[\w][\w\d.]*$', path)
|
||||||
if isinstance(user_stmt, pr.Import) or not is_simple_path:
|
if isinstance(user_stmt, tree.Import) or not is_simple_path:
|
||||||
return super(Interpreter, self)._simple_complete(path, dot, like)
|
return super(Interpreter, self)._simple_complete(path, dot, like)
|
||||||
else:
|
else:
|
||||||
class NamespaceModule(object):
|
class NamespaceModule(object):
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import re
|
|||||||
from jedi._compatibility import unicode, use_metaclass
|
from jedi._compatibility import unicode, use_metaclass
|
||||||
from jedi import settings
|
from jedi import settings
|
||||||
from jedi import common
|
from jedi import common
|
||||||
from jedi.parser import tree as pr
|
from jedi.parser import tree
|
||||||
from jedi.evaluate.cache import memoize_default, CachedMetaClass
|
from jedi.evaluate.cache import memoize_default, CachedMetaClass
|
||||||
from jedi.evaluate import representation as er
|
from jedi.evaluate import representation as er
|
||||||
from jedi.evaluate import iterable
|
from jedi.evaluate import iterable
|
||||||
@@ -152,7 +152,7 @@ class BaseDefinition(object):
|
|||||||
return stripped.api_type()
|
return stripped.api_type()
|
||||||
elif isinstance(stripped, iterable.Array):
|
elif isinstance(stripped, iterable.Array):
|
||||||
return 'instance'
|
return 'instance'
|
||||||
elif isinstance(stripped, pr.Import):
|
elif isinstance(stripped, tree.Import):
|
||||||
return 'import'
|
return 'import'
|
||||||
|
|
||||||
string = type(stripped).__name__.lower().replace('wrapper', '')
|
string = type(stripped).__name__.lower().replace('wrapper', '')
|
||||||
@@ -166,7 +166,7 @@ class BaseDefinition(object):
|
|||||||
path = []
|
path = []
|
||||||
par = self._definition
|
par = self._definition
|
||||||
while par is not None:
|
while par is not None:
|
||||||
if isinstance(par, pr.Import):
|
if isinstance(par, tree.Import):
|
||||||
path += imports.ImportWrapper(self._evaluator, self._name).import_path
|
path += imports.ImportWrapper(self._evaluator, self._name).import_path
|
||||||
break
|
break
|
||||||
try:
|
try:
|
||||||
@@ -317,9 +317,9 @@ class BaseDefinition(object):
|
|||||||
"""
|
"""
|
||||||
Follow both statements and imports, as far as possible.
|
Follow both statements and imports, as far as possible.
|
||||||
"""
|
"""
|
||||||
if self._definition.isinstance(pr.ExprStmt):
|
if self._definition.isinstance(tree.ExprStmt):
|
||||||
return self._evaluator.eval_statement(self._definition)
|
return self._evaluator.eval_statement(self._definition)
|
||||||
elif self._definition.isinstance(pr.Import):
|
elif self._definition.isinstance(tree.Import):
|
||||||
return imports.ImportWrapper(self._evaluator, self._name).follow()
|
return imports.ImportWrapper(self._evaluator, self._name).follow()
|
||||||
else:
|
else:
|
||||||
return [self._definition]
|
return [self._definition]
|
||||||
@@ -383,9 +383,9 @@ class Completion(BaseDefinition):
|
|||||||
append = '('
|
append = '('
|
||||||
|
|
||||||
if settings.add_dot_after_module:
|
if settings.add_dot_after_module:
|
||||||
if isinstance(self._definition, pr.Module):
|
if isinstance(self._definition, tree.Module):
|
||||||
append += '.'
|
append += '.'
|
||||||
if isinstance(self._definition, pr.Param):
|
if isinstance(self._definition, tree.Param):
|
||||||
append += '='
|
append += '='
|
||||||
|
|
||||||
name = str(self._name)
|
name = str(self._name)
|
||||||
@@ -443,7 +443,7 @@ class Completion(BaseDefinition):
|
|||||||
parses all libraries starting with ``a``.
|
parses all libraries starting with ``a``.
|
||||||
"""
|
"""
|
||||||
definition = self._definition
|
definition = self._definition
|
||||||
if isinstance(definition, pr.Import):
|
if isinstance(definition, tree.Import):
|
||||||
i = imports.ImportWrapper(self._evaluator, self._name)
|
i = imports.ImportWrapper(self._evaluator, self._name)
|
||||||
if len(i.import_path) > 1 or not fast:
|
if len(i.import_path) > 1 or not fast:
|
||||||
followed = self._follow_statements_imports()
|
followed = self._follow_statements_imports()
|
||||||
@@ -462,7 +462,7 @@ class Completion(BaseDefinition):
|
|||||||
The type of the completion objects. Follows imports. For a further
|
The type of the completion objects. Follows imports. For a further
|
||||||
description, look at :attr:`jedi.api.classes.BaseDefinition.type`.
|
description, look at :attr:`jedi.api.classes.BaseDefinition.type`.
|
||||||
"""
|
"""
|
||||||
if isinstance(self._definition, pr.Import):
|
if isinstance(self._definition, tree.Import):
|
||||||
i = imports.ImportWrapper(self._evaluator, self._name)
|
i = imports.ImportWrapper(self._evaluator, self._name)
|
||||||
if len(i.import_path) <= 1:
|
if len(i.import_path) <= 1:
|
||||||
return 'module'
|
return 'module'
|
||||||
@@ -480,7 +480,7 @@ class Completion(BaseDefinition):
|
|||||||
# imports completion is very complicated and needs to be treated
|
# imports completion is very complicated and needs to be treated
|
||||||
# separately in Completion.
|
# separately in Completion.
|
||||||
definition = self._definition
|
definition = self._definition
|
||||||
if definition.isinstance(pr.Import):
|
if definition.isinstance(tree.Import):
|
||||||
i = imports.ImportWrapper(self._evaluator, self._name)
|
i = imports.ImportWrapper(self._evaluator, self._name)
|
||||||
return i.follow()
|
return i.follow()
|
||||||
return super(Completion, self)._follow_statements_imports()
|
return super(Completion, self)._follow_statements_imports()
|
||||||
@@ -546,14 +546,14 @@ class Definition(use_metaclass(CachedMetaClass, BaseDefinition)):
|
|||||||
d = typ + ' ' + d.name.get_code()
|
d = typ + ' ' + d.name.get_code()
|
||||||
elif isinstance(d, iterable.Array):
|
elif isinstance(d, iterable.Array):
|
||||||
d = 'class ' + d.type
|
d = 'class ' + d.type
|
||||||
elif isinstance(d, (pr.Class, er.Class, er.Instance)):
|
elif isinstance(d, (tree.Class, er.Class, er.Instance)):
|
||||||
d = 'class ' + unicode(d.name)
|
d = 'class ' + unicode(d.name)
|
||||||
elif isinstance(d, (er.Function, pr.Function)):
|
elif isinstance(d, (er.Function, tree.Function)):
|
||||||
d = 'def ' + unicode(d.name)
|
d = 'def ' + unicode(d.name)
|
||||||
elif isinstance(d, pr.Module):
|
elif isinstance(d, tree.Module):
|
||||||
# only show module name
|
# only show module name
|
||||||
d = 'module %s' % self.module_name
|
d = 'module %s' % self.module_name
|
||||||
elif isinstance(d, pr.Param):
|
elif isinstance(d, tree.Param):
|
||||||
d = d.get_code().strip()
|
d = d.get_code().strip()
|
||||||
if d.endswith(','):
|
if d.endswith(','):
|
||||||
d = d[:-1] # Remove the comma.
|
d = d[:-1] # Remove the comma.
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from jedi._compatibility import unicode
|
from jedi._compatibility import unicode
|
||||||
from jedi.api import classes
|
from jedi.api import classes
|
||||||
from jedi.parser import tree as pr
|
from jedi.parser import tree
|
||||||
from jedi.evaluate import imports
|
from jedi.evaluate import imports
|
||||||
|
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ def usages_add_import_modules(evaluator, definitions):
|
|||||||
new = set()
|
new = set()
|
||||||
for d in definitions:
|
for d in definitions:
|
||||||
imp_or_stmt = d.get_definition()
|
imp_or_stmt = d.get_definition()
|
||||||
if isinstance(imp_or_stmt, pr.Import):
|
if isinstance(imp_or_stmt, tree.Import):
|
||||||
s = imports.ImportWrapper(evaluator, d)
|
s = imports.ImportWrapper(evaluator, d)
|
||||||
new |= set(s.follow(is_goto=True))
|
new |= set(s.follow(is_goto=True))
|
||||||
return set(definitions) | new
|
return set(definitions) | new
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from itertools import chain
|
|||||||
from jedi._compatibility import use_metaclass
|
from jedi._compatibility import use_metaclass
|
||||||
from jedi import settings
|
from jedi import settings
|
||||||
from jedi.parser import Parser
|
from jedi.parser import Parser
|
||||||
from jedi.parser import tree as pr
|
from jedi.parser import tree
|
||||||
from jedi import cache
|
from jedi import cache
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi.parser.tokenize import (source_tokens, NEWLINE,
|
from jedi.parser.tokenize import (source_tokens, NEWLINE,
|
||||||
@@ -18,7 +18,7 @@ from jedi.parser.tokenize import (source_tokens, NEWLINE,
|
|||||||
FLOWS = 'if', 'else', 'elif', 'while', 'with', 'try', 'except', 'finally', 'for'
|
FLOWS = 'if', 'else', 'elif', 'while', 'with', 'try', 'except', 'finally', 'for'
|
||||||
|
|
||||||
|
|
||||||
class FastModule(pr.Module):
|
class FastModule(tree.Module):
|
||||||
type = 'file_input'
|
type = 'file_input'
|
||||||
|
|
||||||
def __init__(self, module_path):
|
def __init__(self, module_path):
|
||||||
@@ -140,7 +140,7 @@ class ParserNode(object):
|
|||||||
middle.
|
middle.
|
||||||
"""
|
"""
|
||||||
c = self._content_scope.children
|
c = self._content_scope.children
|
||||||
if pr.is_node(c[-1], 'suite'): # In a simple_stmt there's no DEDENT.
|
if tree.is_node(c[-1], 'suite'): # In a simple_stmt there's no DEDENT.
|
||||||
end_marker = self.parser.module.children[-1]
|
end_marker = self.parser.module.children[-1]
|
||||||
# Set the DEDENT prefix instead of the ENDMARKER.
|
# Set the DEDENT prefix instead of the ENDMARKER.
|
||||||
c[-1].children[-1].prefix = end_marker.prefix
|
c[-1].children[-1].prefix = end_marker.prefix
|
||||||
@@ -236,7 +236,7 @@ class FastParser(use_metaclass(CachedFastParser)):
|
|||||||
'|'.join(_FLOWS_NEED_COLON)))
|
'|'.join(_FLOWS_NEED_COLON)))
|
||||||
|
|
||||||
def __init__(self, grammar, source, module_path=None):
|
def __init__(self, grammar, source, module_path=None):
|
||||||
# set values like `pr.Module`.
|
# set values like `tree.Module`.
|
||||||
self._grammar = grammar
|
self._grammar = grammar
|
||||||
self.module_path = module_path
|
self.module_path = module_path
|
||||||
self._reset_caches()
|
self._reset_caches()
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ from jedi import common
|
|||||||
from jedi.parser import tokenize, Parser
|
from jedi.parser import tokenize, Parser
|
||||||
from jedi._compatibility import u
|
from jedi._compatibility import u
|
||||||
from jedi.parser.fast import FastParser
|
from jedi.parser.fast import FastParser
|
||||||
from jedi.parser import tree as pr
|
from jedi.parser import tree
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi.common import PushBackIterator
|
from jedi.common import PushBackIterator
|
||||||
|
|
||||||
@@ -323,8 +323,8 @@ class UserContextParser(object):
|
|||||||
def scan(scope):
|
def scan(scope):
|
||||||
for s in scope.children:
|
for s in scope.children:
|
||||||
if s.start_pos <= self._position <= s.end_pos:
|
if s.start_pos <= self._position <= s.end_pos:
|
||||||
if isinstance(s, (pr.Scope, pr.Flow)):
|
if isinstance(s, (tree.Scope, tree.Flow)):
|
||||||
if isinstance(s, pr.Flow):
|
if isinstance(s, tree.Flow):
|
||||||
return s
|
return s
|
||||||
return scan(s) or s
|
return scan(s) or s
|
||||||
elif s.type in ('suite', 'decorated'):
|
elif s.type in ('suite', 'decorated'):
|
||||||
|
|||||||
Reference in New Issue
Block a user