mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 06:44:46 +08:00
Completely remove names_dicts from the diff parser.
This commit is contained in:
@@ -91,7 +91,7 @@ class NameFinder(object):
|
||||
def filter_name(self, filters):
|
||||
"""
|
||||
Searches names that are defined in a scope (the different
|
||||
`names_dicts`), until a name fits.
|
||||
``filters``), until a name fits.
|
||||
"""
|
||||
names = []
|
||||
if self._context.predefined_names:
|
||||
|
||||
@@ -168,6 +168,7 @@ def get_module_names(module, all_scopes):
|
||||
if all_scopes:
|
||||
dct = module.used_names
|
||||
else:
|
||||
raise DeprecationWarning
|
||||
dct = module.names_dict
|
||||
return chain.from_iterable(dct.values())
|
||||
|
||||
|
||||
@@ -100,7 +100,6 @@ class Parser(object):
|
||||
# Todo Remove start_parsing (with False)
|
||||
|
||||
self._used_names = {}
|
||||
self._scope_names_stack = [{}]
|
||||
self._global_names = []
|
||||
|
||||
self.source = source
|
||||
@@ -180,36 +179,18 @@ class Parser(object):
|
||||
# returned by convert multiple times.
|
||||
if symbol == 'global_stmt':
|
||||
self._global_names += new_node.get_global_names()
|
||||
elif isinstance(new_node, pt.Lambda):
|
||||
new_node.names_dict = self._scope_names_stack.pop()
|
||||
elif isinstance(new_node, (pt.ClassOrFunc, pt.Module)) \
|
||||
and symbol in ('funcdef', 'classdef', 'file_input'):
|
||||
# scope_name_stack handling
|
||||
scope_names = self._scope_names_stack.pop()
|
||||
if isinstance(new_node, pt.ClassOrFunc):
|
||||
n = new_node.name
|
||||
scope_names[n.value].remove(n)
|
||||
# Set the func name of the current node
|
||||
arr = self._scope_names_stack[-1].setdefault(n.value, [])
|
||||
arr.append(n)
|
||||
new_node.names_dict = scope_names
|
||||
return new_node
|
||||
|
||||
def convert_leaf(self, grammar, type, value, prefix, start_pos):
|
||||
# print('leaf', repr(value), token.tok_name[type])
|
||||
if type == tokenize.NAME:
|
||||
if value in grammar.keywords:
|
||||
if value in ('def', 'class', 'lambda'):
|
||||
self._scope_names_stack.append({})
|
||||
|
||||
return pt.Keyword(value, start_pos, prefix)
|
||||
else:
|
||||
name = pt.Name(value, start_pos, prefix)
|
||||
# Keep a listing of all used names
|
||||
arr = self._used_names.setdefault(name.value, [])
|
||||
arr.append(name)
|
||||
arr = self._scope_names_stack[-1].setdefault(name.value, [])
|
||||
arr.append(name)
|
||||
return name
|
||||
elif type == STRING:
|
||||
return pt.String(value, start_pos, prefix)
|
||||
@@ -362,8 +343,6 @@ class ParserWithRecovery(Parser):
|
||||
symbol = grammar.number2symbol[typ]
|
||||
failed_stack.append((symbol, nodes))
|
||||
all_nodes += nodes
|
||||
if nodes and nodes[0] in ('def', 'class', 'lambda'):
|
||||
self._scope_names_stack.pop()
|
||||
if failed_stack:
|
||||
stack[start_index - 1][2][1].append(pt.ErrorNode(all_nodes))
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ from jedi.common import splitlines
|
||||
from jedi.parser import ParserWithRecovery
|
||||
from jedi.parser.tree import Module, search_ancestor, EndMarker
|
||||
from jedi.parser.utils import parser_cache
|
||||
from jedi.parser import tokenize
|
||||
from jedi import debug
|
||||
from jedi.parser.tokenize import (generate_tokens, NEWLINE, TokenInfo,
|
||||
ENDMARKER, INDENT, DEDENT, tok_name)
|
||||
@@ -37,7 +36,7 @@ class FastParser(use_metaclass(CachedFastParser)):
|
||||
pass
|
||||
|
||||
|
||||
def _merge_names_dicts(base_dict, other_dict):
|
||||
def _merge_used_names(base_dict, other_dict):
|
||||
for key, names in other_dict.items():
|
||||
base_dict.setdefault(key, []).extend(names)
|
||||
|
||||
@@ -107,7 +106,6 @@ class DiffParser(object):
|
||||
self._new_module = Module(self._new_children)
|
||||
# TODO get rid of Module.global_names in evaluator. It's getting ignored here.
|
||||
self._new_module.path = self._old_module.path
|
||||
self._new_module.names_dict = {}
|
||||
self._new_module.used_names = {}
|
||||
self._new_module.global_names = []
|
||||
self._prefix = ''
|
||||
@@ -221,8 +219,7 @@ class DiffParser(object):
|
||||
to = _get_last_line(nodes[-1])
|
||||
debug.dbg('diff actually copy %s to %s', from_, to)
|
||||
self._update_positions(nodes, line_offset)
|
||||
parent = self._insert_nodes(nodes)
|
||||
self._update_names_dict(parent, nodes)
|
||||
self._insert_nodes(nodes)
|
||||
self._copied_ranges.append((from_, to))
|
||||
# We have copied as much as possible (but definitely not too
|
||||
# much). Therefore we just parse the rest.
|
||||
@@ -352,28 +349,6 @@ class DiffParser(object):
|
||||
return node
|
||||
node = parent
|
||||
|
||||
def _update_names_dict(self, scope_node, nodes):
|
||||
assert scope_node.type in ('suite', 'file_input')
|
||||
|
||||
names_dict = scope_node.names_dict
|
||||
|
||||
def scan(nodes):
|
||||
for node in nodes:
|
||||
if node.type in ('classdef', 'funcdef'):
|
||||
scan([node.children[1]])
|
||||
continue
|
||||
try:
|
||||
scan(node.children)
|
||||
except AttributeError:
|
||||
if node.type == 'name':
|
||||
names_dict.setdefault(node.value, []).append(node)
|
||||
|
||||
scan(nodes)
|
||||
|
||||
def _merge_parsed_node(self, scope_node, parsed_node):
|
||||
_merge_names_dicts(scope_node.names_dict, parsed_node.names_dict)
|
||||
_merge_names_dicts(self._new_module.used_names, parsed_node.used_names)
|
||||
|
||||
def _divide_node(self, node, until_line):
|
||||
"""
|
||||
Breaks up scopes and returns only the part until the given line.
|
||||
@@ -422,8 +397,11 @@ class DiffParser(object):
|
||||
while until_line > self._parsed_until_line:
|
||||
node = self._parse_scope_node(until_line)
|
||||
nodes = self._get_children_nodes(node)
|
||||
parent = self._insert_nodes(nodes)
|
||||
self._merge_parsed_node(parent, node)
|
||||
self._insert_nodes(nodes)
|
||||
_merge_used_names(
|
||||
self._new_module.used_names,
|
||||
node.used_names
|
||||
)
|
||||
|
||||
def _get_children_nodes(self, node):
|
||||
nodes = node.children
|
||||
|
||||
@@ -717,7 +717,7 @@ class Scope(BaseNode, DocstringMixin):
|
||||
:param start_pos: The position (line and column) of the scope.
|
||||
:type start_pos: tuple(int, int)
|
||||
"""
|
||||
__slots__ = ('names_dict',)
|
||||
__slots__ = ()
|
||||
|
||||
def __init__(self, children):
|
||||
super(Scope, self).__init__(children)
|
||||
@@ -1648,17 +1648,6 @@ class CompFor(BaseNode):
|
||||
def is_scope(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def names_dict(self):
|
||||
dct = {}
|
||||
for name in self.get_defined_names():
|
||||
arr = dct.setdefault(name.value, [])
|
||||
arr.append(name)
|
||||
return dct
|
||||
|
||||
def names_dicts(self, search_global):
|
||||
yield self.names_dict
|
||||
|
||||
def get_defined_names(self):
|
||||
return _defined_names(self.children[1])
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ def test_carriage_return_splitting():
|
||||
'''))
|
||||
source = source.replace('\n', '\r\n')
|
||||
p = FastParser(load_grammar(), source)
|
||||
assert [n.value for lst in p.module.names_dict.values() for n in lst] == ['Foo']
|
||||
assert [n.value for lst in p.module.used_names.values() for n in lst] == ['Foo']
|
||||
|
||||
|
||||
def test_class_in_docstr():
|
||||
|
||||
Reference in New Issue
Block a user