1
0
forked from VimPlug/jedi

Fix private variables in filters.

This commit is contained in:
Dave Halter
2016-10-15 19:12:46 +02:00
parent 5c0b2d7aae
commit 129c669bc0
6 changed files with 72 additions and 20 deletions

View File

@@ -183,7 +183,8 @@ class Completion:
filters = get_global_filters( filters = get_global_filters(
self._evaluator, self._evaluator,
scope, scope,
self._position self._position,
origin_scope=scope
) )
completion_names = [] completion_names = []
for filter in filters: for filter in filters:
@@ -191,11 +192,12 @@ class Completion:
return completion_names return completion_names
def _trailer_completions(self, atom_expr): def _trailer_completions(self, atom_expr):
user_scope = get_user_scope(self._module, self._position)
scopes = self._evaluator.eval_element(atom_expr) scopes = self._evaluator.eval_element(atom_expr)
completion_names = [] completion_names = []
debug.dbg('trailer completion scopes: %s', scopes) debug.dbg('trailer completion scopes: %s', scopes)
for s in scopes: for s in scopes:
for filter in s.get_filters(search_global=False): for filter in s.get_filters(search_global=False, origin_scope=user_scope):
completion_names += filter.values() completion_names += filter.values()
return completion_names return completion_names

View File

@@ -157,7 +157,8 @@ class CompiledObject(Base):
def names_dicts(self, search_global, is_instance=False): def names_dicts(self, search_global, is_instance=False):
return self._names_dict_ensure_one_dict(is_instance) return self._names_dict_ensure_one_dict(is_instance)
def get_filters(self, search_global=False, is_instance=False, until_position=None): def get_filters(self, search_global=False, is_instance=False,
until_position=None, origin_scope=None):
yield self._ensure_one_filter(is_instance) yield self._ensure_one_filter(is_instance)
@memoize_method @memoize_method

View File

@@ -123,7 +123,7 @@ class DictFilter(AbstractFilter):
return self._filter(self._dct.values()) return self._filter(self._dct.values())
def get_global_filters(evaluator, context, until_position): def get_global_filters(evaluator, context, until_position, origin_scope):
""" """
Returns all filters in order of priority for name resolution. Returns all filters in order of priority for name resolution.
""" """
@@ -131,7 +131,10 @@ def get_global_filters(evaluator, context, until_position):
while context is not None: while context is not None:
if not (context.type == 'classdef' and in_func): if not (context.type == 'classdef' and in_func):
# Names in methods cannot be resolved within the class. # Names in methods cannot be resolved within the class.
for filter in context.get_filters(search_global=True, until_position=until_position): for filter in context.get_filters(
search_global=True,
until_position=until_position,
origin_scope=origin_scope):
yield filter yield filter
if context.type == 'funcdef': if context.type == 'funcdef':
# The position should be reset if the current scope is a function. # The position should be reset if the current scope is a function.

View File

@@ -134,10 +134,15 @@ class NameFinder(object):
return types return types
def get_filters(self, search_global=False): def get_filters(self, search_global=False):
if search_global: if isinstance(self.name_str, tree.Name):
return get_global_filters(self._evaluator, self.scope, self.position) origin_scope = self.name_str.get_parent_until(tree.Scope, reverse=True)
else: else:
return self.scope.get_filters(search_global, self.position) origin_scope = None
if search_global:
return get_global_filters(self._evaluator, self.scope, self.position, origin_scope)
else:
return self.scope.get_filters(search_global, self.position, origin_scope=origin_scope)
def names_dict_lookup(self, names_dict, position): def names_dict_lookup(self, names_dict, position):
def get_param(scope, el): def get_param(scope, el):

View File

@@ -300,10 +300,22 @@ class InstanceClassFilter(ParserTreeFilter):
) )
self._instance = instance self._instance = instance
def _equals_origin_scope(self):
node = self._origin_scope
while node is not None:
if node == self._parser_scope or node == self._instance:
return True
node = node.get_parent_scope()
return False
def _access_possible(self, name):
return not name.value.startswith('__') or name.value.endswith('__') \
or self._equals_origin_scope()
def _filter(self, names): def _filter(self, names):
names = super(InstanceClassFilter, self)._filter(names) names = super(InstanceClassFilter, self)._filter(names)
return [get_instance_el(self._evaluator, self._instance, name, True) return [get_instance_el(self._evaluator, self._instance, name, True)
for name in names] for name in names if self._access_possible(name)]
def _check_flows(self, names): def _check_flows(self, names):
return names return names
@@ -324,7 +336,7 @@ class SelfNameFilter(InstanceClassFilter):
if tree.is_node(trailer, 'trailer') \ if tree.is_node(trailer, 'trailer') \
and len(trailer.children) == 2 \ and len(trailer.children) == 2 \
and trailer.children[0] == '.': and trailer.children[0] == '.':
if name.is_definition(): if name.is_definition() and self._access_possible(name):
init_execution = self._instance._get_init_execution() init_execution = self._instance._get_init_execution()
# Hopefully we can somehow change this. # Hopefully we can somehow change this.
if init_execution is not None and \ if init_execution is not None and \
@@ -563,16 +575,16 @@ class Class(use_metaclass(CachedMetaClass, Wrapper)):
else: else:
yield scope.names_dict yield scope.names_dict
def get_filters(self, search_global, until_position=None, is_instance=False): def get_filters(self, search_global, until_position=None, origin_scope=None, is_instance=False):
if search_global: if search_global:
yield ParserTreeFilter(self._evaluator, self.base, until_position) yield ParserTreeFilter(self._evaluator, self.base, until_position, origin_scope=origin_scope)
else: else:
for scope in self.py__mro__(): for scope in self.py__mro__():
if isinstance(scope, compiled.CompiledObject): if isinstance(scope, compiled.CompiledObject):
for filter in scope.get_filters(is_instance=is_instance): for filter in scope.get_filters(is_instance=is_instance):
yield filter yield filter
else: else:
yield ParserTreeFilter(self._evaluator, scope.base) yield ParserTreeFilter(self._evaluator, scope.base, origin_scope=origin_scope)
def is_class(self): def is_class(self):
return True return True
@@ -670,12 +682,12 @@ class Function(use_metaclass(CachedMetaClass, Wrapper)):
for names_dict in scope.names_dicts(False): for names_dict in scope.names_dicts(False):
yield names_dict yield names_dict
def get_filters(self, search_global, until_position=None): def get_filters(self, search_global, until_position=None, origin_scope=None):
if search_global: if search_global:
yield ParserTreeFilter(self._evaluator, self.base, until_position) yield ParserTreeFilter(self._evaluator, self.base, until_position, origin_scope=origin_scope)
else: else:
scope = self.py__class__() scope = self.py__class__()
for filter in scope.get_filters(search_global=False): for filter in scope.get_filters(search_global=False, origin_scope=origin_scope):
yield filter yield filter
@Python3Method @Python3Method
@@ -849,11 +861,12 @@ class FunctionExecution(Executed):
yield result yield result
del evaluator.predefined_if_name_dict_dict[for_stmt] del evaluator.predefined_if_name_dict_dict[for_stmt]
def get_filters(self, search_global, until_position=None): def get_filters(self, search_global, until_position=None, origin_scope=None):
yield FunctionExecutionFilter(self._evaluator, self._original_function, yield FunctionExecutionFilter(self._evaluator, self._original_function,
self._copied_funcdef, self._copied_funcdef,
self.param_by_name, self.param_by_name,
until_position) until_position,
origin_scope=origin_scope)
@memoize_default(default=NO_DEFAULT) @memoize_default(default=NO_DEFAULT)
def _get_params(self): def _get_params(self):
@@ -926,8 +939,13 @@ class ModuleWrapper(use_metaclass(CachedMetaClass, tree.Module, Wrapper)):
yield dict((str(n), [GlobalName(n)]) for n in self.base.global_names) yield dict((str(n), [GlobalName(n)]) for n in self.base.global_names)
yield self._sub_modules_dict() yield self._sub_modules_dict()
def get_filters(self, search_global, until_position=None): def get_filters(self, search_global, until_position=None, origin_scope=None):
yield ParserTreeFilter(self._evaluator, self._module, until_position) yield ParserTreeFilter(
self._evaluator,
self._module,
until_position,
origin_scope=origin_scope
)
yield GlobalNameFilter(self._module) yield GlobalNameFilter(self._module)
yield DictFilter(self._sub_modules_dict()) yield DictFilter(self._sub_modules_dict())
yield DictFilter(self._module_attributes_dict()) yield DictFilter(self._module_attributes_dict())

View File

@@ -384,10 +384,33 @@ class PrivateVar():
self.__var self.__var
#? ['__var'] #? ['__var']
self.__var self.__var
def __private_func(self):
return 1
def wrap_private(self):
return self.__private_func()
#? [] #? []
PrivateVar().__var PrivateVar().__var
#? #?
PrivateVar().__var PrivateVar().__var
#? []
PrivateVar().__private_func
#? int()
PrivateVar().wrap_private()
class PrivateSub(PrivateVar):
def test(self):
#? []
self.__var
def wrap_private(self):
#? []
self.__var
#? []
PrivateSub().__var
# ----------------- # -----------------
# super # super