mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 22:14:27 +08:00
fix global_vars
This commit is contained in:
@@ -29,36 +29,20 @@ class Module(pr.Simple, pr.Module):
|
|||||||
def reset_caches(self):
|
def reset_caches(self):
|
||||||
""" This module does a whole lot of caching, because it uses different
|
""" This module does a whole lot of caching, because it uses different
|
||||||
parsers. """
|
parsers. """
|
||||||
self.cache = {}
|
self._used_names = None
|
||||||
for p in self.parsers:
|
for p in self.parsers:
|
||||||
p.user_scope = None
|
p.user_scope = None
|
||||||
p.user_stmt = None
|
p.user_stmt = None
|
||||||
|
|
||||||
def _get(self, name, operation, execute=False, *args, **kwargs):
|
|
||||||
key = (name, args, frozenset(kwargs.items()))
|
|
||||||
if key not in self.cache:
|
|
||||||
if execute:
|
|
||||||
objs = (getattr(p.module, name)(*args, **kwargs)
|
|
||||||
for p in self.parsers)
|
|
||||||
else:
|
|
||||||
objs = (getattr(p.module, name) for p in self.parsers)
|
|
||||||
self.cache[key] = reduce(operation, objs)
|
|
||||||
return self.cache[key]
|
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
if name == 'global_vars':
|
if name.startswith('__'):
|
||||||
return self._get(name, operator.add)
|
|
||||||
elif name.startswith('__'):
|
|
||||||
raise AttributeError('Not available!')
|
raise AttributeError('Not available!')
|
||||||
else:
|
else:
|
||||||
return getattr(self.parsers[0].module, name)
|
return getattr(self.parsers[0].module, name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def used_names(self):
|
def used_names(self):
|
||||||
if not self.parsers:
|
if self._used_names is None:
|
||||||
raise NotImplementedError("Parser doesn't exist.")
|
|
||||||
key = 'used_names'
|
|
||||||
if key not in self.cache:
|
|
||||||
dct = {}
|
dct = {}
|
||||||
for p in self.parsers:
|
for p in self.parsers:
|
||||||
for k, statement_set in p.module.used_names.items():
|
for k, statement_set in p.module.used_names.items():
|
||||||
@@ -67,8 +51,8 @@ class Module(pr.Simple, pr.Module):
|
|||||||
else:
|
else:
|
||||||
dct[k] = set(statement_set)
|
dct[k] = set(statement_set)
|
||||||
|
|
||||||
self.cache[key] = dct
|
self._used_names = dct
|
||||||
return self.cache[key]
|
return self._used_names
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s: %s@%s-%s>" % (type(self).__name__, self.name,
|
return "<%s: %s@%s-%s>" % (type(self).__name__, self.name,
|
||||||
@@ -107,9 +91,9 @@ class ParserNode(object):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# with fast_parser we have either 1 subscope or only statements.
|
# with fast_parser we have either 1 subscope or only statements.
|
||||||
self._content_scope = self.parser.module.subscopes[0]
|
self._content_scope = parser.module.subscopes[0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
self._content_scope = self.parser.module
|
self._content_scope = parser.module
|
||||||
|
|
||||||
scope = self._content_scope
|
scope = self._content_scope
|
||||||
self._contents = {}
|
self._contents = {}
|
||||||
@@ -124,6 +108,11 @@ class ParserNode(object):
|
|||||||
scope.is_generator = self._is_generator
|
scope.is_generator = self._is_generator
|
||||||
self.parser.user_scope = None
|
self.parser.user_scope = None
|
||||||
|
|
||||||
|
if self.parent is None:
|
||||||
|
# Global vars of the first one can be deleted, in the global scope
|
||||||
|
# they make no sense.
|
||||||
|
self.parser.module.global_vars = []
|
||||||
|
|
||||||
for c in self.children:
|
for c in self.children:
|
||||||
c.reset_contents()
|
c.reset_contents()
|
||||||
|
|
||||||
@@ -169,6 +158,13 @@ class ParserNode(object):
|
|||||||
if isinstance(parser.user_scope, pr.SubModule) \
|
if isinstance(parser.user_scope, pr.SubModule) \
|
||||||
and parser.start_pos <= parser.user_position < parser.end_pos:
|
and parser.start_pos <= parser.user_position < parser.end_pos:
|
||||||
parser.user_scope = scope
|
parser.user_scope = scope
|
||||||
|
|
||||||
|
# global_vars
|
||||||
|
cur = self
|
||||||
|
while cur.parent is not None:
|
||||||
|
cur = cur.parent
|
||||||
|
cur.parser.module.global_vars += parser.module.global_vars
|
||||||
|
|
||||||
scope.is_generator |= parser.module.is_generator
|
scope.is_generator |= parser.module.is_generator
|
||||||
|
|
||||||
def add_node(self, node):
|
def add_node(self, node):
|
||||||
@@ -379,7 +375,6 @@ class FastParser(use_metaclass(CachedFastParser)):
|
|||||||
if not self.parsers:
|
if not self.parsers:
|
||||||
self.parsers.append(empty_parser())
|
self.parsers.append(empty_parser())
|
||||||
|
|
||||||
|
|
||||||
self.module.end_pos = self.parsers[-1].end_pos
|
self.module.end_pos = self.parsers[-1].end_pos
|
||||||
|
|
||||||
#print(self.parsers[0].module.get_code())
|
#print(self.parsers[0].module.get_code())
|
||||||
|
|||||||
Reference in New Issue
Block a user